Files
wordpress-theme-aksrt/inc/helpers/aksrt-header-menu-buttons.php
T

303 lines
11 KiB
PHP

<?php
/**
* Header multi-function cluster (zibll zib_menu_button publish/VIP subset).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_header_newposts_config')) :
/**
* @return array{enabled:bool,text:string,radius:int,radius_pill:bool,class:string,types:string[]}
*/
function aksrt_get_header_newposts_config(): array {
$cfg = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_header_newposts_config()
: ['enabled' => true, 'text' => __('发布', 'aksrt'), 'radius' => 20, 'class' => 'jb-blue', 'types' => []];
// Single source: header_newposts_enabled (zibll nav_newposts).
// FacadeBulk maps publish_btn to the same setting.
$types = is_array($cfg['types'] ?? null) ? $cfg['types'] : [];
if (!$types) {
$types = ['post'];
if (class_exists('\AKSRT\Settings\Settings')) {
$forum = \AKSRT\Settings\Settings::get_forum_settings();
$shop = \AKSRT\Settings\Settings::get_shop_settings();
if (!empty($forum['enabled'])) {
$types[] = 'topic';
$types[] = 'plate';
}
if (!empty($shop['enabled'])) {
$types[] = 'product';
}
}
}
$cfg['types'] = $types;
if (empty($cfg['class'])) {
$cfg['class'] = 'jb-blue';
}
return $cfg;
}
endif;
if (!function_exists('aksrt_get_new_add_actions')) :
/**
* Build multi-type publish actions.
*
* @param string[] $types post|topic|product
* @return list<array{type:string,url:string,label:string,icon:string}>
*/
function aksrt_get_new_add_actions(array $types = ['post']): array {
$out = [];
foreach ($types as $type) {
$type = sanitize_key((string) $type);
if ($type === 'post') {
// Front submit page may be unset → aksrt_get_new_post_url() returns ''.
// Never drop the publish chip for that: fall back to WP post-new.
$url = function_exists('aksrt_get_new_post_url') ? (string) aksrt_get_new_post_url() : '';
if ($url === '') {
$url = admin_url('post-new.php');
}
$out[] = [
'type' => 'post',
'url' => $url,
'label' => __('写文章', 'aksrt'),
'icon' => 'fas fa-file-alt',
];
} elseif ($type === 'topic') {
$url = function_exists('aksrt_get_forum_compose_url') ? aksrt_get_forum_compose_url() : '';
if ($url === '') {
continue;
}
$out[] = [
'type' => 'topic',
'url' => $url,
'label' => __('发帖', 'aksrt'),
'icon' => 'fas fa-comments',
];
} elseif ($type === 'product') {
$url = '';
if (function_exists('aksrt_get_product_edit_url')) {
$url = (string) aksrt_get_product_edit_url();
}
if ($url === '') {
$url = admin_url('post-new.php?post_type=aksrt_product');
}
$out[] = [
'type' => 'product',
'url' => $url,
'label' => __('发布商品', 'aksrt'),
'icon' => 'fas fa-shopping-bag',
];
} elseif ($type === 'plate') {
// zibll bbs_plate → AKSRT forum plate CPT (admin create for entitled users).
$can = current_user_can('manage_options');
if (!$can && class_exists('\AKSRT\Forum\Forum')) {
try {
$forum = \AKSRT\Forum\Forum::get_instance();
if (method_exists($forum, 'forum_user_can')) {
$can = (bool) $forum->forum_user_can(get_current_user_id(), 'plate_create');
}
} catch (\Throwable $e) {
$can = false;
}
}
if (!$can || !post_type_exists('aksrt_forum_plate')) {
continue;
}
$out[] = [
'type' => 'plate',
'url' => admin_url('post-new.php?post_type=aksrt_forum_plate'),
'label' => __('创建版块', 'aksrt'),
'icon' => 'fas fa-th-large',
];
}
}
return $out;
}
endif;
if (!function_exists('aksrt_normalize_header_cta_label')) :
/**
* Normalize CTA label HTML: map FA4 classes and keep a single leading icon.
*/
function aksrt_normalize_header_cta_label(string $label, string $fallback_icon = 'fas fa-pen'): string {
$label = trim($label);
if ($label === '') {
return '<i class="' . esc_attr($fallback_icon) . '" aria-hidden="true"></i> ' . esc_html__('发布', 'aksrt');
}
$replacements = [
'fa-file-text-o' => 'fa-file-alt',
'fa-file-text' => 'fa-file-alt',
'fa-pencil-square-o' => 'fa-edit',
'fa-pencil' => 'fa-pen',
'fa-comments-o' => 'fa-comments',
'fa-clock-o' => 'fa-clock',
'fa-calendar-check-o' => 'fa-calendar-check',
'fa-diamond' => 'fa-gem',
];
$label = strtr($label, $replacements);
$label = preg_replace('/\bclass=(["\'])fa\s+/', 'class=$1fas ', $label) ?? $label;
$label = preg_replace('/<i\s+class=(["\'])(?!fas\b|far\b|fab\b)/', '<i class=$1fas ', $label) ?? $label;
if (stripos($label, '<i') === false) {
// No space between icon and label — spacing via CSS margin (zibll .but > .fa).
$label = '<i class="' . esc_attr($fallback_icon) . '" aria-hidden="true"></i><span class="navbar-cta-text">' . esc_html(wp_strip_all_tags($label)) . '</span>';
} else {
$label = preg_replace(
'/(<\/i>)\s*([^<].*)$/u',
'$1<span class="navbar-cta-text">$2</span>',
$label
) ?? $label;
}
return $label;
}
endif;
if (!function_exists('aksrt_render_new_add_btns')) :
/**
* Multi-type publish button (zibll newadd-btns).
*
* @param string[] $types
*/
function aksrt_render_new_add_btns(array $types, string $class = '', string $label = ''): string {
$actions = aksrt_get_new_add_actions($types);
if (!$actions) {
// Last resort: keep a write-post entry so the top-nav chip is never blank.
$fallback = function_exists('aksrt_get_new_post_url') ? (string) aksrt_get_new_post_url() : '';
if ($fallback === '') {
$fallback = admin_url('post-new.php');
}
$actions = [[
'type' => 'post',
'url' => $fallback,
'label' => __('写文章', 'aksrt'),
'icon' => 'fas fa-file-alt',
]];
}
if ($label === '') {
$label = __('发布', 'aksrt');
}
$label_html = aksrt_normalize_header_cta_label((string) $label, 'fas fa-pen');
// Split paint classes vs wrapper tokens.
$class_tokens = preg_split('/\s+/', trim($class), -1, PREG_SPLIT_NO_EMPTY) ?: [];
$cta_tokens = [];
$wrap_tokens = ['newadd-btns'];
foreach ($class_tokens as $token) {
if ($token === 'newadd-btns') {
continue;
}
if ($token === 'btn-primary' || $token === 'but' || $token === 'navbar-cta' || $token === 'radius' || $token === 'main-radius' || $token === 'mini-radius' || str_starts_with($token, 'jb-') || str_starts_with($token, 'b-')) {
$cta_tokens[] = $token;
continue;
}
$wrap_tokens[] = $token;
}
if (!$cta_tokens) {
$cta_tokens[] = 'jb-blue';
}
if (!in_array('but', $cta_tokens, true)) {
$cta_tokens[] = 'but';
}
$cta_class = trim(implode(' ', array_unique($cta_tokens)));
if (count($actions) === 1) {
$a = $actions[0];
return '<a rel="nofollow" class="' . esc_attr(trim(implode(' ', $wrap_tokens) . ' ' . $cta_class . ' navbar-cta')) . '" href="' . esc_url($a['url']) . '">'
. $label_html
. '</a>';
}
// zibll: span.newadd-btns.hover-show > toggle chip + .hover-show-con.drop-newadd
// Paint classes (jb-*, but, radius) MUST live on the toggle — wrapper is layout-only
// (background forced transparent). Bare navbar-but has no .navbar-but-cluster shell.
// data-aksrt-newadd: JS may portal menu to body so chip width cannot track menu.
$html = '<span class="' . esc_attr(trim(implode(' ', $wrap_tokens) . ' hover-show')) . '" data-aksrt-newadd="1">';
$html .= '<span class="newadd-btns__toggle navbar-cta ' . esc_attr($cta_class) . '">' . $label_html
. '<i class="fas fa-caret-down" aria-hidden="true"></i></span>';
$html .= '<div class="hover-show-con dropdown-menu drop-newadd" role="menu">';
foreach ($actions as $a) {
$html .= '<a rel="nofollow" class="btn-newadd newadd-btns__item" role="menuitem" href="' . esc_url($a['url']) . '">';
$html .= '<i class="' . esc_attr($a['icon']) . '" aria-hidden="true"></i> '
. '<text>' . esc_html($a['label']) . '</text>';
$html .= '</a>';
}
$html .= '</div></span>';
return $html;
}
endif;
if (!function_exists('aksrt_render_header_menu_buttons')) :
/**
* Publish + optional VIP open button for header CTA.
*/
function aksrt_render_header_menu_buttons(): string {
$cfg = aksrt_get_header_newposts_config();
$html = '';
if (!empty($cfg['enabled'])) {
// zibll default: but nowave jb-blue (+ radius).
$class = 'but nowave navbar-newposts';
$skin = trim((string) ($cfg['class'] ?? ''));
if ($skin === '') {
$skin = 'jb-blue';
}
$class .= ' ' . $skin;
$radius = (int) ($cfg['radius'] ?? 20);
$pill = !empty($cfg['radius_pill']) || $radius >= 16;
if ($pill && !str_contains($class, 'radius')) {
$class .= ' radius';
}
$radius_attr = $radius > 0
? ' data-radius="' . esc_attr((string) $radius) . '" style="--navbar-cta-radius:' . $radius . 'px"'
: '';
$label = (string) ($cfg['text'] ?? __('发布', 'aksrt'));
$btn = aksrt_render_new_add_btns($cfg['types'] ?? ['post'], $class, $label);
if ($btn !== '') {
$html .= '<div class="navbar-form navbar-right navbar-but"' . $radius_attr . '>' . $btn . '</div>';
}
}
// VIP open CTA for guests / non-VIP (zibll: .pay-vip modal).
$user_id = get_current_user_id();
$show_vip = false;
if (class_exists('\AKSRT\Settings\Settings')) {
$vs = \AKSRT\Settings\Settings::get_payment_vip_settings();
$show_vip = !empty($vs['enabled']) && !empty(\AKSRT\Settings\Settings::get_header_nav_settings()['nav_pay_vip']);
}
if ($show_vip) {
$is_vip = false;
if ($user_id > 0 && function_exists('aksrt_get_user_vip_level')) {
$is_vip = (int) aksrt_get_user_vip_level($user_id) > 0;
}
if (!$is_vip) {
$cls = $user_id > 0 ? 'pay-vip' : 'signin-loader';
$hover = '';
if (class_exists('\AKSRT\Content\VipService')) {
$svc = \AKSRT\Content\VipService::get_instance();
if (method_exists($svc, 'render_vip_card')) {
$cards = $svc->render_vip_card(1, $user_id) . $svc->render_vip_card(2, $user_id);
if ($cards !== '') {
$hover = '<div class="hover-show-con sub-menu sub-vip-card">' . $cards . '</div>';
}
}
}
$html .= '<div class="navbar-form navbar-right navbar-vip">';
// zibll: span.hover-show wraps button + .hover-show-con.sub-vip-card
$html .= '<span class="hover-show inline-block navbar-vip-hover">';
$html .= '<a class="but jb-red radius payvip-icon navbar-cta ' . esc_attr($cls) . '" href="javascript:;"'
. ($user_id > 0 ? ' vip-level="1"' : '') . '>';
$html .= '<i class="fas fa-gem" aria-hidden="true"></i>'
. '<span class="navbar-cta-text">' . esc_html__('开通会员', 'aksrt') . '</span>';
$html .= '</a>' . $hover . '</span></div>';
}
}
return $html;
}
endif;