Files
wordpress-theme-aksrt/inc/classes/widgets/class-aksrt-yiyan-widget.php
T

126 lines
5.8 KiB
PHP

<?php
/**
* AKSRT Yiyan Widget — 一言轮播组件
*
* Fetches random quotes from hitokoto.cn API and displays with auto-rotation.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Widget_Yiyan extends WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_yiyan',
__('一言轮播', 'aksrt'),
['description' => __('显示随机一言/每日语录,支持自动轮播。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : '';
$count = !empty($instance['count']) ? max(3, min(20, (int) $instance['count'])) : 5;
$interval = !empty($instance['interval']) ? max(3, min(30, (int) $instance['interval'])) : 5;
$type = !empty($instance['type']) ? sanitize_text_field($instance['type']) : 'a';
$icon = !empty($instance['icon']) ? $instance['icon'] : 'fas fa-quote-left';
echo $args['before_widget'];
if ($title) {
echo $args['before_title'] . esc_html($title) . $args['after_title'];
}
$types_map = [
'a' => '动画',
'b' => '漫画',
'c' => '游戏',
'd' => '文学',
'e' => '原创',
'f' => '来自网络',
'g' => '其他',
'h' => '影视',
'i' => '诗词',
'j' => '网易云',
'k' => '哲学',
'l' => '抖机灵',
];
$type_label = $types_map[$type] ?? '综合';
?>
<div class="aksrt-yiyan"
data-aksrt-yiyan
data-count="<?php echo esc_attr((string) $count); ?>"
data-interval="<?php echo esc_attr((string) $interval); ?>"
data-type="<?php echo esc_attr($type); ?>">
<div class="aksrt-yiyan__content">
<i class="<?php echo esc_attr($icon); ?> aksrt-yiyan__icon" aria-hidden="true"></i>
<p class="aksrt-yiyan__text">加载中…</p>
<p class="aksrt-yiyan__from"></p>
</div>
<div class="aksrt-yiyan__dots" aria-hidden="true"></div>
</div>
<?php
echo $args['after_widget'];
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : '一言';
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
$interval = !empty($instance['interval']) ? (int) $instance['interval'] : 5;
$type = !empty($instance['type']) ? $instance['type'] : 'a';
$icon = !empty($instance['icon']) ? $instance['icon'] : 'fas fa-quote-left';
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text"
value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('轮播条数', 'aksrt'); ?></label>
<input id="<?php echo esc_attr($this->get_field_id('count')); ?>"
name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number"
min="3" max="20" value="<?php echo esc_attr((string) $count); ?>" style="width:80px;">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('interval')); ?>"><?php esc_html_e('切换间隔(秒)', 'aksrt'); ?></label>
<input id="<?php echo esc_attr($this->get_field_id('interval')); ?>"
name="<?php echo esc_attr($this->get_field_name('interval')); ?>" type="number"
min="3" max="30" value="<?php echo esc_attr((string) $interval); ?>" style="width:80px;">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('type')); ?>"><?php esc_html_e('分类', 'aksrt'); ?></label>
<select id="<?php echo esc_attr($this->get_field_id('type')); ?>"
name="<?php echo esc_attr($this->get_field_name('type')); ?>">
<option value="a" <?php selected($type, 'a'); ?>><?php esc_html_e('综合', 'aksrt'); ?></option>
<option value="d" <?php selected($type, 'd'); ?>><?php esc_html_e('文学', 'aksrt'); ?></option>
<option value="i" <?php selected($type, 'i'); ?>><?php esc_html_e('诗词', 'aksrt'); ?></option>
<option value="k" <?php selected($type, 'k'); ?>><?php esc_html_e('哲学', 'aksrt'); ?></option>
<option value="h" <?php selected($type, 'h'); ?>><?php esc_html_e('影视', 'aksrt'); ?></option>
<option value="j" <?php selected($type, 'j'); ?>><?php esc_html_e('网易云', 'aksrt'); ?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('icon')); ?>"><?php esc_html_e('图标', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('icon')); ?>"
name="<?php echo esc_attr($this->get_field_name('icon')); ?>" type="text"
value="<?php echo esc_attr($icon); ?>">
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
$instance['count'] = absint($new_instance['count'] ?? 5);
$instance['interval'] = absint($new_instance['interval'] ?? 5);
$instance['type'] = sanitize_text_field($new_instance['type'] ?? 'a');
$instance['icon'] = sanitize_text_field($new_instance['icon'] ?? 'fas fa-quote-left');
return $instance;
}
}