Align search experience with settings
This commit is contained in:
@@ -234,10 +234,12 @@ class AKSRT_Settings_Theme_Search {
|
||||
$all = AKSRT_Settings::get_all();
|
||||
foreach ($all_types as $type => $key) {
|
||||
$default_on = in_array($type, ['post', 'category', 'tag'], true);
|
||||
if (!isset($all[$key]) || !empty($all[$key]) && $default_on) {
|
||||
if (!empty(AKSRT_Settings::get_value($key, $default_on))) {
|
||||
$enabled[] = $type;
|
||||
}
|
||||
$is_enabled = array_key_exists($key, $all)
|
||||
? !empty(AKSRT_Settings::get_value($key, $default_on))
|
||||
: $default_on;
|
||||
|
||||
if ($is_enabled) {
|
||||
$enabled[] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -558,8 +558,175 @@ if (!function_exists('aksrt_get_search_form_type')):
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_get_search_form_type(): string {
|
||||
$configured = ['post' => __('文章', 'aksrt')];
|
||||
|
||||
if (class_exists('AKSRT_Settings')) {
|
||||
$configured = aksrt_get_search_type_options();
|
||||
}
|
||||
|
||||
$allowed_types = array_keys($configured);
|
||||
if (empty($allowed_types)) {
|
||||
$allowed_types = ['post'];
|
||||
}
|
||||
|
||||
$type = sanitize_key((string) get_query_var('aksrt_search_type'));
|
||||
return in_array($type, ['post', 'category', 'tag'], true) ? $type : 'post';
|
||||
if ($type === '' && isset($_GET['aksrt_search_type'])) {
|
||||
$type = sanitize_key((string) wp_unslash($_GET['aksrt_search_type']));
|
||||
}
|
||||
|
||||
if (in_array($type, $allowed_types, true)) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
return function_exists('aksrt_get_default_search_type') ? aksrt_get_default_search_type() : 'post';
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_type_options')):
|
||||
/**
|
||||
* 获取前台支持的搜索类型选项
|
||||
*
|
||||
* @return array<string,string>
|
||||
*/
|
||||
function aksrt_get_search_type_options(): array {
|
||||
$label_map = [
|
||||
'post' => __('文章', 'aksrt'),
|
||||
'category' => __('分类', 'aksrt'),
|
||||
'tag' => __('标签', 'aksrt'),
|
||||
];
|
||||
|
||||
if (!class_exists('AKSRT_Settings')) {
|
||||
return ['post' => $label_map['post']];
|
||||
}
|
||||
|
||||
$config = AKSRT_Settings::get_search_content_types();
|
||||
$types = isset($config['types']) && is_array($config['types']) ? $config['types'] : ['post'];
|
||||
$options = [];
|
||||
|
||||
foreach ($types as $type) {
|
||||
$type = sanitize_key((string) $type);
|
||||
if (isset($label_map[$type])) {
|
||||
$options[$type] = $label_map[$type];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($options)) {
|
||||
$options['post'] = $label_map['post'];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_default_search_type')):
|
||||
/**
|
||||
* 获取默认搜索类型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_get_default_search_type(): string {
|
||||
$options = function_exists('aksrt_get_search_type_options') ? aksrt_get_search_type_options() : ['post' => __('文章', 'aksrt')];
|
||||
$allowed = array_keys($options);
|
||||
|
||||
if (class_exists('AKSRT_Settings')) {
|
||||
$config = AKSRT_Settings::get_search_content_types();
|
||||
$default = sanitize_key((string) ($config['default'] ?? 'post'));
|
||||
if (in_array($default, $allowed, true)) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
return in_array('post', $allowed, true) ? 'post' : (string) reset($allowed);
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_default_category_id')):
|
||||
/**
|
||||
* 获取默认搜索分类 ID
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function aksrt_get_search_default_category_id(): int {
|
||||
if (!class_exists('AKSRT_Settings') || !AKSRT_Settings::search_cat_enabled()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return max(0, absint(AKSRT_Settings::search_cat_in()));
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_categories')):
|
||||
/**
|
||||
* 获取搜索可选分类列表
|
||||
*
|
||||
* @return WP_Term[]
|
||||
*/
|
||||
function aksrt_get_search_categories(): array {
|
||||
$categories = get_categories([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => true,
|
||||
'number' => 0,
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
'fields' => 'all',
|
||||
]);
|
||||
|
||||
if (!is_array($categories)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_filter($categories, static function ($term): bool {
|
||||
return $term instanceof WP_Term;
|
||||
}));
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_form_state')):
|
||||
/**
|
||||
* 获取搜索表单运行时状态
|
||||
*
|
||||
* @return array{type:string,types:array<string,string>,category_id:int,category_enabled:bool,categories:array<int,WP_Term>}
|
||||
*/
|
||||
function aksrt_get_search_form_state(): array {
|
||||
$types = function_exists('aksrt_get_search_type_options') ? aksrt_get_search_type_options() : ['post' => __('文章', 'aksrt')];
|
||||
$type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
|
||||
$default_category = function_exists('aksrt_get_search_default_category_id') ? aksrt_get_search_default_category_id() : 0;
|
||||
$current_category = isset($_GET['aksrt_category']) ? absint(wp_unslash($_GET['aksrt_category'])) : $default_category;
|
||||
$category_enabled = class_exists('AKSRT_Settings') ? AKSRT_Settings::search_cat_enabled() : false;
|
||||
$categories = $category_enabled ? aksrt_get_search_categories() : [];
|
||||
|
||||
return [
|
||||
'type' => isset($types[$type]) ? $type : (function_exists('aksrt_get_default_search_type') ? aksrt_get_default_search_type() : 'post'),
|
||||
'types' => $types,
|
||||
'category_id' => $current_category,
|
||||
'category_enabled' => $category_enabled,
|
||||
'categories' => $categories,
|
||||
];
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_build_search_url')):
|
||||
/**
|
||||
* 构建搜索跳转 URL
|
||||
*
|
||||
* @param string $keyword 搜索关键词
|
||||
* @param array $args 追加参数
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_build_search_url(string $keyword = '', array $args = []): string {
|
||||
$params = [];
|
||||
if ($keyword !== '') {
|
||||
$params['s'] = $keyword;
|
||||
}
|
||||
|
||||
foreach ($args as $key => $value) {
|
||||
if ($value === null || $value === '' || $value === false) {
|
||||
continue;
|
||||
}
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
return esc_url(add_query_arg($params, home_url('/')));
|
||||
}
|
||||
endif;
|
||||
|
||||
@@ -660,7 +827,7 @@ function aksrt_render_search_hot_keywords(array $keywords = [], string $class =
|
||||
<span class="<?php echo esc_attr($class . '__label'); ?>"><?php echo esc_html($title); ?></span>
|
||||
<div class="<?php echo esc_attr($class . '__list tabbar-search-overlay__tags'); ?>">
|
||||
<?php foreach ($keywords as $keyword) : ?>
|
||||
<a class="tabbar-search-overlay__tag" href="<?php echo esc_url(add_query_arg('s', $keyword, home_url('/'))); ?>">
|
||||
<a class="tabbar-search-overlay__tag" href="<?php echo aksrt_build_search_url($keyword, ['aksrt_search_type' => aksrt_get_default_search_type()]); ?>">
|
||||
<?php echo esc_html($keyword); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
|
||||
Reference in New Issue
Block a user