esc_html__('搜索框:类型切换、分类范围、热门关键词与搜索历史。', 'aksrt'), 'classname' => 'widget_aksrt_search', ]); } public function widget($args, $instance): void { echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); if ($title !== '') { echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } $placeholder = sanitize_text_field((string) ($instance['placeholder'] ?? __('开启精彩搜索', 'aksrt'))); 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']); $show_types = !empty($instance['show_type']) || !isset($instance['show_type']); // NOTE: SearchWidget owns a business `show_type` checkbox (search-type // toggle) that shares the key name with the common `show_type` (device // visibility). To avoid an admin-form collision, this widget does NOT // render the shared common-form section (render_common_form_section is // intentionally not called). The shared affix helper is still wired in // widget() via aksrt_widget_affix_attrs(), and update_common_fields() // persists common keys — but note the shared show_type cannot be set // here, so display_callback treats it as 'all' (always visible). $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'] ?? ''))); // Affix helper (shared layer sidebar_affix). $affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => '']; $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) ?: []))); } $custom = trim((string) ($instance['keywords'] ?? '')); if ($custom !== '') { $keywords = array_values(array_filter(array_map('trim', preg_split('/[\r\n,,]+/', $custom) ?: []))); } $keywords = array_slice(is_array($keywords) ? $keywords : [], 0, 12); } $uid = 'aksrt-sw-' . esc_attr($this->id); ?> __('文章', 'aksrt')]; $in_type = sanitize_key((string) ($instance['in_type'] ?? 'post')); ?>




update_common_fields($new_instance, $old_instance, [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'placeholder' => sanitize_text_field((string) ($new_instance['placeholder'] ?? '')), 'show_keywords' => !empty($new_instance['show_keywords']) ? 1 : 0, 'show_history' => !empty($new_instance['show_history']) ? 1 : 0, 'show_type' => !empty($new_instance['show_type']) ? 1 : 0, 'show_input_cat' => !empty($new_instance['show_input_cat']) ? 1 : 0, 'in_type' => sanitize_key((string) ($new_instance['in_type'] ?? 'post')), 'in_cat' => max(0, (int) ($new_instance['in_cat'] ?? 0)), 'more_cats' => sanitize_text_field((string) ($new_instance['more_cats'] ?? '')), 'keywords_title' => sanitize_text_field((string) ($new_instance['keywords_title'] ?? '')), 'keywords' => sanitize_textarea_field((string) ($new_instance['keywords'] ?? '')), 'more_but' => sanitize_text_field((string) ($new_instance['more_but'] ?? '')), 'more_but_url' => esc_url_raw(trim((string) ($new_instance['more_but_url'] ?? ''))), ]); } }