Render local/remote history into [data-aksrt-search-history-list], mark widget forms for history capture, and style clear control.
144 lines
6.9 KiB
PHP
144 lines
6.9 KiB
PHP
<?php
|
||
/**
|
||
* Search box with hot keywords (zibll widget_ui_search lite).
|
||
*
|
||
* @package AKSRT
|
||
*/
|
||
|
||
namespace AKSRT\Widgets;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
class SearchWidget extends \WP_Widget {
|
||
|
||
public function __construct() {
|
||
parent::__construct('aksrt_search', esc_html__('AKSRT 搜索', 'aksrt'), [
|
||
'description' => esc_html__('搜索框,可显示热门关键词。', 'aksrt'),
|
||
]);
|
||
}
|
||
|
||
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')));
|
||
$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'] ?? '')));
|
||
|
||
$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_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($keywords, 0, 12);
|
||
}
|
||
?>
|
||
<div class="widget-search-box aksrt-search-widget">
|
||
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form>
|
||
<div class="widget-search-box__inner">
|
||
<input type="search" name="s" class="widget-search-box__input" placeholder="<?php echo esc_attr($placeholder); ?>" value="<?php echo esc_attr(get_search_query()); ?>" autocomplete="off">
|
||
<input type="hidden" name="aksrt_search_type" value="post">
|
||
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
|
||
<i class="fas fa-search"></i>
|
||
</button>
|
||
</div>
|
||
</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">
|
||
<?php if ($kw_title !== '') : ?>
|
||
<div class="aksrt-search-widget__kw-title"><?php echo esc_html($kw_title); ?></div>
|
||
<?php endif; ?>
|
||
<div class="aksrt-search-widget__kw-list">
|
||
<?php foreach ($keywords as $kw) : ?>
|
||
<a class="aksrt-search-widget__kw" href="<?php echo esc_url(add_query_arg('s', $kw, home_url('/'))); ?>"><?php echo esc_html($kw); ?></a>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
</div>
|
||
<?php endif; ?>
|
||
<?php if ($more_but !== '' && $more_url !== '') : ?>
|
||
<div class="aksrt-search-widget__more">
|
||
<a href="<?php echo esc_url($more_url); ?>"><?php echo esc_html($more_but); ?></a>
|
||
</div>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php
|
||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||
}
|
||
|
||
public function form($instance): void {
|
||
?>
|
||
<p>
|
||
<label><?php esc_html_e('标题(可选)', 'aksrt'); ?>
|
||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? '')); ?>">
|
||
</label>
|
||
</p>
|
||
<p>
|
||
<label><?php esc_html_e('占位文本', 'aksrt'); ?>
|
||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('placeholder')); ?>" value="<?php echo esc_attr((string) ($instance['placeholder'] ?? __('开启精彩搜索', 'aksrt'))); ?>">
|
||
</label>
|
||
</p>
|
||
<p>
|
||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_keywords')); ?>" value="1" <?php checked(!empty($instance['show_keywords'])); ?>> <?php esc_html_e('显示热门关键词', 'aksrt'); ?></label>
|
||
</p>
|
||
<p>
|
||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_history')); ?>" value="1" <?php checked(!empty($instance['show_history'])); ?>> <?php esc_html_e('显示搜索历史', 'aksrt'); ?></label>
|
||
</p>
|
||
<p>
|
||
<label><?php esc_html_e('关键词标题', 'aksrt'); ?>
|
||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('keywords_title')); ?>" value="<?php echo esc_attr((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt'))); ?>">
|
||
</label>
|
||
</p>
|
||
<p>
|
||
<label><?php esc_html_e('自定义关键词(逗号/换行;空=后台配置)', 'aksrt'); ?>
|
||
<textarea class="widefat" rows="2" name="<?php echo esc_attr($this->get_field_name('keywords')); ?>"><?php echo esc_textarea((string) ($instance['keywords'] ?? '')); ?></textarea>
|
||
</label>
|
||
</p>
|
||
<p>
|
||
<label><?php esc_html_e('更多按钮文案', 'aksrt'); ?>
|
||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('more_but')); ?>" value="<?php echo esc_attr((string) ($instance['more_but'] ?? '')); ?>">
|
||
</label>
|
||
</p>
|
||
<p>
|
||
<label><?php esc_html_e('更多按钮链接', 'aksrt'); ?>
|
||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('more_but_url')); ?>" value="<?php echo esc_attr((string) ($instance['more_but_url'] ?? '')); ?>">
|
||
</label>
|
||
</p>
|
||
<?php
|
||
}
|
||
|
||
public function update($new_instance, $old_instance): array {
|
||
return [
|
||
'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,
|
||
'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'] ?? ''))),
|
||
];
|
||
}
|
||
}
|