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
@@ -18,9 +18,10 @@
|
||||
|
||||
function graphicTemplate() {
|
||||
return {
|
||||
arrangement: '',
|
||||
arrangement: 'swiper',
|
||||
size: 'md',
|
||||
img_scale: 100,
|
||||
align: '',
|
||||
items: [{ img: '', title: '', badge: '', badge_class: 'jb-red', link: { url: '', text: '', target: '' } }],
|
||||
};
|
||||
}
|
||||
@@ -80,7 +81,12 @@
|
||||
});
|
||||
html += '</div>';
|
||||
html += field('size', '尺寸 sm/md/lg/xs', data.size || 'md');
|
||||
html += field('img_scale', '图片比例%', data.img_scale || 100, 'number');
|
||||
html += field('img_scale', '图片比例%(20-300)', data.img_scale || 100, 'number');
|
||||
html += ' <label>对齐 <select data-name="align">';
|
||||
html += '<option value=""' + (!data.align ? ' selected' : '') + '>靠左</option>';
|
||||
html += '<option value="jc"' + (data.align === 'jc' ? ' selected' : '') + '>居中</option>';
|
||||
html += '<option value="jsa"' + (data.align === 'jsa' ? ' selected' : '') + '>平均分布</option>';
|
||||
html += '</select></label>';
|
||||
$wrap.html(html);
|
||||
}
|
||||
|
||||
@@ -114,6 +120,7 @@
|
||||
var data = graphicTemplate();
|
||||
data.arrangement = $wrap.find('[data-name="arrangement"]').val() || '';
|
||||
data.size = $wrap.find('[data-name="size"]').val() || 'md';
|
||||
data.align = $wrap.find('[data-name="align"]').val() || '';
|
||||
data.img_scale = parseInt($wrap.find('[data-name="img_scale"]').val(), 10) || 100;
|
||||
data.items = [];
|
||||
$wrap.find('.jio-sme-card').each(function () {
|
||||
|
||||
@@ -28,21 +28,26 @@ function jio_menu_pz(int $id, string $key = '', $default = '') {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $icon_class Icon-picker value (FA5 class, jiosvg- or customsvg-).
|
||||
* @param bool $enabled Global switch.
|
||||
* @param string $icon_class Icon-picker value (FA5 class, jiosvg- or customsvg-).
|
||||
* @param bool $enabled Global switch.
|
||||
* @param string $extra_classes Classes appended to the glyph. Top-level menu
|
||||
* titles use the default; mega-menu link lists use
|
||||
* `mr3 fa-fw` so every row's glyph is the same width.
|
||||
*/
|
||||
function jio_render_menu_icon(string $icon_class, bool $enabled = true): string {
|
||||
function jio_render_menu_icon(string $icon_class, bool $enabled = true, string $extra_classes = 'mr3 menu-item-icon'): string {
|
||||
if (!$enabled || $icon_class === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$extra = trim($extra_classes);
|
||||
|
||||
// Sprite/inline values must reach jio_icon_markup untouched — the legacy
|
||||
// FA class padding below would corrupt them (jiosvg-hot → fa fas fa-jiosvg-hot).
|
||||
if (str_starts_with($icon_class, 'jiosvg-')) {
|
||||
return jio_icon_markup($icon_class, 'icon mr3 menu-item-icon');
|
||||
return jio_icon_markup($icon_class, $extra === '' ? 'icon' : 'icon ' . $extra);
|
||||
}
|
||||
if (str_starts_with($icon_class, 'customsvg-')) {
|
||||
return jio_icon_markup($icon_class, 'mr3 menu-item-icon');
|
||||
return jio_icon_markup($icon_class, $extra);
|
||||
}
|
||||
|
||||
// legacy uses the Font Awesome 5 `fa` prefix.
|
||||
@@ -51,7 +56,46 @@ function jio_render_menu_icon(string $icon_class, bool $enabled = true): string
|
||||
} elseif (strpos($icon_class, 'fa-') === 0 && strpos($icon_class, ' ') === false) {
|
||||
$icon_class = 'fa ' . $icon_class;
|
||||
}
|
||||
return jio_icon_markup($icon_class, 'mr3 menu-item-icon');
|
||||
return jio_icon_markup($icon_class, $extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a menu item passes its display policy (show_policy).
|
||||
*
|
||||
* @param int $menu_item_id Menu item ID.
|
||||
* @return bool
|
||||
*/
|
||||
function jio_menu_item_match_show_policy(int $menu_item_id): bool {
|
||||
$policy = (string) jio_menu_pz($menu_item_id, 'show_policy', '');
|
||||
if ($policy === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
if ($user_id <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($policy) {
|
||||
case 'signin':
|
||||
return true;
|
||||
case 'vip':
|
||||
return function_exists('jio_is_vip') && jio_is_vip($user_id);
|
||||
case 'vip_2':
|
||||
return function_exists('jio_is_vip2') && jio_is_vip2($user_id);
|
||||
case 'auth':
|
||||
if (!function_exists('jio_get_user_identity_signals')) {
|
||||
return false;
|
||||
}
|
||||
foreach (jio_get_user_identity_signals($user_id) as $signal) {
|
||||
if ((string) ($signal['type'] ?? '') === 'verify') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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="">';
|
||||
|
||||
@@ -133,7 +133,11 @@ test('custom settings pages and standalone save paths speak the three icon forma
|
||||
test('nav menu icons render every format without legacy fa-prefix mangling', () => {
|
||||
const helper = read('inc/helpers/menu-decoration.php');
|
||||
assert.match(helper, /str_starts_with\(\$icon_class, 'jiosvg-'\)/);
|
||||
assert.match(helper, /jio_icon_markup\(\$icon_class, 'icon mr3 menu-item-icon'\)/);
|
||||
// Top-level titles keep the default glyph classes; mega-menu lists pass
|
||||
// `mr3 fa-fw` so every row's icon occupies the same column.
|
||||
assert.match(helper, /\$extra_classes = 'mr3 menu-item-icon'/);
|
||||
assert.match(helper, /jio_icon_markup\(\$icon_class, \$extra\)/);
|
||||
assert.match(read('src/Frontend/NavWalker.php'), /jio_render_menu_icon\(\(string\) \$args\['icon'\], true, 'mr3 fa-fw'\)/);
|
||||
assert.doesNotMatch(helper, /'<i class="' \. esc_attr\(\$icon_class\)/, 'no raw i-tag bypassing the unified helper');
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user