= 30 || headers_sent()) { return; } $history = aksrt_get_search_history_keywords(); $history = array_values(array_filter($history, static function (string $item) use ($search_text): bool { return aksrt_get_search_keywords_text($item) !== $search_text; })); array_unshift($history, $search); $history = array_slice($history, 0, 30); setcookie('history_search', wp_json_encode($history), [ 'expires' => time() + MONTH_IN_SECONDS, 'path' => COOKIEPATH ?: '/', 'domain' => COOKIE_DOMAIN, 'secure' => is_ssl(), 'httponly' => false, 'samesite' => 'Lax', ]); $_COOKIE['history_search'] = wp_json_encode($history); } endif; if (!function_exists('aksrt_get_main_search')) : /** legacy main-search defaults backed only by the reference option IDs. */ function aksrt_get_main_search(array $args = [], bool $echo = false) { $settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : []; $search_cat = !array_key_exists('search_cat', $settings) || !empty($settings['search_cat']); $search_type = !array_key_exists('search_type', $settings) || !empty($settings['search_type']); $defaults = [ 'class' => '', 'show_keywords' => !array_key_exists('search_popular_key', $settings) || !empty($settings['search_popular_key']), 'show_history' => !array_key_exists('search_history', $settings) || !empty($settings['search_history']), 'show_posts' => !array_key_exists('search_posts', $settings) || !empty($settings['search_posts']), 'show_input_cat' => $search_cat, 'more_cats' => $search_cat ? (array) ($settings['search_more_cat_obj'] ?? []) : [], 'in_cat' => $search_cat ? ($settings['search_cat_in'] ?? '') : '', 'show_type' => $search_type, 'in_type' => $search_type ? sanitize_key((string) ($settings['search_type_in'] ?? 'post')) : '', 'in_user' => '', ]; return aksrt_get_search_box(wp_parse_args($args, $defaults), $echo); } endif; if (!function_exists('aksrt_get_search_history_keywords')) : /** * @return list */ function aksrt_get_search_history_keywords(): array { $raw = isset($_COOKIE['history_search']) ? wp_unslash((string) $_COOKIE['history_search']) : ''; $decoded = $raw !== '' ? json_decode(stripslashes($raw), true) : []; if (!is_array($decoded)) { return []; } $hist = array_map( static fn($item): string => sanitize_text_field((string) $item), array_slice($decoded, 0, 30) ); return array_values(array_unique(array_filter(array_map('trim', $hist)))); } endif; if (!function_exists('aksrt_get_search_keywords_text')) : function aksrt_get_search_keywords_text(string $search): string { $search = str_replace('&type', '&type', $search); $index = strpos($search, '&type='); return trim($index === false ? $search : substr($search, 0, $index)); } endif; if (!function_exists('aksrt_update_search_keywords')) : function aksrt_update_search_keywords(string $search): void { $search = trim(wp_strip_all_tags($search)); $text = aksrt_get_search_keywords_text($search); if (mb_strlen($text) < 2 || mb_strlen($text) > 8) { return; } if (class_exists('\AKSRT\Settings\Settings') && !\AKSRT\Settings\Settings::hot_search_enabled()) { return; } $keywords = get_option('search_keywords', []); $keywords = is_array($keywords) ? $keywords : []; $limit = class_exists('\AKSRT\Settings\Settings') ? max(10, min(50, (int) \AKSRT\Settings\Settings::get_value('search_popular_key_num', 20))) : 20; $keywords = array_slice($keywords, 0, $limit + 10, true); $keywords[$search] = isset($keywords[$search]) ? (int) $keywords[$search] + 1 : 1; arsort($keywords, SORT_NUMERIC); update_option('search_keywords', $keywords, false); } 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; } $text = aksrt_get_search_keywords_text($kw); $query = ['s' => $text]; if (preg_match('/&type=([a-z0-9_-]+)/i', $kw, $match)) { $query['type'] = sanitize_key($match[1]); } $out .= '' . esc_html($text) . ' '; } return $out; } endif; if (!function_exists('aksrt_get_search_box')) : /** * legacy-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 = '
    ' . $type_html . '
    ' . $cat_html . $input_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(); $keyword_link = aksrt_get_search_keywords_but($hist, 'history'); if ($keyword_link !== '') { $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 .= '
    '; } } } $class = $args['class'] ? ' ' . sanitize_html_class((string) $args['class']) : ''; $html = '
    ' . $form_html . $keywords_html . $history_html . $posts_html . '
    '; 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 (legacy). * * @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;