导入 AKSRT 主题开发源码
This commit is contained in:
1 parent
9681dd3d48
commit
1268a55dd0
595 files changed
+127705
No files matched your search
@@ -0,0 +1,109 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT Cover Cards Widget — 图文封面卡片
|
||||
*
|
||||
* 支持图片/视频背景 + 文字叠加的卡片组件。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class AKSRT_Widget_Cover_Cards extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_cover_cards',
|
||||
__('图文封面卡片', 'aksrt'),
|
||||
['description' => __('图片背景 + 文字叠加的卡片网格。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$items_json = !empty($instance['items']) ? $instance['items'] : '[]';
|
||||
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
|
||||
|
||||
$items = json_decode($items_json, true);
|
||||
if (!is_array($items) || empty($items)) return;
|
||||
|
||||
echo $args['before_widget'];
|
||||
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-cover-cards" style="--cols: ' . esc_attr((string) $columns) . ';">';
|
||||
|
||||
foreach ($items as $item) {
|
||||
$image = esc_url($item['image'] ?? '');
|
||||
$link = esc_url($item['link'] ?? '');
|
||||
$card_title = sanitize_text_field($item['title'] ?? '');
|
||||
$desc = sanitize_text_field($item['desc'] ?? '');
|
||||
$overlay = sanitize_hex_color($item['overlay'] ?? '');
|
||||
$text_color = sanitize_hex_color($item['text_color'] ?? '');
|
||||
$height = sanitize_text_field($item['height'] ?? '');
|
||||
|
||||
$card_style = '';
|
||||
if ($height) $card_style .= 'height:' . esc_attr($height) . ';';
|
||||
|
||||
$overlay_style = '';
|
||||
if ($overlay) $overlay_style .= 'background:' . esc_attr($overlay) . ';';
|
||||
|
||||
$text_style = '';
|
||||
if ($text_color) $text_style .= 'color:' . esc_attr($text_color) . ';';
|
||||
|
||||
echo '<div class="aksrt-cover-card"' . ($card_style ? ' style="' . esc_attr($card_style) . '"' : '') . '>';
|
||||
|
||||
if ($image) {
|
||||
echo '<div class="aksrt-cover-card__bg">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr($card_title) . '"' . (function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('widget') : ' loading="lazy"') . '>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
if ($overlay_style) {
|
||||
echo '<div class="aksrt-cover-card__overlay" style="' . esc_attr($overlay_style) . '"></div>';
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-cover-card__content"' . ($text_style ? ' style="' . esc_attr($text_style) . '"' : '') . '>';
|
||||
|
||||
if ($link) {
|
||||
echo '<a href="' . esc_url($link) . '" class="aksrt-cover-card__link">';
|
||||
}
|
||||
|
||||
if ($card_title) {
|
||||
echo '<h3 class="aksrt-cover-card__title">' . esc_html($card_title) . '</h3>';
|
||||
}
|
||||
if ($desc) {
|
||||
echo '<p class="aksrt-cover-card__desc">' . esc_html($desc) . '</p>';
|
||||
}
|
||||
|
||||
if ($link) {
|
||||
echo '</a>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
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'] : '[]';
|
||||
$columns = !empty($instance['columns']) ? (int) $instance['columns'] : 2;
|
||||
?>
|
||||
<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('columns')); ?>"><?php esc_html_e('列数:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('columns')); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>">
|
||||
<option value="1" <?php selected($columns, 1); ?>><?php esc_html_e('1 列', 'aksrt'); ?></option>
|
||||
<option value="2" <?php selected($columns, 2); ?>><?php esc_html_e('2 列', 'aksrt'); ?></option>
|
||||
<option value="3" <?php selected($columns, 3); ?>><?php esc_html_e('3 列', 'aksrt'); ?></option>
|
||||
<option value="4" <?php selected($columns, 4); ?>><?php esc_html_e('4 列', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('items')); ?>"><?php esc_html_e('卡片数据 (JSON):', '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="6" placeholder='[{"image":"https://...","title":"标题","desc":"描述","link":"https://..."}]'><?php echo esc_textarea($items); ?></textarea>
|
||||
<small><?php esc_html_e('JSON 数组,每项支持: image, title, desc, link, overlay, text_color, height', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['columns'] = max(1, min(4, (int) ($new_instance['columns'] ?? 2)));
|
||||
$items_raw = $new_instance['items'] ?? '[]';
|
||||
$items = json_decode($items_raw, true);
|
||||
$instance['items'] = is_array($items) ? wp_json_encode($items) : '[]';
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
/**
|
||||
* Forum-specific widgets.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Forum Hot Topics Widget
|
||||
*/
|
||||
class AKSRT_Widget_Forum_Hot_Topics extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_forum_hot_topics',
|
||||
__('论坛 - 热门话题', 'aksrt'),
|
||||
['description' => __('显示论坛热门话题列表。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
|
||||
$count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5;
|
||||
$period = !empty($instance['period']) ? sanitize_key($instance['period']) : 'week';
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
|
||||
$date_query = [];
|
||||
if ($period === 'week') {
|
||||
$date_query = [['after' => '7 days ago']];
|
||||
} elseif ($period === 'month') {
|
||||
$date_query = [['after' => '30 days ago']];
|
||||
}
|
||||
|
||||
$query = new WP_Query([
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'posts_per_page' => $count,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_aksrt_views',
|
||||
'orderby' => 'meta_value_num',
|
||||
'order' => 'DESC',
|
||||
'date_query' => $date_query,
|
||||
]);
|
||||
|
||||
if ($query->have_posts()) {
|
||||
echo '<ul class="aksrt-forum-widget__list">';
|
||||
$i = 0;
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
$i++;
|
||||
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
|
||||
echo '<li class="aksrt-forum-widget__item">';
|
||||
echo '<span class="aksrt-forum-widget__rank">' . $i . '</span>';
|
||||
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">';
|
||||
echo esc_html(get_the_title());
|
||||
echo '</a>';
|
||||
echo '<span class="aksrt-forum-widget__views"><i class="fas fa-eye"></i> ' . $views . '</span>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
} else {
|
||||
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无话题', 'aksrt') . '</p>';
|
||||
}
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('热门话题', 'aksrt');
|
||||
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
|
||||
$period = !empty($instance['period']) ? $instance['period'] : 'week';
|
||||
?>
|
||||
<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 class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" value="<?php echo esc_attr($count); ?>" min="1" max="20">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('period')); ?>"><?php esc_html_e('时间范围:', 'aksrt'); ?></label>
|
||||
<select id="<?php echo esc_attr($this->get_field_id('period')); ?>" name="<?php echo esc_attr($this->get_field_name('period')); ?>">
|
||||
<option value="all" <?php selected($period, 'all'); ?>><?php esc_html_e('全部', 'aksrt'); ?></option>
|
||||
<option value="week" <?php selected($period, 'week'); ?>><?php esc_html_e('最近一周', 'aksrt'); ?></option>
|
||||
<option value="month" <?php selected($period, 'month'); ?>><?php esc_html_e('最近一月', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
|
||||
$instance['period'] = in_array($new_instance['period'] ?? '', ['all', 'week', 'month'], true) ? $new_instance['period'] : 'week';
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forum Recent Topics Widget
|
||||
*/
|
||||
class AKSRT_Widget_Forum_Recent_Topics extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_forum_recent_topics',
|
||||
__('论坛 - 最新话题', 'aksrt'),
|
||||
['description' => __('显示论坛最新话题列表。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
|
||||
$count = !empty($instance['count']) ? max(1, min(20, (int) $instance['count'])) : 5;
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
|
||||
$query = new WP_Query([
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'posts_per_page' => $count,
|
||||
'post_status' => 'publish',
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
]);
|
||||
|
||||
if ($query->have_posts()) {
|
||||
echo '<ul class="aksrt-forum-widget__list">';
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
$author = get_the_author_meta('display_name');
|
||||
echo '<li class="aksrt-forum-widget__item">';
|
||||
echo '<a href="' . esc_url(get_permalink()) . '" class="aksrt-forum-widget__link">';
|
||||
echo esc_html(get_the_title());
|
||||
echo '</a>';
|
||||
echo '<span class="aksrt-forum-widget__author">' . esc_html($author) . '</span>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
} else {
|
||||
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无话题', 'aksrt') . '</p>';
|
||||
}
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('最新话题', 'aksrt');
|
||||
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
|
||||
?>
|
||||
<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 class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" value="<?php echo esc_attr($count); ?>" min="1" max="20">
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forum Board List Widget
|
||||
*/
|
||||
class AKSRT_Widget_Forum_Board_List extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_forum_board_list',
|
||||
__('论坛 - 版块列表', 'aksrt'),
|
||||
['description' => __('显示论坛版块列表及话题数量。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
|
||||
$boards = get_terms([
|
||||
'taxonomy' => 'aksrt_forum_board',
|
||||
'hide_empty' => true,
|
||||
'orderby' => 'meta_value_num',
|
||||
'meta_key' => '_aksrt_sort_order',
|
||||
'order' => 'ASC',
|
||||
]);
|
||||
|
||||
if (!is_wp_error($boards) && !empty($boards)) {
|
||||
echo '<ul class="aksrt-forum-widget__boards">';
|
||||
foreach ($boards as $board) {
|
||||
$icon = get_term_meta($board->term_id, '_aksrt_icon', true);
|
||||
echo '<li class="aksrt-forum-widget__board">';
|
||||
echo '<a href="' . esc_url(get_term_link($board)) . '">';
|
||||
if ($icon) {
|
||||
echo '<i class="' . esc_attr($icon) . '"></i> ';
|
||||
}
|
||||
echo esc_html($board->name);
|
||||
echo '<span class="aksrt-forum-widget__count">' . (int) $board->count . '</span>';
|
||||
echo '</a>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
} else {
|
||||
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无版块', 'aksrt') . '</p>';
|
||||
}
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
|
||||
?>
|
||||
<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>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register forum widgets.
|
||||
*/
|
||||
function aksrt_register_forum_widgets(): void {
|
||||
register_widget('AKSRT_Widget_Forum_Hot_Topics');
|
||||
register_widget('AKSRT_Widget_Forum_Recent_Topics');
|
||||
register_widget('AKSRT_Widget_Forum_Board_List');
|
||||
}
|
||||
add_action('widgets_init', 'aksrt_register_forum_widgets');
|
||||
@@ -0,0 +1,842 @@
|
||||
<?php
|
||||
/**
|
||||
* Homepage content widgets aligned to the fixed Zibll-like layout.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_home_widget_allowed_layouts')) {
|
||||
/**
|
||||
* Allowed post-card layouts for homepage content widgets.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
function aksrt_home_widget_allowed_layouts(): array {
|
||||
return [
|
||||
'card' => __('卡片', 'aksrt'),
|
||||
'list' => __('列表', 'aksrt'),
|
||||
'grid-cms' => __('资源网格', 'aksrt'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_post_types')) {
|
||||
/**
|
||||
* Post types used by homepage widgets.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
function aksrt_home_widget_post_types(): array {
|
||||
$post_types = ['post'];
|
||||
|
||||
if (function_exists('aksrt_resource_post_types')) {
|
||||
$post_types = array_unique(array_merge($post_types, aksrt_resource_post_types()));
|
||||
}
|
||||
|
||||
return array_values(array_filter(array_map('sanitize_key', $post_types)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_available_categories')) {
|
||||
/**
|
||||
* Return categories for widget admin forms.
|
||||
*
|
||||
* @return array<int, WP_Term>
|
||||
*/
|
||||
function aksrt_home_widget_available_categories(): array {
|
||||
$categories = get_categories([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => false,
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
]);
|
||||
|
||||
return is_array($categories) ? $categories : [];
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_fallback_category_ids')) {
|
||||
/**
|
||||
* Pick top categories as fallback when widget instance has no selection.
|
||||
*
|
||||
* @param int $limit Maximum category ids to return.
|
||||
* @return array<int, int>
|
||||
*/
|
||||
function aksrt_home_widget_fallback_category_ids(int $limit = 1): array {
|
||||
$limit = max(1, min(8, $limit));
|
||||
$terms = get_terms([
|
||||
'taxonomy' => 'category',
|
||||
'orderby' => 'count',
|
||||
'order' => 'DESC',
|
||||
'number' => $limit,
|
||||
'hide_empty' => true,
|
||||
'fields' => 'ids',
|
||||
]);
|
||||
|
||||
if (is_wp_error($terms) || !is_array($terms)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_filter(array_map('absint', $terms)));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_normalize_category_ids')) {
|
||||
/**
|
||||
* Normalize selected category ids.
|
||||
*
|
||||
* @param mixed $raw_ids Raw ids from widget instance.
|
||||
* @param int $limit Maximum ids to keep.
|
||||
* @return array<int, int>
|
||||
*/
|
||||
function aksrt_home_widget_normalize_category_ids($raw_ids, int $limit = 4): array {
|
||||
$ids = array_map('absint', (array) $raw_ids);
|
||||
$ids = array_values(array_filter($ids, static fn($id) => $id > 0));
|
||||
|
||||
if ($limit > 0) {
|
||||
$ids = array_slice($ids, 0, $limit);
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_resolve_layout')) {
|
||||
/**
|
||||
* Sanitize requested widget post layout.
|
||||
*
|
||||
* @param mixed $layout Layout value.
|
||||
* @param string $default Default layout.
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_home_widget_resolve_layout($layout, string $default = 'card'): string {
|
||||
$allowed = array_keys(aksrt_home_widget_allowed_layouts());
|
||||
$layout = is_string($layout) ? $layout : $default;
|
||||
|
||||
return in_array($layout, $allowed, true) ? $layout : $default;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_resolve_context')) {
|
||||
/**
|
||||
* Resolve widget area context from sidebar args.
|
||||
*/
|
||||
function aksrt_home_widget_resolve_context(array $args): string {
|
||||
if (function_exists('aksrt_widget_get_area_context')) {
|
||||
return aksrt_widget_get_area_context($args);
|
||||
}
|
||||
|
||||
return 'homepage';
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_effective_layout')) {
|
||||
/**
|
||||
* Compact rich homepage widgets when placed into a sidebar area.
|
||||
*/
|
||||
function aksrt_home_widget_effective_layout(string $layout, string $context): string {
|
||||
if ($context === 'sidebar' && $layout !== 'list') {
|
||||
return 'list';
|
||||
}
|
||||
|
||||
return $layout;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_effective_columns')) {
|
||||
/**
|
||||
* Sidebar widgets should stay single-column.
|
||||
*/
|
||||
function aksrt_home_widget_effective_columns(int $columns, string $context): int {
|
||||
if ($context === 'sidebar') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return max(1, min(4, $columns));
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_render_header')) {
|
||||
/**
|
||||
* Render a compact Zibll-like section header.
|
||||
*
|
||||
* @param string $title Section title.
|
||||
* @param string $more_link Optional more link.
|
||||
* @param string $more_text Optional more text.
|
||||
* @return void
|
||||
*/
|
||||
function aksrt_home_widget_render_header(string $title, string $more_link = '', string $more_text = ''): void {
|
||||
?>
|
||||
<header class="aksrt-home-widget__header">
|
||||
<h3 class="aksrt-home-widget__title"><?php echo esc_html($title); ?></h3>
|
||||
<?php if ($more_link !== '') : ?>
|
||||
<a class="aksrt-home-widget__more" href="<?php echo esc_url($more_link); ?>">
|
||||
<?php echo esc_html($more_text !== '' ? $more_text : __('查看更多', 'aksrt')); ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
</header>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_render_post_grid')) {
|
||||
/**
|
||||
* Render posts for homepage widgets.
|
||||
*
|
||||
* Delegates to the unified shared renderer (aksrt_render_post_list).
|
||||
*
|
||||
* @param WP_Query $query Posts query.
|
||||
* @param string $layout Card layout.
|
||||
* @param int $columns Desktop columns.
|
||||
* @param string $context Sidebar or homepage context.
|
||||
* @return void
|
||||
*/
|
||||
function aksrt_home_widget_render_post_grid(WP_Query $query, string $layout = 'card', int $columns = 3, string $context = 'homepage'): void {
|
||||
if (!$query->have_posts()) {
|
||||
echo '<p class="widget-empty">' . esc_html__('暂无内容', 'aksrt') . '</p>';
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
}
|
||||
|
||||
if ($context === 'sidebar') {
|
||||
$use_homepage_mini = function_exists('aksrt_is_fixed_homepage_request')
|
||||
&& aksrt_is_fixed_homepage_request()
|
||||
&& function_exists('aksrt_render_mini_list')
|
||||
&& (!class_exists('AKSRT_Settings') || AKSRT_Settings::homepage_sidebar_mini_enabled());
|
||||
|
||||
if ($use_homepage_mini && function_exists('aksrt_render_mini_list')) {
|
||||
echo '<div class="widget-tab-posts__list widget-tab-posts__list--mini aksrt-home-widget__sidebar-list">';
|
||||
aksrt_render_mini_list($query, ['context' => 'sidebar', 'show_thumb' => true]);
|
||||
echo '</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: simple sidebar list
|
||||
echo '<ul class="widget-tab-posts__list aksrt-home-widget__sidebar-list">';
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
?>
|
||||
<li class="widget-tab-posts__item">
|
||||
<?php if (has_post_thumbnail()) : ?>
|
||||
<a href="<?php the_permalink(); ?>" class="widget-tab-posts__thumb">
|
||||
<?php the_post_thumbnail('thumbnail', ['loading' => 'lazy', 'class' => 'fit-cover']); ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<div class="widget-tab-posts__info">
|
||||
<a href="<?php the_permalink(); ?>" class="widget-tab-posts__title"><?php the_title(); ?></a>
|
||||
<span class="widget-tab-posts__meta">
|
||||
<?php echo esc_html(human_time_diff(get_the_time('U'), current_time('timestamp')) . __('前', 'aksrt')); ?>
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
<?php
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
}
|
||||
|
||||
// Homepage / content area: delegate to shared renderer
|
||||
$layout = aksrt_home_widget_resolve_layout($layout, 'card');
|
||||
$columns = max(1, min(4, $columns));
|
||||
|
||||
if (function_exists('aksrt_render_post_list')) {
|
||||
aksrt_render_post_list($query, [
|
||||
'mode' => $layout === 'grid-cms' ? 'grid-cms' : $layout,
|
||||
'context' => 'widget',
|
||||
'columns' => $columns,
|
||||
], true);
|
||||
} else {
|
||||
// Fallback: use template part
|
||||
$previous_layout = get_query_var('aksrt_post_card_layout', '');
|
||||
$previous_scene = get_query_var('aksrt_post_card_scene', '');
|
||||
|
||||
set_query_var('aksrt_post_card_layout', $layout);
|
||||
set_query_var('aksrt_post_card_scene', 'home');
|
||||
|
||||
$classes = [
|
||||
'aksrt-home-widget__post-grid',
|
||||
'aksrt-home-widget__post-grid--' . $layout,
|
||||
'post-grid',
|
||||
'post-grid--' . $layout,
|
||||
];
|
||||
|
||||
printf(
|
||||
'<div class="%1$s" style="--aksrt-widget-columns:%2$d;">',
|
||||
esc_attr(implode(' ', $classes)),
|
||||
(int) $columns
|
||||
);
|
||||
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
get_template_part('templates/parts/content', 'post-card');
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
|
||||
set_query_var('aksrt_post_card_layout', $previous_layout);
|
||||
set_query_var('aksrt_post_card_scene', $previous_scene);
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('aksrt_home_widget_get_lazyload_args')) {
|
||||
/**
|
||||
* Lazyload settings reused by category recommend widget.
|
||||
*
|
||||
* @return array{enabled: bool, placeholder_url: string}
|
||||
*/
|
||||
function aksrt_home_widget_get_lazyload_args(): array {
|
||||
$config = class_exists('AKSRT_Settings')
|
||||
? AKSRT_Settings::get_lazyload_config()
|
||||
: ['enabled' => false, 'placeholder_url' => ''];
|
||||
|
||||
return [
|
||||
'enabled' => ! empty($config['enabled']),
|
||||
'placeholder_url' => ! empty($config['placeholder_url'])
|
||||
? (string) $config['placeholder_url']
|
||||
: get_template_directory_uri() . '/assets/img/placeholder.svg',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Homepage category block widget.
|
||||
*/
|
||||
class AKSRT_Category_Block_Widget extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_category_block',
|
||||
esc_html__('AKSRT 分类块', 'aksrt'),
|
||||
[
|
||||
'classname' => 'widget_aksrt_category_block',
|
||||
'description' => esc_html__('指定分类的最新内容区块,可挂载到首页内容挂载区。', 'aksrt'),
|
||||
'customize_selective_refresh' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$context = aksrt_home_widget_resolve_context($args);
|
||||
$category_id = $this->resolve_category_id($instance);
|
||||
$term = $category_id > 0 ? get_term($category_id, 'category') : null;
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
||||
$title = $title !== '' ? $title : (($term && ! is_wp_error($term)) ? $term->name : __('精选分类', 'aksrt'));
|
||||
$count = max(1, min(12, (int) ($instance['count'] ?? 6)));
|
||||
$columns = aksrt_home_widget_effective_columns((int) ($instance['columns'] ?? 3), $context);
|
||||
$layout = aksrt_home_widget_effective_layout(
|
||||
aksrt_home_widget_resolve_layout($instance['layout'] ?? 'card', 'card'),
|
||||
$context
|
||||
);
|
||||
$show_more = ! empty($instance['show_more']);
|
||||
|
||||
$query_args = [
|
||||
'post_type' => aksrt_home_widget_post_types(),
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
];
|
||||
|
||||
if ($category_id > 0) {
|
||||
$query_args['cat'] = $category_id;
|
||||
}
|
||||
|
||||
$query = new WP_Query($query_args);
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo '<section class="aksrt-home-widget aksrt-home-widget--category-block">';
|
||||
|
||||
aksrt_home_widget_render_header(
|
||||
(string) $title,
|
||||
($show_more && $term && ! is_wp_error($term)) ? (string) get_category_link($category_id) : '',
|
||||
__('查看分类', 'aksrt')
|
||||
);
|
||||
|
||||
aksrt_home_widget_render_post_grid($query, $layout, $columns, $context);
|
||||
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = (string) ($instance['title'] ?? '');
|
||||
$category_id = (int) ($instance['category_id'] ?? 0);
|
||||
$count = (int) ($instance['count'] ?? 6);
|
||||
$columns = (int) ($instance['columns'] ?? 3);
|
||||
$layout = aksrt_home_widget_resolve_layout($instance['layout'] ?? 'card', 'card');
|
||||
$show_more = ! empty($instance['show_more']);
|
||||
$categories = aksrt_home_widget_available_categories();
|
||||
?>
|
||||
<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('category_id')); ?>"><?php esc_html_e('分类:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category_id')); ?>" name="<?php echo esc_attr($this->get_field_name('category_id')); ?>">
|
||||
<option value="0"><?php esc_html_e('自动选择最热门分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($categories as $category) : ?>
|
||||
<option value="<?php echo (int) $category->term_id; ?>" <?php selected($category_id, (int) $category->term_id); ?>>
|
||||
<?php echo esc_html($category->name); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('文章数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" min="1" max="12" value="<?php echo (int) $count; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('columns')); ?>"><?php esc_html_e('桌面列数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('columns')); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" type="number" min="1" max="4" value="<?php echo (int) $columns; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('layout')); ?>"><?php esc_html_e('布局:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('layout')); ?>" name="<?php echo esc_attr($this->get_field_name('layout')); ?>">
|
||||
<?php foreach (aksrt_home_widget_allowed_layouts() as $value => $label) : ?>
|
||||
<option value="<?php echo esc_attr($value); ?>" <?php selected($layout, $value); ?>>
|
||||
<?php echo esc_html($label); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_more); ?> id="<?php echo esc_attr($this->get_field_id('show_more')); ?>" name="<?php echo esc_attr($this->get_field_name('show_more')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_more')); ?>"><?php esc_html_e('显示分类链接', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('hybrid'); } ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'category_id' => absint($new_instance['category_id'] ?? 0),
|
||||
'count' => max(1, min(12, absint($new_instance['count'] ?? 6))),
|
||||
'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,
|
||||
];
|
||||
}
|
||||
|
||||
private function resolve_category_id(array $instance): int {
|
||||
$category_id = absint($instance['category_id'] ?? 0);
|
||||
if ($category_id > 0) {
|
||||
return $category_id;
|
||||
}
|
||||
|
||||
$fallback = aksrt_home_widget_fallback_category_ids(1);
|
||||
return $fallback[0] ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Homepage category tabs widget.
|
||||
*/
|
||||
class AKSRT_Category_Tabs_Widget extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_category_tabs',
|
||||
esc_html__('AKSRT 分类 Tabs', 'aksrt'),
|
||||
[
|
||||
'classname' => 'widget_aksrt_category_tabs',
|
||||
'description' => esc_html__('多分类切换内容流,可挂载到首页内容挂载区。', 'aksrt'),
|
||||
'customize_selective_refresh' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$context = aksrt_home_widget_resolve_context($args);
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
||||
$title = $title !== '' ? $title : __('热门分类', 'aksrt');
|
||||
$count = max(1, min(12, (int) ($instance['count'] ?? 6)));
|
||||
$columns = aksrt_home_widget_effective_columns((int) ($instance['columns'] ?? 3), $context);
|
||||
$layout = aksrt_home_widget_effective_layout(
|
||||
aksrt_home_widget_resolve_layout($instance['layout'] ?? 'card', 'card'),
|
||||
$context
|
||||
);
|
||||
$show_more = ! empty($instance['show_more']);
|
||||
$tabs = $this->resolve_tabs($instance);
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo '<section class="aksrt-home-widget aksrt-home-widget--tabs widget-tab-posts">';
|
||||
|
||||
if (empty($tabs)) {
|
||||
echo '<p class="widget-empty">' . esc_html__('暂无可用分类', 'aksrt') . '</p>';
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<header class="aksrt-home-widget__header aksrt-home-widget__header--tabs">
|
||||
<h3 class="aksrt-home-widget__title"><?php echo esc_html($title); ?></h3>
|
||||
<ul class="widget-tab-posts__nav aksrt-home-widget__tab-nav" role="tablist">
|
||||
<?php foreach ($tabs as $index => $tab) : ?>
|
||||
<li role="presentation">
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
class="widget-tab-posts__tab<?php echo 0 === $index ? ' active' : ''; ?>"
|
||||
data-tab="cat-<?php echo (int) $tab['id']; ?>"
|
||||
aria-selected="<?php echo 0 === $index ? 'true' : 'false'; ?>"
|
||||
>
|
||||
<?php echo esc_html($tab['name']); ?>
|
||||
</button>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</header>
|
||||
<?php
|
||||
|
||||
foreach ($tabs as $index => $tab) {
|
||||
$query = new WP_Query([
|
||||
'post_type' => aksrt_home_widget_post_types(),
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'cat' => (int) $tab['id'],
|
||||
]);
|
||||
?>
|
||||
<div class="widget-tab-posts__panel<?php echo 0 === $index ? ' active' : ''; ?>" data-tab-panel="cat-<?php echo (int) $tab['id']; ?>" role="tabpanel"<?php echo 0 === $index ? '' : ' hidden'; ?>>
|
||||
<?php aksrt_home_widget_render_post_grid($query, $layout, $columns, $context); ?>
|
||||
<?php if ($show_more) : ?>
|
||||
<div class="aksrt-home-widget__footer">
|
||||
<a class="aksrt-home-widget__more" href="<?php echo esc_url($tab['link']); ?>">
|
||||
<?php esc_html_e('查看更多', 'aksrt'); ?>
|
||||
</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = (string) ($instance['title'] ?? '');
|
||||
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 6);
|
||||
$count = (int) ($instance['count'] ?? 6);
|
||||
$columns = (int) ($instance['columns'] ?? 3);
|
||||
$layout = aksrt_home_widget_resolve_layout($instance['layout'] ?? 'card', 'card');
|
||||
$show_more = ! empty($instance['show_more']);
|
||||
$categories = aksrt_home_widget_available_categories();
|
||||
?>
|
||||
<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('category_ids')); ?>"><?php esc_html_e('分类(可多选):', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('category_ids')); ?>[]" multiple size="6">
|
||||
<?php foreach ($categories as $category) : ?>
|
||||
<option value="<?php echo (int) $category->term_id; ?>" <?php selected(in_array((int) $category->term_id, $category_ids, true)); ?>>
|
||||
<?php echo esc_html($category->name); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small><?php esc_html_e('留空时自动选择热门分类。', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('每个 Tab 的文章数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" min="1" max="12" value="<?php echo (int) $count; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('columns')); ?>"><?php esc_html_e('桌面列数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('columns')); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" type="number" min="1" max="4" value="<?php echo (int) $columns; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('layout')); ?>"><?php esc_html_e('布局:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('layout')); ?>" name="<?php echo esc_attr($this->get_field_name('layout')); ?>">
|
||||
<?php foreach (aksrt_home_widget_allowed_layouts() as $value => $label) : ?>
|
||||
<option value="<?php echo esc_attr($value); ?>" <?php selected($layout, $value); ?>>
|
||||
<?php echo esc_html($label); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_more); ?> id="<?php echo esc_attr($this->get_field_id('show_more')); ?>" name="<?php echo esc_attr($this->get_field_name('show_more')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_more')); ?>"><?php esc_html_e('显示“查看更多”链接', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('hybrid'); } ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'category_ids' => aksrt_home_widget_normalize_category_ids($new_instance['category_ids'] ?? [], 6),
|
||||
'count' => max(1, min(12, absint($new_instance['count'] ?? 6))),
|
||||
'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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve configured category tabs.
|
||||
*
|
||||
* @param array $instance Widget instance.
|
||||
* @return array<int, array{id:int,name:string,link:string}>
|
||||
*/
|
||||
private function resolve_tabs(array $instance): array {
|
||||
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 6);
|
||||
if (empty($category_ids)) {
|
||||
$category_ids = aksrt_home_widget_fallback_category_ids(4);
|
||||
}
|
||||
|
||||
$tabs = [];
|
||||
foreach ($category_ids as $category_id) {
|
||||
$term = get_term($category_id, 'category');
|
||||
if (! $term || is_wp_error($term)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tabs[] = [
|
||||
'id' => (int) $category_id,
|
||||
'name' => $term->name,
|
||||
'link' => get_category_link($category_id),
|
||||
];
|
||||
}
|
||||
|
||||
return $tabs;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Homepage category recommend cards widget.
|
||||
*/
|
||||
class AKSRT_Category_Recommend_Widget extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_category_recommend',
|
||||
esc_html__('AKSRT 分类推荐卡片', 'aksrt'),
|
||||
[
|
||||
'classname' => 'widget_aksrt_category_recommend',
|
||||
'description' => esc_html__('分类推荐卡片区块,可挂载到首页内容挂载区。', 'aksrt'),
|
||||
'customize_selective_refresh' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
||||
$title = $title !== '' ? $title : __('分类推荐', 'aksrt');
|
||||
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 4);
|
||||
$posts_per_cat = max(1, min(8, (int) ($instance['posts_per_category'] ?? 5)));
|
||||
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
|
||||
$show_views = ! empty($instance['show_views']);
|
||||
$show_description = ! empty($instance['show_description']);
|
||||
$show_dates = ! empty($instance['show_dates']);
|
||||
$show_more = ! empty($instance['show_more']);
|
||||
|
||||
if (empty($category_ids)) {
|
||||
$category_ids = aksrt_home_widget_fallback_category_ids(4);
|
||||
}
|
||||
|
||||
$cards = $this->build_cards($category_ids, $posts_per_cat, $show_views, $show_description);
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo '<section class="aksrt-home-widget aksrt-home-widget--recommend">';
|
||||
|
||||
aksrt_home_widget_render_header($title);
|
||||
|
||||
if (empty($cards)) {
|
||||
echo '<p class="widget-empty">' . esc_html__('暂无推荐分类', 'aksrt') . '</p>';
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
return;
|
||||
}
|
||||
|
||||
$lazy = aksrt_home_widget_get_lazyload_args();
|
||||
printf('<div class="aksrt-category-recommend__grid" style="--aksrt-recommend-columns:%d;">', (int) $columns);
|
||||
|
||||
foreach ($cards as $card) {
|
||||
?>
|
||||
<article class="aksrt-category-recommend__card">
|
||||
<div class="aksrt-category-recommend__hero">
|
||||
<a href="<?php echo esc_url($card['link']); ?>">
|
||||
<?php if ($lazy['enabled']) : ?>
|
||||
<span class="aksrt-skeleton"></span>
|
||||
<img class="aksrt-category-recommend__cover aksrt-lazy" data-src="<?php echo esc_url($card['cover']); ?>" src="<?php echo esc_url($lazy['placeholder_url']); ?>" alt="<?php echo esc_attr($card['name']); ?>">
|
||||
<?php else : ?>
|
||||
<img class="aksrt-category-recommend__cover" src="<?php echo esc_url($card['cover']); ?>" alt="<?php echo esc_attr($card['name']); ?>">
|
||||
<?php endif; ?>
|
||||
</a>
|
||||
<span class="aksrt-category-recommend__badge"><?php echo esc_html(sprintf(_n('%d 篇文章', '%d 篇文章', $card['count'], 'aksrt'), $card['count'])); ?></span>
|
||||
<?php if ($show_views && $card['views'] > 0) : ?>
|
||||
<span class="aksrt-category-recommend__views">
|
||||
<i class="fas fa-eye" aria-hidden="true"></i>
|
||||
<?php echo esc_html(function_exists('aksrt_format_number') ? aksrt_format_number($card['views']) : number_format_i18n($card['views'])); ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="aksrt-category-recommend__body">
|
||||
<div class="aksrt-category-recommend__meta">
|
||||
<h4 class="aksrt-category-recommend__name">
|
||||
<a href="<?php echo esc_url($card['link']); ?>"><?php echo esc_html($card['name']); ?></a>
|
||||
</h4>
|
||||
<?php if ($show_description && $card['description'] !== '') : ?>
|
||||
<p class="aksrt-category-recommend__desc"><?php echo esc_html($card['description']); ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<ul class="aksrt-category-recommend__posts">
|
||||
<?php foreach ($card['posts'] as $post_item) : ?>
|
||||
<li class="aksrt-category-recommend__post">
|
||||
<a class="aksrt-category-recommend__post-link" href="<?php echo esc_url($post_item['link']); ?>">
|
||||
<?php echo esc_html($post_item['title']); ?>
|
||||
</a>
|
||||
<?php if ($show_dates && $post_item['date'] !== '') : ?>
|
||||
<span class="aksrt-category-recommend__date"><?php echo esc_html($post_item['date']); ?></span>
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php if ($show_more) : ?>
|
||||
<div class="aksrt-home-widget__footer">
|
||||
<a class="aksrt-home-widget__more" href="<?php echo esc_url($card['link']); ?>">
|
||||
<?php esc_html_e('查看更多', 'aksrt'); ?>
|
||||
</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</article>
|
||||
<?php
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = (string) ($instance['title'] ?? __('分类推荐', 'aksrt'));
|
||||
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 4);
|
||||
$posts_per_cat = (int) ($instance['posts_per_category'] ?? 5);
|
||||
$columns = (int) ($instance['columns'] ?? 2);
|
||||
$show_views = ! empty($instance['show_views']);
|
||||
$show_description = ! array_key_exists('show_description', $instance) || ! empty($instance['show_description']);
|
||||
$show_dates = ! array_key_exists('show_dates', $instance) || ! empty($instance['show_dates']);
|
||||
$show_more = ! array_key_exists('show_more', $instance) || ! empty($instance['show_more']);
|
||||
$categories = aksrt_home_widget_available_categories();
|
||||
?>
|
||||
<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('category_ids')); ?>"><?php esc_html_e('分类(最多 4 个):', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('category_ids')); ?>[]" multiple size="6">
|
||||
<?php foreach ($categories as $category) : ?>
|
||||
<option value="<?php echo (int) $category->term_id; ?>" <?php selected(in_array((int) $category->term_id, $category_ids, true)); ?>>
|
||||
<?php echo esc_html($category->name); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<small><?php esc_html_e('留空时自动选择热门分类。', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('posts_per_category')); ?>"><?php esc_html_e('每个分类展示文章数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('posts_per_category')); ?>" name="<?php echo esc_attr($this->get_field_name('posts_per_category')); ?>" type="number" min="1" max="8" value="<?php echo (int) $posts_per_cat; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('columns')); ?>"><?php esc_html_e('桌面列数:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('columns')); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" type="number" min="1" max="4" value="<?php echo (int) $columns; ?>">
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_views); ?> id="<?php echo esc_attr($this->get_field_id('show_views')); ?>" name="<?php echo esc_attr($this->get_field_name('show_views')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_views')); ?>"><?php esc_html_e('显示分类总浏览量', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_description); ?> id="<?php echo esc_attr($this->get_field_id('show_description')); ?>" name="<?php echo esc_attr($this->get_field_name('show_description')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_description')); ?>"><?php esc_html_e('显示分类描述', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_dates); ?> id="<?php echo esc_attr($this->get_field_id('show_dates')); ?>" name="<?php echo esc_attr($this->get_field_name('show_dates')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_dates')); ?>"><?php esc_html_e('显示文章时间', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<input class="checkbox" type="checkbox" <?php checked($show_more); ?> id="<?php echo esc_attr($this->get_field_id('show_more')); ?>" name="<?php echo esc_attr($this->get_field_name('show_more')); ?>" value="1">
|
||||
<label for="<?php echo esc_attr($this->get_field_id('show_more')); ?>"><?php esc_html_e('显示“查看更多”链接', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('homepage'); } ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'category_ids' => aksrt_home_widget_normalize_category_ids($new_instance['category_ids'] ?? [], 4),
|
||||
'posts_per_category' => max(1, min(8, absint($new_instance['posts_per_category'] ?? 5))),
|
||||
'columns' => max(1, min(4, absint($new_instance['columns'] ?? 2))),
|
||||
'show_views' => ! empty($new_instance['show_views']) ? 1 : 0,
|
||||
'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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build category recommend cards.
|
||||
*
|
||||
* @param array<int, int> $category_ids Category ids.
|
||||
* @param int $posts_per_cat Post count per category.
|
||||
* @param bool $show_views Whether to calculate views.
|
||||
* @param bool $show_description Whether to fetch description.
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function build_cards(array $category_ids, int $posts_per_cat, bool $show_views, bool $show_description): array {
|
||||
$cards = [];
|
||||
|
||||
foreach ($category_ids as $category_id) {
|
||||
$term = get_term($category_id, 'category');
|
||||
if (! $term || is_wp_error($term)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$posts = get_posts([
|
||||
'post_type' => aksrt_home_widget_post_types(),
|
||||
'cat' => $category_id,
|
||||
'posts_per_page' => $posts_per_cat,
|
||||
'post_status' => 'publish',
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'no_found_rows' => true,
|
||||
]);
|
||||
|
||||
$post_items = [];
|
||||
foreach ($posts as $post) {
|
||||
$post_items[] = [
|
||||
'title' => get_the_title($post->ID),
|
||||
'link' => get_permalink($post->ID),
|
||||
'date' => function_exists('aksrt_time_ago') ? aksrt_time_ago($post->post_date) : get_the_date('Y-m-d', $post->ID),
|
||||
];
|
||||
}
|
||||
|
||||
$cards[] = [
|
||||
'name' => $term->name,
|
||||
'link' => get_category_link($category_id),
|
||||
'cover' => class_exists('AKSRT_Settings') ? AKSRT_Settings::get_category_cover_url($category_id) : (get_template_directory_uri() . '/assets/img/placeholder.svg'),
|
||||
'description' => $show_description && class_exists('AKSRT_Settings') ? AKSRT_Settings::get_category_recommend_desc($category_id) : ($show_description ? (string) $term->description : ''),
|
||||
'count' => (int) $term->count,
|
||||
'views' => $show_views && function_exists('aksrt_get_category_total_views') ? aksrt_get_category_total_views($category_id) : 0,
|
||||
'posts' => $post_items,
|
||||
];
|
||||
}
|
||||
|
||||
return $cards;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT Language Switch Widget — 语言切换器
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class AKSRT_Widget_Lang_Switch extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_lang_switch',
|
||||
__('语言切换器', 'aksrt'),
|
||||
['description' => __('在侧栏显示多语言切换按钮。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$style = !empty($instance['style']) ? $instance['style'] : 'dropdown';
|
||||
|
||||
if (!function_exists('aksrt_has_multilingual') || !aksrt_has_multilingual()) return;
|
||||
|
||||
$html = function_exists('aksrt_render_language_switcher') ? aksrt_render_language_switcher($style) : '';
|
||||
if (empty($html)) return;
|
||||
|
||||
echo $args['before_widget'];
|
||||
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$style = !empty($instance['style']) ? $instance['style'] : 'dropdown';
|
||||
?>
|
||||
<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('style')); ?>"><?php esc_html_e('样式:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="dropdown" <?php selected($style, 'dropdown'); ?>><?php esc_html_e('下拉菜单', 'aksrt'); ?></option>
|
||||
<option value="flags" <?php selected($style, 'flags'); ?>><?php esc_html_e('国旗图标', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p><small><?php esc_html_e('需要安装 Polylang 或 WPML 等多语言插件才能生效。', 'aksrt'); ?></small></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['style'] = in_array($new_instance['style'] ?? '', ['dropdown', 'flags'], true) ? $new_instance['style'] : 'dropdown';
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,410 @@
|
||||
<?php
|
||||
/**
|
||||
* Shop product widgets (§6.9)
|
||||
*
|
||||
* - AKSRT_Widget_Product_List: product list widget (filter/sort/paginate/AJAX)
|
||||
* - AKSRT_Widget_Product_Oneline: horizontal swiper product widget
|
||||
* - AKSRT_Widget_Product_Tab: multi-tab product list widget
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Product list widget — equivalent to zibll `zib_shop_widget_ui_product_lists`.
|
||||
*
|
||||
* Supports: include/exclude category, discount, tag; sort order; list style;
|
||||
* AJAX pagination; per-page count.
|
||||
*/
|
||||
class AKSRT_Widget_Product_List extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_list',
|
||||
__('商品列表', 'aksrt'),
|
||||
['description' => __('AKSRT 商品列表组件:筛选、排序、AJAX 翻页。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '');
|
||||
$per_page = max(1, min(50, (int) ($instance['per_page'] ?? 8)));
|
||||
$orderby = sanitize_key($instance['orderby'] ?? 'date');
|
||||
$order = in_array(strtoupper($instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($instance['order']) : 'DESC';
|
||||
$cat_ids = array_filter(array_map('intval', explode(',', (string) ($instance['cat_ids'] ?? ''))));
|
||||
$tag_ids = array_filter(array_map('intval', explode(',', (string) ($instance['tag_ids'] ?? ''))));
|
||||
$discount_ids = array_filter(array_map('intval', explode(',', (string) ($instance['discount_ids'] ?? ''))));
|
||||
$exclude_cat = !empty($instance['exclude_cat']);
|
||||
$style = sanitize_key($instance['style'] ?? 'default');
|
||||
$paged = max(1, (int) ($_GET['aksrt_product_page'] ?? 1));
|
||||
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_product',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $per_page,
|
||||
'paged' => $paged,
|
||||
'orderby' => $orderby === 'sales' ? 'meta_value_num' : $orderby,
|
||||
'order' => $order,
|
||||
];
|
||||
|
||||
if ($orderby === 'sales') {
|
||||
$query_args['meta_key'] = '_aksrt_sales';
|
||||
}
|
||||
|
||||
// Taxonomy filters
|
||||
$tax_query = [];
|
||||
if (!empty($cat_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_cat',
|
||||
'field' => 'term_id',
|
||||
'terms' => $cat_ids,
|
||||
'operator' => $exclude_cat ? 'NOT IN' : 'IN',
|
||||
];
|
||||
}
|
||||
if (!empty($tag_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_tag',
|
||||
'field' => 'term_id',
|
||||
'terms' => $tag_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($discount_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'shop_discount',
|
||||
'field' => 'term_id',
|
||||
'terms' => $discount_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = $tax_query;
|
||||
}
|
||||
|
||||
$products = new WP_Query($query_args);
|
||||
|
||||
echo $args['before_widget'];
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
$style_class = 'aksrt-product-widget--' . $style;
|
||||
echo '<div class="aksrt-product-widget ' . esc_attr($style_class) . '" data-aksrt-product-widget>';
|
||||
|
||||
if ($products->have_posts()) {
|
||||
echo '<div class="aksrt-product-widget__grid">';
|
||||
while ($products->have_posts()) {
|
||||
$products->the_post();
|
||||
$pid = get_the_ID();
|
||||
$card = class_exists('AKSRT_Shop')
|
||||
? AKSRT_Shop::get_instance()->build_product_card_view($pid)
|
||||
: [];
|
||||
$price = (float) ($card['price'] ?? 0);
|
||||
$image = $card['image'] ?? '';
|
||||
echo '<div class="aksrt-product-widget__item">';
|
||||
if ($image) {
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '<div class="aksrt-product-widget__info">';
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
||||
echo '<div class="aksrt-product-widget__meta">';
|
||||
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
||||
if (!empty($card['sales'])) {
|
||||
echo '<span class="aksrt-product-widget__sales">' . esc_html($card['sales']) . __(' 已售', 'aksrt') . '</span>';
|
||||
}
|
||||
echo '</div></div></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
// Pagination
|
||||
if ($products->max_num_pages > 1) {
|
||||
echo '<div class="aksrt-product-widget__pagination">';
|
||||
echo paginate_links([
|
||||
'base' => add_query_arg('aksrt_product_page', '%#%'),
|
||||
'format' => '',
|
||||
'current' => $paged,
|
||||
'total' => $products->max_num_pages,
|
||||
'prev_text' => '«',
|
||||
'next_text' => '»',
|
||||
]);
|
||||
echo '</div>';
|
||||
}
|
||||
} else {
|
||||
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
echo $args['after_widget'];
|
||||
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品推荐', 'aksrt'),
|
||||
'per_page' => 8,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'cat_ids' => '',
|
||||
'tag_ids' => '',
|
||||
'discount_ids' => '',
|
||||
'exclude_cat' => false,
|
||||
'style' => 'default',
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="50" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('orderby')); ?>"><?php esc_html_e('排序:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby')); ?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="date" <?php selected($instance['orderby'], 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="sales" <?php selected($instance['orderby'], 'sales'); ?>><?php esc_html_e('销量', 'aksrt'); ?></option>
|
||||
<option value="title" <?php selected($instance['orderby'], 'title'); ?>><?php esc_html_e('标题', 'aksrt'); ?></option>
|
||||
<option value="rand" <?php selected($instance['orderby'], 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php esc_html_e('方向:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
|
||||
<option value="DESC" <?php selected($instance['order'], 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
|
||||
<option value="ASC" <?php selected($instance['order'], 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>"><?php esc_html_e('分类ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_ids')); ?>" value="<?php echo esc_attr($instance['cat_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('exclude_cat')); ?>" value="1" <?php checked($instance['exclude_cat']); ?>>
|
||||
<?php esc_html_e('排除上述分类', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>"><?php esc_html_e('标签ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('tag_ids')); ?>" value="<?php echo esc_attr($instance['tag_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>"><?php esc_html_e('优惠活动ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('discount_ids')); ?>" value="<?php echo esc_attr($instance['discount_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('列表样式:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="default" <?php selected($instance['style'], 'default'); ?>><?php esc_html_e('默认', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($instance['style'], 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
<option value="list" <?php selected($instance['style'], 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
||||
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'per_page' => max(1, min(50, (int) ($new_instance['per_page'] ?? 8))),
|
||||
'orderby' => sanitize_key($new_instance['orderby'] ?? 'date'),
|
||||
'order' => in_array(strtoupper($new_instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($new_instance['order']) : 'DESC',
|
||||
'cat_ids' => sanitize_text_field($new_instance['cat_ids'] ?? ''),
|
||||
'tag_ids' => sanitize_text_field($new_instance['tag_ids'] ?? ''),
|
||||
'discount_ids' => sanitize_text_field($new_instance['discount_ids'] ?? ''),
|
||||
'exclude_cat' => !empty($new_instance['exclude_cat']),
|
||||
'style' => sanitize_key($new_instance['style'] ?? 'default'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Horizontal swiper product widget — equivalent to zibll `zib_shop_widget_ui_oneline_product_lists`.
|
||||
*/
|
||||
class AKSRT_Widget_Product_Oneline extends AKSRT_Widget_Product_List {
|
||||
|
||||
public function __construct() {
|
||||
WP_Widget::__construct(
|
||||
'aksrt_product_oneline',
|
||||
__('横滑商品', 'aksrt'),
|
||||
['description' => __('AKSRT 单行横滑商品 Swiper 组件。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$instance['style'] = 'oneline';
|
||||
parent::widget($args, $instance);
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$instance = wp_parse_args($instance, ['style' => 'oneline']);
|
||||
parent::form($instance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Multi-tab product widget — equivalent to zibll `zib_shop_widget_ui_tab_product`.
|
||||
* Each tab has independent category/discount filter.
|
||||
*/
|
||||
class AKSRT_Widget_Product_Tab extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_tab',
|
||||
__('多TAB商品', 'aksrt'),
|
||||
['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选)。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '');
|
||||
$tabs_raw = (string) ($instance['tabs'] ?? '');
|
||||
$per_page = max(1, min(20, (int) ($instance['per_page'] ?? 8)));
|
||||
|
||||
// Parse tabs: "Tab Name|cat_ids|discount_ids\n..."
|
||||
$tabs = [];
|
||||
foreach (array_filter(explode("\n", $tabs_raw)) as $line) {
|
||||
$parts = explode('|', trim($line));
|
||||
if (count($parts) >= 1 && trim($parts[0]) !== '') {
|
||||
$tabs[] = [
|
||||
'label' => trim($parts[0]),
|
||||
'cat_ids' => trim($parts[1] ?? ''),
|
||||
'discount_ids' => trim($parts[2] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
if (empty($tabs)) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo $args['before_widget'];
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-product-tab-widget" data-aksrt-product-tab>';
|
||||
|
||||
// Tab headers
|
||||
echo '<div class="aksrt-product-tab__headers">';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$active = $i === 0 ? ' is-active' : '';
|
||||
echo '<button type="button" class="aksrt-product-tab__header' . esc_attr($active) . '" data-tab-index="' . esc_attr((string) $i) . '">';
|
||||
echo esc_html($tab['label']);
|
||||
echo '</button>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
// Tab panels
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$hidden = $i > 0 ? ' style="display:none"' : '';
|
||||
echo '<div class="aksrt-product-tab__panel" data-tab-index="' . esc_attr((string) $i) . '"' . $hidden . '>';
|
||||
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_product',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $per_page,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
];
|
||||
|
||||
$tax_query = [];
|
||||
$cat_ids = array_filter(array_map('intval', explode(',', $tab['cat_ids'])));
|
||||
$discount_ids = array_filter(array_map('intval', explode(',', $tab['discount_ids'])));
|
||||
if (!empty($cat_ids)) {
|
||||
$tax_query[] = ['taxonomy' => 'aksrt_product_cat', 'field' => 'term_id', 'terms' => $cat_ids];
|
||||
}
|
||||
if (!empty($discount_ids)) {
|
||||
$tax_query[] = ['taxonomy' => 'shop_discount', 'field' => 'term_id', 'terms' => $discount_ids];
|
||||
}
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = $tax_query;
|
||||
}
|
||||
|
||||
$products = new WP_Query($query_args);
|
||||
if ($products->have_posts()) {
|
||||
echo '<div class="aksrt-product-widget__grid">';
|
||||
while ($products->have_posts()) {
|
||||
$products->the_post();
|
||||
$pid = get_the_ID();
|
||||
$card = class_exists('AKSRT_Shop')
|
||||
? AKSRT_Shop::get_instance()->build_product_card_view($pid)
|
||||
: [];
|
||||
$price = (float) ($card['price'] ?? 0);
|
||||
$image = $card['image'] ?? '';
|
||||
echo '<div class="aksrt-product-widget__item">';
|
||||
if ($image) {
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '<div class="aksrt-product-widget__info">';
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
||||
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
||||
echo '</div></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
// Tab switching JS
|
||||
echo '<script>';
|
||||
echo '(function(){';
|
||||
echo 'var w=document.currentScript.parentElement;';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(btn){';
|
||||
echo 'btn.addEventListener("click",function(){';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(b){b.classList.remove("is-active")});';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__panel").forEach(function(p){p.style.display="none"});';
|
||||
echo 'btn.classList.add("is-active");';
|
||||
echo 'var idx=btn.getAttribute("data-tab-index");';
|
||||
echo 'var panel=w.querySelector(".aksrt-product-tab__panel[data-tab-index=\'\"+idx+\"\']");';
|
||||
echo 'if(panel)panel.style.display="";';
|
||||
echo '});';
|
||||
echo '});';
|
||||
echo '})();';
|
||||
echo '</script>';
|
||||
|
||||
echo '</div>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品精选', 'aksrt'),
|
||||
'tabs' => "热门推荐|\n最新上架|",
|
||||
'per_page' => 8,
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tabs')); ?>"><?php esc_html_e('TAB配置(每行: 名称|分类ID|优惠ID):', 'aksrt'); ?></label>
|
||||
<textarea class="widefat" rows="5" id="<?php echo esc_attr($this->get_field_id('tabs')); ?>" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>"><?php echo esc_textarea($instance['tabs']); ?></textarea>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'tabs' => sanitize_textarea_field($new_instance['tabs'] ?? ''),
|
||||
'per_page' => max(1, min(20, (int) ($new_instance['per_page'] ?? 8))),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT User Card Widget — 侧栏用户卡片
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class AKSRT_Widget_User_Card extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_user_card',
|
||||
__('用户卡片', 'aksrt'),
|
||||
['description' => __('在侧栏显示当前登录用户或指定用户的信息卡片。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$show_stats = !empty($instance['show_stats']);
|
||||
$show_actions = !empty($instance['show_actions']);
|
||||
|
||||
// 获取目标用户
|
||||
$user_id = !empty($instance['user_id']) ? (int) $instance['user_id'] : 0;
|
||||
if ($user_id <= 0 && is_user_logged_in()) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
if ($user_id <= 0) return;
|
||||
|
||||
$user = get_userdata($user_id);
|
||||
if (!$user) return;
|
||||
|
||||
// 获取用户统计
|
||||
$post_count = count_user_posts($user_id, 'post', true);
|
||||
$comment_count = get_comments(['user_id' => $user_id, 'count' => true]);
|
||||
$follower_count = 0;
|
||||
$following_count = 0;
|
||||
if (class_exists('AKSRT_Follow')) {
|
||||
$follower_count = AKSRT_Follow::get_instance()->get_follower_count($user_id);
|
||||
$following_count = AKSRT_Follow::get_instance()->get_following_count($user_id);
|
||||
}
|
||||
|
||||
// 头像
|
||||
$avatar_url = function_exists('aksrt_get_avatar_url')
|
||||
? aksrt_get_avatar_url($user_id, ['size' => 144])
|
||||
: get_avatar_url($user_id, ['size' => 144]);
|
||||
|
||||
// 用户主页链接
|
||||
$profile_url = function_exists('aksrt_get_user_home_url')
|
||||
? aksrt_get_user_home_url($user_id)
|
||||
: get_author_posts_url($user_id);
|
||||
|
||||
// 简介
|
||||
$bio = get_user_meta($user_id, 'description', true);
|
||||
if (!$bio) $bio = $user->description ?: '';
|
||||
|
||||
echo $args['before_widget'];
|
||||
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-user-card">';
|
||||
|
||||
// 头像
|
||||
echo '<div class="aksrt-user-card__avatar">';
|
||||
echo '<a href="' . esc_url($profile_url) . '">';
|
||||
echo '<img src="' . esc_url($avatar_url) . '" alt="' . esc_attr($user->display_name) . '"' . (function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('avatar') : ' loading="lazy"') . '>';
|
||||
echo '</a>';
|
||||
echo '</div>';
|
||||
|
||||
// 昵称
|
||||
echo '<h4 class="aksrt-user-card__name">';
|
||||
echo '<a href="' . esc_url($profile_url) . '">' . esc_html($user->display_name) . '</a>';
|
||||
echo '</h4>';
|
||||
|
||||
// 简介
|
||||
if ($bio) {
|
||||
echo '<p class="aksrt-user-card__bio">' . esc_html(wp_trim_words($bio, 20)) . '</p>';
|
||||
}
|
||||
|
||||
// 统计
|
||||
if ($show_stats) {
|
||||
echo '<div class="aksrt-user-card__stats">';
|
||||
echo '<div class="aksrt-user-card__stat"><span class="aksrt-user-card__stat-value">' . number_format_i18n($post_count) . '</span><span class="aksrt-user-card__stat-label">' . esc_html__('文章', 'aksrt') . '</span></div>';
|
||||
echo '<div class="aksrt-user-card__stat"><span class="aksrt-user-card__stat-value">' . number_format_i18n($follower_count) . '</span><span class="aksrt-user-card__stat-label">' . esc_html__('粉丝', 'aksrt') . '</span></div>';
|
||||
echo '<div class="aksrt-user-card__stat"><span class="aksrt-user-card__stat-value">' . number_format_i18n($following_count) . '</span><span class="aksrt-user-card__stat-label">' . esc_html__('关注', 'aksrt') . '</span></div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// 操作按钮
|
||||
if ($show_actions) {
|
||||
echo '<div class="aksrt-user-card__actions">';
|
||||
if (is_user_logged_in() && (int) get_current_user_id() === $user_id) {
|
||||
echo '<a href="' . esc_url($profile_url) . '" class="aksrt-user-card__btn aksrt-user-card__btn--primary">' . esc_html__('个人主页', 'aksrt') . '</a>';
|
||||
if (function_exists('aksrt_get_user_center_url')) {
|
||||
echo '<a href="' . esc_url(aksrt_get_user_center_url()) . '" class="aksrt-user-card__btn aksrt-user-card__btn--ghost">' . esc_html__('设置', 'aksrt') . '</a>';
|
||||
}
|
||||
} else {
|
||||
echo '<a href="' . esc_url($profile_url) . '" class="aksrt-user-card__btn aksrt-user-card__btn--primary">' . esc_html__('查看主页', 'aksrt') . '</a>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$user_id = !empty($instance['user_id']) ? (int) $instance['user_id'] : 0;
|
||||
$show_stats = !empty($instance['show_stats']);
|
||||
$show_actions = !empty($instance['show_actions']);
|
||||
?>
|
||||
<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('user_id')); ?>"><?php esc_html_e('用户 ID(0 = 当前登录用户):', 'aksrt'); ?></label>
|
||||
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('user_id')); ?>" name="<?php echo esc_attr($this->get_field_name('user_id')); ?>" type="number" value="<?php echo esc_attr((string) $user_id); ?>" min="0">
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" id="<?php echo esc_attr($this->get_field_id('show_stats')); ?>" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked($show_stats); ?>>
|
||||
<?php esc_html_e('显示统计(文章、粉丝、关注)', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" id="<?php echo esc_attr($this->get_field_id('show_actions')); ?>" name="<?php echo esc_attr($this->get_field_name('show_actions')); ?>" value="1" <?php checked($show_actions); ?>>
|
||||
<?php esc_html_e('显示操作按钮', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['user_id'] = (int) ($new_instance['user_id'] ?? 0);
|
||||
$instance['show_stats'] = !empty($new_instance['show_stats']);
|
||||
$instance['show_actions'] = !empty($new_instance['show_actions']);
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT User Ranking Widget — 用户排行榜
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class AKSRT_Widget_User_Ranking extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_user_ranking',
|
||||
__('用户排行榜', 'aksrt'),
|
||||
['description' => __('按积分、文章数或粉丝数排行的用户列表。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
|
||||
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
|
||||
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
|
||||
$show_avatar = !empty($instance['show_avatar']);
|
||||
|
||||
$users = $this->get_ranking($rank_by, $count);
|
||||
if (empty($users)) return;
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
|
||||
echo '<ul class="aksrt-user-ranking">';
|
||||
$rank = 0;
|
||||
foreach ($users as $user) {
|
||||
$rank++;
|
||||
$rank_class = $rank <= 3 ? ' aksrt-user-ranking__item--top' : '';
|
||||
$user_url = function_exists('aksrt_get_user_home_url')
|
||||
? aksrt_get_user_home_url($user['id'])
|
||||
: get_author_posts_url($user['id']);
|
||||
|
||||
echo '<li class="aksrt-user-ranking__item' . esc_attr($rank_class) . '">';
|
||||
|
||||
// 排名
|
||||
echo '<span class="aksrt-user-ranking__rank">' . esc_html((string) $rank) . '</span>';
|
||||
|
||||
// 头像
|
||||
if ($show_avatar) {
|
||||
$avatar_url = function_exists('aksrt_get_avatar_url')
|
||||
? aksrt_get_avatar_url($user['id'], ['size' => 36])
|
||||
: get_avatar_url($user['id'], ['size' => 36]);
|
||||
echo '<a href="' . esc_url($user_url) . '" class="aksrt-user-ranking__avatar">';
|
||||
echo '<img src="' . esc_url($avatar_url) . '" alt=""' . (function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('avatar') : ' loading="lazy"') . ' width="36" height="36">';
|
||||
echo '</a>';
|
||||
}
|
||||
|
||||
// 昵称
|
||||
echo '<a href="' . esc_url($user_url) . '" class="aksrt-user-ranking__name">' . esc_html($user['name']) . '</a>';
|
||||
|
||||
// 数值
|
||||
echo '<span class="aksrt-user-ranking__value">' . esc_html($user['value']) . '</span>';
|
||||
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
private function get_ranking(string $rank_by, int $count): array {
|
||||
$users = [];
|
||||
|
||||
switch ($rank_by) {
|
||||
case 'credits':
|
||||
global $wpdb;
|
||||
$rows = $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT user_id, meta_value as credits FROM {$wpdb->usermeta}
|
||||
WHERE meta_key = 'aksrt_credits'
|
||||
ORDER BY CAST(meta_value AS UNSIGNED) DESC
|
||||
LIMIT %d",
|
||||
$count
|
||||
), ARRAY_A);
|
||||
foreach ((array) $rows as $row) {
|
||||
$user = get_userdata((int) $row['user_id']);
|
||||
if ($user) {
|
||||
$users[] = [
|
||||
'id' => (int) $row['user_id'],
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['credits']) . ' 积分',
|
||||
];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'posts':
|
||||
$blog_users = get_users([
|
||||
'number' => $count,
|
||||
'orderby' => 'post_count',
|
||||
'order' => 'DESC',
|
||||
'who' => 'authors',
|
||||
]);
|
||||
foreach ($blog_users as $user) {
|
||||
$post_count = count_user_posts($user->ID, 'post', true);
|
||||
if ($post_count > 0) {
|
||||
$users[] = [
|
||||
'id' => $user->ID,
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n($post_count) . ' 篇',
|
||||
];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'followers':
|
||||
if (class_exists('AKSRT_Follow')) {
|
||||
global $wpdb;
|
||||
$follow_table = AKSRT_DB_Migrator::table('follow');
|
||||
if ($follow_table) {
|
||||
$rows = $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT following_id, COUNT(*) as cnt
|
||||
FROM {$follow_table}
|
||||
GROUP BY following_id
|
||||
ORDER BY cnt DESC
|
||||
LIMIT %d",
|
||||
$count
|
||||
), ARRAY_A);
|
||||
foreach ((array) $rows as $row) {
|
||||
$user = get_userdata((int) $row['following_id']);
|
||||
if ($user) {
|
||||
$users[] = [
|
||||
'id' => (int) $row['following_id'],
|
||||
'name' => $user->display_name,
|
||||
'value' => number_format_i18n((int) $row['cnt']) . ' 粉丝',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
|
||||
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
|
||||
$count = !empty($instance['count']) ? (int) $instance['count'] : 5;
|
||||
$show_avatar = !empty($instance['show_avatar']);
|
||||
?>
|
||||
<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('rank_by')); ?>"><?php esc_html_e('排行依据:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('rank_by')); ?>" name="<?php echo esc_attr($this->get_field_name('rank_by')); ?>">
|
||||
<option value="credits" <?php selected($rank_by, 'credits'); ?>><?php esc_html_e('积分', 'aksrt'); ?></option>
|
||||
<option value="posts" <?php selected($rank_by, 'posts'); ?>><?php esc_html_e('文章数', 'aksrt'); ?></option>
|
||||
<option value="followers" <?php selected($rank_by, 'followers'); ?>><?php esc_html_e('粉丝数', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('count')); ?>"><?php esc_html_e('显示数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('count')); ?>" name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" value="<?php echo esc_attr((string) $count); ?>" min="1" max="20">
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" id="<?php echo esc_attr($this->get_field_id('show_avatar')); ?>" name="<?php echo esc_attr($this->get_field_name('show_avatar')); ?>" value="1" <?php checked($show_avatar); ?>>
|
||||
<?php esc_html_e('显示头像', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['rank_by'] = in_array($new_instance['rank_by'] ?? '', ['credits', 'posts', 'followers'], true) ? $new_instance['rank_by'] : 'credits';
|
||||
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
|
||||
$instance['show_avatar'] = !empty($new_instance['show_avatar']);
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* AKSRT Video Player Widget — 视频播放器
|
||||
*
|
||||
* 支持 MP4、YouTube、Bilibili 等视频源。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class AKSRT_Widget_Video_Player extends WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_video_player',
|
||||
__('视频播放器', 'aksrt'),
|
||||
['description' => __('在侧栏嵌入视频播放器。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$video_url = !empty($instance['video_url']) ? $instance['video_url'] : '';
|
||||
$poster = !empty($instance['poster']) ? $instance['poster'] : '';
|
||||
$autoplay = !empty($instance['autoplay']);
|
||||
$aspect_ratio = !empty($instance['aspect_ratio']) ? $instance['aspect_ratio'] : '16:9';
|
||||
|
||||
if (empty($video_url)) return;
|
||||
|
||||
// 解析视频类型
|
||||
$embed_url = $this->get_embed_url($video_url);
|
||||
$is_embed = !empty($embed_url);
|
||||
|
||||
echo $args['before_widget'];
|
||||
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
|
||||
// 计算 padding-bottom
|
||||
$ratio_parts = explode(':', $aspect_ratio);
|
||||
if (count($ratio_parts) === 2 && (int) $ratio_parts[1] > 0) {
|
||||
$padding = ((int) $ratio_parts[1] / (int) $ratio_parts[0]) * 100;
|
||||
} else {
|
||||
$padding = 56.25; // 16:9
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-video-widget" style="padding-bottom: ' . esc_attr(number_format($padding, 4)) . '%;">';
|
||||
|
||||
if ($is_embed) {
|
||||
echo '<iframe src="' . esc_url($embed_url) . '" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen' . (function_exists('aksrt_lazy_loading') ? aksrt_lazy_loading('widget') : ' loading="lazy"') . '></iframe>';
|
||||
} else {
|
||||
echo '<video' . ($autoplay ? ' autoplay' : '') . ' controls playsinline';
|
||||
if ($poster) echo ' poster="' . esc_url($poster) . '"';
|
||||
echo '>';
|
||||
echo '<source src="' . esc_url($video_url) . '">';
|
||||
echo '</video>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 YouTube / Bilibili 链接为嵌入 URL
|
||||
*/
|
||||
private function get_embed_url(string $url): string {
|
||||
// YouTube
|
||||
if (preg_match('/(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)/', $url, $m)) {
|
||||
return 'https://www.youtube.com/embed/' . $m[1];
|
||||
}
|
||||
|
||||
// Bilibili
|
||||
if (preg_match('/bilibili\.com\/video\/(BV[a-zA-Z0-9]+)/', $url, $m)) {
|
||||
return 'https://player.bilibili.com/player.html?bvid=' . $m[1] . '&high_quality=1';
|
||||
}
|
||||
|
||||
// 腾讯视频
|
||||
if (preg_match('/v\.qq\.com\/x\/page\/([a-zA-Z0-9]+)/', $url, $m)) {
|
||||
return 'https://v.qq.com/txp/iframe/player.html?vid=' . $m[1];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$title = !empty($instance['title']) ? $instance['title'] : '';
|
||||
$video_url = !empty($instance['video_url']) ? $instance['video_url'] : '';
|
||||
$poster = !empty($instance['poster']) ? $instance['poster'] : '';
|
||||
$autoplay = !empty($instance['autoplay']);
|
||||
$aspect_ratio = !empty($instance['aspect_ratio']) ? $instance['aspect_ratio'] : '16:9';
|
||||
?>
|
||||
<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('video_url')); ?>"><?php esc_html_e('视频 URL:', 'aksrt'); ?></label>
|
||||
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('video_url')); ?>" name="<?php echo esc_attr($this->get_field_name('video_url')); ?>" type="url" value="<?php echo esc_attr($video_url); ?>" placeholder="https://...">
|
||||
<small><?php esc_html_e('支持 MP4、YouTube、Bilibili、腾讯视频链接', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('poster')); ?>"><?php esc_html_e('封面图 URL:', 'aksrt'); ?></label>
|
||||
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('poster')); ?>" name="<?php echo esc_attr($this->get_field_name('poster')); ?>" type="url" value="<?php echo esc_attr($poster); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('aspect_ratio')); ?>"><?php esc_html_e('宽高比:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('aspect_ratio')); ?>" name="<?php echo esc_attr($this->get_field_name('aspect_ratio')); ?>">
|
||||
<option value="16:9" <?php selected($aspect_ratio, '16:9'); ?>>16:9</option>
|
||||
<option value="4:3" <?php selected($aspect_ratio, '4:3'); ?>>4:3</option>
|
||||
<option value="1:1" <?php selected($aspect_ratio, '1:1'); ?>>1:1</option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" id="<?php echo esc_attr($this->get_field_id('autoplay')); ?>" name="<?php echo esc_attr($this->get_field_name('autoplay')); ?>" value="1" <?php checked($autoplay); ?>>
|
||||
<?php esc_html_e('自动播放(仅 MP4)', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$instance = [];
|
||||
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
|
||||
$instance['video_url'] = esc_url_raw($new_instance['video_url'] ?? '');
|
||||
$instance['poster'] = esc_url_raw($new_instance['poster'] ?? '');
|
||||
$instance['autoplay'] = !empty($new_instance['autoplay']);
|
||||
$instance['aspect_ratio'] = in_array($new_instance['aspect_ratio'] ?? '', ['16:9', '4:3', '1:1'], true) ? $new_instance['aspect_ratio'] : '16:9';
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user