*/ function aksrt_get_search_history_keywords(): array { $hist = []; if (is_user_logged_in()) { $saved = get_user_meta(get_current_user_id(), 'aksrt_search_history', true); if (is_array($saved)) { foreach (array_slice($saved, 0, 10) as $item) { if (is_array($item) && !empty($item['keyword'])) { $hist[] = (string) $item['keyword']; } elseif (is_string($item) && $item !== '') { $hist[] = $item; } } } } return array_values(array_unique(array_filter(array_map('trim', $hist)))); } endif; if (!function_exists('aksrt_get_search_keywords_but')) : /** * Render keyword chip links. * * @param list $keywords * @param string $type popular|history */ function aksrt_get_search_keywords_but(array $keywords, string $type = 'popular', int $limit = 0): string { if ($limit > 0) { $keywords = array_slice($keywords, 0, $limit); } $out = ''; foreach ($keywords as $kw) { $kw = trim((string) $kw); if ($kw === '') { continue; } $out .= '' . esc_html($kw) . ' '; } return $out; } endif; if (!function_exists('aksrt_get_search_box')) : /** * zibll-compatible search box. * * @param array $args * @param bool $echo * @return string|void */ function aksrt_get_search_box(array $args = [], bool $echo = false) { $defaults = [ 's' => '', 'class' => '', 'show_form' => true, 'show_keywords' => true, 'popular_limit' => 0, 'show_history' => true, 'keywords_title' => '', 'placeholder' => '', 'show_input_cat' => true, 'show_posts' => false, 'show_type' => false, 'in_cat' => '', 'in_type' => 'post', 'in_user' => '', 'more_cats' => [], ]; $args = wp_parse_args($args, $defaults); if ($args['keywords_title'] === '' && class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'search_popular_title')) { $args['keywords_title'] = (string) \AKSRT\Settings\Settings::search_popular_title(); } if ($args['keywords_title'] === '') { $args['keywords_title'] = __('热门搜索', 'aksrt'); } if ($args['placeholder'] === '' && function_exists('aksrt_get_search_placeholder')) { $args['placeholder'] = aksrt_get_search_placeholder(); } if ($args['placeholder'] === '') { $args['placeholder'] = __('开启精彩搜索', 'aksrt'); } $args['placeholder'] = esc_attr((string) $args['placeholder']); // Settings gates when caller left defaults on. if (class_exists('\AKSRT\Settings\Settings')) { if (!array_key_exists('show_keywords_forced', $args) && method_exists('\AKSRT\Settings\Settings', 'hot_search_enabled') && !\AKSRT\Settings\Settings::hot_search_enabled()) { // Keep explicit false from caller; only gate when show_keywords true by default path. } } $form_html = ''; if (!empty($args['show_form'])) { $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' => [], ]; $type_html = ''; $types = is_array($state['types'] ?? null) ? $state['types'] : []; $in_type = (string) ($args['in_type'] ?: ($state['type'] ?? 'post')); if (!empty($args['show_type']) && count($types) > 1) { $type_items = ''; foreach ($types as $k => $v) { $cls = $in_type === (string) $k ? ' active' : ''; $type_items .= '' . esc_html((string) $v) . ''; } $type_html = '
'; $type_html .= '
' . $type_items . '
'; } $cat_html = ''; if (!empty($args['show_input_cat']) && !empty($state['category_enabled']) && !empty($state['categories']) && is_array($state['categories'])) { $in_cat_id = (int) ($args['in_cat'] ?: ($state['category_id'] ?? 0)); $in_cat_name = ''; $all_cat = '
  • ' . esc_html__('全部', 'aksrt') . '
  • '; foreach ($state['categories'] as $cat) { if (!is_array($cat)) { continue; } $cid = (int) ($cat['id'] ?? 0); $cname = (string) ($cat['name'] ?? ''); if ($cid <= 0 || $cname === '') { continue; } if ($cid === $in_cat_id) { $in_cat_name = $cname; } $all_cat .= '
  • ' . esc_html(mb_strlen($cname) > 8 ? mb_substr($cname, 0, 8) . '…' : $cname) . '
  • '; } $all_cat = ''; $label = $in_cat_name !== '' ? (mb_strlen($in_cat_name) > 6 ? mb_substr($in_cat_name, 0, 6) . '…' : $in_cat_name) : __('请选择', 'aksrt'); $input_cat = '' . esc_html($label) . ''; $input_cat .= $all_cat; $cat_html = '
    '; } $s = esc_attr(wp_strip_all_tags((string) $args['s'])); if ($s === '' && is_search()) { $s = esc_attr(get_search_query()); } $input_html = '
    '; $input_html .= ''; $input_html .= ''; $input_html .= '
    ' . $args['placeholder'] . '
    '; $input_html .= '
    '; $input_html .= '
    '; $input_html .= ''; if ($cat_html !== '' || (string) $args['in_cat'] !== '') { $input_html .= ''; } if ((string) $args['in_user'] !== '') { $input_html .= ''; } if ($cat_html === '') { $cat_html = $type_html; $type_html = ''; } $form_html = ''; } // Popular keywords. $keywords_html = ''; if (!empty($args['show_keywords'])) { $show = true; if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'hot_search_enabled')) { $show = (bool) \AKSRT\Settings\Settings::hot_search_enabled(); } if ($show) { $keywords = function_exists('aksrt_get_search_hot_keywords') ? aksrt_get_search_hot_keywords() : []; $limit = (int) $args['popular_limit']; $keyword_link = aksrt_get_search_keywords_but(is_array($keywords) ? $keywords : [], 'popular', $limit); if ($keyword_link !== '') { $keywords_html = '
    ' . '

    ' . esc_html((string) $args['keywords_title']) . '

    ' . '
    ' . $keyword_link . '
    ' . '
    '; } } } // History. $history_html = ''; if (!empty($args['show_history'])) { $show_h = true; if (function_exists('aksrt_search_history_enabled')) { $show_h = aksrt_search_history_enabled(); } elseif (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'search_history_enabled')) { $show_h = (bool) \AKSRT\Settings\Settings::search_history_enabled(); } if ($show_h) { $hist = aksrt_get_search_history_keywords(); // Empty shell still rendered so client localStorage can fill it. $keyword_link = aksrt_get_search_keywords_but($hist, 'history'); $history_html = ''; } } // Hot posts (thumb swiper). $posts_html = ''; if (!empty($args['show_posts'])) { $show_p = true; if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'search_posts_enabled')) { $show_p = (bool) \AKSRT\Settings\Settings::search_posts_enabled(); } if ($show_p && function_exists('aksrt_get_search_posts')) { $slides = aksrt_get_search_posts(6, 'views', true); if ($slides !== '') { $posts_html = '
    '; $posts_html .= '

    ' . esc_html__('热门文章', 'aksrt') . '

    '; $posts_html .= '
    '; $posts_html .= '
    ' . $slides . '
    '; $posts_html .= '
    '; $posts_html .= '
    '; } } } // Live suggestions mount (AKSRT extension; does not break zibll classes). $suggest = ''; $class = $args['class'] ? ' ' . sanitize_html_class((string) $args['class']) : ''; $html = '
    ' . $form_html . $keywords_html . $history_html . $posts_html . $suggest . '
    '; if ($echo) { echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } return $html; } endif; if (!function_exists('aksrt_get_remote_box')) : /** * Lazy remote HTML mount (zibll zib_get_remote_box). * * @param array $args */ function aksrt_get_remote_box(array $args = []): string { $defaults = [ 'type' => 'ias', 'class' => '', 'loader' => '', 'url' => admin_url('admin-ajax.php'), 'query' => false, ]; $args = wp_parse_args($args, $defaults); $url = (string) $args['url']; if (!empty($args['query']) && is_array($args['query'])) { $url = add_query_arg($args['query'], $url); } $attr = ' remote-box="' . esc_url($url) . '"'; $class = (string) $args['class']; if (($args['type'] ?? '') === 'ias') { $attr .= ' lazyload-action="ias"'; $class .= ' lazyload'; } if (($args['type'] ?? '') === 'load') { $attr .= ' load-click'; } $class_attr = $class !== '' ? ' class="' . esc_attr(trim($class)) . '"' : ''; return '' . $args['loader'] . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } endif;