468 lines
22 KiB
PHP
468 lines
22 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT 新增小工具 — v4.0 Zibll-Inspired
|
|
*
|
|
* 新增:
|
|
* 1. AKSRT_Hot_Posts_Widget — 热榜文章(按浏览/评论/点赞排序)
|
|
* 2. AKSRT_Tab_Posts_Widget — 多栏目 Tab 切换文章列表
|
|
*
|
|
* @package AKSRT
|
|
* @version 4.0.0
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
// ============================================================
|
|
// 1. 热榜文章小工具
|
|
// ============================================================
|
|
|
|
class AKSRT_Hot_Posts_Widget extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_hot_posts',
|
|
esc_html__('AKSRT 热榜文章', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('按浏览量/评论数/点赞数排序的热门文章列表', 'aksrt'),
|
|
'customize_selective_refresh' => true,
|
|
]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
echo $args['before_widget'];
|
|
|
|
$title = apply_filters('widget_title', $instance['title'] ?? __('热门文章', 'aksrt'), $instance, $this->id_base);
|
|
$count = absint($instance['count'] ?? 5);
|
|
$order_by = $instance['order_by'] ?? 'views';
|
|
$days = absint($instance['days'] ?? 30);
|
|
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
|
}
|
|
|
|
$query_args = [
|
|
'post_type' => 'post',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => $count,
|
|
'ignore_sticky_posts' => true,
|
|
'no_found_rows' => true,
|
|
];
|
|
|
|
if ($days > 0 && $days < 365) {
|
|
$query_args['date_query'] = [['after' => $days . ' days ago']];
|
|
}
|
|
|
|
switch ($order_by) {
|
|
case 'comments':
|
|
$query_args['orderby'] = 'comment_count';
|
|
$query_args['order'] = 'DESC';
|
|
break;
|
|
case 'likes':
|
|
$query_args['meta_key'] = '_aksrt_like_count';
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
$query_args['order'] = 'DESC';
|
|
break;
|
|
case 'favorites':
|
|
$query_args['meta_key'] = '_aksrt_favorite_count';
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
$query_args['order'] = 'DESC';
|
|
break;
|
|
case 'downloads':
|
|
$query_args['meta_key'] = '_aksrt_res_download_count';
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
$query_args['order'] = 'DESC';
|
|
break;
|
|
case 'views':
|
|
default:
|
|
$query_args['meta_key'] = 'aksrt_post_views';
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
$query_args['order'] = 'DESC';
|
|
break;
|
|
}
|
|
|
|
$query = new WP_Query($query_args);
|
|
|
|
if ($query->have_posts()) {
|
|
echo '<div class="widget-hot-posts widget-hot-posts--mini">';
|
|
$index = 0;
|
|
while ($query->have_posts()) {
|
|
$query->the_post();
|
|
$index++;
|
|
$rank_class = $index <= 3 ? ' rank-top' : '';
|
|
$views = (int) get_post_meta(get_the_ID(), 'aksrt_post_views', true);
|
|
?>
|
|
<div class="hot-post-item <?php echo esc_attr($rank_class); ?>">
|
|
<span class="hot-post-item__rank"><?php echo esc_html($index); ?></span>
|
|
<div class="hot-post-item__info">
|
|
<a href="<?php the_permalink(); ?>" class="hot-post-item__title"><?php the_title(); ?></a>
|
|
<div class="hot-post-item__meta">
|
|
<span><i class="fas fa-eye" aria-hidden="true"></i> <?php echo esc_html(function_exists('aksrt_format_number') ? aksrt_format_number($views) : (string) $views); ?></span>
|
|
<span class="hot-post-item__time"><?php echo esc_html(human_time_diff(get_the_time('U'), current_time('timestamp')) . __('前', 'aksrt')); ?></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
echo '</div>';
|
|
wp_reset_postdata();
|
|
} else {
|
|
echo '<p class="widget-empty">' . esc_html__('暂无热门文章', 'aksrt') . '</p>';
|
|
}
|
|
|
|
echo $args['after_widget'];
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$title = $instance['title'] ?? __('热门文章', 'aksrt');
|
|
$count = (int) ($instance['count'] ?? 5);
|
|
$order_by = $instance['order_by'] ?? 'views';
|
|
$days = (int) ($instance['days'] ?? 30);
|
|
?>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
|
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('count'); ?>"><?php esc_html_e('显示数量:', 'aksrt'); ?></label>
|
|
<input class="tiny-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" min="1" max="20" value="<?php echo $count; ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('order_by'); ?>"><?php esc_html_e('排序方式:', 'aksrt'); ?></label>
|
|
<select class="widefat" id="<?php echo $this->get_field_id('order_by'); ?>" name="<?php echo $this->get_field_name('order_by'); ?>">
|
|
<option value="views" <?php selected($order_by, 'views'); ?>><?php esc_html_e('浏览量', 'aksrt'); ?></option>
|
|
<option value="comments" <?php selected($order_by, 'comments'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
|
|
<option value="likes" <?php selected($order_by, 'likes'); ?>><?php esc_html_e('点赞数', 'aksrt'); ?></option>
|
|
<option value="favorites" <?php selected($order_by, 'favorites'); ?>><?php esc_html_e('收藏数', 'aksrt'); ?></option>
|
|
<option value="downloads" <?php selected($order_by, 'downloads'); ?>><?php esc_html_e('下载数', 'aksrt'); ?></option>
|
|
</select>
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('days'); ?>"><?php esc_html_e('时间范围(天,0=不限):', 'aksrt'); ?></label>
|
|
<input class="tiny-text" id="<?php echo $this->get_field_id('days'); ?>" name="<?php echo $this->get_field_name('days'); ?>" type="number" min="0" max="365" value="<?php echo $days; ?>">
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
return [
|
|
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
|
'count' => absint($new_instance['count'] ?? 5),
|
|
'order_by' => in_array($new_instance['order_by'] ?? '', ['views', 'comments', 'likes', 'favorites', 'downloads'], true) ? $new_instance['order_by'] : 'views',
|
|
'days' => absint($new_instance['days'] ?? 30),
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
// ============================================================
|
|
// 2. 多栏目 Tab 切换文章列表小工具
|
|
// ============================================================
|
|
|
|
class AKSRT_Tab_Posts_Widget extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_tab_posts',
|
|
esc_html__('AKSRT 多栏目文章', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('Tab 切换显示不同分类的文章列表', 'aksrt'),
|
|
'customize_selective_refresh' => true,
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Normalize tabs from legacy comma-separated string or structured array.
|
|
*
|
|
* @param mixed $tabs Raw tabs data.
|
|
* @return array Normalized structured tabs.
|
|
*/
|
|
private function normalize_tabs($tabs): array {
|
|
// Legacy comma-separated format
|
|
if (is_string($tabs) && $tabs !== '') {
|
|
$legacy_keys = array_map('trim', explode(',', $tabs));
|
|
$legacy_defs = [
|
|
'latest' => ['title' => '最新', 'orderby' => 'date'],
|
|
'comments' => ['title' => '热评', 'orderby' => 'comments'],
|
|
'random' => ['title' => '随机', 'orderby' => 'rand'],
|
|
'views' => ['title' => '热门', 'orderby' => 'views'],
|
|
];
|
|
$result = [];
|
|
foreach ($legacy_keys as $key) {
|
|
if (isset($legacy_defs[$key])) {
|
|
$result[] = [
|
|
'title' => __($legacy_defs[$key]['title'], 'aksrt'),
|
|
'category_id' => 0,
|
|
'orderby' => $legacy_defs[$key]['orderby'],
|
|
'count' => 0,
|
|
'enabled' => true,
|
|
];
|
|
} elseif (is_numeric($key) && absint($key) > 0) {
|
|
$term = get_term(absint($key), 'category');
|
|
$result[] = [
|
|
'title' => ($term && !is_wp_error($term)) ? $term->name : '',
|
|
'category_id' => absint($key),
|
|
'orderby' => 'date',
|
|
'count' => 0,
|
|
'enabled' => true,
|
|
];
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
// Structured array format
|
|
if (is_array($tabs)) {
|
|
$allowed_orderby = ['date', 'modified', 'views', 'likes', 'comments', 'favorites', 'downloads', 'rand'];
|
|
$result = [];
|
|
foreach ($tabs as $tab) {
|
|
if (!is_array($tab)) continue;
|
|
$orderby = sanitize_key($tab['orderby'] ?? 'date');
|
|
if (!in_array($orderby, $allowed_orderby, true)) $orderby = 'date';
|
|
$result[] = [
|
|
'title' => sanitize_text_field($tab['title'] ?? ''),
|
|
'category_id' => absint($tab['category_id'] ?? 0),
|
|
'orderby' => $orderby,
|
|
'count' => max(0, min(20, (int) ($tab['count'] ?? 0))),
|
|
'enabled' => !empty($tab['enabled']),
|
|
];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Build WP_Query args for a tab's orderby.
|
|
*/
|
|
private function build_tab_query_args(array $tab, int $default_count): array {
|
|
$count = $tab['count'] > 0 ? $tab['count'] : $default_count;
|
|
$args = [
|
|
'post_type' => 'post',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => $count,
|
|
'ignore_sticky_posts' => true,
|
|
'no_found_rows' => true,
|
|
];
|
|
|
|
if ($tab['category_id'] > 0) {
|
|
$args['cat'] = $tab['category_id'];
|
|
}
|
|
|
|
switch ($tab['orderby']) {
|
|
case 'views':
|
|
$args['meta_key'] = 'aksrt_post_views';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'likes':
|
|
$args['meta_key'] = '_aksrt_like_count';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'favorites':
|
|
$args['meta_key'] = '_aksrt_favorite_count';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'downloads':
|
|
$args['meta_key'] = '_aksrt_res_download_count';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'comments':
|
|
$args['orderby'] = 'comment_count';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'rand':
|
|
$args['orderby'] = 'rand';
|
|
break;
|
|
case 'modified':
|
|
$args['orderby'] = 'modified';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'date':
|
|
default:
|
|
$args['orderby'] = 'date';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
}
|
|
|
|
return $args;
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
echo $args['before_widget'];
|
|
|
|
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
|
$count = absint($instance['count'] ?? 5);
|
|
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
|
|
|
// Filter enabled tabs
|
|
$tabs = array_filter($tabs, fn($t) => !empty($t['enabled']));
|
|
if (empty($tabs)) {
|
|
echo $args['after_widget'];
|
|
return;
|
|
}
|
|
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
|
}
|
|
|
|
$widget_id = $this->id;
|
|
$use_shared = function_exists('aksrt_render_mini_list');
|
|
?>
|
|
<div class="widget-tab-posts" id="<?php echo esc_attr($widget_id); ?>-tabs">
|
|
<!-- Tab 导航 -->
|
|
<ul class="widget-tab-posts__nav" role="tablist">
|
|
<?php $first = true; foreach ($tabs as $i => $tab) :
|
|
$tab_key = 'tab-' . $i;
|
|
$tab_title = $tab['title'] !== '' ? $tab['title'] : __('Tab ' . ($i + 1), 'aksrt');
|
|
?>
|
|
<li role="presentation">
|
|
<button type="button" role="tab"
|
|
class="widget-tab-posts__tab<?php echo $first ? ' active' : ''; ?>"
|
|
data-tab="<?php echo esc_attr($tab_key); ?>"
|
|
aria-selected="<?php echo $first ? 'true' : 'false'; ?>"
|
|
><?php echo esc_html($tab_title); ?></button>
|
|
</li>
|
|
<?php $first = false; endforeach; ?>
|
|
</ul>
|
|
|
|
<!-- Tab 内容 -->
|
|
<?php $first = true; foreach ($tabs as $i => $tab) :
|
|
$tab_key = 'tab-' . $i;
|
|
$q = new WP_Query($this->build_tab_query_args($tab, $count));
|
|
?>
|
|
<div class="widget-tab-posts__panel<?php echo $first ? ' active' : ''; ?>" data-tab-panel="<?php echo esc_attr($tab_key); ?>" role="tabpanel"<?php echo $first ? '' : ' hidden'; ?>>
|
|
<?php if ($q->have_posts()) : ?>
|
|
<?php if ($use_shared) : ?>
|
|
<div class="widget-tab-posts__list widget-tab-posts__list--mini">
|
|
<?php aksrt_render_mini_list($q, ['context' => 'sidebar', 'show_thumb' => true, 'show_meta' => true], true); ?>
|
|
</div>
|
|
<?php else : ?>
|
|
<ul class="widget-tab-posts__list">
|
|
<?php while ($q->have_posts()) : $q->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' => function_exists('aksrt_is_lazyload_enabled') && aksrt_is_lazyload_enabled('widget') ? 'lazy' : false, '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 endwhile; ?>
|
|
</ul>
|
|
<?php endif; ?>
|
|
<?php else : ?>
|
|
<p class="widget-empty"><?php esc_html_e('暂无文章', 'aksrt'); ?></p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php wp_reset_postdata(); $first = false; endforeach; ?>
|
|
</div>
|
|
<?php
|
|
|
|
echo $args['after_widget'];
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$title = $instance['title'] ?? '';
|
|
$count = (int) ($instance['count'] ?? 5);
|
|
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
|
$orderby_options = [
|
|
'date' => __('最新', 'aksrt'),
|
|
'modified' => __('更新', 'aksrt'),
|
|
'views' => __('浏览', 'aksrt'),
|
|
'likes' => __('点赞', 'aksrt'),
|
|
'comments' => __('评论', 'aksrt'),
|
|
'favorites' => __('收藏', 'aksrt'),
|
|
'downloads' => __('下载', 'aksrt'),
|
|
'rand' => __('随机', 'aksrt'),
|
|
];
|
|
?>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
|
|
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo $this->get_field_id('count'); ?>"><?php esc_html_e('默认每个 Tab 显示数量:', 'aksrt'); ?></label>
|
|
<input class="tiny-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" min="1" max="20" value="<?php echo $count; ?>">
|
|
</p>
|
|
<hr style="margin:12px 0;">
|
|
<p style="font-weight:600;margin-bottom:8px;"><?php esc_html_e('Tab 配置', 'aksrt'); ?></p>
|
|
<?php
|
|
// Render each tab as a mini-form
|
|
$max_tabs = 8;
|
|
$tabs = array_slice($tabs, 0, $max_tabs);
|
|
// Ensure at least 3 slots
|
|
while (count($tabs) < 3) {
|
|
$tabs[] = ['title' => '', 'category_id' => 0, 'orderby' => 'date', 'count' => 0, 'enabled' => false];
|
|
}
|
|
foreach ($tabs as $i => $tab) :
|
|
?>
|
|
<div style="border:1px solid #ddd;padding:8px;margin-bottom:8px;border-radius:4px;background:#fafafa;">
|
|
<p style="margin:0 0 6px;">
|
|
<label><input type="checkbox" name="<?php echo $this->get_field_name("tabs[{$i}][enabled]"); ?>" value="1" <?php checked(!empty($tab['enabled'])); ?>> <?php printf(esc_html__('Tab %d 启用', 'aksrt'), $i + 1); ?></label>
|
|
</p>
|
|
<p style="margin:0 0 6px;">
|
|
<label><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" name="<?php echo $this->get_field_name("tabs[{$i}][title]"); ?>" value="<?php echo esc_attr($tab['title']); ?>" placeholder="<?php esc_attr_e('留空则使用分类名', 'aksrt'); ?>">
|
|
</p>
|
|
<p style="margin:0 0 6px;">
|
|
<label><?php esc_html_e('分类 ID:', 'aksrt'); ?></label>
|
|
<input class="tiny-text" type="number" min="0" name="<?php echo $this->get_field_name("tabs[{$i}][category_id]"); ?>" value="<?php echo (int) $tab['category_id']; ?>" placeholder="0">
|
|
</p>
|
|
<p style="margin:0 0 6px;">
|
|
<label><?php esc_html_e('排序:', 'aksrt'); ?></label>
|
|
<select class="widefat" name="<?php echo $this->get_field_name("tabs[{$i}][orderby]"); ?>">
|
|
<?php foreach ($orderby_options as $val => $label) : ?>
|
|
<option value="<?php echo esc_attr($val); ?>" <?php selected($tab['orderby'], $val); ?>><?php echo esc_html($label); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</p>
|
|
<p style="margin:0;">
|
|
<label><?php esc_html_e('数量(0=使用默认):', 'aksrt'); ?></label>
|
|
<input class="tiny-text" type="number" min="0" max="20" name="<?php echo $this->get_field_name("tabs[{$i}][count]"); ?>" value="<?php echo (int) $tab['count']; ?>">
|
|
</p>
|
|
</div>
|
|
<?php endforeach;
|
|
// Render hidden fields for any remaining tabs to preserve data
|
|
for ($i = count($tabs); $i < $max_tabs; $i++) :
|
|
?>
|
|
<input type="hidden" name="<?php echo $this->get_field_name("tabs[{$i}][enabled]"); ?>" value="">
|
|
<?php endfor;
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
$result = [
|
|
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
|
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
|
|
];
|
|
|
|
$tabs = [];
|
|
if (isset($new_instance['tabs']) && is_array($new_instance['tabs'])) {
|
|
$allowed_orderby = ['date', 'modified', 'views', 'likes', 'comments', 'favorites', 'downloads', 'rand'];
|
|
foreach ($new_instance['tabs'] as $tab) {
|
|
if (!is_array($tab)) continue;
|
|
$orderby = sanitize_key($tab['orderby'] ?? 'date');
|
|
if (!in_array($orderby, $allowed_orderby, true)) $orderby = 'date';
|
|
$tabs[] = [
|
|
'title' => sanitize_text_field($tab['title'] ?? ''),
|
|
'category_id' => absint($tab['category_id'] ?? 0),
|
|
'orderby' => $orderby,
|
|
'count' => max(0, min(20, (int) ($tab['count'] ?? 0))),
|
|
'enabled' => !empty($tab['enabled']),
|
|
];
|
|
}
|
|
}
|
|
|
|
$result['tabs'] = !empty($tabs) ? $tabs : sanitize_text_field($new_instance['tabs'] ?? 'latest,comments,random');
|
|
return $result;
|
|
}
|
|
}
|