110 lines
5.0 KiB
PHP
110 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Announcement Widget — 公告跑马灯
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Widget_Announcement extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_announcement',
|
|
__('公告跑马灯', 'aksrt'),
|
|
['description' => __('滚动显示公告/通知。', 'aksrt')]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
$title = !empty($instance['title']) ? $instance['title'] : '';
|
|
$items = !empty($instance['items']) ? $instance['items'] : '';
|
|
$icon = !empty($instance['icon']) ? $instance['icon'] : 'fas fa-bullhorn';
|
|
$speed = !empty($instance['speed']) ? max(5, min(30, (int) $instance['speed'])) : 10;
|
|
|
|
$lines = array_filter(array_map('trim', explode("\n", $items)));
|
|
if (empty($lines)) return;
|
|
|
|
echo $args['before_widget'];
|
|
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
|
}
|
|
|
|
echo '<div class="aksrt-announcement">';
|
|
echo '<span class="aksrt-announcement__icon"><i class="' . esc_attr($icon) . '"></i></span>';
|
|
echo '<div class="aksrt-announcement__track">';
|
|
echo '<ul class="aksrt-announcement__list" style="animation-duration: ' . esc_attr((string) $speed) . 's;">';
|
|
|
|
foreach ($lines as $line) {
|
|
// 支持 "文本|链接" 格式
|
|
$parts = explode('|', $line, 2);
|
|
$text = trim($parts[0]);
|
|
$link = isset($parts[1]) ? trim($parts[1]) : '';
|
|
|
|
echo '<li class="aksrt-announcement__item">';
|
|
if ($link) {
|
|
echo '<a href="' . esc_url($link) . '">' . esc_html($text) . '</a>';
|
|
} else {
|
|
echo esc_html($text);
|
|
}
|
|
echo '</li>';
|
|
}
|
|
|
|
// 复制第一条实现无缝滚动
|
|
$first_line = $lines[0];
|
|
$first_parts = explode('|', $first_line, 2);
|
|
$first_text = trim($first_parts[0]);
|
|
$first_link = isset($first_parts[1]) ? trim($first_parts[1]) : '';
|
|
echo '<li class="aksrt-announcement__item">';
|
|
if ($first_link) {
|
|
echo '<a href="' . esc_url($first_link) . '">' . esc_html($first_text) . '</a>';
|
|
} else {
|
|
echo esc_html($first_text);
|
|
}
|
|
echo '</li>';
|
|
|
|
echo '</ul>';
|
|
echo '</div>';
|
|
echo '</div>';
|
|
|
|
echo $args['after_widget'];
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$title = !empty($instance['title']) ? $instance['title'] : '';
|
|
$items = !empty($instance['items']) ? $instance['items'] : '';
|
|
$icon = !empty($instance['icon']) ? $instance['icon'] : 'fas fa-bullhorn';
|
|
$speed = !empty($instance['speed']) ? (int) $instance['speed'] : 10;
|
|
?>
|
|
<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('items')); ?>"><?php esc_html_e('公告内容(每行一条):', 'aksrt'); ?></label>
|
|
<textarea class="widefat" id="<?php echo esc_attr($this->get_field_id('items')); ?>" name="<?php echo esc_attr($this->get_field_name('items')); ?>" rows="5" placeholder="系统维护通知 新功能上线|https://example.com 活动公告"><?php echo esc_textarea($items); ?></textarea>
|
|
<small><?php esc_html_e('每行一条,格式:文本 或 文本|链接', 'aksrt'); ?></small>
|
|
</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>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('speed')); ?>"><?php esc_html_e('滚动速度(秒):', 'aksrt'); ?></label>
|
|
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('speed')); ?>" name="<?php echo esc_attr($this->get_field_name('speed')); ?>" type="number" value="<?php echo esc_attr((string) $speed); ?>" min="5" max="30">
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
$instance = [];
|
|
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
|
$instance['items'] = sanitize_textarea_field($new_instance['items'] ?? '');
|
|
$instance['icon'] = sanitize_text_field($new_instance['icon'] ?? 'fas fa-bullhorn');
|
|
$instance['speed'] = max(5, min(30, (int) ($new_instance['speed'] ?? 10)));
|
|
return $instance;
|
|
}
|
|
}
|