fix(navigation): align mega-menu rendering and menu-item backend with the reference
Renderer: - Graphic-card images carry fit-cover so they crop into the fixed card box instead of stretching it and pushing the title out of alignment. - Mega-menu link icons use fa-fw (fixed glyph column) instead of the undefined menu-item-icon class, so link titles line up. Backend: - Restore show_policy (login / vip / vip_2 / auth) on menu items; the field was missing entirely, so items could not be gated by identity. - Default submenu_type to graphic_card, badge_class to jb-red, arrangement to swiper, size to md. - Clamp img_scale to 20-300 to match the reference's spinner bounds. - Drop the now-meaningless "none" submenu_type option: the senior-submenu switch is itself the on/off control. - Admin JS gains the align control (left/center/space-around). Cleanup: - Remove the unused header_actions nav menu location. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
55423403a6
commit
e6d9ff49a2
6 files changed
+113
-29
No files matched your search
@@ -46,8 +46,9 @@ class NavMenuOptions {
|
||||
$icon = (string) ($opts['icon'] ?? '');
|
||||
$badge = (string) ($opts['badge'] ?? '');
|
||||
$badge_class = (string) ($opts['badge_class'] ?? '');
|
||||
$show_policy = (string) ($opts['show_policy'] ?? '');
|
||||
$submenu_s = !empty($opts['submenu_s']);
|
||||
$submenu_type = (string) ($opts['submenu_type'] ?? '');
|
||||
$submenu_type = (string) ($opts['submenu_type'] ?? 'graphic_card');
|
||||
$graphic_json = !empty($opts['graphic_card_opts'])
|
||||
? wp_json_encode($opts['graphic_card_opts'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
|
||||
: self::sample_graphic_json();
|
||||
@@ -83,6 +84,19 @@ class NavMenuOptions {
|
||||
value="<?php echo esc_attr($badge_class); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-jio-show-policy description description-wide">
|
||||
<label for="edit-menu-item-jio-show-policy-<?php echo esc_attr((string) $item_id); ?>">
|
||||
<?php esc_html_e('显示限制', 'jio'); ?><br>
|
||||
<select id="edit-menu-item-jio-show-policy-<?php echo esc_attr((string) $item_id); ?>"
|
||||
class="widefat" name="jio_menu_options[<?php echo esc_attr((string) $item_id); ?>][show_policy]">
|
||||
<option value="" <?php selected($show_policy, ''); ?>><?php esc_html_e('不限制', 'jio'); ?></option>
|
||||
<option value="signin" <?php selected($show_policy, 'signin'); ?>><?php esc_html_e('登录后显示', 'jio'); ?></option>
|
||||
<option value="vip" <?php selected($show_policy, 'vip'); ?>><?php esc_html_e('所有会员显示', 'jio'); ?></option>
|
||||
<option value="vip_2" <?php selected($show_policy, 'vip_2'); ?>><?php esc_html_e('二级会员显示', 'jio'); ?></option>
|
||||
<option value="auth" <?php selected($show_policy, 'auth'); ?>><?php esc_html_e('认证用户显示', 'jio'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-jio-submenu description description-wide">
|
||||
<label>
|
||||
<input type="checkbox" name="jio_menu_options[<?php echo esc_attr((string) $item_id); ?>][submenu_s]" value="1" <?php checked($submenu_s); ?>>
|
||||
@@ -94,9 +108,8 @@ class NavMenuOptions {
|
||||
<?php esc_html_e('高级子菜单类型', 'jio'); ?><br>
|
||||
<select id="edit-menu-item-jio-submenu-type-<?php echo esc_attr((string) $item_id); ?>"
|
||||
name="jio_menu_options[<?php echo esc_attr((string) $item_id); ?>][submenu_type]">
|
||||
<option value="" <?php selected($submenu_type, ''); ?>><?php esc_html_e('无', 'jio'); ?></option>
|
||||
<option value="graphic_card" <?php selected($submenu_type, 'graphic_card'); ?>><?php esc_html_e('图文卡片', 'jio'); ?></option>
|
||||
<option value="multi_column_links" <?php selected($submenu_type, 'multi_column_links'); ?>><?php esc_html_e('多列链接', 'jio'); ?></option>
|
||||
<option value="multi_column_links" <?php selected($submenu_type, 'multi_column_links'); ?>><?php esc_html_e('多栏目链接', 'jio'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
@@ -134,6 +147,7 @@ class NavMenuOptions {
|
||||
'icon' => jio_sanitize_icon_value((string) ($raw['icon'] ?? '')),
|
||||
'badge' => sanitize_text_field((string) ($raw['badge'] ?? '')),
|
||||
'badge_class' => sanitize_html_class((string) ($raw['badge_class'] ?? '')),
|
||||
'show_policy' => self::sanitize_show_policy((string) ($raw['show_policy'] ?? '')),
|
||||
'submenu_s' => !empty($raw['submenu_s']),
|
||||
'submenu_type' => sanitize_key((string) ($raw['submenu_type'] ?? '')),
|
||||
];
|
||||
@@ -150,6 +164,15 @@ class NavMenuOptions {
|
||||
update_post_meta($menu_item_db_id, 'jio_menu_options', $clean);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $policy
|
||||
*/
|
||||
private static function sanitize_show_policy(string $policy): string {
|
||||
$allowed = ['', 'signin', 'vip', 'vip_2', 'auth'];
|
||||
$policy = sanitize_key($policy);
|
||||
return in_array($policy, $allowed, true) ? $policy : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>|null
|
||||
*/
|
||||
@@ -177,7 +200,7 @@ class NavMenuOptions {
|
||||
'img' => esc_url_raw((string) ($item['img'] ?? '')),
|
||||
'title' => sanitize_text_field((string) ($item['title'] ?? '')),
|
||||
'badge' => sanitize_text_field((string) ($item['badge'] ?? '')),
|
||||
'badge_class' => sanitize_html_class((string) ($item['badge_class'] ?? '')),
|
||||
'badge_class' => sanitize_html_class((string) ($item['badge_class'] ?? 'jb-red')),
|
||||
'link' => [
|
||||
'url' => esc_url_raw((string) ($item['link']['url'] ?? '')),
|
||||
'text' => sanitize_text_field((string) ($item['link']['text'] ?? '')),
|
||||
@@ -186,10 +209,10 @@ class NavMenuOptions {
|
||||
];
|
||||
}
|
||||
return [
|
||||
'arrangement' => sanitize_key((string) ($opts['arrangement'] ?? '')),
|
||||
'arrangement' => sanitize_key((string) ($opts['arrangement'] ?? 'swiper')),
|
||||
'align' => sanitize_key((string) ($opts['align'] ?? '')),
|
||||
'size' => sanitize_key((string) ($opts['size'] ?? '')),
|
||||
'img_scale' => max(0, min(200, (int) ($opts['img_scale'] ?? 100))),
|
||||
'size' => sanitize_key((string) ($opts['size'] ?? 'md')),
|
||||
'img_scale' => max(20, min(300, (int) ($opts['img_scale'] ?? 100))),
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
@@ -240,7 +263,7 @@ class NavMenuOptions {
|
||||
|
||||
private static function sample_graphic_json(): string {
|
||||
$sample = [
|
||||
'arrangement' => '',
|
||||
'arrangement' => 'swiper',
|
||||
'size' => 'md',
|
||||
'img_scale' => 100,
|
||||
'items' => [
|
||||
|
||||
+4
-5
@@ -78,11 +78,10 @@ class Setup {
|
||||
|
||||
public function register_nav_menus(): void {
|
||||
register_nav_menus([
|
||||
'primary' => __('主导航', 'jio'),
|
||||
'mobile' => __('移动端菜单', 'jio'),
|
||||
'footer' => __('页脚菜单', 'jio'),
|
||||
'social' => __('社交链接', 'jio'),
|
||||
'header_actions' => __('顶部右侧入口', 'jio'),
|
||||
'primary' => __('主导航', 'jio'),
|
||||
'mobile' => __('移动端菜单', 'jio'),
|
||||
'footer' => __('页脚菜单', 'jio'),
|
||||
'social' => __('社交链接', 'jio'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,13 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
public static $submenu_ids = [];
|
||||
|
||||
/**
|
||||
* Skip WP children when parent uses senior submenu.
|
||||
* Skip hidden items (display policy) and WP children when the parent uses
|
||||
* a senior submenu.
|
||||
*/
|
||||
public function display_element($element, &$children_elements, $max_depth, $depth, $args, &$output) {
|
||||
if (function_exists('jio_menu_item_match_show_policy') && !jio_menu_item_match_show_policy((int) $element->ID)) {
|
||||
return;
|
||||
}
|
||||
if (!empty($element->menu_item_parent) && in_array((int) $element->menu_item_parent, self::$submenu_ids, true)) {
|
||||
return;
|
||||
}
|
||||
@@ -29,7 +33,7 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
|
||||
$icon = function_exists('jio_menu_pz') ? (string) jio_menu_pz((int) $item->ID, 'icon', '') : '';
|
||||
$badge = function_exists('jio_menu_pz') ? (string) jio_menu_pz((int) $item->ID, 'badge', '') : '';
|
||||
$badge_class = function_exists('jio_menu_pz') ? (string) jio_menu_pz((int) $item->ID, 'badge_class', '') : '';
|
||||
$badge_class = function_exists('jio_menu_pz') ? (string) jio_menu_pz((int) $item->ID, 'badge_class', 'jb-red') : '';
|
||||
|
||||
if ($icon && function_exists('jio_render_menu_icon')) {
|
||||
$title = jio_render_menu_icon($icon) . $title;
|
||||
@@ -58,7 +62,9 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
* @param int $item_id
|
||||
*/
|
||||
public function submenu(int $item_id): string {
|
||||
$type = function_exists('jio_menu_pz') ? sanitize_key((string) jio_menu_pz($item_id, 'submenu_type', '')) : '';
|
||||
$type = function_exists('jio_menu_pz')
|
||||
? sanitize_key((string) jio_menu_pz($item_id, 'submenu_type', 'graphic_card'))
|
||||
: '';
|
||||
$method = 'submenu_' . $type;
|
||||
if ($type !== '' && method_exists($this, $method)) {
|
||||
return (string) $this->$method($item_id);
|
||||
@@ -76,7 +82,7 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
if (!$items) {
|
||||
return '';
|
||||
}
|
||||
$arrangement = (string) ($opts['arrangement'] ?? '');
|
||||
$arrangement = (string) ($opts['arrangement'] ?? 'swiper');
|
||||
$lists = '';
|
||||
$i = 0;
|
||||
foreach ($items as $item) {
|
||||
@@ -123,10 +129,10 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
$title = $link_title;
|
||||
}
|
||||
$badge = !empty($args['badge'])
|
||||
? '<div class="img-badg-box"><span class="badg badg-sm ' . esc_attr((string) ($args['badge_class'] ?? '')) . '">'
|
||||
? '<div class="img-badg-box"><span class="badg badg-sm ' . esc_attr((string) ($args['badge_class'] ?? 'jb-red')) . '">'
|
||||
. esc_html((string) $args['badge']) . '</span></div>'
|
||||
: '';
|
||||
$img_html = '<div class="img-box hover-zoom-img"><img' . jio_get_lazy_attr('lazy_other', (string) $args['img']) . ' alt="">' . $badge . '</div>';
|
||||
$img_html = '<div class="img-box hover-zoom-img"><img' . jio_get_lazy_attr('lazy_other', (string) $args['img'], 'fit-cover') . ' alt="">' . $badge . '</div>';
|
||||
$title_html = $title !== '' ? '<div class="title mt10 text-ellipsis">' . esc_html($title) . '</div>' : '';
|
||||
return '<div class="menu-graphic-card-item"><a href="' . $link_url . '"' . $link_target . '>'
|
||||
. $img_html . $title_html . '</a></div>';
|
||||
@@ -181,7 +187,8 @@ class NavWalker extends \Walker_Nav_Menu {
|
||||
}
|
||||
$icon = '';
|
||||
if (!empty($args['icon']) && function_exists('jio_render_menu_icon')) {
|
||||
$icon = jio_render_menu_icon((string) $args['icon'], true);
|
||||
// Mega-menu rows reserve a fixed glyph column so titles line up.
|
||||
$icon = jio_render_menu_icon((string) $args['icon'], true, 'mr3 fa-fw');
|
||||
}
|
||||
if (!empty($args['img_icon'])) {
|
||||
$icon = '<img' . jio_get_lazy_attr('lazy_other', (string) $args['img_icon'], 'menu-item-icon mr3 fa-fw') . ' alt="">';
|
||||
|
||||
Reference in new issue
Block a user