feat(shell): header multifunc + window._win + float cart/VIP

Multi-type publish/VIP CTA in nav, zibll-like _win runtime bridge for
lightbox flags, and floating shop_cart/pay_vip commerce buttons.
This commit is contained in:
kate522 committed 2026-07-18 22:16:17 +08:00
1 parent 09029116ae
commit f929cc649c
9 files changed
+994 -441

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
<?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,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' => ''];
$all = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
// Prefer publish_btn from header nav when newposts not explicitly set.
$nav = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_header_nav_settings()
: ['publish_btn' => true];
if (empty($cfg['enabled']) && !empty($nav['publish_btn'])) {
$cfg['enabled'] = true;
}
$types = [];
if (!empty($all['header_newposts_types']) && is_array($all['header_newposts_types'])) {
$types = array_values(array_filter(array_map('sanitize_key', $all['header_newposts_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';
}
if (!empty($shop['enabled'])) {
$types[] = 'product';
}
}
}
$cfg['types'] = $types;
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') {
$url = function_exists('aksrt_get_new_post_url') ? aksrt_get_new_post_url() : admin_url('post-new.php');
if ($url === '') {
continue;
}
$out[] = [
'type' => 'post',
'url' => $url,
'label' => __('写文章', 'aksrt'),
'icon' => 'fa fa-file-text-o',
];
} 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' => 'fa fa-comments-o',
];
} elseif ($type === 'product') {
// Prefer front product edit page when registered.
$url = '';
if (function_exists('aksrt_get_product_edit_url')) {
$url = (string) aksrt_get_product_edit_url();
}
if ($url === '') {
// Fallback: product-edit page template lookup is optional.
$url = home_url('/?page_id=0');
// Use admin product CPT if available.
$url = admin_url('post-new.php?post_type=aksrt_product');
}
$out[] = [
'type' => 'product',
'url' => $url,
'label' => __('发布商品', 'aksrt'),
'icon' => 'fa fa-shopping-bag',
];
}
}
return $out;
}
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) {
return '';
}
if ($label === '') {
$label = __('发布', 'aksrt');
}
$class = trim('newadd-btns ' . $class);
if (count($actions) === 1) {
$a = $actions[0];
return '<a rel="nofollow" class="' . esc_attr(trim($class . ' but')) . '" href="' . esc_url($a['url']) . '">'
. '<i class="' . esc_attr($a['icon']) . '" aria-hidden="true"></i> '
. wp_kses_post($label)
. '</a>';
}
$html = '<span class="' . esc_attr($class) . ' hover-show">';
$html .= '<a href="javascript:;" class="but newadd-btns__toggle" rel="nofollow">' . wp_kses_post($label) . ' <i class="fa fa-caret-down" aria-hidden="true"></i></a>';
$html .= '<div class="hover-show-con dropdown-menu drop-newadd">';
foreach ($actions as $a) {
$html .= '<a rel="nofollow" class="newadd-btns__item" href="' . esc_url($a['url']) . '">';
$html .= '<i class="' . esc_attr($a['icon']) . '" aria-hidden="true"></i> ' . esc_html($a['label']);
$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'])) {
$class = 'btn-primary navbar-newposts';
if (!empty($cfg['class'])) {
$class .= ' ' . $cfg['class'];
}
$radius = (int) ($cfg['radius'] ?? 20);
$style = $radius > 0 ? ' style="border-radius:' . $radius . 'px"' : '';
$label = (string) ($cfg['text'] ?? __('发布', 'aksrt'));
// Strip outer <i> if present in settings HTML text — keep simple.
$btn = aksrt_render_new_add_btns($cfg['types'] ?? ['post'], $class, $label);
if ($btn !== '') {
$html .= '<div class="navbar-form navbar-right navbar-but"' . $style . '>' . $btn . '</div>';
}
}
// VIP open CTA for guests / non-VIP.
$user_id = get_current_user_id();
$vip_url = function_exists('aksrt_get_vip_purchase_url') ? aksrt_get_vip_purchase_url() : (function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url('vip') : '#');
$show_vip = true;
if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_vip_settings')) {
$vs = \AKSRT\Settings\Settings::get_vip_settings();
$show_vip = !empty($vs['enabled']) || !empty($vs['vip1_enabled']) || !empty($vs['vip2_enabled']);
}
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 ? 'pay-vip' : 'signin-loader';
$html .= '<div class="navbar-form navbar-right navbar-vip hover-show inline-block">';
$html .= '<a class="but jb-red radius payvip-icon ml10 ' . esc_attr($cls) . '" href="' . ($user_id ? esc_url($vip_url) : 'javascript:;') . '">';
$html .= '<i class="fa fa-diamond em12 mr3" aria-hidden="true"></i>' . esc_html__('开通会员', 'aksrt');
$html .= '</a></div>';
}
}
return $html;
}
endif;
+179
View File
@@ -0,0 +1,179 @@
<?php
/**
* Float button commerce types + runtime window._win bridge (zibll parity).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_get_float_shop_cart_html')) :
/**
* Floating shop cart button with badge.
*/
function aksrt_get_float_shop_cart_html(array $item = []): string {
if (!is_user_logged_in()) {
return '';
}
$shop_on = true;
if (class_exists('\AKSRT\Settings\Settings')) {
$shop = \AKSRT\Settings\Settings::get_shop_settings();
$shop_on = !empty($shop['enabled']);
}
if (!$shop_on) {
return '';
}
// Skip on cart page.
if (function_exists('is_page_template') && is_page_template('templates/pages/page-cart.php')) {
return '';
}
$url = function_exists('aksrt_get_cart_page_url') ? aksrt_get_cart_page_url() : '';
if ($url === '') {
return '';
}
$count = 0;
if (class_exists('\AKSRT\Shop\Shop')) {
$count = (int) \AKSRT\Shop\Shop::get_instance()->get_cart_count(get_current_user_id());
}
$icon = !empty($item['icon']) ? (string) $item['icon'] : 'fas fa-shopping-cart';
$style = [];
if (!empty($item['color'])) {
$style[] = '--this-color:' . esc_attr((string) $item['color']);
}
if (!empty($item['bg'])) {
$style[] = '--this-bg:' . esc_attr((string) $item['bg']);
}
$style_attr = $style ? ' style="' . implode(';', $style) . '"' : '';
$badge = $count > 0 ? (string) min(99, $count) : '';
return '<a class="float-btn float-btn-shop-cart nowave" href="' . esc_url($url) . '" title="' . esc_attr__('购物车', 'aksrt') . '"' . $style_attr . '>'
. '<i class="' . esc_attr($icon) . '" aria-hidden="true"></i>'
. '<badge cart-count="' . esc_attr($badge) . '">' . esc_html($badge) . '</badge>'
. '</a>';
}
endif;
if (!function_exists('aksrt_get_float_pay_vip_html')) :
/**
* Floating VIP open button.
*/
function aksrt_get_float_pay_vip_html(array $item = []): string {
$show_vip = true;
if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_vip_settings')) {
$vs = \AKSRT\Settings\Settings::get_vip_settings();
$show_vip = !empty($vs['enabled']) || !empty($vs['vip1_enabled']) || !empty($vs['vip2_enabled']);
}
if (!$show_vip) {
return '';
}
$user_id = get_current_user_id();
$level = 0;
if ($user_id > 0 && function_exists('aksrt_get_user_vip_level')) {
$level = (int) aksrt_get_user_vip_level($user_id);
}
// Hide for max VIP if we only have 2 tiers and already VIP.
if ($level >= 2) {
return '';
}
$vip_url = function_exists('aksrt_get_vip_purchase_url')
? aksrt_get_vip_purchase_url()
: (function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url('vip') : '#');
$title = $level === 1 ? __('升级会员', 'aksrt') : __('开通会员', 'aksrt');
$icon = !empty($item['icon']) ? (string) $item['icon'] : 'fa fa-diamond';
$cls = $user_id > 0 ? 'float-btn pay-vip' : 'float-btn signin-loader';
$href = $user_id > 0 ? $vip_url : 'javascript:;';
$style = [];
if (!empty($item['color'])) {
$style[] = '--this-color:' . esc_attr((string) $item['color']);
}
if (!empty($item['bg'])) {
$style[] = '--this-bg:' . esc_attr((string) $item['bg']);
}
$style_attr = $style ? ' style="' . implode(';', $style) . '"' : '';
return '<a class="' . esc_attr($cls) . '" href="' . esc_url($href) . '" title="' . esc_attr($title) . '"' . $style_attr . '>'
. '<i class="' . esc_attr($icon) . '" aria-hidden="true"></i>'
. '</a>';
}
endif;
if (!function_exists('aksrt_get_win_runtime_config')) :
/**
* Build window._win payload (zibll zib_win_var subset).
*
* @return array<string,mixed>
*/
function aksrt_get_win_runtime_config(): array {
$post_display = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_post_page_settings()
: [];
$is_mobile = wp_is_mobile();
$list_style = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_post_list_style_settings()
: [];
$signin = function_exists('aksrt_get_login_url')
? aksrt_get_login_url(function_exists('aksrt_get_current_url') ? aksrt_get_current_url() : home_url('/'))
: wp_login_url();
$signup = function_exists('aksrt_get_register_url')
? aksrt_get_register_url()
: wp_registration_url();
$views = 0;
if (is_singular()) {
$views = (int) get_queried_object_id();
}
return [
'views' => $views,
'www' => home_url('/'),
'uri' => get_template_directory_uri(),
'ver' => defined('AKSRT_VERSION') ? AKSRT_VERSION : '3.4.0-dev',
'imgbox' => !isset($post_display['lightbox']) || !empty($post_display['lightbox']) ? '1' : '',
'imgbox_type' => 'group',
'imgbox_thumbs' => !isset($post_display['lightbox_thumbs']) || !empty($post_display['lightbox_thumbs']) ? '1' : '',
'imgbox_zoom' => !isset($post_display['lightbox_zoom']) || !empty($post_display['lightbox_zoom']) ? '1' : '',
'imgbox_full' => !isset($post_display['lightbox_full']) || !empty($post_display['lightbox_full']) ? '1' : '',
'imgbox_play' => !empty($post_display['lightbox_play']) ? '1' : '',
'imgbox_down' => !empty($post_display['lightbox_down']) ? '1' : '',
'sign_type' => class_exists('\AKSRT\Settings\Settings')
? (string) (\AKSRT\Settings\Settings::get_all()['user_sign_type'] ?? 'page')
: 'page',
'signin_url' => $signin,
'signup_url' => $signup,
'ajax_url' => admin_url('admin-ajax.php'),
'ajaxpager' => '1',
'ajax_trigger' => (string) ($list_style['ajax_trigger_text'] ?? __('加载更多', 'aksrt')),
'ajax_nomore' => (string) ($list_style['ajax_nomore_text'] ?? __('没有更多了', 'aksrt')),
'qj_loading' => __('加载中...', 'aksrt'),
'is_mobile' => $is_mobile ? '1' : '',
'is_logged_in' => is_user_logged_in() ? '1' : '',
'user_id' => get_current_user_id(),
'rest_url' => esc_url_raw(rest_url('aksrt/v1')),
'rest_nonce' => wp_create_nonce('wp_rest'),
'nonce' => wp_create_nonce('aksrt_public_nonce'),
];
}
endif;
if (!function_exists('aksrt_print_win_var')) :
/**
* Echo window._win + alias window.aksrtData bridge.
*/
function aksrt_print_win_var(): void {
static $done = false;
if ($done) {
return;
}
$done = true;
$cfg = aksrt_get_win_runtime_config();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<script type="text/javascript">window._win=' . wp_json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . ';'
. 'window.aksrtData=window.aksrtData||{};'
. 'window.aksrtData.ajaxUrl=window.aksrtData.ajaxUrl||window._win.ajax_url;'
. 'window.aksrtData.siteUrl=window.aksrtData.siteUrl||window._win.www;'
. 'window.aksrtData.themeUri=window.aksrtData.themeUri||window._win.uri;'
. 'window.aksrtData.restUrl=window.aksrtData.restUrl||window._win.rest_url;'
. 'window.aksrtData.restNonce=window.aksrtData.restNonce||window._win.rest_nonce;'
. 'window.aksrtData.nonce=window.aksrtData.nonce||window._win.nonce;'
. 'window.aksrtData.imgbox=window._win;'
. '</script>' . "\n";
}
endif;