146 lines
4.9 KiB
PHP
146 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Navigation Walker
|
|
*
|
|
* Custom walker for WordPress nav menus that adds:
|
|
* - FA5 icons from menu item classes
|
|
* - Badge labels from title attribute pattern [text:type]
|
|
* - Submenu style overrides (dropdown / mega / cards)
|
|
* - CSS class augmentation for hover effects
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Nav_Walker extends Walker_Nav_Menu {
|
|
|
|
/**
|
|
* @var array Decoration settings from aksrt_get_menu_decoration_settings().
|
|
*/
|
|
private $settings;
|
|
|
|
public function __construct(array $settings = []) {
|
|
$this->settings = $settings;
|
|
}
|
|
|
|
/**
|
|
* 当前处理的菜单项是否为 Mega Menu 容器
|
|
* @var bool
|
|
*/
|
|
private $is_mega_menu = false;
|
|
|
|
/**
|
|
* Mega Menu 列数
|
|
* @var int
|
|
*/
|
|
private $mega_columns = 3;
|
|
|
|
/**
|
|
* Start level: open submenu with appropriate class based on style.
|
|
*/
|
|
public function start_lvl(&$output, $depth = 0, $args = null): void {
|
|
$indent = str_repeat("\t", $depth);
|
|
|
|
if ($this->is_mega_menu && $depth === 0) {
|
|
// Mega Menu 子菜单容器
|
|
$output .= "\n{$indent}<ul class=\"sub-menu mega-menu__panel mega-menu__columns-{$this->mega_columns}\">\n";
|
|
} else {
|
|
$output .= "\n{$indent}<ul class=\"sub-menu\">\n";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start element: add icon, badge, and classes.
|
|
*/
|
|
public function start_el(&$output, $item, $depth = 0, $args = null, $id = 0): void {
|
|
$indent = ($depth) ? str_repeat("\t", $depth) : '';
|
|
|
|
$classes = (array) $item->classes;
|
|
$decoration = function_exists('aksrt_get_menu_item_decoration')
|
|
? aksrt_get_menu_item_decoration($item)
|
|
: ['icon' => '', 'badge_text' => '', 'badge_type' => 'new', 'submenu_style' => ''];
|
|
|
|
// 检测 Mega Menu 标记
|
|
$this->is_mega_menu = false;
|
|
$this->mega_columns = 3;
|
|
if ($depth === 0) {
|
|
foreach ($classes as $cls) {
|
|
if ($cls === 'mega-menu') {
|
|
$this->is_mega_menu = true;
|
|
}
|
|
if (preg_match('/^mega-menu-columns-(\d)$/', $cls, $m)) {
|
|
$this->mega_columns = max(2, min(5, (int) $m[1]));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Build li classes
|
|
$li_classes = ['menu-item', 'menu-item-' . $item->ID];
|
|
if (in_array('current-menu-item', $classes, true)) {
|
|
$li_classes[] = 'current-menu-item';
|
|
}
|
|
if (in_array('menu-item-has-children', $classes, true)) {
|
|
$li_classes[] = 'menu-item-has-children';
|
|
}
|
|
if ($this->is_mega_menu) {
|
|
$li_classes[] = 'mega-menu-item';
|
|
}
|
|
|
|
// Add decoration classes
|
|
$li_classes[] = 'aksrt-nav__item';
|
|
if ($decoration['icon'] !== '' && !empty($this->settings['icon_enabled'])) {
|
|
$li_classes[] = 'aksrt-nav__item--has-icon';
|
|
}
|
|
if ($decoration['badge_text'] !== '' && !empty($this->settings['badge_enabled'])) {
|
|
$li_classes[] = 'aksrt-nav__item--has-badge';
|
|
}
|
|
$hover = $this->settings['hover_effect'] ?? 'underline';
|
|
if ($hover !== 'none') {
|
|
$li_classes[] = 'aksrt-nav__item--hover-' . $hover;
|
|
}
|
|
|
|
$class_str = implode(' ', array_unique($li_classes));
|
|
$output .= $indent . '<li class="' . esc_attr($class_str) . '" data-menu-item-id="' . (int) $item->ID . '" data-menu-depth="' . (int) $depth . '"' . (in_array('menu-item-has-children', $classes, true) ? ' data-menu-has-children="1"' : '') . '>';
|
|
|
|
// Build link content
|
|
$icon_html = '';
|
|
if ($decoration['icon'] !== '' && !empty($this->settings['icon_enabled'])) {
|
|
$icon_html = aksrt_render_menu_icon($decoration['icon'], true);
|
|
}
|
|
|
|
$badge_html = '';
|
|
if ($decoration['badge_text'] !== '' && !empty($this->settings['badge_enabled'])) {
|
|
$badge_html = aksrt_render_menu_badge($decoration['badge_text'], $decoration['badge_type'], true);
|
|
}
|
|
|
|
// Mega Menu 子项:检查是否有描述文字(用作卡片副标题)
|
|
$description_html = '';
|
|
if ($this->is_mega_menu && $depth === 1 && !empty($item->description)) {
|
|
$description_html = '<span class="mega-menu__item-desc">' . esc_html($item->description) . '</span>';
|
|
}
|
|
|
|
$atts = [
|
|
'href' => $item->url,
|
|
'title' => $item->attr_title ?: '',
|
|
];
|
|
|
|
$link_inner = $icon_html . '<span>' . esc_html($item->title) . '</span>' . $description_html . $badge_html;
|
|
|
|
$output .= '<a';
|
|
foreach ($atts as $attr => $value) {
|
|
if ('' !== $value) {
|
|
$output .= ' ' . $attr . '="' . esc_attr($value) . '"';
|
|
}
|
|
}
|
|
$output .= '>' . $link_inner . '</a>';
|
|
}
|
|
|
|
/**
|
|
* End element.
|
|
*/
|
|
public function end_el(&$output, $item, $depth = 0, $args = null): void {
|
|
$output .= "</li>\n";
|
|
}
|
|
}
|