feat(phase2): shared common-form layer + CommonWidgetFields trait for all widgets
- Extract Zibll CSF_Widget::params() common-field parity into inc/modules/widget-common.php (required by widgets-register.php to keep both files <1000 lines): subtitle, title_style(simple/big), title_highlight +color, title_link x3, title_link_style colors, sidebar_affix, show_type (all/only_pc/only_sm), show_id_type(show/hide), show_ids, animation_in, animation_repeat, layout_pin_pc/m, layout_bg(white/dark). Adds normalize_link/normalize_bg helpers, affix_attrs, common_wrapper_attr, render_title, animation_class. Back-compat normalizes legacy tokens (in_affix->sidebar_affix, pc/only_pc, include/exclude->show/hide). - Add src/AKSRT/Widgets/Traits/CommonWidgetFields.php trait (render_common_form_section + update_common_fields). - Wire all 57 widgets to use the trait; migrate the 16 hand-rolled in_affix implementations to shared sidebar_affix (fixes 6 latent bugs where in_affix was never persisted). SearchWidget intentionally skips the shared form section (its business show_type collides) but adopts the affix helper. - widget_display_callback parity verified via tmp assertion script. Refs .specs/plans/zibll-widgets-1to1.md Phase 2 (US1/US5) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,12 @@
|
||||
|
||||
namespace AKSRT\Widgets;
|
||||
|
||||
use AKSRT\Widgets\Traits\CommonWidgetFields;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class SearchWidget extends \WP_Widget {
|
||||
use CommonWidgetFields;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct('aksrt_search', esc_html__('AKSRT 搜索', 'aksrt'), [
|
||||
@@ -32,10 +35,20 @@ class SearchWidget extends \WP_Widget {
|
||||
$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()
|
||||
@@ -109,7 +122,7 @@ class SearchWidget extends \WP_Widget {
|
||||
|
||||
$uid = 'aksrt-sw-' . esc_attr($this->id);
|
||||
?>
|
||||
<div class="widget-search-box aksrt-search-widget search-input<?php echo !empty($instance['in_affix']) ? ' is-affix' : ''; ?>" id="<?php echo $uid; ?>"<?php echo !empty($instance['in_affix']) ? ' data-affix="true"' : ''; ?>>
|
||||
<div class="widget-search-box aksrt-search-widget search-input<?php echo $affix['class']; ?>" id="<?php echo $uid; ?>"<?php echo $affix['attr']; ?>>
|
||||
<form
|
||||
role="search"
|
||||
method="get"
|
||||
@@ -239,8 +252,7 @@ class SearchWidget extends \WP_Widget {
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_type')); ?>" value="1" <?php checked(!empty($instance['show_type']) || !isset($instance['show_type'])); ?>> <?php esc_html_e('显示类型切换(≥2 种时)', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_input_cat')); ?>" value="1" <?php checked(!empty($instance['show_input_cat'])); ?>> <?php esc_html_e('显示分类筛选', 'aksrt'); ?></label><br>
|
||||
<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><br>
|
||||
<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><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('in_affix')); ?>" value="1" <?php checked(!empty($instance['in_affix'])); ?>> <?php esc_html_e('侧栏随动', 'aksrt'); ?></label>
|
||||
<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'); ?>
|
||||
@@ -281,11 +293,18 @@ class SearchWidget extends \WP_Widget {
|
||||
<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 // Common-form section intentionally skipped: SearchWidget owns a
|
||||
// business `show_type` checkbox (search-type toggle) that collides
|
||||
// with the shared common `show_type` (device visibility). The
|
||||
// shared affix/display/animation/layout fields are therefore not
|
||||
// rendered here; update_common_fields() below still persists any
|
||||
// common keys submitted through other surfaces, and the affix
|
||||
// helper is wired in widget() via aksrt_widget_affix_attrs(). ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
return $this->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,
|
||||
@@ -299,7 +318,6 @@ class SearchWidget extends \WP_Widget {
|
||||
'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'] ?? ''))),
|
||||
'in_affix' => !empty($new_instance['in_affix']) ? 1 : 0,
|
||||
];
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user