Files
wordpress-theme-aksrt/inc/helpers/header/aksrt-header-render.php
T
kate522 1d1fef50af feat(nav): deep 1:1 zibll search-box + navbar-search + newposts unify
Port aksrt_get_search_box/posts for menu_search and layout 1/3 slide;
unify publish switch; expand user-box admin row; CSS/JS remote-box parity.
2026-07-22 17:06:24 +08:00

199 lines
6.9 KiB
PHP

<?php
/**
* Header shell orchestration (zibll zib_header contract).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_header_nav_context')) :
/**
* Shared header render context (settings + brand + user flags).
*
* @return array<string,mixed>
*/
function aksrt_get_header_nav_context(): array {
static $ctx = null;
if ($ctx !== null) {
return $ctx;
}
$settings = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_header_nav_settings()
: [
'fixed' => true,
'search_enabled' => true,
'user_menu' => true,
'publish_btn' => true,
'mobile_drawer' => true,
'nav_style' => 'default',
'max_width' => 1200,
'scroll_down_hide' => true,
'layout' => '1',
'mobile_layout' => 'center',
'mobile_align' => 'left',
'nav_pay_vip' => true,
'nav_user_pay_vip' => true,
];
$site_name = get_bloginfo('name');
$brand_name = function_exists('aksrt_get_site_logo_text') ? aksrt_get_site_logo_text() : $site_name;
$brand_name = $brand_name !== '' ? $brand_name : $site_name;
$logo_url = function_exists('aksrt_get_site_logo_url') ? trim((string) aksrt_get_site_logo_url()) : '';
$logo_dark = function_exists('aksrt_get_site_logo_dark_url') ? trim((string) aksrt_get_site_logo_dark_url()) : '';
$logo_width = function_exists('aksrt_get_site_logo_width') ? aksrt_get_site_logo_width() : 150;
$logo_more = $logo_width > 0
? 'style="height:36px;width:auto;max-width:' . max(36, (int) $logo_width) . 'px"'
: 'style="height:36px;width:auto"';
$logo_html = '';
if ($logo_url !== '' || $logo_dark !== '') {
$logo_html = function_exists('aksrt_get_adaptive_theme_img')
? aksrt_get_adaptive_theme_img($logo_url, $logo_dark, $brand_name, $logo_more)
: '';
}
$theme_positions = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_theme_mode_button_positions()
: ['pc_nav', 'm_menu'];
$can_publish = !empty($settings['publish_btn']) && (
current_user_can('manage_options')
|| (class_exists('\AKSRT\Core\FrontSubmit') && \AKSRT\Core\FrontSubmit::get_instance()->can_submit())
);
$show_slide = function_exists('aksrt_header_slide_is_show')
? aksrt_header_slide_is_show()
: false;
$current_user = wp_get_current_user();
$user_id = (int) $current_user->ID;
$user_desc = $user_id > 0 ? (string) get_user_meta($user_id, 'description', true) : '';
if ($user_desc === '' && $user_id > 0) {
$user_desc = (string) $current_user->user_email;
}
$ctx = [
'settings' => $settings,
'layout' => (string) ($settings['layout'] ?? '1'),
'mobile_layout' => (string) ($settings['mobile_layout'] ?? 'center'),
'mobile_align' => (string) ($settings['mobile_align'] ?? 'left'),
'fixed' => !empty($settings['fixed']),
'scroll_down_hide' => !empty($settings['scroll_down_hide']) && !empty($settings['fixed']),
'search_enabled' => !empty($settings['search_enabled']),
'user_menu' => !empty($settings['user_menu']),
'mobile_drawer' => !empty($settings['mobile_drawer']),
'nav_pay_vip' => !isset($settings['nav_pay_vip']) || !empty($settings['nav_pay_vip']),
'nav_user_pay_vip' => !isset($settings['nav_user_pay_vip']) || !empty($settings['nav_user_pay_vip']),
'max_width' => (int) ($settings['max_width'] ?? 1200),
'brand_name' => $brand_name,
'logo_html' => $logo_html,
'has_logo' => $logo_html !== '',
'theme_positions' => is_array($theme_positions) ? $theme_positions : [],
'can_publish' => $can_publish,
'show_slide' => $show_slide,
'current_user' => $current_user,
'user_id' => $user_id,
'user_desc' => $user_desc,
'unread_count' => ($user_id > 0 && function_exists('aksrt_get_user_unread_notification_count'))
? (int) aksrt_get_user_unread_notification_count($user_id)
: 0,
'messages_url' => function_exists('aksrt_get_messages_page_url') ? aksrt_get_messages_page_url() : '',
'orders_url' => function_exists('aksrt_get_orders_page_url') ? aksrt_get_orders_page_url() : '',
'publish_url' => function_exists('aksrt_get_new_post_url') ? aksrt_get_new_post_url() : admin_url('post-new.php'),
'theme_settings_url' => function_exists('aksrt_get_admin_settings_url')
? aksrt_get_admin_settings_url('')
: admin_url('admin.php?page=aksrt-settings'),
'search_state' => function_exists('aksrt_get_search_form_state')
? aksrt_get_search_form_state()
: [
'type' => 'post',
'types' => ['post' => __('文章', 'aksrt')],
'category_id' => 0,
'category_enabled' => false,
'categories' => [],
],
'search_placeholder' => function_exists('aksrt_get_search_placeholder')
? aksrt_get_search_placeholder()
: __('开启精彩搜索', 'aksrt'),
'search_hot_keywords' => function_exists('aksrt_get_search_hot_keywords')
? aksrt_get_search_hot_keywords()
: [],
];
if ($ctx['publish_url'] === '') {
$ctx['publish_url'] = admin_url('post-new.php');
}
return $ctx;
}
endif;
if (!function_exists('aksrt_header')) :
/**
* Render top header + mobile drawer (+ optional slide after caller).
*/
function aksrt_header(): void {
$ctx = aksrt_get_header_nav_context();
$layout = $ctx['layout'];
$header_class = [
'header',
'header-layout-' . $layout,
];
if (!empty($ctx['show_slide'])) {
$header_class[] = 'show-slide';
}
if (!empty($ctx['scroll_down_hide'])) {
$header_class[] = 'scroll-down-hide';
}
$nav_class = [
'navbar',
'navbar-top',
$ctx['mobile_layout'],
];
$nav_style = '';
if ((int) $ctx['max_width'] > 0) {
$nav_style = ' style="--nav-max-width:' . (int) $ctx['max_width'] . 'px"';
}
echo '<header class="' . esc_attr(implode(' ', array_map('sanitize_html_class', $header_class))) . '">';
echo '<nav class="' . esc_attr(implode(' ', array_map('sanitize_html_class', $nav_class))) . '"' . $nav_style . '>';
echo '<div class="container-fluid container-header">';
aksrt_navbar_header($ctx);
echo '<div class="collapse navbar-collapse">';
if ('3' !== $layout) {
aksrt_menu_items('primary', true);
}
if ('2' === $layout && !empty($ctx['search_enabled'])) {
aksrt_get_menu_search($ctx);
}
aksrt_menu_button($ctx);
if ('3' === $layout) {
echo '<div class="navbar-right">';
aksrt_menu_items('primary', true);
echo '</div>';
}
echo '</div></div></nav></header>';
if (!empty($ctx['mobile_drawer'])) {
aksrt_nav_mobile_shell($ctx);
}
// Layout 1/3 search slide (zibll .navbar-search). Layout-2 uses inline menu_search.
if (!empty($ctx['search_enabled'])) {
$navbar_search = locate_template('templates/parts/navbar-search.php');
if ($navbar_search) {
include $navbar_search;
}
}
if (function_exists('aksrt_render_signout_modal')) {
aksrt_render_signout_modal();
}
}
endif;