refactor(widgets): remove shared redundant settings

This commit is contained in:
2026-08-16 15:03:49 +08:00
parent 30b08282f5
commit d97174dadb
31 changed files with 199 additions and 1321 deletions
+148
View File
@@ -0,0 +1,148 @@
<?php
/**
* Widget placement and area filtering.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
function aksrt_widget_sidebar_area_ids(): array {
return ['home_sidebar', 'sidebar-article', 'all_sidebar_top', 'all_sidebar_bottom', 'cat_sidebar', 'tag_sidebar', 'search_sidebar'];
}
function aksrt_widget_archive_area_ids(): array {
return ['cat_top_fluid', 'cat_top_content', 'cat_bottom_content', 'cat_bottom_fluid', 'cat_sidebar', 'tag_top_fluid', 'tag_top_content', 'tag_bottom_content', 'tag_bottom_fluid', 'tag_sidebar', 'search_top_fluid', 'search_top_content', 'search_bottom_content', 'search_bottom_fluid', 'search_sidebar'];
}
function aksrt_widget_homepage_area_ids(): array {
return ['home_top_fluid', 'home_top_content', 'home_bottom_content', 'home_bottom_fluid'];
}
function aksrt_widget_single_area_ids(): array {
return ['single_top_fluid', 'single_top_content', 'single_bottom_content', 'single_bottom_fluid'];
}
function aksrt_widget_base_from_id(string $widget_id): string {
return preg_replace('/-\d+$/', '', $widget_id) ?: $widget_id;
}
function aksrt_widget_allowed_sidebars(string $widget_base_id): array {
$sidebar_only = ['aksrt_categories', 'aksrt_tag_cloud', 'aksrt_hot_posts'];
$homepage_only = ['aksrt_category_recommend'];
$hybrid = ['aksrt_tab_posts', 'aksrt_category_block', 'aksrt_category_tabs'];
if (in_array($widget_base_id, $sidebar_only, true)) {
return aksrt_widget_sidebar_area_ids();
}
if (in_array($widget_base_id, $homepage_only, true)) {
return aksrt_widget_homepage_area_ids();
}
if (in_array($widget_base_id, $hybrid, true)) {
return array_values(array_unique(array_merge(aksrt_widget_sidebar_area_ids(), aksrt_widget_homepage_area_ids())));
}
if ($widget_base_id === 'aksrt_post_pay_box') {
return ['sidebar-article'];
}
return [];
}
function aksrt_widget_is_allowed_in_sidebar(string $widget_base_id, string $sidebar_id): bool {
if ($widget_base_id === '' || $sidebar_id === '') {
return true;
}
if ($sidebar_id === 'wp_inactive_widgets' || strpos($sidebar_id, 'orphaned_widgets') === 0) {
return true;
}
$allowed = aksrt_widget_allowed_sidebars($widget_base_id);
return empty($allowed) || in_array($sidebar_id, $allowed, true);
}
function aksrt_widget_get_area_context(array $args): string {
$sidebar_id = isset($args['id']) ? (string) $args['id'] : '';
if (in_array($sidebar_id, aksrt_widget_sidebar_area_ids(), true)) {
return 'sidebar';
}
if (in_array($sidebar_id, aksrt_widget_homepage_area_ids(), true)) {
return 'homepage';
}
if (in_array($sidebar_id, aksrt_widget_archive_area_ids(), true)) {
return 'archive';
}
if (in_array($sidebar_id, aksrt_widget_single_area_ids(), true)) {
return 'single';
}
return 'other';
}
function aksrt_widget_render_location_help(string $scope): void {
$messages = [
'sidebar' => __('仅限侧栏区域:首页侧栏、文章页侧边栏、全站侧栏、分类/标签/搜索侧栏。', 'aksrt'),
'homepage' => __('仅限首页挂载区域:首页顶部/底部全宽、首页内容上方/下方。', 'aksrt'),
'hybrid' => __('支持侧栏、首页和归档页挂载区域。', 'aksrt'),
'archive' => __('仅限归档页挂载区域:分类页、标签页、搜索页的顶部/底部/侧栏。', 'aksrt'),
];
if (empty($messages[$scope])) {
return;
}
printf('<p style="margin-top:10px;font-size:12px;color:#666;">%s</p>', esc_html($messages[$scope]));
}
function aksrt_filter_widget_display_by_area($instance, WP_Widget $widget, array $args) {
if ($instance === false) {
return false;
}
$sidebar_id = isset($args['id']) ? (string) $args['id'] : '';
$widget_base_id = isset($widget->id_base) ? (string) $widget->id_base : '';
return aksrt_widget_is_allowed_in_sidebar($widget_base_id, $sidebar_id) ? $instance : false;
}
add_filter('widget_display_callback', 'aksrt_filter_widget_display_by_area', 10, 3);
function aksrt_filter_widget_sidebar_assignments($new_value, $old_value) {
if (!is_array($new_value)) {
return $new_value;
}
$removed = [];
foreach ($new_value as $sidebar_id => $widget_ids) {
if (!is_array($widget_ids) || $sidebar_id === 'wp_inactive_widgets') {
continue;
}
$filtered = [];
foreach ($widget_ids as $widget_id) {
if (!is_string($widget_id) || $widget_id === '') {
continue;
}
$widget_base_id = aksrt_widget_base_from_id($widget_id);
if (aksrt_widget_is_allowed_in_sidebar($widget_base_id, (string) $sidebar_id)) {
$filtered[] = $widget_id;
} else {
$removed[] = $widget_id;
}
}
$new_value[$sidebar_id] = array_values($filtered);
}
if (empty($removed)) {
return $new_value;
}
if (!isset($new_value['wp_inactive_widgets']) || !is_array($new_value['wp_inactive_widgets'])) {
$new_value['wp_inactive_widgets'] = [];
}
$assigned = [];
foreach ($new_value as $widget_ids) {
if (!is_array($widget_ids)) {
continue;
}
foreach ($widget_ids as $widget_id) {
if (is_string($widget_id) && $widget_id !== '') {
$assigned[$widget_id] = true;
}
}
}
foreach (array_values(array_unique($removed)) as $widget_id) {
if (!isset($assigned[$widget_id]) && !in_array($widget_id, $new_value['wp_inactive_widgets'], true)) {
$new_value['wp_inactive_widgets'][] = $widget_id;
}
}
return $new_value;
}
add_filter('pre_update_option_sidebars_widgets', 'aksrt_filter_widget_sidebar_assignments', 10, 2);
-638
View File
@@ -1,638 +0,0 @@
<?php
/**
* Shared widget common-form layer — Zibll CSF_Widget::params() parity.
*
* Extracted from widgets-register.php to keep each file under the 1000-line
* hard limit. Provides:
* - common field defaults / normalization (subtitle, title_style, highlight,
* 3 title links, sidebar_affix, show_type, show_id_type, animation, layout)
* - shared admin form renderer + update processor
* - wrapper class/attr builders (affix, animation, show-type, layout bg)
* - Zibll-aligned title block renderer (simple / big styles)
* - display-by-id gate consumed by widget_display_callback
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
// ============================================================
// Shared Widget Form Helper — 公共表单字段与显示规则
// ============================================================
if (!function_exists('aksrt_widget_common_defaults')) {
/**
* Default values for shared widget instance fields.
*
* Zibll CSF_Widget::params() common-field parity:
* subtitle, title_style, title_highlight, title_highlight_color,
* title_link (×3), 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).
*
* @return array<string, mixed>
*/
function aksrt_widget_common_defaults(): array {
return [
'subtitle' => '',
'title_style' => '',
'title_highlight' => '',
'title_highlight_color'=> 'focus-color',
'title_link' => ['url' => '', 'text' => '', 'target' => ''],
'title_link2' => ['url' => '', 'text' => '', 'target' => ''],
'title_link3' => ['url' => '', 'text' => '', 'target' => ''],
'title_link_style' => ['color_1' => 'jb-blue', 'color_2' => 'jb-yellow', 'color_3' => 'jb-green'],
'sidebar_affix' => false,
'show_type' => 'all',
'show_id_type' => '',
'show_ids' => '',
'animation_in' => '',
'animation_repeat' => false,
'layout_pin_pc' => ['top' => '', 'bottom' => ''],
'layout_pin_m' => ['top' => '', 'bottom' => ''],
'layout_bg' => ['img_white' => [], 'img_dark' => []],
];
}
}
if (!function_exists('aksrt_widget_normalize_link')) {
/**
* Normalize a title_linkN array {url,text,target}.
*
* @param mixed $raw
* @return array{url:string,text:string,target:string}
*/
function aksrt_widget_normalize_link($raw): array {
$raw = is_array($raw) ? $raw : [];
return [
'url' => esc_url_raw(trim((string) ($raw['url'] ?? ''))),
'text' => sanitize_text_field((string) ($raw['text'] ?? '')),
'target' => ($raw['target'] ?? '') === '_blank' ? '_blank' : '',
];
}
}
if (!function_exists('aksrt_widget_normalize_common')) {
/**
* Normalize common widget instance fields, filling defaults for legacy instances.
*
* Accepts both the new structured shape (title_link as array) and legacy
* flat shapes (title_link as URL string, in_affix bool) emitted by older
* widget versions, normalizing them onto the canonical shared schema.
*
* @param array<string, mixed> $instance
* @return array<string, mixed>
*/
function aksrt_widget_normalize_common(array $instance): array {
$defaults = aksrt_widget_common_defaults();
$result = array_merge($defaults, $instance);
$result['subtitle'] = sanitize_text_field($result['subtitle'] ?? '');
$result['title_style'] = in_array($result['title_style'] ?? '', ['', 'big'], true) ? $result['title_style'] : '';
$result['title_highlight'] = sanitize_text_field($result['title_highlight'] ?? '');
$result['title_highlight_color'] = sanitize_html_class((string) ($result['title_highlight_color'] ?? 'focus-color'));
if ($result['title_highlight_color'] === '') {
$result['title_highlight_color'] = 'focus-color';
}
// Legacy flat title_link (URL string) → structured first link.
if (is_string($result['title_link'] ?? null)) {
$result['title_link'] = ['url' => esc_url_raw(trim($result['title_link'])), 'text' => '', 'target' => ''];
}
$result['title_link'] = aksrt_widget_normalize_link($result['title_link'] ?? []);
$result['title_link2'] = aksrt_widget_normalize_link($result['title_link2'] ?? []);
$result['title_link3'] = aksrt_widget_normalize_link($result['title_link3'] ?? []);
$tl_style = is_array($result['title_link_style'] ?? null) ? $result['title_link_style'] : [];
$result['title_link_style'] = [
'color_1' => sanitize_html_class((string) ($tl_style['color_1'] ?? 'jb-blue')) ?: 'jb-blue',
'color_2' => sanitize_html_class((string) ($tl_style['color_2'] ?? 'jb-yellow')) ?: 'jb-yellow',
'color_3' => sanitize_html_class((string) ($tl_style['color_3'] ?? 'jb-green')) ?: 'jb-green',
];
// show_type: accept both AKSRT legacy (pc/mobile) and Zibll (only_pc/only_sm) tokens.
$show_type = (string) ($result['show_type'] ?? 'all');
if ($show_type === 'only_pc' || $show_type === 'pc') {
$show_type = 'only_pc';
} elseif ($show_type === 'only_sm' || $show_type === 'mobile') {
$show_type = 'only_sm';
} else {
$show_type = 'all';
}
$result['show_type'] = $show_type;
// show_id_type: Zibll (show/hide) ↔ AKSRT legacy (include/exclude).
$id_type = (string) ($result['show_id_type'] ?? '');
if ($id_type === 'include') {
$id_type = 'show';
} elseif ($id_type === 'exclude') {
$id_type = 'hide';
} elseif (!in_array($id_type, ['', 'show', 'hide'], true)) {
$id_type = '';
}
$result['show_id_type'] = $id_type;
$result['show_ids'] = sanitize_text_field($result['show_ids'] ?? '');
$result['sidebar_affix'] = !empty($result['sidebar_affix']) || !empty($result['in_affix'] ?? false);
$result['animation_in'] = sanitize_key($result['animation_in'] ?? '');
$result['animation_repeat'] = !empty($result['animation_repeat']);
foreach (['layout_pin_pc', 'layout_pin_m'] as $pin_key) {
$pin = is_array($result[$pin_key] ?? null) ? $result[$pin_key] : [];
$result[$pin_key] = [
'top' => preg_match('/^-?\d+$/', (string) ($pin['top'] ?? '')) ? (string) (int) $pin['top'] : '',
'bottom' => preg_match('/^-?\d+$/', (string) ($pin['bottom'] ?? '')) ? (string) (int) $pin['bottom'] : '',
];
}
$bg = is_array($result['layout_bg'] ?? null) ? $result['layout_bg'] : [];
$result['layout_bg'] = [
'img_white' => aksrt_widget_normalize_bg($bg['img_white'] ?? []),
'img_dark' => aksrt_widget_normalize_bg($bg['img_dark'] ?? []),
];
return $result;
}
}
if (!function_exists('aksrt_widget_normalize_bg')) {
/**
* Normalize a layout_bg sub-block (background CSS props).
*
* @param mixed $raw
* @return array<string,string>
*/
function aksrt_widget_normalize_bg($raw): array {
$raw = is_array($raw) ? $raw : [];
$out = [];
foreach (['background-color', 'background-image', 'background-position', 'background-repeat', 'background-attachment', 'background-size', 'background-origin'] as $k) {
$v = trim((string) ($raw[$k] ?? ''));
if ($k === 'background-image' && $v !== '') {
$v = esc_url_raw($v);
} elseif ($k === 'background-color' && $v !== '') {
// Allow hex / rgb / rgba / named; strip tags defensively.
$v = sanitize_text_field($v);
} else {
$v = sanitize_text_field($v);
}
if ($v !== '') {
$out[$k] = $v;
}
}
return $out;
}
}
if (!function_exists('aksrt_widget_animation_options')) {
/**
* Zibll-aligned module entrance animation options.
*
* @return array<string,string>
*/
function aksrt_widget_animation_options(): array {
return [
'' => __('无动画', 'aksrt'),
'fade' => __('淡入', 'aksrt'),
'slideup' => __('从下往上滑出', 'aksrt'),
'slidedown' => __('从上往下滑出', 'aksrt'),
'slideright' => __('从左往右滑出', 'aksrt'),
'slideleft' => __('从右往左滑出', 'aksrt'),
'zoomin' => __('由小变大', 'aksrt'),
'zoomout' => __('由大变小', 'aksrt'),
];
}
}
if (!function_exists('aksrt_widget_highlight_color_options')) {
/**
* Highlight color palette (Zibll zib_palette parity, theme classes).
*
* @return array<string,string>
*/
function aksrt_widget_highlight_color_options(): array {
return [
'focus-color' => __('主题色', 'aksrt'),
'jb-blue' => __('渐变蓝', 'aksrt'),
'jb-green' => __('渐变绿', 'aksrt'),
'jb-yellow' => __('渐变黄', 'aksrt'),
'jb-orange' => __('渐变橙', 'aksrt'),
'jb-red' => __('渐变红', 'aksrt'),
'jb-purple' => __('渐变紫', 'aksrt'),
'jb-pink' => __('渐变粉', 'aksrt'),
'jb-cyan' => __('渐变青', 'aksrt'),
'c-blue' => __('蓝色', 'aksrt'),
'c-green' => __('绿色', 'aksrt'),
'c-red' => __('红色', 'aksrt'),
'c-yellow' => __('黄色', 'aksrt'),
'c-purple' => __('紫色', 'aksrt'),
];
}
}
if (!function_exists('aksrt_widget_render_link_field')) {
/**
* Render a single title-link sub-field (url/text/target) for the admin form.
*
* @param string $base Field base name, e.g. 'title_link'.
* @param array $link Normalized link {url,text,target}.
* @param string $label Field label.
*/
function aksrt_widget_render_link_field(string $base, array $link, string $label): void {
?>
<p>
<label><?php echo esc_html($label); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($base); ?>[text]" value="<?php echo esc_attr($link['text']); ?>" placeholder="<?php esc_attr_e('按钮文字', 'aksrt'); ?>">
</label>
<input class="widefat" type="url" name="<?php echo esc_attr($base); ?>[url]" value="<?php echo esc_attr($link['url']); ?>" placeholder="<?php esc_attr_e('链接地址', 'aksrt'); ?>">
<label><input type="checkbox" name="<?php echo esc_attr($base); ?>[target]" value="_blank" <?php checked($link['target'] === '_blank'); ?>> <?php esc_html_e('新窗口打开', 'aksrt'); ?></label>
</p>
<?php
}
}
if (!function_exists('aksrt_widget_render_common_form')) {
/**
* Render shared widget admin form fields.
*
* Call this from your widget's form() method. Renders the full Zibll
* common-field set: title block (subtitle / style / highlight / 3 links),
* sidebar affix, display rules, entrance animation, layout padding + bg.
*
* @param array<string, mixed> $instance Current widget instance.
*/
function aksrt_widget_render_common_form(array $instance): void {
$instance = aksrt_widget_normalize_common($instance);
$pin_pc = $instance['layout_pin_pc'];
$pin_m = $instance['layout_pin_m'];
$bg_w = $instance['layout_bg']['img_white'];
$bg_d = $instance['layout_bg']['img_dark'];
?>
<hr style="margin:16px 0 12px;border-color:#e0e0e0;">
<p style="font-size:12px;color:#666;margin-bottom:8px;font-weight:600;"><?php esc_html_e('标题与链接', 'aksrt'); ?></p>
<p>
<label><?php esc_html_e('副标题', 'aksrt'); ?>
<input class="widefat" type="text" name="subtitle" value="<?php echo esc_attr($instance['subtitle']); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('标题样式', 'aksrt'); ?></label>
<label><input type="radio" name="title_style" value="" <?php checked($instance['title_style'], ''); ?>> <?php esc_html_e('简约', 'aksrt'); ?></label>
<label><input type="radio" name="title_style" value="big" <?php checked($instance['title_style'], 'big'); ?>> <?php esc_html_e('居中大标题', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('标题选字高亮(标题中要被高亮的部分)', 'aksrt'); ?>
<input class="widefat" type="text" name="title_highlight" value="<?php echo esc_attr($instance['title_highlight']); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('高亮颜色', 'aksrt'); ?>
<select class="widefat" name="title_highlight_color">
<?php foreach (aksrt_widget_highlight_color_options() as $val => $lbl) : ?>
<option value="<?php echo esc_attr($val); ?>" <?php selected($instance['title_highlight_color'], $val); ?>><?php echo esc_html($lbl); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<?php aksrt_widget_render_link_field('title_link', $instance['title_link'], __('标题链接', 'aksrt')); ?>
<?php if ($instance['title_style'] !== 'big') : ?>
<?php aksrt_widget_render_link_field('title_link2', $instance['title_link2'], __('标题链接 2', 'aksrt')); ?>
<?php aksrt_widget_render_link_field('title_link3', $instance['title_link3'], __('标题链接 3', 'aksrt')); ?>
<?php endif; ?>
<?php if ($instance['title_style'] === 'big') : ?>
<p>
<label><?php esc_html_e('链接样式(颜色1/2/3)', 'aksrt'); ?></label>
<select name="title_link_style[color_1]">
<?php foreach (aksrt_widget_highlight_color_options() as $val => $lbl) : ?>
<option value="<?php echo esc_attr($val); ?>" <?php selected($instance['title_link_style']['color_1'], $val); ?>><?php echo esc_html($lbl); ?></option>
<?php endforeach; ?>
</select>
<select name="title_link_style[color_2]">
<?php foreach (aksrt_widget_highlight_color_options() as $val => $lbl) : ?>
<option value="<?php echo esc_attr($val); ?>" <?php selected($instance['title_link_style']['color_2'], $val); ?>><?php echo esc_html($lbl); ?></option>
<?php endforeach; ?>
</select>
<select name="title_link_style[color_3]">
<?php foreach (aksrt_widget_highlight_color_options() as $val => $lbl) : ?>
<option value="<?php echo esc_attr($val); ?>" <?php selected($instance['title_link_style']['color_3'], $val); ?>><?php echo esc_html($lbl); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php endif; ?>
<p style="font-size:12px;color:#666;margin:10px 0 8px;font-weight:600;"><?php esc_html_e('显示规则', 'aksrt'); ?></p>
<p>
<label><?php esc_html_e('显示设备', 'aksrt'); ?></label>
<label><input type="radio" name="show_type" value="all" <?php checked($instance['show_type'], 'all'); ?>> <?php esc_html_e('全部', 'aksrt'); ?></label>
<label><input type="radio" name="show_type" value="only_pc" <?php checked($instance['show_type'], 'only_pc'); ?>> <?php esc_html_e('仅 PC', 'aksrt'); ?></label>
<label><input type="radio" name="show_type" value="only_sm" <?php checked($instance['show_type'], 'only_sm'); ?>> <?php esc_html_e('仅移动端', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('按 ID 显示或隐藏', 'aksrt'); ?></label>
<label><input type="radio" name="show_id_type" value="" <?php checked($instance['show_id_type'], ''); ?>> <?php esc_html_e('不限制', 'aksrt'); ?></label>
<label><input type="radio" name="show_id_type" value="show" <?php checked($instance['show_id_type'], 'show'); ?>> <?php esc_html_e('指定 ID 显示', 'aksrt'); ?></label>
<label><input type="radio" name="show_id_type" value="hide" <?php checked($instance['show_id_type'], 'hide'); ?>> <?php esc_html_e('指定 ID 隐藏', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('文章/页面 ID(逗号分隔)', 'aksrt'); ?>
<input class="widefat" type="text" name="show_ids" value="<?php echo esc_attr($instance['show_ids']); ?>" placeholder="1,2,3">
</label>
</p>
<p>
<label><input type="checkbox" name="sidebar_affix" value="1" <?php checked($instance['sidebar_affix']); ?>> <?php esc_html_e('侧栏吸顶(仅侧栏有效)', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('入场动画', 'aksrt'); ?>
<select class="widefat" name="animation_in">
<?php foreach (aksrt_widget_animation_options() as $val => $lbl) : ?>
<option value="<?php echo esc_attr($val); ?>" <?php selected($instance['animation_in'], $val); ?>><?php echo esc_html($lbl); ?></option>
<?php endforeach; ?>
</select>
</label>
</p>
<p>
<label><input type="checkbox" name="animation_repeat" value="1" <?php checked($instance['animation_repeat']); ?>> <?php esc_html_e('动画重复播放', 'aksrt'); ?></label>
</p>
<p style="font-size:12px;color:#666;margin:10px 0 8px;font-weight:600;"><?php esc_html_e('布局:间距与背景', 'aksrt'); ?></p>
<p>
<label><?php esc_html_e('PC 端额外上/下间距 (px)', 'aksrt'); ?>
<input class="tiny-text" type="number" name="layout_pin_pc[top]" value="<?php echo esc_attr($pin_pc['top']); ?>" placeholder="0">
<input class="tiny-text" type="number" name="layout_pin_pc[bottom]" value="<?php echo esc_attr($pin_pc['bottom']); ?>" placeholder="0">
</label>
</p>
<p>
<label><?php esc_html_e('移动端额外上/下间距 (px)', 'aksrt'); ?>
<input class="tiny-text" type="number" name="layout_pin_m[top]" value="<?php echo esc_attr($pin_m['top']); ?>" placeholder="0">
<input class="tiny-text" type="number" name="layout_pin_m[bottom]" value="<?php echo esc_attr($pin_m['bottom']); ?>" placeholder="0">
</label>
</p>
<p>
<label><?php esc_html_e('日间背景色', 'aksrt'); ?>
<input class="widefat" type="text" name="layout_bg[img_white][background-color]" value="<?php echo esc_attr($bg_w['background-color'] ?? ''); ?>">
</label>
<label><?php esc_html_e('日间背景图', 'aksrt'); ?>
<input class="widefat" type="url" name="layout_bg[img_white][background-image]" value="<?php echo esc_attr($bg_w['background-image'] ?? ''); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('夜间背景色', 'aksrt'); ?>
<input class="widefat" type="text" name="layout_bg[img_dark][background-color]" value="<?php echo esc_attr($bg_d['background-color'] ?? ''); ?>">
</label>
<label><?php esc_html_e('夜间背景图', 'aksrt'); ?>
<input class="widefat" type="url" name="layout_bg[img_dark][background-image]" value="<?php echo esc_attr($bg_d['background-image'] ?? ''); ?>">
</label>
</p>
<?php
}
}
if (!function_exists('aksrt_widget_update_common')) {
/**
* Process shared widget form fields in update().
*
* @param array<string, mixed> $new_instance
* @param array<string, mixed> $old_instance
* @return array<string, mixed>
*/
function aksrt_widget_update_common(array $new_instance, array $old_instance): array {
$merged = array_merge($old_instance, $new_instance);
return aksrt_widget_normalize_common($merged);
}
}
if (!function_exists('aksrt_widget_render_common_before')) {
/**
* Build common wrapper classes/attrs for a widget instance.
*
* Zibll CSF_Widget::wrap_attributes() parity: show-type class, sidebar
* affix data-affix, entrance animation class, layout padding/bg CSS vars.
* Call this at the start of widget() before rendering content.
*
* @param array $args Sidebar args.
* @param array $instance Widget instance.
* @return array<int,string> CSS class names (Zibll show_class style).
*/
function aksrt_widget_get_common_wrapper_classes(array $args, array $instance): array {
$instance = aksrt_widget_normalize_common($instance);
$classes = [];
// Show type: PC or mobile (Zibll hidden-xs / visible-xs-block).
if ($instance['show_type'] === 'only_pc') {
$classes[] = 'hidden-xs';
} elseif ($instance['show_type'] === 'only_sm') {
$classes[] = 'visible-xs-block';
}
// Sidebar affix
if ($instance['sidebar_affix']) {
$classes[] = 'is-affix';
}
// Animation (Zibll obs-animate ani-*).
if ($instance['animation_in'] !== '') {
$classes[] = 'obs-animate';
$classes[] = 'ani-' . $instance['animation_in'];
if ($instance['animation_repeat']) {
$classes[] = 'obs-animate-repeat';
}
}
return $classes;
}
}
if (!function_exists('aksrt_widget_common_wrapper_attr')) {
/**
* Build the full wrapper attribute string (class + style + data-affix) for
* the Zibll .zib-widget-wrap parity layer.
*
* Combine with a widget-specific class via a leading space, or use as-is.
*
* @param array<string,mixed> $instance Widget instance.
* @param string $extra_class Additional class (widget-specific).
* @return string Escaped attribute string beginning with a leading space, e.g. ' class="…" style="…" data-affix="true"'.
*/
function aksrt_widget_common_wrapper_attr(array $instance, string $extra_class = ''): string {
$instance = aksrt_widget_normalize_common($instance);
$classes = ['zib-widget-wrap'];
foreach (aksrt_widget_get_common_wrapper_classes([], $instance) as $c) {
$classes[] = $c;
}
$extra_class = trim($extra_class);
if ($extra_class !== '') {
$classes[] = $extra_class;
}
$styles = [];
if ($instance['layout_pin_pc']['top'] !== '') {
$styles[] = '--pt-pc:' . (int) $instance['layout_pin_pc']['top'] . 'px;';
}
if ($instance['layout_pin_pc']['bottom'] !== '') {
$styles[] = '--pb-pc:' . (int) $instance['layout_pin_pc']['bottom'] . 'px;';
}
if ($instance['layout_pin_m']['top'] !== '') {
$styles[] = '--pt-m:' . (int) $instance['layout_pin_m']['top'] . 'px;';
}
if ($instance['layout_pin_m']['bottom'] !== '') {
$styles[] = '--pb-m:' . (int) $instance['layout_pin_m']['bottom'] . 'px;';
}
foreach (['white' => 'img_white', 'dark' => 'img_dark'] as $mode => $bg_key) {
$bg = $instance['layout_bg'][$bg_key] ?? [];
if (!empty($bg['background-color'])) {
$styles[] = '--bg-color-' . $mode . ':' . $bg['background-color'] . ';';
}
if (!empty($bg['background-image'])) {
$styles[] = '--bg-img-' . $mode . ':url("' . $bg['background-image'] . '");';
}
}
$attr = ' class="' . esc_attr(implode(' ', $classes)) . '"';
if ($styles) {
$attr .= ' style="' . esc_attr(implode('', $styles)) . '"';
}
if ($instance['sidebar_affix']) {
$attr .= ' data-affix="true"';
}
return $attr;
}
}
if (!function_exists('aksrt_widget_affix_attrs')) {
/**
* Compact affix class+attribute helper for widgets that apply affix onto an
* inner wrapper (the two-family pattern from the legacy in_affix widgets).
*
* @param array<string,mixed> $instance Widget instance.
* @return array{class:string,attr:string} class includes leading space when active; attr includes leading space.
*/
function aksrt_widget_affix_attrs(array $instance): array {
$instance = aksrt_widget_normalize_common($instance);
if (empty($instance['sidebar_affix'])) {
return ['class' => '', 'attr' => ''];
}
return ['class' => ' is-affix', 'attr' => ' data-affix="true"'];
}
}
if (!function_exists('aksrt_widget_render_title')) {
/**
* Render the Zibll-aligned widget title block (CSF_Widget::show_title parity).
*
* Two styles:
* - '' (简约): .box-body notop > .title-theme with optional subtitle + more link
* - 'big' (居中大标题): .big-title-wrap with up to 3 button links
*
* Highlight wraps the matched substring in a colored span.
*
* @param array<string,mixed> $instance Widget instance.
* @return string Title HTML (empty when no title).
*/
function aksrt_widget_render_title(array $instance): string {
$instance = aksrt_widget_normalize_common($instance);
$title = trim((string) ($instance['title'] ?? ''));
if ($title === '') {
return '';
}
$highlight = trim((string) $instance['title_highlight']);
$subtitle = (string) $instance['subtitle'];
$anim = function_exists('aksrt_widget_animation_class')
? aksrt_widget_animation_class($instance)
: '';
$title = esc_html($title);
if ($highlight !== '' && $highlight !== $title) {
$color = $instance['title_highlight_color'];
$hl_esc = esc_html($highlight);
$title = str_replace($hl_esc, '<span class="title-highlight ' . esc_attr($color) . '">' . $hl_esc . '</span>', $title);
}
if ($instance['title_style'] === 'big') {
$style = $instance['title_link_style'];
$links = '';
foreach (['title_link' => 'color_1', 'title_link2' => 'color_2', 'title_link3' => 'color_3'] as $key => $color_key) {
$link = $instance[$key];
if ($link['url'] !== '' && $link['text'] !== '') {
$blank = $link['target'] === '_blank' ? ' target="_blank"' : '';
$links .= '<a' . $blank . ' href="' . esc_url($link['url']) . '" class="but ' . esc_attr($style[$color_key]) . '">' . esc_html($link['text']) . '</a>';
}
}
$links = $links !== '' ? '<div class="flex jc big-title-buts' . $anim . '">' . $links . '</div>' : '';
$sub = $subtitle !== '' ? '<p class="big-title-sub muted-2-color' . $anim . '">' . esc_html($subtitle) . '</p>' : '';
return '<div class="big-title-wrap"><h2 class="big-title' . $anim . '">' . $title . '</h2>' . $sub . $links . '</div>';
}
// Simple style.
$link = $instance['title_link'];
$sub = $subtitle !== '' ? '<small class="ml10">' . esc_html($subtitle) . '</small>' : '';
$more = '';
if ($link['url'] !== '') {
$text = $link['text'] !== '' ? $link['text'] : '<i class="fa fa-angle-right fa-fw"></i>' . __('更多', 'aksrt');
$blank = $link['target'] === '_blank' ? ' target="_blank"' : '';
$more = '<div class="pull-right em09 mt3"><a' . $blank . ' href="' . esc_url($link['url']) . '" class="muted-2-color">' . wp_kses_post($text) . '</a></div>';
}
return '<div class="box-body notop' . $anim . '"><div class="title-theme">' . $title . $sub . $more . '</div></div>';
}
}
if (!function_exists('aksrt_widget_animation_class')) {
/**
* Zibll CSF_Widget::animation_class parity: obs-animate ani-* [+repeat].
*
* @param array<string,mixed> $instance Normalized widget instance.
* @param bool $is_title Reserved for title-specific animation.
* @return string
*/
function aksrt_widget_animation_class(array $instance, bool $is_title = false): string {
$animation = (string) ($instance['animation_in'] ?? '');
if ($animation === '') {
return '';
}
$class = ' obs-animate ani-' . $animation;
if (!empty($instance['animation_repeat'])) {
$class .= ' obs-animate-repeat';
}
return $class;
}
}
if (!function_exists('aksrt_widget_should_display_by_ids')) {
/**
* Check if the current post/page should display the widget based on show_id_type and show_ids.
*
* show_id_type tokens: '' (no limit) | 'show' (whitelist) | 'hide' (blacklist).
* Accepts legacy 'include'/'exclude' via normalize_common.
*
* @param array $instance Widget instance.
* @return bool
*/
function aksrt_widget_should_display_by_ids(array $instance): bool {
$instance = aksrt_widget_normalize_common($instance);
if ($instance['show_id_type'] === '' || $instance['show_ids'] === '') {
return true;
}
$current_id = get_the_ID();
if (!$current_id) {
return true; // Not on a singular page, allow display
}
// Zibll splits on comma/Chinese-comma/whitespace/newline.
$ids = array_filter(array_map('absint', preg_split('/[,,\s]+/', $instance['show_ids']) ?: []));
if ($instance['show_id_type'] === 'show') {
return in_array($current_id, $ids, true);
}
if ($instance['show_id_type'] === 'hide') {
return !in_array($current_id, $ids, true);
}
return true;
}
}
+1 -319
View File
@@ -7,7 +7,7 @@
defined('ABSPATH') || exit;
require_once AKSRT_DIR . '/inc/modules/widget-common.php';
require_once AKSRT_DIR . '/inc/modules/widget-areas.php';
/**
* Register custom widgets.
@@ -71,324 +71,6 @@ function aksrt_register_widgets() {
}
add_action('widgets_init', 'aksrt_register_widgets');
if (!function_exists('aksrt_widget_sidebar_area_ids')) {
/**
* Sidebars intended for sidebar-only widgets.
*
* @return array<int, string>
*/
function aksrt_widget_sidebar_area_ids(): array {
return [
'home_sidebar',
'sidebar-article',
'all_sidebar_top',
'all_sidebar_bottom',
'cat_sidebar',
'tag_sidebar',
'search_sidebar',
];
}
}
if (!function_exists('aksrt_widget_archive_area_ids')) {
/**
* Widget zones for archive pages (cat/tag/search).
*
* @return array<int, string>
*/
function aksrt_widget_archive_area_ids(): array {
return [
'cat_top_fluid',
'cat_top_content',
'cat_bottom_content',
'cat_bottom_fluid',
'cat_sidebar',
'tag_top_fluid',
'tag_top_content',
'tag_bottom_content',
'tag_bottom_fluid',
'tag_sidebar',
'search_top_fluid',
'search_top_content',
'search_bottom_content',
'search_bottom_fluid',
'search_sidebar',
];
}
}
if (!function_exists('aksrt_widget_homepage_area_ids')) {
/**
* Homepage widget zones for homepage-only widgets.
*
* @return array<int, string>
*/
function aksrt_widget_homepage_area_ids(): array {
return [
'home_top_fluid',
'home_top_content',
'home_bottom_content',
'home_bottom_fluid',
];
}
}
if (!function_exists('aksrt_widget_single_area_ids')) {
/**
* Single post/page widget zones.
*
* @return array<int, string>
*/
function aksrt_widget_single_area_ids(): array {
return [
'single_top_fluid',
'single_top_content',
'single_bottom_content',
'single_bottom_fluid',
];
}
}
if (!function_exists('aksrt_widget_base_from_id')) {
/**
* Get widget base id from a stored widget instance id.
*/
function aksrt_widget_base_from_id(string $widget_id): string {
return preg_replace('/-\d+$/', '', $widget_id) ?: $widget_id;
}
}
if (!function_exists('aksrt_widget_allowed_sidebars')) {
/**
* Allowed sidebar ids for a widget base id.
*
* @return array<int, string>
*/
function aksrt_widget_allowed_sidebars(string $widget_base_id): array {
$sidebar_only = [
'aksrt_categories',
'aksrt_tag_cloud',
'aksrt_hot_posts',
];
$homepage_only = [
'aksrt_category_recommend',
];
$hybrid = [
'aksrt_tab_posts',
'aksrt_category_block',
'aksrt_category_tabs',
];
if (in_array($widget_base_id, $sidebar_only, true)) {
return aksrt_widget_sidebar_area_ids();
}
if (in_array($widget_base_id, $homepage_only, true)) {
return aksrt_widget_homepage_area_ids();
}
if (in_array($widget_base_id, $hybrid, true)) {
return array_values(array_unique(array_merge(
aksrt_widget_sidebar_area_ids(),
aksrt_widget_homepage_area_ids()
)));
}
if ($widget_base_id === 'aksrt_post_pay_box') {
return ['sidebar-article'];
}
return [];
}
}
if (!function_exists('aksrt_widget_is_allowed_in_sidebar')) {
/**
* Whether a widget base id can be assigned to the given sidebar id.
*/
function aksrt_widget_is_allowed_in_sidebar(string $widget_base_id, string $sidebar_id): bool {
if ($widget_base_id === '' || $sidebar_id === '') {
return true;
}
if ($sidebar_id === 'wp_inactive_widgets' || strpos($sidebar_id, 'orphaned_widgets') === 0) {
return true;
}
$allowed = aksrt_widget_allowed_sidebars($widget_base_id);
if (empty($allowed)) {
return true;
}
return in_array($sidebar_id, $allowed, true);
}
}
if (!function_exists('aksrt_widget_get_area_context')) {
/**
* Resolve widget area context from sidebar args.
*/
function aksrt_widget_get_area_context(array $args): string {
$sidebar_id = isset($args['id']) ? (string) $args['id'] : '';
if (in_array($sidebar_id, aksrt_widget_sidebar_area_ids(), true)) {
return 'sidebar';
}
if (in_array($sidebar_id, aksrt_widget_homepage_area_ids(), true)) {
return 'homepage';
}
if (in_array($sidebar_id, aksrt_widget_archive_area_ids(), true)) {
return 'archive';
}
if (in_array($sidebar_id, aksrt_widget_single_area_ids(), true)) {
return 'single';
}
return 'other';
}
}
if (!function_exists('aksrt_widget_render_location_help')) {
/**
* Render placement hint inside widget admin forms.
*/
function aksrt_widget_render_location_help(string $scope): void {
$messages = [
'sidebar' => __('仅限侧栏区域:首页侧栏、文章页侧边栏、全站侧栏、分类/标签/搜索侧栏。', 'aksrt'),
'homepage' => __('仅限首页挂载区域:首页顶部/底部全宽、首页内容上方/下方。', 'aksrt'),
'hybrid' => __('支持侧栏、首页和归档页挂载区域。', 'aksrt'),
'archive' => __('仅限归档页挂载区域:分类页、标签页、搜索页的顶部/底部/侧栏。', 'aksrt'),
];
if (empty($messages[$scope])) {
return;
}
printf(
'<p style="margin-top:10px;font-size:12px;color:#666;">%s</p>',
esc_html($messages[$scope])
);
}
}
if (!function_exists('aksrt_filter_widget_display_by_area')) {
/**
* Prevent widgets from rendering outside their allowed areas,
* and apply per-widget display rules (show_type, show_id_type/show_ids).
*
* @param array|false $instance Widget instance.
* @param WP_Widget $widget Widget object.
* @param array $args Sidebar args.
* @return array|false
*/
function aksrt_filter_widget_display_by_area($instance, WP_Widget $widget, array $args) {
$sidebar_id = isset($args['id']) ? (string) $args['id'] : '';
$widget_base_id = isset($widget->id_base) ? (string) $widget->id_base : '';
if ($instance === false) {
return false;
}
// Layer 1: Area legality check
if (!aksrt_widget_is_allowed_in_sidebar($widget_base_id, $sidebar_id)) {
return false;
}
// Layer 2: Display rule check (show_type for PC/mobile)
if (is_array($instance) && function_exists('aksrt_widget_normalize_common')) {
$normalized = aksrt_widget_normalize_common($instance);
// Show type: PC / mobile filter (Zibll only_pc / only_sm).
if ($normalized['show_type'] === 'only_pc' && wp_is_mobile()) {
return false;
}
if ($normalized['show_type'] === 'only_sm' && !wp_is_mobile()) {
return false;
}
// Show IDs: whitelist / blacklist filter
if (!aksrt_widget_should_display_by_ids($normalized)) {
return false;
}
}
return $instance;
}
}
add_filter('widget_display_callback', 'aksrt_filter_widget_display_by_area', 10, 3);
if (!function_exists('aksrt_filter_widget_sidebar_assignments')) {
/**
* Move disallowed widget assignments back to the inactive widgets bucket.
*
* @param mixed $new_value New sidebars_widgets option value.
* @param mixed $old_value Previous option value.
* @return mixed
*/
function aksrt_filter_widget_sidebar_assignments($new_value, $old_value) {
if (!is_array($new_value)) {
return $new_value;
}
$removed = [];
foreach ($new_value as $sidebar_id => $widget_ids) {
if (!is_array($widget_ids) || $sidebar_id === 'wp_inactive_widgets') {
continue;
}
$filtered = [];
foreach ($widget_ids as $widget_id) {
if (!is_string($widget_id) || $widget_id === '') {
continue;
}
$widget_base_id = aksrt_widget_base_from_id($widget_id);
if (aksrt_widget_is_allowed_in_sidebar($widget_base_id, (string) $sidebar_id)) {
$filtered[] = $widget_id;
} else {
$removed[] = $widget_id;
}
}
$new_value[$sidebar_id] = array_values($filtered);
}
if (empty($removed)) {
return $new_value;
}
if (!isset($new_value['wp_inactive_widgets']) || !is_array($new_value['wp_inactive_widgets'])) {
$new_value['wp_inactive_widgets'] = [];
}
$assigned = [];
foreach ($new_value as $widget_ids) {
if (!is_array($widget_ids)) {
continue;
}
foreach ($widget_ids as $widget_id) {
if (is_string($widget_id) && $widget_id !== '') {
$assigned[$widget_id] = true;
}
}
}
foreach (array_values(array_unique($removed)) as $widget_id) {
if (!isset($assigned[$widget_id]) && !in_array($widget_id, $new_value['wp_inactive_widgets'], true)) {
$new_value['wp_inactive_widgets'][] = $widget_id;
}
}
return $new_value;
}
}
add_filter('pre_update_option_sidebars_widgets', 'aksrt_filter_widget_sidebar_assignments', 10, 2);
/**
* Register a block editor collection for AKSRT widgets.
*/
+3 -28
View File
@@ -7,17 +7,15 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class ButtonsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct('aksrt_buttons', esc_html__('AKSRT 按钮组', 'aksrt'), [
'description' => esc_html__('显示一组自定义按钮,支持外链跳转、色系与入场动画。', 'aksrt'),
'description' => esc_html__('显示一组自定义按钮,支持外链跳转与色系。', 'aksrt'),
'classname' => 'widget_aksrt_buttons',
]);
}
@@ -60,8 +58,6 @@ class ButtonsWidget extends \WP_Widget {
if (!in_array($m_pad, ['small', 'medium', 'large'], true)) {
$m_pad = 'medium';
}
$anim = sanitize_key((string) ($instance['obs_animation'] ?? ''));
$anim_repeat = !empty($instance['animation_repeat']);
$pc_width = max(0, min(400, (int) ($instance['pc_width'] ?? 0)));
$m_width = max(0, min(400, (int) ($instance['m_width'] ?? 0)));
@@ -74,7 +70,6 @@ class ButtonsWidget extends \WP_Widget {
}
$buts = '';
$key = 0;
foreach ($buttons as $btn) {
if (!is_array($btn)) {
continue;
@@ -122,19 +117,13 @@ class ButtonsWidget extends \WP_Widget {
$left_html = $icon_left !== '' ? '<i class="' . esc_attr($icon_left) . '" aria-hidden="true"></i> ' : '';
$right_html = $icon_right !== '' ? ' <span class="ml6"><i class="' . esc_attr($icon_right) . '" aria-hidden="true"></i></span>' : '';
$anim_pack = function_exists('aksrt_obs_animate_attrs')
? aksrt_obs_animate_attrs($anim, $anim_repeat, $key * 0.15)
: ['class' => '', 'attr' => ''];
$tgt = $target !== '' && strpos($url, 'javascript:') !== 0
? ' target="' . esc_attr($target) . '" rel="noopener noreferrer"'
: '';
$buts .= '<div class="' . esc_attr(trim($anim_pack['class'])) . '"' . $anim_pack['attr'] . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$buts .= '<a class="' . esc_attr($item_class) . '" href="' . esc_url($url) . '"' . $tgt . '>'
$buts .= '<div><a class="' . esc_attr($item_class) . '" href="' . esc_url($url) . '"' . $tgt . '>'
. $left_html . esc_html($text) . $right_html . '</a>';
$buts .= '</div>';
$key++;
}
if ($buts === '') {
@@ -192,17 +181,6 @@ class ButtonsWidget extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hollow')); ?>" value="1" <?php checked(!empty($instance['hollow'])); ?>> <?php esc_html_e('镂空', 'aksrt'); ?></label>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('go_link')); ?>" value="1" <?php checked(!empty($instance['go_link'])); ?>> <?php esc_html_e('外链跳转页', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('入场动画', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('obs_animation')); ?>">
<option value="" <?php selected(($instance['obs_animation'] ?? ''), ''); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
<option value="fade" <?php selected(($instance['obs_animation'] ?? ''), 'fade'); ?>><?php esc_html_e('淡入', 'aksrt'); ?></option>
<option value="slideup" <?php selected(($instance['obs_animation'] ?? ''), 'slideup'); ?>><?php esc_html_e('上滑', 'aksrt'); ?></option>
<option value="slideright" <?php selected(($instance['obs_animation'] ?? ''), 'slideright'); ?>><?php esc_html_e('右滑', 'aksrt'); ?></option>
<option value="zoomin" <?php selected(($instance['obs_animation'] ?? ''), 'zoomin'); ?>><?php esc_html_e('放大', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php foreach ($buttons as $i => $btn) : ?>
<div style="border:1px solid #ddd;padding:6px;margin-bottom:6px;border-radius:4px;">
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name("buttons[{$i}][text]")); ?>" value="<?php echo esc_attr((string) ($btn['text'] ?? '')); ?>" placeholder="<?php esc_attr_e('按钮文字', 'aksrt'); ?>">
@@ -217,7 +195,6 @@ class ButtonsWidget extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name("buttons[{$i}][new_window]")); ?>" value="1" <?php checked(!empty($btn['new_window'])); ?>> <?php esc_html_e('新窗口', 'aksrt'); ?></label>
</div>
<?php endforeach;
$this->render_common_form_section($instance);
}
/**
@@ -262,9 +239,7 @@ class ButtonsWidget extends \WP_Widget {
'm_padding' => in_array(($new_instance['m_padding'] ?? 'medium'), ['small', 'medium', 'large'], true) ? $new_instance['m_padding'] : 'medium',
'pc_width' => max(0, min(500, (int) ($new_instance['pc_width'] ?? 0))),
'm_width' => max(0, min(500, (int) ($new_instance['m_width'] ?? 0))),
'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? '')),
'animation_repeat'=> !empty($new_instance['animation_repeat']) ? 1 : 0,
'buttons' => $buttons,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+2 -12
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class CategoriesWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
@@ -179,14 +177,7 @@ class CategoriesWidget extends \WP_Widget {
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
if ($affix['class'] !== '') {
echo '<div class="is-affix"' . $affix['attr'] . '>';
}
echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -241,7 +232,6 @@ class CategoriesWidget extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_empty')); ?>" value="1" <?php checked(!isset($instance['hide_empty']) || !empty($instance['hide_empty'])); ?>> <?php esc_html_e('隐藏空分类', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hierarchical')); ?>" value="1" <?php checked(!empty($instance['hierarchical'])); ?>> <?php esc_html_e('层级树', 'aksrt'); ?></label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -255,7 +245,7 @@ class CategoriesWidget extends \WP_Widget {
if (!in_array($orderby, ['count', 'name', 'id', 'slug'], true)) {
$orderby = 'count';
}
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(50, (int) ($new_instance['count'] ?? 8))),
'orderby' => $orderby,
@@ -268,6 +258,6 @@ class CategoriesWidget extends \WP_Widget {
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
'hierarchical' => !empty($new_instance['hierarchical']) ? 1 : 0,
]);
];
}
}
+1 -4
View File
@@ -5,7 +5,6 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
@@ -313,7 +312,6 @@ if (!function_exists('aksrt_home_widget_get_lazyload_args')) {
* Homepage category block widget.
*/
class CategoryBlockWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -490,7 +488,6 @@ class CategoryBlockWidget extends \WP_Widget {
</p>
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('hybrid'); } ?>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -502,7 +499,7 @@ class CategoryBlockWidget extends \WP_Widget {
'layout' => aksrt_home_widget_resolve_layout($new_instance['layout'] ?? 'card', 'card'),
'show_more' => ! empty($new_instance['show_more']) ? 1 : 0,
'paginate' => in_array(sanitize_key((string) ($new_instance['paginate'] ?? 'none')), ['none', 'ajax', 'number'], true) ? sanitize_key((string) ($new_instance['paginate'] ?? 'none')) : 'none',
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
public static function resolve_category_id_static(array $instance): int {
+1 -4
View File
@@ -5,7 +5,6 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
@@ -13,7 +12,6 @@ defined('ABSPATH') || exit;
* Homepage category recommend cards widget.
*/
class CategoryRecommendWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -198,7 +196,6 @@ class CategoryRecommendWidget extends \WP_Widget {
</p>
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('homepage'); } ?>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -211,7 +208,7 @@ class CategoryRecommendWidget extends \WP_Widget {
'show_description' => ! empty($new_instance['show_description']) ? 1 : 0,
'show_dates' => ! empty($new_instance['show_dates']) ? 1 : 0,
'show_more' => ! empty($new_instance['show_more']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
/**
+1 -4
View File
@@ -5,7 +5,6 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
@@ -13,7 +12,6 @@ defined('ABSPATH') || exit;
* Homepage category tabs widget.
*/
class CategoryTabsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -161,7 +159,6 @@ public function form($instance): void {
</p>
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('hybrid'); } ?>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -172,7 +169,7 @@ public function form($instance): void {
'columns' => max(1, min(4, absint($new_instance['columns'] ?? 3))),
'layout' => aksrt_home_widget_resolve_layout($new_instance['layout'] ?? 'card', 'card'),
'show_more' => ! empty($new_instance['show_more']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
/**
+3 -8
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class HotPostsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
@@ -209,12 +207,10 @@ class HotPostsWidget extends \WP_Widget {
if ($title) {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
$instance['paginate'] = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$payload = self::query_payload($instance, 1);
$rest = rest_url('aksrt/v1/widgets/hot-posts');
$wrap = 'ajaxpager' . $affix['class'];
echo '<div class="' . esc_attr($wrap) . '" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '"' . $affix['attr'] . '>';
echo '<div class="ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
@@ -263,12 +259,11 @@ class HotPostsWidget extends \WP_Widget {
</select>
</label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'count' => absint($new_instance['count'] ?? 5),
'order_by' => in_array($new_instance['order_by'] ?? '', ['views', 'comments', 'likes', 'favorites', 'downloads'], true) ? $new_instance['order_by'] : 'views',
@@ -276,6 +271,6 @@ class HotPostsWidget extends \WP_Widget {
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
'target_blank' => !empty($new_instance['target_blank']) ? 1 : 0,
'paginate' => in_array(sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')), ['none', 'ajax', 'number'], true) ? sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) : 'ajax',
]);
];
}
}
+3 -17
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class IconCardsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -45,8 +43,6 @@ class IconCardsWidget extends \WP_Widget {
$show_bg = !isset($instance['show_widget_bg']) || !empty($instance['show_widget_bg']);
$icon_size = max(0, min(64, (int) ($instance['icon_size'] ?? $instance['size'] ?? 0)));
$icon_radius = !empty($instance['icon_radius']);
$anim = sanitize_key((string) ($instance['obs_animation'] ?? ''));
$anim_repeat = !empty($instance['animation_repeat']);
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
@@ -64,7 +60,6 @@ class IconCardsWidget extends \WP_Widget {
echo '<div class="row gutters-5">';
}
$idx = 0;
foreach ($cards as $card) {
$url = (string) ($card['url'] ?? '');
if ($url !== '' && $go_link && function_exists('aksrt_go_link')) {
@@ -86,24 +81,17 @@ class IconCardsWidget extends \WP_Widget {
],
];
$anim_pack = function_exists('aksrt_obs_animate_attrs')
? aksrt_obs_animate_attrs($anim, $anim_repeat, $idx * 0.08)
: ['class' => '', 'attr' => ''];
if ($is_row) {
echo '<div class="' . esc_attr(trim($row_class . $anim_pack['class'])) . '"' . $anim_pack['attr'] . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} elseif ($anim_pack['class'] !== '') {
echo '<div class="' . esc_attr(trim($anim_pack['class'])) . '"' . $anim_pack['attr'] . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="' . esc_attr($row_class) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if (function_exists('aksrt_icon_card')) {
echo aksrt_icon_card($card_args); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($is_row || $anim_pack['class'] !== '') {
if ($is_row) {
echo '</div>';
}
$idx++;
}
if ($is_row) {
@@ -197,7 +185,6 @@ class IconCardsWidget extends \WP_Widget {
<input class="widefat" type="url" name="<?php echo esc_attr($this->get_field_name("cards[{$i}][url]")); ?>" value="<?php echo esc_attr((string) ($card['url'] ?? '')); ?>" placeholder="<?php esc_attr_e('链接', 'aksrt'); ?>">
</div>
<?php endforeach;
$this->render_common_form_section($instance);
}
/**
@@ -213,8 +200,7 @@ class IconCardsWidget extends \WP_Widget {
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
'show_widget_bg' => !empty($new_instance['show_widget_bg']) ? 1 : 0,
'icon_radius' => !empty($new_instance['icon_radius']) ? 1 : 0,
'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? '')),
'cards' => $this->normalize($new_instance['cards'] ?? []),
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+3 -9
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class NoticeWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct('aksrt_notice', esc_html__('AKSRT 滚动公告', 'aksrt'), [
@@ -39,7 +37,6 @@ if (!$items) {
$radius = !empty($instance['radius']);
$interval = max(2, min(15, (int) ($instance['interval'] ?? 4)));
$align = sanitize_html_class((string) ($instance['alignment'] ?? ''));
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
@@ -54,9 +51,7 @@ if (!$items) {
if ($align !== '') {
$wrap_cls .= ' ' . $align;
}
$wrap_cls .= $affix['class'];
echo '<div class="' . esc_attr($wrap_cls) . '"' . $affix['attr'] . '>';
echo '<div class="' . esc_attr($wrap_cls) . '">';
echo '<div class="aksrt-scroll-notice__icon bulletin-icon"><i class="fas fa-bullhorn" aria-hidden="true"></i></div>';
// Prefer shared vertical new-swiper (zib_notice parity) via slider.js Type C.
@@ -194,12 +189,11 @@ if (!$items) {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('blank')); ?>" value="1" <?php checked(!empty($instance['blank'])); ?>> <?php esc_html_e('链接新窗口', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('radius')); ?>" value="1" <?php checked(!empty($instance['radius'])); ?>> <?php esc_html_e('两端圆形', 'aksrt'); ?></label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'items' => $this->normalize_items(['items' => $new_instance['items'] ?? []]),
'interval' => max(2, min(15, (int) ($new_instance['interval'] ?? 4))),
@@ -207,6 +201,6 @@ if (!$items) {
'alignment' => sanitize_html_class((string) ($new_instance['alignment'] ?? '')),
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'radius' => !empty($new_instance['radius']) ? 1 : 0,
]);
];
}
}
+2 -12
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class RecentCommentsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct('aksrt_recent_comments', esc_html__('AKSRT 最新评论', 'aksrt'), [
@@ -162,14 +160,7 @@ class RecentCommentsWidget extends \WP_Widget {
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
if ($affix['class'] !== '') {
echo '<div class="is-affix"' . $affix['attr'] . '>';
}
echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -202,7 +193,6 @@ class RecentCommentsWidget extends \WP_Widget {
</label>
<small><?php esc_html_e('填数字 ID 时按作者排除;填文字时按内容关键词排除(兼容旧配置)。', 'aksrt'); ?></small>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -212,13 +202,13 @@ class RecentCommentsWidget extends \WP_Widget {
* @return array<string,mixed>
*/
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(50, (int) ($new_instance['count'] ?? 5))),
'show_avatar' => !empty($new_instance['show_avatar']) ? 1 : 0,
'outpost' => sanitize_text_field((string) ($new_instance['outpost'] ?? '')),
'outer' => sanitize_text_field((string) ($new_instance['outer'] ?? '')),
'outer_keywords' => sanitize_text_field((string) ($new_instance['outer_keywords'] ?? '')),
]);
];
}
}
+3 -8
View File
@@ -7,12 +7,10 @@
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'), [
@@ -35,9 +33,6 @@ class SearchWidget extends \WP_Widget {
$show_kw = !empty($instance['show_keywords']);
$kw_title = sanitize_text_field((string) ($instance['keywords_title'] ?? __('热门搜索', 'aksrt')));
$keyword_limit = max(1, min(50, (int) ($instance['keyword_limit'] ?? 12)));
// 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';
@@ -71,7 +66,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 $affix['class']; ?>" id="<?php echo $uid; ?>"<?php echo $affix['attr']; ?>>
<div class="widget-search-box aksrt-search-widget search-input" id="<?php echo $uid; ?>">
<form
role="search"
method="get"
@@ -169,13 +164,13 @@ class SearchWidget extends \WP_Widget {
}
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
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,
'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))),
]);
];
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class TabPostsWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -353,7 +351,6 @@ class TabPostsWidget extends \WP_Widget {
</p>
</div>
<?php endforeach;
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -396,6 +393,6 @@ class TabPostsWidget extends \WP_Widget {
}
}
$result['tabs'] = !empty($tabs) ? $tabs : 'latest,comments,random';
return $this->update_common_fields($new_instance, $old_instance, $result);
return $result;
}
}
+2 -12
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class TagCloudWidget extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
@@ -108,14 +106,7 @@ class TagCloudWidget extends \WP_Widget {
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
if ($affix['class'] !== '') {
echo '<div class="is-affix"' . $affix['attr'] . '>';
}
echo self::render_html($instance); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -180,12 +171,11 @@ class TagCloudWidget extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_empty')); ?>" value="1" <?php checked(!empty($instance['hide_empty'])); ?>> <?php esc_html_e('隐藏空标签', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('scale')); ?>" value="1" <?php checked(!empty($instance['scale'])); ?>> <?php esc_html_e('按数量缩放字号', 'aksrt'); ?></label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'taxonomy' => sanitize_key((string) ($new_instance['taxonomy'] ?? 'post_tag')),
'count' => max(1, min(100, (int) ($new_instance['count'] ?? 20))),
@@ -197,6 +187,6 @@ class TagCloudWidget extends \WP_Widget {
'fixed_width' => !empty($new_instance['fixed_width']) ? 1 : 0,
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
'scale' => !empty($new_instance['scale']) ? 1 : 0,
]);
];
}
}
-73
View File
@@ -1,73 +0,0 @@
<?php
/**
* CommonWidgetFields trait — single seat-belt for shared widget form/update.
*
* Every AKSRT widget `use`s this trait so the Zibll common-field family
* (title block, display rules, affix, animation, layout bg) is declared and
* persisted in exactly one place: inc/modules/widget-common.php.
*
* Widgets keep only their business fields; the trait wires the shared section
* into form() and folds the shared keys into update().
*
* @package AKSRT\Widgets\Traits
*/
namespace AKSRT\Widgets\Traits;
defined('ABSPATH') || exit;
trait CommonWidgetFields {
/**
* Whether this widget should render the shared common-form section.
*
* Override and return false for pure-display widgets with no admin form.
* Default: true (every widget gets the shared block).
*
* @return bool
*/
protected function use_common_form(): bool {
return true;
}
/**
* Render the shared common-form section at the end of a widget's form().
*
* Call from form(): render business fields, then `$this->render_common_form_section($instance);`.
*
* @param array<string,mixed> $instance Widget instance.
*/
protected function render_common_form_section(array $instance): void {
if (!$this->use_common_form()) {
return;
}
if (function_exists('aksrt_widget_render_common_form')) {
aksrt_widget_render_common_form($instance);
}
}
/**
* Merge shared common fields into a widget's update() result.
*
* Usage in update():
* return $this->update_common_fields($new_instance, $old_instance, [
* 'title' => sanitize_text_field(...),
* ...business fields...
* ]);
*
* @param array<string,mixed> $new_instance New submitted instance.
* @param array<string,mixed> $old_instance Previous instance.
* @param array<string,mixed> $business Sanitized business fields to keep.
* @return array<string,mixed>
*/
protected function update_common_fields(array $new_instance, array $old_instance, array $business = []): array {
if (!$this->use_common_form()) {
return $business;
}
if (function_exists('aksrt_widget_update_common')) {
$common = aksrt_widget_update_common($new_instance, $old_instance);
return array_merge($business, $common);
}
return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetAnnouncement extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -152,7 +150,6 @@ class WidgetAnnouncement extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('blank')); ?>" value="1" <?php checked(!empty($instance['blank'])); ?>> <?php esc_html_e('链接新窗口', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -173,6 +170,6 @@ class WidgetAnnouncement extends \WP_Widget {
'mode' => $mode,
'interval' => max(2, min(15, (int) ($new_instance['interval'] ?? 4))),
'blank' => !empty($new_instance['blank']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+2 -24
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetComboInfo extends \WP_Widget {
use CommonWidgetFields;
private const BANDS = ['blue', 'cyan', 'green', 'yellow', 'pink', 'purple'];
@@ -57,27 +55,20 @@ class WidgetComboInfo extends \WP_Widget {
$style = sprintf('--aksrt-zci-radius:%dpx;--aksrt-zci-band-h:%dpx;--aksrt-zci-pc:%d;--aksrt-zci-m:%d;--zci-radius:%dpx;--zci-band-h:%dpx;', $radius, $band_h, $pc_row, $m_row, $radius, $band_h);
$wrap = 'aksrt-combo-info zib-combo-info-card row gutters-10' . ($top_band ? ' has-band zci-has-band' : '');
$anim = sanitize_key((string) ($instance['obs_animation'] ?? ''));
$anim_repeat = !empty($instance['animation_repeat']);
$pc_span = max(1, (int) floor(12 / $pc_row));
$m_span = max(1, (int) floor(12 / $m_row));
$col = 'col-sm-' . $pc_span . ' col-xs-' . $m_span;
echo '<div class="' . esc_attr($wrap) . '" style="' . esc_attr($style) . '">';
$auto_idx = 0;
$ci = 0;
foreach ($cards as $card) {
$html = $this->render_card($card, $cta_default, $auto_idx, $go_link, $top_band);
if ($html === '') {
continue;
}
$pack = function_exists('aksrt_obs_animate_attrs')
? aksrt_obs_animate_attrs($anim, $anim_repeat, $ci * 0.1)
: ['class' => '', 'attr' => ''];
echo '<div class="' . esc_attr(trim($col . $pack['class'])) . '"' . $pack['attr'] . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="' . esc_attr($col) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
$ci++;
}
echo '</div>';
@@ -243,18 +234,7 @@ class WidgetComboInfo extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('top_band')); ?>" value="1" <?php checked(!empty($instance['top_band']) || !isset($instance['top_band'])); ?>> <?php esc_html_e('顶部色带', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('go_link')); ?>" value="1" <?php checked(!empty($instance['go_link'])); ?>> <?php esc_html_e('外链跳转页', 'aksrt'); ?></label>
</p>
<p>
<label><?php esc_html_e('入场动画', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('obs_animation')); ?>">
<option value="" <?php selected(($instance['obs_animation'] ?? ''), ''); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
<option value="slideup" <?php selected(($instance['obs_animation'] ?? ''), 'slideup'); ?>><?php esc_html_e('上滑', 'aksrt'); ?></option>
<option value="fade" <?php selected(($instance['obs_animation'] ?? ''), 'fade'); ?>><?php esc_html_e('淡入', 'aksrt'); ?></option>
<option value="zoomin" <?php selected(($instance['obs_animation'] ?? ''), 'zoomin'); ?>><?php esc_html_e('放大', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -268,8 +248,6 @@ class WidgetComboInfo extends \WP_Widget {
'top_band_height' => max(20, min(160, (int) ($new_instance['top_band_height'] ?? 70))),
'cta_text' => sanitize_text_field((string) ($new_instance['cta_text'] ?? '')),
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? '')),
'animation_repeat'=> !empty($new_instance['animation_repeat']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetCoverCards extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -204,7 +202,6 @@ class WidgetCoverCards extends \WP_Widget {
<small><?php esc_html_e('字段: image, video, title, link, target, hide(pc|m), height, mask', 'aksrt'); ?></small>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -225,6 +222,6 @@ class WidgetCoverCards extends \WP_Widget {
'font_bold' => !empty($new_instance['font_bold']) ? 1 : 0,
'font_color' => sanitize_hex_color((string) ($new_instance['font_color'] ?? '')) ?: '',
'items' => $this->normalize($new_instance['items'] ?? []),
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetIconCoverCards extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -187,7 +185,6 @@ class WidgetIconCoverCards extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('go_link')); ?>" value="1" <?php checked(!empty($instance['go_link'])); ?>> <?php esc_html_e('外链跳转页', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -205,6 +202,6 @@ class WidgetIconCoverCards extends \WP_Widget {
'icon_radius4' => !empty($new_instance['icon_radius4']) ? 1 : 0,
'show_widget_bg' => !empty($new_instance['show_widget_bg']) ? 1 : 0,
'go_link' => !empty($new_instance['go_link']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetIframe extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -141,7 +139,6 @@ class WidgetIframe extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('allowfullscreen')); ?>" value="1" <?php checked(!empty($instance['allowfullscreen'])); ?>> <?php esc_html_e('允许全屏', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -155,6 +152,6 @@ class WidgetIframe extends \WP_Widget {
'url' => trim((string) ($new_instance['url'] ?? '')),
'aspect' => max(20, min(300, (int) ($new_instance['aspect'] ?? 55))),
'allowfullscreen' => !empty($new_instance['allowfullscreen']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+3 -8
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetMainPosts extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
@@ -331,13 +329,11 @@ class WidgetMainPosts extends \WP_Widget {
], static function ($v) {
return $v !== '' && $v !== null;
}));
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
$wrap_cls = 'aksrt-main-posts style-' . $style . ' ajaxpager';
if ($style === 'mini') {
$wrap_cls .= ' zib-widget posts-mini-lists';
}
$wrap_cls .= $affix['class'];
echo '<div class="' . esc_attr($wrap_cls) . '" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '" data-load-mode="' . esc_attr($load_mode) . '" data-query="' . esc_attr($qs) . '"' . ($load_mode === 'ajax' ? ' data-loaded="0"' : ' data-loaded="1"') . $affix['attr'] . '>';
echo '<div class="' . esc_attr($wrap_cls) . '" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '" data-load-mode="' . esc_attr($load_mode) . '" data-query="' . esc_attr($qs) . '"' . ($load_mode === 'ajax' ? ' data-loaded="0"' : ' data-loaded="1"') . '>';
if ($load_mode === 'ajax') {
// Skeleton first paint (zibll load_mode=ajax).
@@ -450,7 +446,6 @@ class WidgetMainPosts extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_meta')); ?>" value="1" <?php checked(!isset($instance['show_meta']) || !empty($instance['show_meta'])); ?>> <?php esc_html_e('显示 meta', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_number')); ?>" value="1" <?php checked(!empty($instance['show_number'])); ?>> <?php esc_html_e('显示序号', 'aksrt'); ?></label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -467,7 +462,7 @@ class WidgetMainPosts extends \WP_Widget {
if (!in_array($paginate, ['none', 'ajax', 'number'], true)) {
$paginate = 'none';
}
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(30, (int) ($new_instance['count'] ?? 6))),
'style' => $style,
@@ -482,6 +477,6 @@ class WidgetMainPosts extends \WP_Widget {
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
'show_meta' => !empty($new_instance['show_meta']) ? 1 : 0,
'show_number' => !empty($new_instance['show_number']) ? 1 : 0,
]);
];
}
}
+1 -4
View File
@@ -9,12 +9,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetOnelinePosts extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -275,7 +273,6 @@ class WidgetOnelinePosts extends \WP_Widget {
</label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -302,6 +299,6 @@ class WidgetOnelinePosts extends \WP_Widget {
'limit_day' => max(0, (int) ($new_instance['limit_day'] ?? 0)),
'topics' => sanitize_text_field((string) ($new_instance['topics'] ?? '')),
'pay_type' => sanitize_key((string) ($new_instance['pay_type'] ?? '')),
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetPostPayBox extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
@@ -173,7 +171,6 @@ class WidgetPostPayBox extends \WP_Widget {
</select>
</label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -184,6 +181,6 @@ class WidgetPostPayBox extends \WP_Widget {
$theme = 'jb-red';
}
return $this->update_common_fields($new_instance, $old_instance, ['theme' => $theme]);
return ['theme' => $theme];
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetSlider extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -230,7 +228,6 @@ class WidgetSlider extends \WP_Widget {
<p class="description"><?php esc_html_e('使用主题共享 Swiper(.new-swiper)。图片 URL 必填;仅 1 张时不滑动。', 'aksrt'); ?></p>
<p class="description"><?php esc_html_e('对齐 Zibll 幻灯片:方向/效果/比例自动高度/间距/速度/自动播放/间隔。视频背景与图层视差由共享 Swiper 栈支持。', 'aksrt'); ?></p>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -260,7 +257,7 @@ class WidgetSlider extends \WP_Widget {
'autoplay' => !empty($new_instance['autoplay']) ? 1 : 0,
'blank' => !empty($new_instance['blank']) ? 1 : 0,
'slides' => $slides,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
/**
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetTermAggregation extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -329,7 +327,6 @@ class WidgetTermAggregation extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('target_blank')); ?>" value="1" <?php checked(!empty($instance['target_blank'])); ?>> <?php esc_html_e('新窗口打开', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -352,6 +349,6 @@ class WidgetTermAggregation extends \WP_Widget {
'order' => strtoupper(sanitize_key((string) ($new_instance['order'] ?? 'desc'))) === 'ASC' ? 'ASC' : 'DESC',
'auto_terms' => max(1, min(4, (int) ($new_instance['auto_terms'] ?? 2))),
'target_blank' => !empty($new_instance['target_blank']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetTermCards extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -275,7 +273,6 @@ class WidgetTermCards extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('target_blank')); ?>" value="1" <?php checked(!empty($instance['target_blank'])); ?>> <?php esc_html_e('新窗口打开', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
/**
@@ -302,6 +299,6 @@ class WidgetTermCards extends \WP_Widget {
'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')),
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
'target_blank' => !empty($new_instance['target_blank']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+7 -54
View File
@@ -7,19 +7,17 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetTextTitle extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
parent::__construct(
'aksrt_text_title',
esc_html__('AKSRT 文字标题', 'aksrt'),
[
'description' => esc_html__('自定义标题 + 副标题区块,支持逐字入场动画。', 'aksrt'),
'description' => esc_html__('自定义标题 + 副标题区块。', 'aksrt'),
'classname' => 'widget_aksrt_text_title',
]
);
@@ -53,8 +51,6 @@ class WidgetTextTitle extends \WP_Widget {
$desc_size = max(11, min(24, (int) ($instance['desc_size'] ?? 14)));
$title_color = sanitize_hex_color((string) ($instance['title_color'] ?? '')) ?: '';
$desc_color = sanitize_hex_color((string) ($instance['desc_color'] ?? '')) ?: '';
$anim = sanitize_key((string) ($instance['obs_animation'] ?? ''));
$anim_repeat = !empty($instance['animation_repeat']);
$icon_width = max(0, min(64, (int) ($instance['title_icon_width'] ?? 0)));
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
@@ -75,45 +71,15 @@ class WidgetTextTitle extends \WP_Widget {
? '<span class="text-title-icon aksrt-text-title__icon ml6"' . $icon_w_attr . '><i class="' . esc_attr($icon_right) . '" aria-hidden="true"></i></span>'
: '';
$title_html = '';
if ($title !== '' && $anim !== '' && function_exists('aksrt_obs_animate_attrs')) {
// Split title into per-char spans with staggered delay (zibll).
$chars = preg_split('//u', $title, -1, PREG_SPLIT_NO_EMPTY) ?: [];
$pieces = [];
if ($left_html !== '') {
$pieces[] = $left_html;
}
foreach ($chars as $ch) {
$pieces[] = $ch;
}
if ($right_html !== '') {
$pieces[] = $right_html;
}
foreach ($pieces as $i => $piece) {
$pack = aksrt_obs_animate_attrs($anim, $anim_repeat, $i * 0.05);
$is_html = str_starts_with((string) $piece, '<');
$inner = $is_html ? $piece : esc_html((string) $piece);
$title_html .= '<span class="' . esc_attr(trim($pack['class'])) . '"' . $pack['attr']
. ' style="--delay:' . esc_attr((string) ($i * 0.05)) . 's;">' . $inner . '</span>';
}
} elseif ($title !== '') {
$title_html = $left_html . esc_html($title) . $right_html;
}
$title_html = $title !== '' ? $left_html . esc_html($title) . $right_html : '';
$desc_html = '';
if ($desc !== '') {
if ($anim !== '' && function_exists('aksrt_obs_animate_attrs')) {
$pack = aksrt_obs_animate_attrs($anim, $anim_repeat, 0.6);
$desc_html = '<div class="aksrt-text-title__desc' . esc_attr($pack['class']) . '"' . $pack['attr']
. ' style="--delay:.6s;">' . esc_html($desc) . '</div>';
} else {
$desc_html = '<div class="aksrt-text-title__desc">' . esc_html($desc) . '</div>';
}
$desc_html = '<div class="aksrt-text-title__desc">' . esc_html($desc) . '</div>';
}
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
$box = 'aksrt-text-title text-title align-' . esc_attr($align) . $affix['class'];
echo '<div class="' . esc_attr($box) . '" style="' . esc_attr($style) . '"' . $affix['attr'] . '>';
$box = 'aksrt-text-title text-title align-' . esc_attr($align);
echo '<div class="' . esc_attr($box) . '" style="' . esc_attr($style) . '">';
if ($title_html !== '') {
echo '<div class="aksrt-text-title__main">' . $title_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -165,16 +131,6 @@ class WidgetTextTitle extends \WP_Widget {
<input class="tiny-text" type="number" min="11" max="24" name="<?php echo esc_attr($this->get_field_name('desc_size')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['desc_size'] ?? 14))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('入场动画', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('obs_animation')); ?>">
<option value="" <?php selected(($instance['obs_animation'] ?? ''), ''); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
<option value="fade" <?php selected(($instance['obs_animation'] ?? ''), 'fade'); ?>><?php esc_html_e('淡入', 'aksrt'); ?></option>
<option value="slideup" <?php selected(($instance['obs_animation'] ?? ''), 'slideup'); ?>><?php esc_html_e('上滑', 'aksrt'); ?></option>
<option value="zoomin" <?php selected(($instance['obs_animation'] ?? ''), 'zoomin'); ?>><?php esc_html_e('放大', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('标题色 #hex', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title_color')); ?>" value="<?php echo esc_attr((string) ($instance['title_color'] ?? '')); ?>">
@@ -185,7 +141,6 @@ class WidgetTextTitle extends \WP_Widget {
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('desc_color')); ?>" value="<?php echo esc_attr((string) ($instance['desc_color'] ?? '')); ?>">
</label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -195,7 +150,7 @@ class WidgetTextTitle extends \WP_Widget {
* @return array<string,mixed>
*/
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'desc' => sanitize_text_field((string) ($new_instance['desc'] ?? '')),
'align' => in_array(($new_instance['align'] ?? ''), ['left', 'center', 'right'], true) ? $new_instance['align'] : 'center',
@@ -205,9 +160,7 @@ class WidgetTextTitle extends \WP_Widget {
'desc_size' => max(11, min(24, (int) ($new_instance['desc_size'] ?? 14))),
'title_color' => sanitize_hex_color((string) ($new_instance['title_color'] ?? '')) ?: '',
'desc_color' => sanitize_hex_color((string) ($new_instance['desc_color'] ?? '')) ?: '',
'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? '')),
'animation_repeat' => !empty($new_instance['animation_repeat']) ? 1 : 0,
'title_icon_width' => max(0, min(64, (int) ($new_instance['title_icon_width'] ?? 0))),
]);
];
}
}
-14
View File
@@ -28,7 +28,6 @@ class WidgetUserCard extends \WP_Widget {
$show_stats = !empty($instance['show_stats']) || !isset($instance['show_stats']);
$show_actions = !empty($instance['show_button']) || !isset($instance['show_button']);
$show_checkin = !empty($instance['show_checkin']) || !isset($instance['show_checkin']);
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
$loged_title = (string) ($instance['loged_title'] ?? __('Hi!请登录', 'aksrt'));
$btn1_text = (string) ($instance['button_1_text'] ?? __('发布文章', 'aksrt'));
$btn2_text = (string) ($instance['button_2_text'] ?? __('用户中心', 'aksrt'));
@@ -49,24 +48,14 @@ class WidgetUserCard extends \WP_Widget {
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($affix['class'] !== '') {
echo '<div class="is-affix"' . $affix['attr'] . '>';
}
if ($user_id <= 0) {
$this->render_guest($show_cover, $loged_title, $this->get_guest_cover_url($instance));
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
$user = get_userdata($user_id);
if (!$user) {
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
@@ -148,9 +137,6 @@ class WidgetUserCard extends \WP_Widget {
echo '</div></div>';
if ($affix['class'] !== '') {
echo '</div>';
}
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
+1 -4
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetVideoPlayer extends \WP_Widget {
use CommonWidgetFields;
public function __construct() {
@@ -134,7 +132,6 @@ class WidgetVideoPlayer extends \WP_Widget {
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_controller')); ?>" value="1" <?php checked(!empty($instance['hide_controller'])); ?>> <?php esc_html_e('隐藏控制条样式', 'aksrt'); ?></label>
</p>
<?php
$this->render_common_form_section($instance);
}
public function update($new_instance, $old_instance): array {
@@ -148,6 +145,6 @@ class WidgetVideoPlayer extends \WP_Widget {
'loop' => !empty($new_instance['loop']) ? 1 : 0,
'muted' => !empty($new_instance['muted']) ? 1 : 0,
'hide_controller' => !empty($new_instance['hide_controller']) ? 1 : 0,
]; return $this->update_common_fields($new_instance, $old_instance, $business);
]; return $business;
}
}
+3 -29
View File
@@ -7,12 +7,10 @@
namespace AKSRT\Widgets;
use AKSRT\Widgets\Traits\CommonWidgetFields;
defined('ABSPATH') || exit;
class WidgetYiyan extends \WP_Widget {
use CommonWidgetFields;
/**
* @return array<string,string>
@@ -61,9 +59,6 @@ class WidgetYiyan extends \WP_Widget {
$interval = !empty($instance['interval']) ? max(3, min(30, (int) $instance['interval'])) : 5;
$type = self::sanitize_type((string) ($instance['type'] ?? ''));
$icon = !empty($instance['icon']) ? (string) $instance['icon'] : 'fas fa-quote-left';
$affix = function_exists('aksrt_widget_affix_attrs') ? aksrt_widget_affix_attrs($instance) : ['class' => '', 'attr' => ''];
$anim = sanitize_key((string) ($instance['obs_animation'] ?? ''));
$anim_repeat = !empty($instance['animation_repeat']);
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
@@ -72,15 +67,7 @@ class WidgetYiyan extends \WP_Widget {
}
// zibll shell: theme-box + yiyan-box main-bg text-center box-body radius8 main-shadow.
// NOTE: obs_animation is this widget's own per-widget animation business
// field (kept as-is); the shared common `animation_in` field added via
// render_common_form_section() is a separate additional control.
$pack = function_exists('aksrt_obs_animate_attrs')
? aksrt_obs_animate_attrs($anim, $anim_repeat, 0)
: ['class' => '', 'attr' => ''];
$box_class = 'theme-box yiyan-box-wrap' . $pack['class'];
echo '<div' . $affix['attr'] . ' class="' . esc_attr(trim($box_class)) . '"' . $pack['attr'] . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="theme-box yiyan-box-wrap">';
echo '<div class="yiyan-box main-bg text-center box-body radius8 main-shadow">';
if (function_exists('aksrt_render_yiyan_block')) {
@@ -160,17 +147,6 @@ class WidgetYiyan extends \WP_Widget {
name="<?php echo esc_attr($this->get_field_name('icon')); ?>" type="text"
value="<?php echo esc_attr($icon); ?>">
</p>
<p>
<label><?php esc_html_e('入场动画', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('obs_animation')); ?>">
<option value="" <?php selected(($instance['obs_animation'] ?? ''), ''); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
<option value="fade" <?php selected(($instance['obs_animation'] ?? ''), 'fade'); ?>><?php esc_html_e('淡入', 'aksrt'); ?></option>
<option value="slideup" <?php selected(($instance['obs_animation'] ?? ''), 'slideup'); ?>><?php esc_html_e('上滑', 'aksrt'); ?></option>
<option value="zoomin" <?php selected(($instance['obs_animation'] ?? ''), 'zoomin'); ?>><?php esc_html_e('放大', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php $this->render_common_form_section($instance); ?>
<?php
}
@@ -180,14 +156,12 @@ class WidgetYiyan extends \WP_Widget {
* @return array<string,mixed>
*/
public function update($new_instance, $old_instance): array {
return $this->update_common_fields($new_instance, $old_instance, [
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(10, absint($new_instance['count'] ?? 5))),
'interval' => max(3, min(30, absint($new_instance['interval'] ?? 5))),
'type' => self::sanitize_type((string) ($new_instance['type'] ?? '')),
'icon' => sanitize_text_field((string) ($new_instance['icon'] ?? 'fas fa-quote-left')),
'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? '')),
'animation_repeat' => !empty($new_instance['animation_repeat']) ? 1 : 0,
]);
];
}
}