feat(nav): rewrite top header to zibll 1:1 DOM/CSS/JS contract
Replace hybrid site-nav/drawer shell with zibll navbar-top layouts 1/2/3, body.mobile-navbar-show drawer, wired NavWalker + aksrt_menu_options meta, nav_folding/chevron, VIP switches, and SPA/PHP schema parity. No dual-class compat paths.
This commit is contained in:
1 parent
b8ee6b6a43
commit
259dbdbe04
28 files changed
+2463
-885
No files matched your search
+65
-128
@@ -1,9 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Menu Decoration Helpers
|
||||
*
|
||||
* Helper functions for reading menu item meta (icons, badges, submenu styles)
|
||||
* and rendering decorated menu items.
|
||||
* Menu decoration helpers — aksrt_menu_options meta only (zibll zib_menu_pz).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -11,151 +8,91 @@
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Get menu decoration settings from \AKSRT\Settings\Settings.
|
||||
* Global decoration switches (SPA). Per-item data is always from meta.
|
||||
*
|
||||
* @return array
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
function aksrt_get_menu_decoration_settings(): array {
|
||||
static $cache = null;
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
static $cache = null;
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||||
$all = \AKSRT\Settings\Settings::get_all();
|
||||
} else {
|
||||
$all = [];
|
||||
}
|
||||
$all = class_exists('\AKSRT\Settings\Settings')
|
||||
? \AKSRT\Settings\Settings::get_all()
|
||||
: [];
|
||||
|
||||
$cache = [
|
||||
'icon_enabled' => !empty($all['menu_icon_enabled']),
|
||||
'badge_enabled' => !empty($all['menu_badge_enabled']),
|
||||
'submenu_style' => in_array($all['menu_submenu_style'] ?? '', ['dropdown', 'mega', 'cards'], true)
|
||||
? $all['menu_submenu_style']
|
||||
: 'dropdown',
|
||||
'submenu_columns' => max(1, min(5, (int) ($all['menu_submenu_columns'] ?? 1))),
|
||||
'submenu_width' => max(160, min(800, (int) ($all['menu_submenu_width'] ?? 220))),
|
||||
'hover_effect' => in_array($all['menu_hover_effect'] ?? '', ['underline', 'background', 'color', 'none'], true)
|
||||
? $all['menu_hover_effect']
|
||||
: 'underline',
|
||||
'item_padding' => max(4, min(32, (int) ($all['menu_item_padding'] ?? 12))),
|
||||
'mobile_style' => in_array($all['menu_mobile_style'] ?? '', ['drawer', 'fullscreen', 'dropdown'], true)
|
||||
? $all['menu_mobile_style']
|
||||
: 'drawer',
|
||||
];
|
||||
$cache = [
|
||||
'icon_enabled' => !isset($all['menu_icon_enabled']) || !empty($all['menu_icon_enabled']),
|
||||
'badge_enabled' => !isset($all['menu_badge_enabled']) || !empty($all['menu_badge_enabled']),
|
||||
];
|
||||
|
||||
return $cache;
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get decoration meta for a specific menu item.
|
||||
* Read one menu item option from post meta aksrt_menu_options.
|
||||
*
|
||||
* Reads CSS class and title attribute from the menu item to extract:
|
||||
* - icon: FA5 class (e.g. "fas fa-home")
|
||||
* - badge_text: badge label (e.g. "新", "热")
|
||||
* - badge_type: badge color type (e.g. "new", "hot", "info")
|
||||
* - submenu_style: override for this item's submenu
|
||||
* @param int $id Menu item ID.
|
||||
* @param string $key Option key.
|
||||
* @param mixed $default Default.
|
||||
* @return mixed
|
||||
*/
|
||||
function aksrt_menu_pz(int $id, string $key = '', $default = '') {
|
||||
static $options = [];
|
||||
if (!isset($options[$id])) {
|
||||
$raw = get_post_meta($id, 'aksrt_menu_options', true);
|
||||
$options[$id] = is_array($raw) ? $raw : [];
|
||||
}
|
||||
if ($key === '') {
|
||||
return $options[$id];
|
||||
}
|
||||
return $options[$id][$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decoration for a menu item (meta only).
|
||||
*
|
||||
* @param WP_Post $menu_item The menu item post object.
|
||||
* @return array
|
||||
* @param WP_Post|object $menu_item Menu item.
|
||||
* @return array{icon:string,badge_text:string,badge_class:string,submenu_s:bool,submenu_type:string}
|
||||
*/
|
||||
function aksrt_get_menu_item_decoration($menu_item): array {
|
||||
$classes = (array) ($menu_item->classes ?? []);
|
||||
$title = (string) ($menu_item->title ?? '');
|
||||
$attr = (string) ($menu_item->attr_title ?? '');
|
||||
|
||||
$result = [
|
||||
'icon' => '',
|
||||
'badge_text' => '',
|
||||
'badge_type' => 'new',
|
||||
'submenu_style' => '',
|
||||
];
|
||||
|
||||
// Extract icon from CSS classes (pattern: icon-* or fa-*)
|
||||
foreach ($classes as $cls) {
|
||||
$cls = trim($cls);
|
||||
if (strpos($cls, 'fa-') === 0 || strpos($cls, 'fas ') === 0 || strpos($cls, 'fab ') === 0 || strpos($cls, 'far ') === 0) {
|
||||
$result['icon'] = $cls;
|
||||
break;
|
||||
}
|
||||
if (strpos($cls, 'icon-') === 0) {
|
||||
$result['icon'] = 'fas fa-' . substr($cls, 5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Extract badge from title attribute (pattern: [新] or [热:hot])
|
||||
if (preg_match('/^\[([^\]:]+)(?::(\w+))?\]/', $title, $m)) {
|
||||
$result['badge_text'] = $m[1];
|
||||
$result['badge_type'] = $m[2] ?? 'new';
|
||||
// Remove badge markup from display title
|
||||
$menu_item->title = trim(substr($title, strlen($m[0])));
|
||||
}
|
||||
|
||||
// Extract submenu style override from classes
|
||||
foreach ($classes as $cls) {
|
||||
if (strpos($cls, 'submenu-') === 0) {
|
||||
$style = substr($cls, 8);
|
||||
if (in_array($style, ['dropdown', 'mega', 'cards'], true)) {
|
||||
$result['submenu_style'] = $style;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
$id = (int) ($menu_item->ID ?? 0);
|
||||
return [
|
||||
'icon' => (string) aksrt_menu_pz($id, 'icon', ''),
|
||||
'badge_text' => (string) aksrt_menu_pz($id, 'badge', ''),
|
||||
'badge_class' => (string) aksrt_menu_pz($id, 'badge_class', ''),
|
||||
'submenu_s' => !empty(aksrt_menu_pz($id, 'submenu_s', false)),
|
||||
'submenu_type' => sanitize_key((string) aksrt_menu_pz($id, 'submenu_type', '')),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a menu item icon if enabled.
|
||||
*
|
||||
* @param string $icon_class FA5 icon class.
|
||||
* @param bool $enabled Whether icons are enabled globally.
|
||||
* @return string HTML string.
|
||||
* @param string $icon_class FA5 / icon class.
|
||||
* @param bool $enabled Global switch.
|
||||
*/
|
||||
function aksrt_render_menu_icon(string $icon_class, bool $enabled = true): string {
|
||||
if (!$enabled || $icon_class === '') {
|
||||
return '';
|
||||
}
|
||||
return '<i class="' . esc_attr($icon_class) . ' aksrt-nav__icon" aria-hidden="true"></i>';
|
||||
if (!$enabled || $icon_class === '') {
|
||||
return '';
|
||||
}
|
||||
// Allow bare "fa-home" → "fas fa-home".
|
||||
if (strpos($icon_class, 'fa') === false) {
|
||||
$icon_class = 'fas fa-' . ltrim($icon_class, '-');
|
||||
} elseif (strpos($icon_class, 'fa-') === 0 && strpos($icon_class, ' ') === false) {
|
||||
$icon_class = 'fas ' . $icon_class;
|
||||
}
|
||||
return '<i class="' . esc_attr($icon_class) . ' mr3 menu-item-icon" aria-hidden="true"></i>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a menu item badge if enabled.
|
||||
*
|
||||
* @param string $text Badge text.
|
||||
* @param string $type Badge type (new, hot, info, etc.).
|
||||
* @param bool $enabled Whether badges are enabled globally.
|
||||
* @return string HTML string.
|
||||
* @param string $class Extra class (jb-red etc.).
|
||||
* @param bool $enabled Global switch.
|
||||
*/
|
||||
function aksrt_render_menu_badge(string $text, string $type = 'new', bool $enabled = true): string {
|
||||
if (!$enabled || $text === '') {
|
||||
return '';
|
||||
}
|
||||
return '<span class="aksrt-nav__badge aksrt-nav__badge--' . esc_attr($type) . '">' . esc_html($text) . '</span>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get CSS classes for a menu item based on decoration settings.
|
||||
*
|
||||
* @param array $decoration Decoration meta from aksrt_get_menu_item_decoration().
|
||||
* @param array $settings Global decoration settings.
|
||||
* @return string Space-separated CSS classes.
|
||||
*/
|
||||
function aksrt_get_menu_item_classes(array $decoration, array $settings): string {
|
||||
$classes = ['aksrt-nav__item'];
|
||||
|
||||
if ($settings['hover_effect'] !== 'none') {
|
||||
$classes[] = 'aksrt-nav__item--hover-' . $settings['hover_effect'];
|
||||
}
|
||||
|
||||
if ($decoration['icon'] !== '') {
|
||||
$classes[] = 'aksrt-nav__item--has-icon';
|
||||
}
|
||||
|
||||
if ($decoration['badge_text'] !== '') {
|
||||
$classes[] = 'aksrt-nav__item--has-badge';
|
||||
}
|
||||
|
||||
return implode(' ', $classes);
|
||||
function aksrt_render_menu_badge(string $text, string $class = '', bool $enabled = true): string {
|
||||
if (!$enabled || $text === '') {
|
||||
return '';
|
||||
}
|
||||
$cls = trim('menu-item-badge ' . $class);
|
||||
return ' <badge class="' . esc_attr($cls) . '">' . esc_html($text) . '</badge>';
|
||||
}
|
||||
Reference in new issue
Block a user