Align search experience with settings
This commit is contained in:
+70
-30
@@ -12,9 +12,19 @@ global $wp_query;
|
||||
|
||||
$search_query = get_search_query();
|
||||
$loop_query = $wp_query;
|
||||
$search_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' => [],
|
||||
];
|
||||
$has_search_query = '' !== trim($search_query);
|
||||
$search_placeholder = function_exists('aksrt_get_search_placeholder') ? aksrt_get_search_placeholder() : __('开启精彩搜索', 'aksrt');
|
||||
$search_type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
|
||||
$search_type = $search_state['type'];
|
||||
$search_type_options = !empty($search_state['types']) && is_array($search_state['types']) ? $search_state['types'] : ['post' => __('文章', 'aksrt')];
|
||||
$hot_keywords = function_exists('aksrt_get_search_hot_keywords') ? aksrt_get_search_hot_keywords() : [];
|
||||
$filter_settings = class_exists('AKSRT_Settings')
|
||||
? AKSRT_Settings::get_post_filter_settings()
|
||||
@@ -33,15 +43,9 @@ $popular_categories = get_categories([
|
||||
'order' => 'DESC',
|
||||
]);
|
||||
|
||||
$current_category = isset($_GET['aksrt_category']) ? absint($_GET['aksrt_category']) : 0;
|
||||
$search_categories = get_categories([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => true,
|
||||
'number' => 0,
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
'fields' => 'all',
|
||||
]);
|
||||
$current_category = (int) ($search_state['category_id'] ?? 0);
|
||||
$search_categories = is_array($search_state['categories'] ?? null) ? $search_state['categories'] : [];
|
||||
$search_category_enabled = !empty($search_state['category_enabled']);
|
||||
|
||||
$search_sidebar_id = is_active_sidebar('search_sidebar') ? 'search_sidebar' : 'sidebar-blog';
|
||||
$has_sidebar = !empty($other_page_settings['page_search_sidebar']) && is_active_sidebar($search_sidebar_id);
|
||||
@@ -57,7 +61,7 @@ if ($current_orderby === 'date' && $current_order === 'DESC') {
|
||||
$current_sort = 'popular';
|
||||
}
|
||||
|
||||
if ($current_category > 0) {
|
||||
if ($current_category > 0 && $search_type === 'post') {
|
||||
$filtered_query_args = [
|
||||
's' => $search_query,
|
||||
'post_type' => 'post',
|
||||
@@ -87,16 +91,6 @@ $render_config = [
|
||||
'highlight_query' => $search_query,
|
||||
];
|
||||
|
||||
$search_type_options = [
|
||||
'post' => __('文章', 'aksrt'),
|
||||
];
|
||||
if (!empty($filter_settings['categories'])) {
|
||||
$search_type_options['category'] = __('分类', 'aksrt');
|
||||
}
|
||||
if (!empty($filter_settings['tags'])) {
|
||||
$search_type_options['tag'] = __('标签', 'aksrt');
|
||||
}
|
||||
|
||||
$search_sort_options = [
|
||||
'relevance' => [
|
||||
'label' => __('综合', 'aksrt'),
|
||||
@@ -148,8 +142,11 @@ $search_sort_options = [
|
||||
?>
|
||||
|
||||
<section class="content-stream content-stream--search" data-page-kind="search" data-render-mode="<?php echo esc_attr((string) $render_config['mode']); ?>" data-card-style="<?php echo esc_attr((string) $render_config['card_style']); ?>">
|
||||
<form role="search" method="get" class="search-hero-form search-page-form" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="search-page">
|
||||
<form role="search" method="get" class="search-hero-form search-page-form" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="search-page" data-default-search-type="<?php echo esc_attr($search_type); ?>" data-default-category="<?php echo esc_attr((string) $current_category); ?>">
|
||||
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($search_type); ?>">
|
||||
<?php if ($search_category_enabled) : ?>
|
||||
<input type="hidden" name="aksrt_category" value="<?php echo esc_attr((string) $current_category); ?>">
|
||||
<?php endif; ?>
|
||||
<span class="search-icon search-page-form__icon" aria-hidden="true">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
|
||||
</span>
|
||||
@@ -158,6 +155,26 @@ $search_sort_options = [
|
||||
<button type="button" class="clear-search search-page-form__clear" id="search-clear-btn" aria-label="<?php esc_attr_e('清空搜索内容', 'aksrt'); ?>"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
|
||||
<button type="submit" class="search-page-form__submit"><?php esc_html_e('搜索', 'aksrt'); ?></button>
|
||||
</form>
|
||||
<?php if (!empty($search_type_options)) : ?>
|
||||
<div class="search-page-form__meta">
|
||||
<div class="search-page-form__types" aria-label="<?php esc_attr_e('搜索类型', 'aksrt'); ?>">
|
||||
<?php foreach ($search_type_options as $type_key => $type_label) : ?>
|
||||
<button type="button" class="search-page-form__type<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" data-search-type="<?php echo esc_attr($type_key); ?>"><?php echo esc_html($type_label); ?></button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php if ($search_category_enabled && !empty($search_categories)) : ?>
|
||||
<div class="search-page-form__category-wrap">
|
||||
<label class="screen-reader-text" for="search-page-category"><?php esc_html_e('筛选分类', 'aksrt'); ?></label>
|
||||
<select class="search-page-form__category" id="search-page-category" data-search-category-select>
|
||||
<option value="0"><?php esc_html_e('全部分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($search_categories as $search_category) : ?>
|
||||
<option value="<?php echo esc_attr((string) $search_category->term_id); ?>"<?php selected($current_category, (int) $search_category->term_id); ?>><?php echo esc_html($search_category->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php aksrt_render_search_hot_keywords($hot_keywords, 'aksrt-search-hot-keywords'); ?>
|
||||
|
||||
<?php if (!empty($filter_settings['enabled'])) : ?>
|
||||
@@ -167,7 +184,18 @@ $search_sort_options = [
|
||||
<span class="search-page-filters__label"><?php esc_html_e('类型', 'aksrt'); ?></span>
|
||||
<div class="search-page-filters__chips">
|
||||
<?php foreach ($search_type_options as $type_key => $type_label) : ?>
|
||||
<a class="search-page-filters__chip<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" href="<?php echo esc_url(add_query_arg(['aksrt_search_type' => $type_key, 'paged' => false])); ?>">
|
||||
<?php
|
||||
$type_args = [
|
||||
'aksrt_search_type' => $type_key,
|
||||
'paged' => false,
|
||||
];
|
||||
if ($type_key !== 'post') {
|
||||
$type_args['aksrt_category'] = false;
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$type_args['aksrt_category'] = $current_category;
|
||||
}
|
||||
?>
|
||||
<a class="search-page-filters__chip<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" href="<?php echo esc_url(add_query_arg($type_args)); ?>">
|
||||
<?php echo esc_html($type_label); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
@@ -175,16 +203,17 @@ $search_sort_options = [
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($filter_settings['categories']) && !empty($search_categories)) : ?>
|
||||
<?php if ($search_category_enabled && !empty($search_categories)) : ?>
|
||||
<div class="search-page-filters__group">
|
||||
<span class="search-page-filters__label"><?php esc_html_e('分类', 'aksrt'); ?></span>
|
||||
<div class="search-page-filters__select-wrap">
|
||||
<select class="search-page-filters__select" id="search-category-filter" aria-label="<?php esc_attr_e('选择分类', 'aksrt'); ?>">
|
||||
<select class="search-page-filters__select" id="search-category-filter" aria-label="<?php esc_attr_e('选择分类', 'aksrt'); ?>"<?php echo $search_type !== 'post' ? ' disabled' : ''; ?>>
|
||||
<option value=""><?php esc_html_e('全部分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($search_categories as $cat) : ?>
|
||||
<option value="<?php echo esc_attr($cat->term_id); ?>"<?php selected($current_category, $cat->term_id); ?>>
|
||||
<?php $cat_count = isset($cat->count) ? (int) $cat->count : 0; ?>
|
||||
<option value="<?php echo esc_attr((string) $cat->term_id); ?>"<?php selected($current_category, (int) $cat->term_id); ?>>
|
||||
<?php echo esc_html($cat->name); ?>
|
||||
<?php if ($cat->count > 0) : ?>(<?php echo esc_html((string) $cat->count); ?>)<?php endif; ?>
|
||||
<?php if ($cat_count > 0) : ?>(<?php echo esc_html((string) $cat_count); ?>)<?php endif; ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
@@ -198,15 +227,26 @@ $search_sort_options = [
|
||||
<div class="search-page-filters__chips">
|
||||
<?php foreach ($search_sort_options as $sort_key => $sort_option) : ?>
|
||||
<?php
|
||||
$sort_url = add_query_arg(array_filter([
|
||||
$sort_args = [
|
||||
'orderby' => $sort_option['args']['orderby'],
|
||||
'order' => $sort_option['args']['order'],
|
||||
'paged' => false,
|
||||
], static function ($value): bool {
|
||||
];
|
||||
if ($search_type !== 'post') {
|
||||
$sort_args['aksrt_category'] = false;
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$sort_args['aksrt_category'] = $current_category;
|
||||
}
|
||||
$sort_url = add_query_arg(array_filter($sort_args, static function ($value): bool {
|
||||
return false !== $value;
|
||||
}));
|
||||
if ($sort_key === 'relevance') {
|
||||
$sort_url = remove_query_arg(['orderby', 'order', 'paged']);
|
||||
$sort_url = remove_query_arg(['orderby', 'order', 'paged'], $sort_url);
|
||||
if ($search_type !== 'post') {
|
||||
$sort_url = remove_query_arg('aksrt_category', $sort_url);
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$sort_url = add_query_arg('aksrt_category', $current_category, $sort_url);
|
||||
}
|
||||
}
|
||||
?>
|
||||
<a class="search-page-filters__chip<?php echo $current_sort === $sort_key ? ' is-active' : ''; ?>" href="<?php echo esc_url($sort_url); ?>">
|
||||
|
||||
Reference in New Issue
Block a user