From 0d4d5166b4a589b2290a893dab35804a8b65896e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Jul 2026 08:19:06 +0800 Subject: [PATCH] feat(widgets): SearchWidget type tabs + category scope Align sidebar search with header/search.js contracts: type buttons, category select + hidden aksrt_category, history/hot keywords retained. --- src/AKSRT/Widgets/SearchWidget.php | 218 +++++++++++++++++++++++++---- 1 file changed, 189 insertions(+), 29 deletions(-) diff --git a/src/AKSRT/Widgets/SearchWidget.php b/src/AKSRT/Widgets/SearchWidget.php index 080eb543..edb25bdd 100644 --- a/src/AKSRT/Widgets/SearchWidget.php +++ b/src/AKSRT/Widgets/SearchWidget.php @@ -1,6 +1,6 @@ esc_html__('搜索框,可显示热门关键词。', 'aksrt'), + 'description' => esc_html__('搜索框:类型切换、分类范围、热门关键词与搜索历史。', 'aksrt'), + 'classname' => 'widget_aksrt_search', ]); } @@ -25,16 +26,76 @@ class SearchWidget extends \WP_Widget { } $placeholder = sanitize_text_field((string) ($instance['placeholder'] ?? __('开启精彩搜索', 'aksrt'))); - $show_kw = !empty($instance['show_keywords']); + if ($placeholder === '' && function_exists('aksrt_get_search_placeholder')) { + $placeholder = aksrt_get_search_placeholder(); + } + $show_kw = !empty($instance['show_keywords']); $show_history = !empty($instance['show_history']); - $kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt'))); - $more_but = sanitize_text_field((string) ($instance['more_but'] ?? '')); - $more_url = esc_url_raw(trim((string) ($instance['more_but_url'] ?? ''))); + $show_types = !empty($instance['show_type']) || !isset($instance['show_type']); + $show_cat = !empty($instance['show_input_cat']); + $kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt'))); + $more_but = sanitize_text_field((string) ($instance['more_but'] ?? '')); + $more_url = esc_url_raw(trim((string) ($instance['more_but_url'] ?? ''))); + + $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_options = is_array($state['types'] ?? null) ? $state['types'] : ['post' => __('文章', 'aksrt')]; + $in_type = sanitize_key((string) ($instance['in_type'] ?? $state['type'] ?? 'post')); + if (!isset($type_options[$in_type])) { + $in_type = array_key_first($type_options) ?: 'post'; + } + + // Category scope. + $in_cat = max(0, (int) ($instance['in_cat'] ?? 0)); + if ($in_cat <= 0) { + $in_cat = (int) ($state['category_id'] ?? 0); + } + $cat_enabled = $show_cat && ($state['category_enabled'] || !empty($instance['show_input_cat'])); + $categories = []; + if ($cat_enabled) { + $more_raw = trim((string) ($instance['more_cats'] ?? '')); + if ($more_raw !== '') { + $ids = array_values(array_filter(array_map('intval', preg_split('/[,,\s]+/', $more_raw) ?: []))); + if ($ids) { + $got = get_categories([ + 'taxonomy' => 'category', + 'include' => $ids, + 'hide_empty' => false, + 'orderby' => 'include', + ]); + $categories = is_array($got) ? $got : []; + } + } + if (!$categories) { + $categories = is_array($state['categories'] ?? null) ? $state['categories'] : []; + } + if (!$categories && function_exists('aksrt_get_search_categories')) { + $categories = aksrt_get_search_categories(); + } + } + + // Only show type UI when more than one type is configured, unless forced. + if ($show_types && count($type_options) < 2 && empty($instance['force_show_type'])) { + // Still keep hidden type field for default. + $show_types_ui = false; + } else { + $show_types_ui = $show_types && count($type_options) >= 1; + } $keywords = []; if ($show_kw) { if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'hot_search_keywords')) { $keywords = \AKSRT\Settings\Settings::hot_search_keywords(); + } elseif (function_exists('aksrt_get_search_hot_keywords')) { + $keywords = aksrt_get_search_hot_keywords(); } elseif (function_exists('aksrt_get_setting')) { $raw = (string) aksrt_get_setting('hot_search_keywords', ''); $keywords = array_values(array_filter(array_map('trim', preg_split('/[\r\n,,]+/', $raw) ?: []))); @@ -43,40 +104,111 @@ class SearchWidget extends \WP_Widget { if ($custom !== '') { $keywords = array_values(array_filter(array_map('trim', preg_split('/[\r\n,,]+/', $custom) ?: []))); } - $keywords = array_slice($keywords, 0, 12); + $keywords = array_slice(is_array($keywords) ? $keywords : [], 0, 12); } + + $uid = 'aksrt-sw-' . esc_attr($this->id); ?> -