feat(widget): improve search history popup
This commit is contained in:
@@ -32,16 +32,20 @@ class SearchWidget extends \WP_Widget {
|
||||
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')));
|
||||
$show_kw = !empty($instance['show_keywords']);
|
||||
$show_history = !empty($instance['show_history']);
|
||||
$kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt')));
|
||||
$keyword_limit = max(1, min(50, (int) ($instance['keyword_limit'] ?? 12)));
|
||||
$history_limit = max(1, min(8, (int) ($instance['history_limit'] ?? 8)));
|
||||
// Affix helper (shared layer sidebar_affix).
|
||||
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
|
||||
|
||||
$search_type = function_exists('aksrt_get_default_search_type')
|
||||
? aksrt_get_default_search_type()
|
||||
: 'post';
|
||||
$history_enabled = function_exists('aksrt_search_history_enabled')
|
||||
? aksrt_search_history_enabled()
|
||||
: true;
|
||||
|
||||
$keywords = [];
|
||||
if ($show_kw) {
|
||||
@@ -92,26 +96,36 @@ class SearchWidget extends \WP_Widget {
|
||||
placeholder="<?php echo esc_attr($placeholder); ?>"
|
||||
value="<?php echo esc_attr(get_search_query()); ?>"
|
||||
autocomplete="off"
|
||||
aria-haspopup="true"
|
||||
aria-controls="<?php echo $uid; ?>-history"
|
||||
aria-expanded="false"
|
||||
>
|
||||
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
|
||||
<i class="fas fa-search" aria-hidden="true"></i>
|
||||
</button>
|
||||
<?php if ($show_history && $history_enabled) : ?>
|
||||
<div
|
||||
id="<?php echo $uid; ?>-history"
|
||||
class="aksrt-search-widget__history"
|
||||
data-aksrt-search-history
|
||||
data-history-limit="<?php echo esc_attr((string) $history_limit); ?>"
|
||||
role="region"
|
||||
aria-label="<?php esc_attr_e('搜索历史', 'aksrt'); ?>"
|
||||
hidden
|
||||
>
|
||||
<div class="aksrt-search-widget__kw-head">
|
||||
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
|
||||
<button type="button" class="aksrt-search-widget__history-clear" data-aksrt-search-history-clear hidden><?php esc_html_e('清空', 'aksrt'); ?></button>
|
||||
</div>
|
||||
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list role="list"></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($search_type); ?>">
|
||||
</form>
|
||||
|
||||
<?php if ($show_history) : ?>
|
||||
<div class="aksrt-search-widget__history" data-aksrt-search-history>
|
||||
<div class="aksrt-search-widget__kw-head">
|
||||
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
|
||||
<button type="button" class="aksrt-search-widget__history-clear" data-aksrt-search-history-clear hidden><?php esc_html_e('清空', 'aksrt'); ?></button>
|
||||
</div>
|
||||
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($keywords) : ?>
|
||||
<div class="aksrt-search-widget__keywords">
|
||||
<div class="aksrt-search-widget__kw-head">
|
||||
@@ -177,6 +191,11 @@ class SearchWidget extends \WP_Widget {
|
||||
<input class="tiny-text" type="number" min="1" max="50" name="<?php echo esc_attr($this->get_field_name('keyword_limit')); ?>" value="<?php echo esc_attr((string) max(1, min(50, (int) ($instance['keyword_limit'] ?? 12)))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('搜索历史最多显示数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="8" name="<?php echo esc_attr($this->get_field_name('history_limit')); ?>" value="<?php echo esc_attr((string) max(1, min(8, (int) ($instance['history_limit'] ?? 8)))); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
@@ -189,6 +208,7 @@ class SearchWidget extends \WP_Widget {
|
||||
'keywords_title' => sanitize_text_field((string) ($new_instance['keywords_title'] ?? '')),
|
||||
'keywords' => sanitize_textarea_field((string) ($new_instance['keywords'] ?? '')),
|
||||
'keyword_limit' => max(1, min(50, (int) ($new_instance['keyword_limit'] ?? 12))),
|
||||
'history_limit' => max(1, min(8, (int) ($new_instance['history_limit'] ?? 8))),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user