Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
162 lines
5.1 KiB
PHP
162 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* Menu Decoration Helpers
|
|
*
|
|
* Helper functions for reading menu item meta (icons, badges, submenu styles)
|
|
* and rendering decorated menu items.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Get menu decoration settings from \AKSRT\Settings\Settings.
|
|
*
|
|
* @return array
|
|
*/
|
|
function aksrt_get_menu_decoration_settings(): array {
|
|
static $cache = null;
|
|
if ($cache !== null) {
|
|
return $cache;
|
|
}
|
|
|
|
if (class_exists('\AKSRT\Settings\Settings')) {
|
|
$all = \AKSRT\Settings\Settings::get_all();
|
|
} else {
|
|
$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',
|
|
];
|
|
|
|
return $cache;
|
|
}
|
|
|
|
/**
|
|
* Get decoration meta for a specific menu item.
|
|
*
|
|
* 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 WP_Post $menu_item The menu item post object.
|
|
* @return array
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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>';
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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);
|
|
}
|