Files
wordpress-theme-aksrt/inc/classes/widgets/class-aksrt-widgets-batch1.php
T
kate522 ce1c13b947 refactor(classes): organize inc/classes into domain folders
Move ~94 root class shells and root *-traits into domain directories
(core, auth, user, credits, content, payment/gateways, forum, shop,
sms, ai, messages, extensions, widgets, settings, admin). Update
bootstrap require paths, co-locate traits with owners, fix forum/user
relative requires, drop duplicate shop controller requires, and wire
home-modules load. Path-only; class names and init order unchanged.
2026-07-17 11:11:57 +08:00

565 lines
31 KiB
PHP

<?php
/**
* AKSRT 第一批通用小工具 — v4.0 Zibll-aligned
*
* New widgets:
* 1. AKSRT_Search_Widget — 搜索框
* 2. AKSRT_Notice_Widget — 公告通知
* 3. AKSRT_Recent_Comments_Widget — 最新评论
* 4. AKSRT_Friend_Links_Widget — 友情链接
* 5. AKSRT_Icon_Cards_Widget — 图标卡片
* 6. AKSRT_Buttons_Widget — 按钮组
* 7. AKSRT_Custom_HTML_Gap_Widget — 自定义 HTML / 间距
*
* @package AKSRT
* @version 4.0.0
*/
defined('ABSPATH') || exit;
// ============================================================
// 1. 搜索框小工具
// ============================================================
class AKSRT_Search_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_search', esc_html__('AKSRT 搜索', 'aksrt'), [
'description' => esc_html__('带样式的搜索框', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
$placeholder = sanitize_text_field($instance['placeholder'] ?? __('搜索文章...', 'aksrt'));
?>
<div class="widget-search-box">
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<div class="widget-search-box__inner">
<input type="search" name="s" class="widget-search-box__input" placeholder="<?php echo esc_attr($placeholder); ?>" value="<?php echo esc_attr(get_search_query()); ?>" autocomplete="off">
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
<i class="fas fa-search"></i>
</button>
</div>
</form>
</div>
<?php
echo $args['after_widget'];
}
public function form($instance): void {
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('placeholder'); ?>"><?php esc_html_e('占位文本:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('placeholder'); ?>" name="<?php echo $this->get_field_name('placeholder'); ?>" value="<?php echo esc_attr($instance['placeholder'] ?? __('搜索文章...', 'aksrt')); ?>">
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'placeholder' => sanitize_text_field($new_instance['placeholder'] ?? ''),
];
}
}
// ============================================================
// 2. 公告通知小工具
// ============================================================
class AKSRT_Notice_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_notice', esc_html__('AKSRT 公告', 'aksrt'), [
'description' => esc_html__('显示公告/通知信息', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
$content = wp_kses_post($instance['content'] ?? '');
$style = sanitize_key($instance['style'] ?? 'info');
$icon = sanitize_text_field($instance['icon'] ?? 'fas fa-bullhorn');
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
?>
<div class="widget-notice widget-notice--<?php echo esc_attr($style); ?>">
<?php if ($icon) : ?>
<div class="widget-notice__icon"><i class="<?php echo esc_attr($icon); ?>" aria-hidden="true"></i></div>
<?php endif; ?>
<div class="widget-notice__content"><?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
</div>
<?php
echo $args['after_widget'];
}
public function form($instance): void {
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('content'); ?>"><?php esc_html_e('公告内容:', 'aksrt'); ?></label>
<textarea class="widefat" rows="4" id="<?php echo $this->get_field_id('content'); ?>" name="<?php echo $this->get_field_name('content'); ?>"><?php echo esc_textarea($instance['content'] ?? ''); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('style'); ?>"><?php esc_html_e('样式:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('style'); ?>" name="<?php echo $this->get_field_name('style'); ?>">
<option value="info" <?php selected($instance['style'] ?? '', 'info'); ?>><?php esc_html_e('信息(蓝色)', 'aksrt'); ?></option>
<option value="success" <?php selected($instance['style'] ?? '', 'success'); ?>><?php esc_html_e('成功(绿色)', 'aksrt'); ?></option>
<option value="warning" <?php selected($instance['style'] ?? '', 'warning'); ?>><?php esc_html_e('警告(黄色)', 'aksrt'); ?></option>
<option value="danger" <?php selected($instance['style'] ?? '', 'danger'); ?>><?php esc_html_e('危险(红色)', 'aksrt'); ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('icon'); ?>"><?php esc_html_e('图标类名:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('icon'); ?>" name="<?php echo $this->get_field_name('icon'); ?>" value="<?php echo esc_attr($instance['icon'] ?? 'fas fa-bullhorn'); ?>">
<small style="color:#666;"><?php esc_html_e('Font Awesome 类名,如: fas fa-bullhorn', 'aksrt'); ?></small>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'content' => wp_kses_post($new_instance['content'] ?? ''),
'style' => in_array($new_instance['style'] ?? '', ['info', 'success', 'warning', 'danger'], true) ? $new_instance['style'] : 'info',
'icon' => sanitize_text_field($new_instance['icon'] ?? 'fas fa-bullhorn'),
];
}
}
// ============================================================
// 3. 最新评论小工具
// ============================================================
class AKSRT_Recent_Comments_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_recent_comments', esc_html__('AKSRT 最新评论', 'aksrt'), [
'description' => esc_html__('显示最新评论列表', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? __('最新评论', 'aksrt'), $instance, $this->id_base);
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
$show_avatar = !empty($instance['show_avatar']);
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
$comments = get_comments([
'number' => $count,
'status' => 'approve',
'post_status' => 'publish',
]);
if ($comments) : ?>
<div class="widget-recent-comments">
<?php foreach ($comments as $comment) :
$post = get_post($comment->comment_post_ID);
if (!$post) continue;
?>
<div class="widget-recent-comments__item">
<?php if ($show_avatar) : ?>
<div class="widget-recent-comments__avatar">
<?php echo get_avatar($comment, 32); ?>
</div>
<?php endif; ?>
<div class="widget-recent-comments__body">
<div class="widget-recent-comments__meta">
<strong class="widget-recent-comments__author"><?php echo esc_html($comment->comment_author); ?></strong>
<span class="widget-recent-comments__time"><?php echo esc_html(human_time_diff(strtotime($comment->comment_date), current_time('timestamp'))); ?></span>
</div>
<p class="widget-recent-comments__text">
<a href="<?php echo esc_url(get_comment_link($comment)); ?>"><?php echo esc_html(wp_trim_words($comment->comment_content, 15, '...')); ?></a>
</p>
<span class="widget-recent-comments__post">
<?php esc_html_e('于', 'aksrt'); ?> <a href="<?php echo esc_url(get_permalink($post->ID)); ?>"><?php echo esc_html(get_the_title($post->ID)); ?></a>
</span>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<?php echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无评论', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无评论', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif;
echo $args['after_widget'];
}
public function form($instance): void {
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? __('最新评论', 'aksrt')); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>"><?php esc_html_e('显示数量:', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" value="<?php echo (int) ($instance['count'] ?? 5); ?>">
</p>
<p>
<label><input type="checkbox" name="<?php echo $this->get_field_name('show_avatar'); ?>" value="1" <?php checked(!empty($instance['show_avatar'])); ?>> <?php esc_html_e('显示头像', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
'show_avatar' => !empty($new_instance['show_avatar']),
];
}
}
// ============================================================
// 4. 友情链接小工具
// ============================================================
class AKSRT_Friend_Links_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_friend_links', esc_html__('AKSRT 友情链接', 'aksrt'), [
'description' => esc_html__('显示友情链接列表', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? __('友情链接', 'aksrt'), $instance, $this->id_base);
$show_icon = !empty($instance['show_icon']);
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
$links = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_friend_links() : [];
if (!empty($links)) : ?>
<div class="widget-friend-links" style="--fl-columns:<?php echo (int) $columns; ?>;">
<?php foreach ($links as $link) :
$name = sanitize_text_field($link['name'] ?? '');
$url = esc_url($link['url'] ?? '');
$desc = sanitize_text_field($link['description'] ?? '');
$icon = esc_url($link['icon'] ?? '');
if ($name === '' || $url === '') continue;
?>
<a href="<?php echo $url; ?>" class="widget-friend-links__item" target="_blank" rel="noopener noreferrer" title="<?php echo esc_attr($desc); ?>">
<?php if ($show_icon && $icon) : ?>
<img src="<?php echo $icon; ?>" alt="" class="widget-friend-links__icon" width="16" height="16"<?php echo function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('widget') : ' loading="lazy"'; ?>>
<?php endif; ?>
<span class="widget-friend-links__name"><?php echo esc_html($name); ?></span>
</a>
<?php endforeach; ?>
</div>
<?php else : ?>
<?php echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无友情链接', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无友情链接', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif;
echo $args['after_widget'];
}
public function form($instance): void {
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? __('友情链接', 'aksrt')); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('columns'); ?>"><?php esc_html_e('列数:', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="1" max="4" id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo $this->get_field_name('columns'); ?>" value="<?php echo (int) ($instance['columns'] ?? 2); ?>">
</p>
<p>
<label><input type="checkbox" name="<?php echo $this->get_field_name('show_icon'); ?>" value="1" <?php checked(!empty($instance['show_icon'])); ?>> <?php esc_html_e('显示站点图标', 'aksrt'); ?></label>
</p>
<p style="color:#666;font-size:12px;"><?php esc_html_e('链接数据在后台"友链管理"中配置。', 'aksrt'); ?></p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
'show_icon'=> !empty($new_instance['show_icon']),
];
}
}
// ============================================================
// 5. 图标卡片小工具
// ============================================================
class AKSRT_Icon_Cards_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_icon_cards', esc_html__('AKSRT 图标卡片', 'aksrt'), [
'description' => esc_html__('显示图标+文字卡片网格', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
if (!empty($cards)) : ?>
<div class="widget-icon-cards" style="--ic-columns:<?php echo (int) $columns; ?>;">
<?php foreach ($cards as $card) :
$card_title = sanitize_text_field($card['title'] ?? '');
$card_icon = sanitize_text_field($card['icon'] ?? '');
$card_link = esc_url($card['link'] ?? '');
$card_desc = sanitize_text_field($card['description'] ?? '');
if ($card_title === '') continue;
$tag = $card_link !== '' ? 'a' : 'div';
$extra = $card_link !== '' ? ' href="' . $card_link . '" target="_blank" rel="noopener"' : '';
?>
<<?php echo $tag; ?> class="widget-icon-cards__item"<?php echo $extra; ?>>
<?php if ($card_icon) : ?>
<div class="widget-icon-cards__icon"><i class="<?php echo esc_attr($card_icon); ?>" aria-hidden="true"></i></div>
<?php endif; ?>
<div class="widget-icon-cards__text">
<div class="widget-icon-cards__title"><?php echo esc_html($card_title); ?></div>
<?php if ($card_desc) : ?>
<div class="widget-icon-cards__desc"><?php echo esc_html($card_desc); ?></div>
<?php endif; ?>
</div>
</<?php echo $tag; ?>>
<?php endforeach; ?>
</div>
<?php endif;
echo $args['after_widget'];
}
public function form($instance): void {
$cards = isset($instance['cards']) && is_array($instance['cards']) ? $instance['cards'] : [];
while (count($cards) < 4) {
$cards[] = ['title' => '', 'icon' => '', 'link' => '', 'description' => ''];
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('columns'); ?>"><?php esc_html_e('列数:', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="1" max="4" id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo $this->get_field_name('columns'); ?>" value="<?php echo (int) ($instance['columns'] ?? 2); ?>">
</p>
<hr>
<?php foreach ($cards as $i => $card) : ?>
<div style="border:1px solid #ddd;padding:6px;margin-bottom:6px;border-radius:4px;">
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][title]"); ?>" value="<?php echo esc_attr($card['title']); ?>" placeholder="<?php esc_attr_e('标题', 'aksrt'); ?>"></p>
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][icon]"); ?>" value="<?php echo esc_attr($card['icon']); ?>" placeholder="<?php esc_attr_e('图标类名 (如 fas fa-star)', 'aksrt'); ?>"></p>
<p style="margin:0 0 4px;"><input class="widefat" type="text" name="<?php echo $this->get_field_name("cards[{$i}][description]"); ?>" value="<?php echo esc_attr($card['description']); ?>" placeholder="<?php esc_attr_e('描述(可选)', 'aksrt'); ?>"></p>
<p style="margin:0;"><input class="widefat" type="url" name="<?php echo $this->get_field_name("cards[{$i}][link]"); ?>" value="<?php echo esc_attr($card['link']); ?>" placeholder="<?php esc_attr_e('链接(可选)', 'aksrt'); ?>"></p>
</div>
<?php endforeach;
}
public function update($new_instance, $old_instance): array {
$cards = [];
if (isset($new_instance['cards']) && is_array($new_instance['cards'])) {
foreach ($new_instance['cards'] as $card) {
if (!is_array($card)) continue;
$cards[] = [
'title' => sanitize_text_field($card['title'] ?? ''),
'icon' => sanitize_text_field($card['icon'] ?? ''),
'link' => esc_url_raw($card['link'] ?? ''),
'description' => sanitize_text_field($card['description'] ?? ''),
];
}
}
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))),
'cards' => $cards,
];
}
}
// ============================================================
// 6. 按钮组小工具
// ============================================================
class AKSRT_Buttons_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_buttons', esc_html__('AKSRT 按钮组', 'aksrt'), [
'description' => esc_html__('显示一组自定义按钮', 'aksrt'),
]);
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
$buttons = isset($instance['buttons']) && is_array($instance['buttons']) ? $instance['buttons'] : [];
$layout = sanitize_key($instance['layout'] ?? 'vertical');
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
if (!empty($buttons)) : ?>
<div class="widget-buttons widget-buttons--<?php echo esc_attr($layout); ?>">
<?php foreach ($buttons as $btn) :
$btn_text = sanitize_text_field($btn['text'] ?? '');
$btn_url = esc_url($btn['url'] ?? '');
$btn_style = sanitize_key($btn['style'] ?? 'primary');
$btn_icon = sanitize_text_field($btn['icon'] ?? '');
$btn_target = !empty($btn['new_window']) ? '_blank' : '_self';
if ($btn_text === '' || $btn_url === '') continue;
?>
<a href="<?php echo $btn_url; ?>" class="widget-buttons__btn widget-buttons__btn--<?php echo esc_attr($btn_style); ?>" target="<?php echo esc_attr($btn_target); ?>" rel="noopener">
<?php if ($btn_icon) : ?><i class="<?php echo esc_attr($btn_icon); ?>" aria-hidden="true"></i> <?php endif; ?>
<?php echo esc_html($btn_text); ?>
</a>
<?php endforeach; ?>
</div>
<?php endif;
echo $args['after_widget'];
}
public function form($instance): void {
$buttons = isset($instance['buttons']) && is_array($instance['buttons']) ? $instance['buttons'] : [];
while (count($buttons) < 3) {
$buttons[] = ['text' => '', 'url' => '', 'style' => 'primary', 'icon' => '', 'new_window' => false];
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('layout'); ?>"><?php esc_html_e('布局:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('layout'); ?>" name="<?php echo $this->get_field_name('layout'); ?>">
<option value="vertical" <?php selected($instance['layout'] ?? '', 'vertical'); ?>><?php esc_html_e('垂直', 'aksrt'); ?></option>
<option value="horizontal" <?php selected($instance['layout'] ?? '', 'horizontal'); ?>><?php esc_html_e('水平', 'aksrt'); ?></option>
</select>
</p>
<hr>
<?php foreach ($buttons as $i => $btn) : ?>
<div style="border:1px solid #ddd;padding:6px;margin-bottom:6px;border-radius:4px;">
<p style="margin:0 0 4px;">
<input class="widefat" type="text" name="<?php echo $this->get_field_name("buttons[{$i}][text]"); ?>" value="<?php echo esc_attr($btn['text']); ?>" placeholder="<?php esc_attr_e('按钮文字', 'aksrt'); ?>">
</p>
<p style="margin:0 0 4px;">
<input class="widefat" type="url" name="<?php echo $this->get_field_name("buttons[{$i}][url]"); ?>" value="<?php echo esc_attr($btn['url']); ?>" placeholder="<?php esc_attr_e('链接地址', 'aksrt'); ?>">
</p>
<p style="margin:0 0 4px;">
<select class="widefat" name="<?php echo $this->get_field_name("buttons[{$i}][style]"); ?>">
<option value="primary" <?php selected($btn['style'] ?? '', 'primary'); ?>><?php esc_html_e('主要', 'aksrt'); ?></option>
<option value="secondary" <?php selected($btn['style'] ?? '', 'secondary'); ?>><?php esc_html_e('次要', 'aksrt'); ?></option>
<option value="outline" <?php selected($btn['style'] ?? '', 'outline'); ?>><?php esc_html_e('线框', 'aksrt'); ?></option>
<option value="text" <?php selected($btn['style'] ?? '', 'text'); ?>><?php esc_html_e('文字', 'aksrt'); ?></option>
</select>
</p>
<p style="margin:0 0 4px;">
<input class="widefat" type="text" name="<?php echo $this->get_field_name("buttons[{$i}][icon]"); ?>" value="<?php echo esc_attr($btn['icon'] ?? ''); ?>" placeholder="<?php esc_attr_e('图标(可选)', 'aksrt'); ?>">
</p>
<p style="margin:0;">
<label><input type="checkbox" name="<?php echo $this->get_field_name("buttons[{$i}][new_window]"); ?>" value="1" <?php checked(!empty($btn['new_window'])); ?>> <?php esc_html_e('新窗口打开', 'aksrt'); ?></label>
</p>
</div>
<?php endforeach;
}
public function update($new_instance, $old_instance): array {
$buttons = [];
if (isset($new_instance['buttons']) && is_array($new_instance['buttons'])) {
$allowed_styles = ['primary', 'secondary', 'outline', 'text'];
foreach ($new_instance['buttons'] as $btn) {
if (!is_array($btn)) continue;
$style = sanitize_key($btn['style'] ?? 'primary');
if (!in_array($style, $allowed_styles, true)) $style = 'primary';
$buttons[] = [
'text' => sanitize_text_field($btn['text'] ?? ''),
'url' => esc_url_raw($btn['url'] ?? ''),
'style' => $style,
'icon' => sanitize_text_field($btn['icon'] ?? ''),
'new_window' => !empty($btn['new_window']),
];
}
}
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'layout' => in_array($new_instance['layout'] ?? '', ['vertical', 'horizontal'], true) ? $new_instance['layout'] : 'vertical',
'buttons' => $buttons,
];
}
}
// ============================================================
// 7. 自定义 HTML / 间距小工具
// ============================================================
class AKSRT_Custom_HTML_Gap_Widget extends WP_Widget {
public function __construct() {
parent::__construct('aksrt_custom_html_gap', esc_html__('AKSRT 自定义HTML/间距', 'aksrt'), [
'description' => esc_html__('插入自定义 HTML 或间距', 'aksrt'),
]);
}
public function widget($args, $instance): void {
$mode = sanitize_key($instance['mode'] ?? 'html');
if ($mode === 'gap') {
$height = max(0, min(200, (int) ($instance['gap_height'] ?? 20)));
echo '<div class="widget-custom-gap" style="height:' . (int) $height . 'px;" aria-hidden="true"></div>';
return;
}
echo $args['before_widget'];
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
if ($title) echo $args['before_title'] . esc_html($title) . $args['after_title'];
echo '<div class="widget-custom-html">' . wp_kses_post($instance['content'] ?? '') . '</div>';
echo $args['after_widget'];
}
public function form($instance): void {
$mode = sanitize_key($instance['mode'] ?? 'html');
?>
<p>
<label for="<?php echo $this->get_field_id('mode'); ?>"><?php esc_html_e('模式:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('mode'); ?>" name="<?php echo $this->get_field_name('mode'); ?>">
<option value="html" <?php selected($mode, 'html'); ?>><?php esc_html_e('自定义 HTML', 'aksrt'); ?></option>
<option value="gap" <?php selected($mode, 'gap'); ?>><?php esc_html_e('间距', 'aksrt'); ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(仅 HTML 模式,可选):', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title'] ?? ''); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('content'); ?>"><?php esc_html_e('HTML 内容:', 'aksrt'); ?></label>
<textarea class="widefat" rows="6" id="<?php echo $this->get_field_id('content'); ?>" name="<?php echo $this->get_field_name('content'); ?>"><?php echo esc_textarea($instance['content'] ?? ''); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('gap_height'); ?>"><?php esc_html_e('间距高度(px,仅间距模式):', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="0" max="200" id="<?php echo $this->get_field_id('gap_height'); ?>" name="<?php echo $this->get_field_name('gap_height'); ?>" value="<?php echo (int) ($instance['gap_height'] ?? 20); ?>">
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'mode' => in_array($new_instance['mode'] ?? '', ['html', 'gap'], true) ? $new_instance['mode'] : 'html',
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'content' => wp_kses_post($new_instance['content'] ?? ''),
'gap_height' => max(0, min(200, (int) ($new_instance['gap_height'] ?? 20))),
];
}
}