Merge pull request 'Worktree ux depth batch7' (#225) from worktree-ux-depth-batch7 into main

Reviewed-on: CYL/wordpress-theme-aksrt#225
This commit is contained in:
2026-07-20 21:43:41 +08:00
11 changed files with 903 additions and 392 deletions
+34
View File
@@ -17,6 +17,39 @@
var tabs = widget.querySelectorAll('.widget-tab-posts__tab');
var panels = widget.querySelectorAll('.widget-tab-posts__panel');
if (!tabs.length || !panels.length) return;
var rest = widget.getAttribute('data-rest') || '';
var defaultCount = widget.getAttribute('data-count') || '5';
function loadPanel(panel, tab) {
if (!panel || panel.getAttribute('data-loaded') === '1' || !rest) return;
var orderby = tab.getAttribute('data-orderby') || 'date';
var cat = tab.getAttribute('data-cat') || '0';
var tabCount = tab.getAttribute('data-tab-count') || '0';
var qs = new URLSearchParams({
count: defaultCount,
orderby: orderby,
cat: cat,
tab_count: tabCount,
});
var url = rest + (rest.indexOf('?') > -1 ? '&' : '?') + qs.toString();
panel.classList.add('is-ajax-loading');
var headers = { Accept: 'application/json' };
if (window.aksrtData && aksrtData.restNonce) {
headers['X-WP-Nonce'] = aksrtData.restNonce;
}
fetch(url, { credentials: 'same-origin', headers: headers })
.then(function (r) { return r.json(); })
.then(function (j) {
var d = (j && j.data) || j || {};
var html = d.html || '';
panel.innerHTML = html || '<p class="widget-empty">暂无文章</p>';
panel.setAttribute('data-loaded', '1');
panel.classList.remove('is-ajax-loading');
})
.catch(function () {
panel.classList.remove('is-ajax-loading');
});
}
tabs.forEach(function (tab) {
tab.addEventListener('click', function () {
@@ -33,6 +66,7 @@
panel.classList.toggle('active', active);
if (active) {
panel.removeAttribute('hidden');
loadPanel(panel, tab);
} else {
panel.setAttribute('hidden', '');
}
+4
View File
@@ -234,6 +234,10 @@ if (class_exists('\AKSRT\Core\DocsPresenter')) {
if (class_exists('\AKSRT\Core\HomeModules')) {
\AKSRT\Core\HomeModules::get_instance()->init();
}
// Public widget list REST (posts / tab-posts ajax)
if (class_exists('\AKSRT\Core\WidgetAjax')) {
\AKSRT\Core\WidgetAjax::get_instance()->init();
}
if (class_exists('\AKSRT\Content\Visibility')) {
\AKSRT\Content\Visibility::get_instance()->init();
}
+158
View File
@@ -0,0 +1,158 @@
<?php
/**
* Public widget list REST endpoints (posts / tab-posts).
*
* @package AKSRT
*/
namespace AKSRT\Core;
defined('ABSPATH') || exit;
class WidgetAjax {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
add_action('rest_api_init', [$this, 'register_routes']);
}
public function register_routes(): void {
register_rest_route('aksrt/v1', '/widgets/posts', [
'methods' => 'GET',
'callback' => [$this, 'rest_main_posts'],
'permission_callback' => '__return_true',
]);
register_rest_route('aksrt/v1', '/widgets/tab-posts', [
'methods' => 'GET',
'callback' => [$this, 'rest_tab_posts'],
'permission_callback' => '__return_true',
]);
register_rest_route('aksrt/v1', '/widgets/paid-posts', [
'methods' => 'GET',
'callback' => [$this, 'rest_paid_posts'],
'permission_callback' => '__return_true',
]);
register_rest_route('aksrt/v1', '/widgets/category-tabs', [
'methods' => 'GET',
'callback' => [$this, 'rest_category_tabs'],
'permission_callback' => '__return_true',
]);
}
public function rest_main_posts(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\WidgetMainPosts')) {
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
}
$instance = [
'count' => max(1, min(30, (int) ($request->get_param('count') ?? 6))),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
'cat' => max(0, (int) ($request->get_param('cat') ?? 0)),
'limit_day' => max(0, (int) ($request->get_param('limit_day') ?? 0)),
'style' => sanitize_key((string) ($request->get_param('style') ?? 'list')),
'show_thumb' => $request->get_param('show_thumb') === null ? 1 : (int) $request->get_param('show_thumb'),
'show_meta' => $request->get_param('show_meta') === null ? 1 : (int) $request->get_param('show_meta'),
'show_number' => (int) ($request->get_param('show_number') ?? 0),
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
];
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
$payload = \AKSRT\Widgets\WidgetMainPosts::query_payload($instance, $page);
return new \WP_REST_Response([
'success' => true,
'html' => (string) ($payload['html'] ?? ''),
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
'has_more' => !empty($payload['has_more']),
'page' => (int) ($payload['page'] ?? $page),
'max_pages' => (int) ($payload['max_pages'] ?? 1),
'total' => (int) ($payload['total'] ?? 0),
], 200);
}
public function rest_tab_posts(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\TabPostsWidget')) {
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
}
$default_count = max(1, min(20, (int) ($request->get_param('count') ?? 5)));
$tab = [
'title' => '',
'category_id' => max(0, (int) ($request->get_param('cat') ?? $request->get_param('category_id') ?? 0)),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
'count' => max(0, min(20, (int) ($request->get_param('tab_count') ?? 0))),
'enabled' => true,
];
$html = \AKSRT\Widgets\TabPostsWidget::render_tab_html($tab, $default_count);
if ($html === '') {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
} else {
$html = '<div class="widget-tab-posts__list widget-tab-posts__list--mini aksrt-main-posts style-mini">' . $html . '</div>';
}
return new \WP_REST_Response([
'success' => true,
'html' => $html,
], 200);
}
public function rest_paid_posts(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\WidgetPaidPosts')) {
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
}
$instance = [
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 6))),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
];
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
$payload = \AKSRT\Widgets\WidgetPaidPosts::query_payload($instance, $page);
return new \WP_REST_Response([
'success' => true,
'html' => (string) ($payload['html'] ?? ''),
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
'has_more' => !empty($payload['has_more']),
'page' => (int) ($payload['page'] ?? $page),
'max_pages' => (int) ($payload['max_pages'] ?? 1),
'total' => (int) ($payload['total'] ?? 0),
], 200);
}
public function rest_category_tabs(\WP_REST_Request $request) {
$cat = max(0, (int) ($request->get_param('cat') ?? 0));
$count = max(1, min(12, (int) ($request->get_param('count') ?? 6)));
$layout = sanitize_key((string) ($request->get_param('layout') ?? 'card'));
$columns = max(1, min(4, (int) ($request->get_param('columns') ?? 3)));
$context = sanitize_key((string) ($request->get_param('context') ?? 'homepage'));
if ($cat <= 0) {
return new \WP_REST_Response(['success' => true, 'html' => ''], 200);
}
$post_types = function_exists('aksrt_home_widget_post_types') ? aksrt_home_widget_post_types() : ['post'];
$query = new \WP_Query([
'post_type' => $post_types,
'post_status' => 'publish',
'posts_per_page' => $count,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'cat' => $cat,
]);
ob_start();
if (function_exists('aksrt_home_widget_render_post_grid')) {
aksrt_home_widget_render_post_grid($query, $layout, $columns, $context);
} elseif ($query->have_posts()) {
echo '<ul class="widget-tab-posts__list">';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
$html = (string) ob_get_clean();
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
}
}
+23
View File
@@ -252,6 +252,11 @@ class Forum {
'callback' => [$this, 'rest_widget_topics'],
'permission_callback' => '__return_true',
]);
register_rest_route('aksrt/v1', '/forum/frontend/widgets/plates', [
'methods' => 'GET',
'callback' => [$this, 'rest_widget_plates'],
'permission_callback' => '__return_true',
]);
}
public function rest_widget_topics(\WP_REST_Request $request) {
@@ -278,6 +283,24 @@ class Forum {
], 200);
}
public function rest_widget_plates(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\WidgetForumPlateLists')) {
return new \WP_REST_Response(['success' => false, 'message' => 'widget unavailable'], 500);
}
$instance = [
'showposts' => max(1, min(30, (int) ($request->get_param('showposts') ?? $request->get_param('count') ?? 8))),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'menu_order')),
'style' => sanitize_key((string) ($request->get_param('style') ?? 'default')),
'filter' => sanitize_key((string) ($request->get_param('filter') ?? '')),
'cat' => sanitize_text_field((string) ($request->get_param('cat') ?? '')),
];
$payload = \AKSRT\Widgets\WidgetForumPlateLists::query_payload($instance);
return new \WP_REST_Response([
'success' => true,
'html' => (string) ($payload['html'] ?? ''),
], 200);
}
public function render_board_cards(array $boards): void {
+81 -18
View File
@@ -38,7 +38,7 @@ class CategoryTabsWidget extends \WP_Widget {
$tabs = $this->resolve_tabs($instance);
echo $args['before_widget'];
echo '<section class="aksrt-home-widget aksrt-home-widget--tabs widget-tab-posts">';
echo '<section class="aksrt-home-widget aksrt-home-widget--tabs widget-tab-posts" data-aksrt-home-cat-tabs>';
if (empty($tabs)) {
echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无可用分类', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无可用分类', 'aksrt') . '</p>';
@@ -69,33 +69,96 @@ class CategoryTabsWidget extends \WP_Widget {
<?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>
<div class="widget-tab-posts__panel<?php echo 0 === $index ? ' active' : ''; ?>"
data-tab-panel="cat-<?php echo (int) $tab['id']; ?>"
data-loaded="<?php echo 0 === $index ? '1' : '0'; ?>"
data-cat="<?php echo (int) $tab['id']; ?>"
data-more="<?php echo esc_url($tab['link']); ?>"
role="tabpanel"<?php echo 0 === $index ? '' : ' hidden'; ?>>
<?php if (0 === $index) :
$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'],
]);
aksrt_home_widget_render_post_grid($query, $layout, $columns, $context);
wp_reset_postdata();
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;
else : ?>
<div class="post_ajax_loader" data-aksrt-ajax-loader></div>
<?php endif; ?>
</div>
<?php
}
$rest = rest_url('aksrt/v1/widgets/category-tabs');
?>
<script>
(function(){
var section=document.currentScript?document.currentScript.parentElement:null;
if(!section||section._aksrtCatTabs)return; section._aksrtCatTabs=1;
var rest=<?php echo wp_json_encode($rest); ?>;
var count=<?php echo (int) $count; ?>;
var layout=<?php echo wp_json_encode($layout); ?>;
var columns=<?php echo (int) $columns; ?>;
var context=<?php echo wp_json_encode($context); ?>;
var showMore=<?php echo $show_more ? 'true' : 'false'; ?>;
var tabs=[].slice.call(section.querySelectorAll('.widget-tab-posts__tab'));
var panels=[].slice.call(section.querySelectorAll('.widget-tab-posts__panel'));
function load(panel){
if(!panel||panel.getAttribute('data-loaded')==='1')return;
var cat=panel.getAttribute('data-cat')||'0';
var more=panel.getAttribute('data-more')||'';
var qs=new URLSearchParams({cat:cat,count:String(count),layout:layout,columns:String(columns),context:context});
var url=rest+(rest.indexOf('?')>-1?'&':'?')+qs.toString();
panel.classList.add('is-ajax-loading');
var headers={Accept:'application/json'};
if(window.aksrtData&&aksrtData.restNonce)headers['X-WP-Nonce']=aksrtData.restNonce;
fetch(url,{credentials:'same-origin',headers:headers}).then(function(r){return r.json()}).then(function(j){
var d=(j&&j.data)||j||{};
var html=d.html||'';
if(showMore&&more){
html+='<div class="aksrt-home-widget__footer"><a class="aksrt-home-widget__more" href="'+more+'">查看更多</a></div>';
}
panel.innerHTML=html||'<p class="widget-empty">暂无文章</p>';
panel.setAttribute('data-loaded','1');
panel.classList.remove('is-ajax-loading');
}).catch(function(){panel.classList.remove('is-ajax-loading');});
}
tabs.forEach(function(tab){
tab.addEventListener('click',function(){
var target=tab.getAttribute('data-tab');
tabs.forEach(function(item){
var active=item===tab;
item.classList.toggle('active',active);
item.setAttribute('aria-selected',active?'true':'false');
});
panels.forEach(function(panel){
var active=panel.getAttribute('data-tab-panel')===target;
panel.classList.toggle('active',active);
if(active){panel.removeAttribute('hidden');load(panel);} else {panel.setAttribute('hidden','');}
});
});
});
})();
</script>
<?php
echo '</section>';
echo $args['after_widget'];
}
public function form($instance): void {
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);
+5 -4
View File
@@ -149,10 +149,11 @@ class HotPostsWidget extends \WP_Widget {
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),
'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),
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
];
}
}
+264 -292
View File
@@ -1,5 +1,7 @@
<?php
/**
* Multi-tab posts (zibll zib_widget_ui_tab_post + lazy REST).
*
* @package AKSRT
*/
@@ -9,307 +11,277 @@ defined('ABSPATH') || exit;
class TabPostsWidget extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_tab_posts',
esc_html__('AKSRT 多栏目文章', 'aksrt'),
[
'description' => esc_html__('Tab 切换显示不同分类的文章列表', 'aksrt'),
'customize_selective_refresh' => true,
]
);
}
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;
}
/**
* @param mixed $tabs
* @return list<array{title:string,category_id:int,orderby:string,count:int,enabled:bool}>
*/
public static function normalize_tabs_static($tabs): array {
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;
}
if (is_array($tabs)) {
$allowed = ['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, true)) {
$orderby = 'date';
}
$result[] = [
'title' => sanitize_text_field((string) ($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 [];
}
// 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;
}
private function normalize_tabs($tabs): array {
return self::normalize_tabs_static($tabs);
}
return [];
}
/**
* Build query for a tab → reuse main posts payload renderer.
*
* @param array $tab
* @param int $default_count
* @return array
*/
public static function tab_to_main_instance(array $tab, int $default_count): array {
$count = !empty($tab['count']) ? (int) $tab['count'] : $default_count;
return [
'count' => max(1, min(30, $count)),
'orderby' => sanitize_key((string) ($tab['orderby'] ?? 'date')),
'cat' => absint($tab['category_id'] ?? 0),
'style' => 'mini',
'show_thumb' => 1,
'show_meta' => 1,
'show_number'=> 0,
'paginate' => 'none',
];
}
/**
* 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,
];
/**
* REST helper: render one tab list HTML.
*/
public static function render_tab_html(array $tab, int $default_count): string {
$inst = self::tab_to_main_instance($tab, $default_count);
if (class_exists('\AKSRT\Widgets\WidgetMainPosts')) {
$payload = WidgetMainPosts::query_payload($inst, 1);
return (string) ($payload['html'] ?? '');
}
return '';
}
if ($tab['category_id'] > 0) {
$args['cat'] = $tab['category_id'];
}
public function widget($args, $instance): void {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
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;
}
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
$count = absint($instance['count'] ?? 5);
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
$tabs = array_values(array_filter($tabs, static fn($t) => !empty($t['enabled'])));
if (!$tabs) {
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
return $args;
}
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function widget($args, $instance): void {
echo $args['before_widget'];
$widget_id = $this->id;
$rest = rest_url('aksrt/v1/widgets/tab-posts');
$use_shared = function_exists('aksrt_render_mini_list');
?>
<div class="widget-tab-posts" id="<?php echo esc_attr($widget_id); ?>-tabs"
data-aksrt-tab-posts
data-rest="<?php echo esc_url($rest); ?>"
data-count="<?php echo esc_attr((string) $count); ?>">
<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'] : sprintf(__('Tab %d', 'aksrt'), $i + 1);
?>
<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); ?>"
data-index="<?php echo (int) $i; ?>"
data-orderby="<?php echo esc_attr($tab['orderby']); ?>"
data-cat="<?php echo (int) $tab['category_id']; ?>"
data-tab-count="<?php echo (int) $tab['count']; ?>"
aria-selected="<?php echo $first ? 'true' : 'false'; ?>"
><?php echo esc_html($tab_title); ?></button>
</li>
<?php $first = false; endforeach; ?>
</ul>
$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');
<?php $first = true; foreach ($tabs as $i => $tab) :
$tab_key = 'tab-' . $i;
?>
<div class="widget-tab-posts__panel<?php echo $first ? ' active' : ''; ?>"
data-tab-panel="<?php echo esc_attr($tab_key); ?>"
data-loaded="<?php echo $first ? '1' : '0'; ?>"
role="tabpanel"<?php echo $first ? '' : ' hidden'; ?>>
<?php if ($first) :
$html = self::render_tab_html($tab, $count);
if ($html !== '') {
echo '<div class="widget-tab-posts__list widget-tab-posts__list--mini aksrt-main-posts style-mini">' . $html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
// Fallback to old mini list path.
$q = new \WP_Query(WidgetMainPosts::build_query_args(self::tab_to_main_instance($tab, $count), 1));
if ($q->have_posts() && $use_shared) {
echo '<div class="widget-tab-posts__list widget-tab-posts__list--mini">';
aksrt_render_mini_list($q, ['context' => 'sidebar', 'show_thumb' => true, 'show_meta' => true], true);
echo '</div>';
} else {
echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无文章', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无文章', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
wp_reset_postdata();
}
else : ?>
<div class="post_ajax_loader" data-aksrt-ajax-loader></div>
<?php endif; ?>
</div>
<?php $first = false; endforeach; ?>
</div>
<?php
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
// Filter enabled tabs
$tabs = array_filter($tabs, fn($t) => !empty($t['enabled']));
if (empty($tabs)) {
echo $args['after_widget'];
return;
}
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 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((string) $title); ?>">
</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="20" value="<?php echo (int) $count; ?>">
</p>
<hr style="margin:12px 0;">
<p style="font-weight:600;margin-bottom:8px;"><?php esc_html_e('Tab 配置', 'aksrt'); ?></p>
<?php
$max_tabs = 8;
$tabs = array_slice($tabs, 0, $max_tabs);
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 esc_attr($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 esc_attr($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 esc_attr($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 esc_attr($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 esc_attr($this->get_field_name("tabs[{$i}][count]")); ?>" value="<?php echo (int) $tab['count']; ?>">
</p>
</div>
<?php endforeach;
}
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 : ?>
<?php echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无文章', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无文章', 'aksrt') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif; ?>
</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;
}
public function update($new_instance, $old_instance): array {
$result = [
'title' => sanitize_text_field((string) ($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 = ['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, true)) {
$orderby = 'date';
}
$tabs[] = [
'title' => sanitize_text_field((string) ($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 : 'latest,comments,random';
return $result;
}
}
+68 -24
View File
@@ -1,6 +1,6 @@
<?php
/**
* Forum plate list (zibll zib_bbs_widget_ui_plate_lists lite).
* Forum plate list (zibll zib_bbs_widget_ui_plate_lists + ajax).
*
* @package AKSRT
*/
@@ -16,20 +16,18 @@ class WidgetForumPlateLists extends \WP_Widget {
'aksrt_forum_plate_lists',
esc_html__('论坛 - 版块列表(Plate)', 'aksrt'),
[
'description' => esc_html__('展示论坛 plate 版块列表,支持排序与卡片样式。', 'aksrt'),
'description' => esc_html__('展示论坛 plate 版块列表,支持排序/卡片/AJAX。', 'aksrt'),
'classname' => 'widget_aksrt_forum_plate_lists',
]
);
}
public function widget($args, $instance): void {
if (!post_type_exists('aksrt_forum_plate')) {
return;
}
/**
* @return array
*/
public static function build_query_args(array $instance): array {
$limit = max(1, min(30, (int) ($instance['showposts'] ?? 8)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'menu_order'));
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
$filter = sanitize_key((string) ($instance['filter'] ?? ''));
$cat_raw = (string) ($instance['cat'] ?? '');
$cat_ids = array_values(array_filter(array_map('intval', preg_split('/[\s,]+/', $cat_raw) ?: [])));
@@ -39,6 +37,7 @@ class WidgetForumPlateLists extends \WP_Widget {
'post_status' => 'publish',
'posts_per_page' => $limit,
'order' => 'ASC',
'no_found_rows' => true,
];
switch ($orderby) {
@@ -81,8 +80,61 @@ class WidgetForumPlateLists extends \WP_Widget {
]];
}
$plates = get_posts($query_args);
return $query_args;
}
public static function render_items_html(array $plates, string $style = 'default'): string {
if (!$plates) {
return '';
}
ob_start();
$self = new self();
if ($style === 'card') {
echo '<div class="aksrt-plate-cards ajax-item">';
foreach ($plates as $post) {
if ($post instanceof \WP_Post) {
$self->render_card($post);
}
}
echo '</div>';
} else {
echo '<div class="aksrt-plate-lists ajax-item">';
foreach ($plates as $post) {
if ($post instanceof \WP_Post) {
$self->render_row($post);
}
}
echo '</div>';
}
return (string) ob_get_clean();
}
/**
* @return array{html:string}
*/
public static function query_payload(array $instance): array {
if (!post_type_exists('aksrt_forum_plate')) {
return ['html' => ''];
}
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
$plates = get_posts(self::build_query_args($instance));
$html = self::render_items_html($plates ?: [], $style === 'card' ? 'card' : 'default');
if ($html === '') {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无版块', 'aksrt'))
: '<p class="aksrt-plate-empty ajax-item">' . esc_html__('暂无版块', 'aksrt') . '</p>';
}
return ['html' => $html];
}
public function widget($args, $instance): void {
if (!post_type_exists('aksrt_forum_plate')) {
return;
}
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
$payload = self::query_payload($instance);
if ($payload['html'] === '') {
return;
}
@@ -92,24 +144,16 @@ class WidgetForumPlateLists extends \WP_Widget {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ($style === 'card') {
echo '<div class="aksrt-plate-cards">';
foreach ($plates as $post) {
$this->render_card($post);
}
echo '</div>';
} else {
echo '<div class="aksrt-plate-lists">';
foreach ($plates as $post) {
$this->render_row($post);
}
echo '</div>';
}
// IAS-like: first paint is SSR; optional remote refresh via rest for customize.
$rest = rest_url('aksrt/v1/forum/frontend/widgets/plates');
echo '<div class="widget-plate-lists ajaxpager mb20" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
private function render_row(\WP_Post $post): void {
public function render_row(\WP_Post $post): void {
$id = (int) $post->ID;
$url = get_permalink($id);
$thumb = get_the_post_thumbnail_url($id, 'thumbnail');
@@ -136,7 +180,7 @@ class WidgetForumPlateLists extends \WP_Widget {
echo '</div></a>';
}
private function render_card(\WP_Post $post): void {
public function render_card(\WP_Post $post): void {
$id = (int) $post->ID;
$url = get_permalink($id);
$thumb = get_the_post_thumbnail_url($id, 'medium');
+123 -30
View File
@@ -1,6 +1,6 @@
<?php
/**
* Main posts list (zibll zib_widget_ui_main_post lite).
* Main posts list (zibll zib_widget_ui_main_post + ajax pager).
*
* @package AKSRT
*/
@@ -16,31 +16,29 @@ class WidgetMainPosts extends \WP_Widget {
'aksrt_main_posts',
esc_html__('AKSRT 文章列表', 'aksrt'),
[
'description' => esc_html__('卡片/迷你/列表样式的文章模块。', 'aksrt'),
'description' => esc_html__('卡片/迷你/列表样式的文章模块(支持 AJAX)。', 'aksrt'),
'classname' => 'widget_aksrt_main_posts',
]
);
}
public function widget($args, $instance): void {
/**
* @param array $instance
* @param int $paged
* @return array
*/
public static function build_query_args(array $instance, int $paged = 1): array {
$count = max(1, min(30, (int) ($instance['count'] ?? 6)));
$style = sanitize_key((string) ($instance['style'] ?? 'list'));
if (!in_array($style, ['list', 'card', 'mini'], true)) {
$style = 'list';
}
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
$cat = (int) ($instance['cat'] ?? 0);
$limit_day = max(0, (int) ($instance['limit_day'] ?? 0));
$show_thumb = !isset($instance['show_thumb']) || !empty($instance['show_thumb']);
$show_meta = !isset($instance['show_meta']) || !empty($instance['show_meta']);
$show_number = !empty($instance['show_number']);
$query = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'paged' => max(1, $paged),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
];
if ($cat > 0) {
$query['cat'] = $cat;
@@ -51,39 +49,48 @@ class WidgetMainPosts extends \WP_Widget {
switch ($orderby) {
case 'views':
$query['meta_key'] = 'aksrt_post_views';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'DESC';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'DESC';
break;
case 'comments':
$query['orderby'] = 'comment_count';
$query['order'] = 'DESC';
$query['order'] = 'DESC';
break;
case 'likes':
$query['meta_key'] = '_aksrt_like_count';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'DESC';
break;
case 'rand':
$query['orderby'] = 'rand';
break;
case 'modified':
$query['orderby'] = 'modified';
$query['order'] = 'DESC';
$query['order'] = 'DESC';
break;
default:
$query['orderby'] = 'date';
$query['order'] = 'DESC';
$query['order'] = 'DESC';
}
return $query;
}
$q = new \WP_Query($query);
public static function render_items_html(\WP_Query $q, array $instance): string {
if (!$q->have_posts()) {
wp_reset_postdata();
return;
return '';
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$style = sanitize_key((string) ($instance['style'] ?? 'list'));
if (!in_array($style, ['list', 'card', 'mini'], true)) {
$style = 'list';
}
$show_thumb = !isset($instance['show_thumb']) || !empty($instance['show_thumb']);
$show_meta = !isset($instance['show_meta']) || !empty($instance['show_meta']);
$show_number = !empty($instance['show_number']);
$start = max(0, (int) ($instance['_start_index'] ?? 0));
echo '<div class="aksrt-main-posts style-' . esc_attr($style) . '">';
$i = 0;
ob_start();
// Outer wrapper only on page 1 full render handled by caller for ajax append items.
$i = $start;
while ($q->have_posts()) {
$q->the_post();
$i++;
@@ -91,7 +98,7 @@ class WidgetMainPosts extends \WP_Widget {
$views = (int) get_post_meta(get_the_ID(), 'aksrt_post_views', true);
if ($style === 'card') {
echo '<a class="aksrt-main-posts__card" href="' . esc_url(get_permalink()) . '">';
echo '<a class="aksrt-main-posts__card ajax-item" href="' . esc_url(get_permalink()) . '">';
echo '<div class="aksrt-main-posts__thumb' . ($thumb ? '' : ' is-empty') . '">';
if ($thumb) {
echo '<img src="' . esc_url($thumb) . '" alt="" loading="lazy" />';
@@ -108,7 +115,7 @@ class WidgetMainPosts extends \WP_Widget {
}
echo '</div></a>';
} else {
echo '<a class="aksrt-main-posts__row" href="' . esc_url(get_permalink()) . '">';
echo '<a class="aksrt-main-posts__row ajax-item" href="' . esc_url(get_permalink()) . '">';
if ($show_number) {
echo '<span class="aksrt-main-posts__num">' . esc_html((string) $i) . '</span>';
}
@@ -127,8 +134,84 @@ class WidgetMainPosts extends \WP_Widget {
echo '</span></a>';
}
}
echo '</div>';
wp_reset_postdata();
return (string) ob_get_clean();
}
/**
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
*/
public static function query_payload(array $instance, int $page = 1): array {
$page = max(1, $page);
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'none'));
$count = max(1, min(30, (int) ($instance['count'] ?? 6)));
$instance['_start_index'] = ($page - 1) * $count;
$q = new \WP_Query(self::build_query_args($instance, $page));
$html = self::render_items_html($q, $instance);
if ($html === '' && $page === 1) {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
: '<p class="aksrt-main-posts__empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
}
$max_pages = max(1, (int) $q->max_num_pages);
$total = (int) $q->found_posts;
$has_more = $page < $max_pages;
$paginate_html = '';
if ($paginate === 'ajax' && $has_more) {
$next = add_query_arg(array_filter([
'page' => $page + 1,
'count' => $count,
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
'cat' => (int) ($instance['cat'] ?? 0),
'limit_day' => (int) ($instance['limit_day'] ?? 0),
'style' => sanitize_key((string) ($instance['style'] ?? 'list')),
'show_thumb' => !isset($instance['show_thumb']) || !empty($instance['show_thumb']) ? 1 : 0,
'show_meta' => !isset($instance['show_meta']) || !empty($instance['show_meta']) ? 1 : 0,
'show_number' => !empty($instance['show_number']) ? 1 : 0,
'paginate' => 'ajax',
]), rest_url('aksrt/v1/widgets/posts'));
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
? aksrt_get_ajax_next_paginate($total, $page, $count, $next)
: '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
} else {
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
}
return [
'html' => $html,
'paginate_html' => $paginate_html,
'has_more' => $has_more,
'page' => $page,
'max_pages' => $max_pages,
'total' => $total,
];
}
public function widget($args, $instance): void {
$style = sanitize_key((string) ($instance['style'] ?? 'list'));
if (!in_array($style, ['list', 'card', 'mini'], true)) {
$style = 'list';
}
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'none'));
$instance['style'] = $style;
$instance['paginate'] = $paginate;
$payload = self::query_payload($instance, 1);
if ($payload['html'] === '' && empty($payload['total'])) {
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$rest = rest_url('aksrt/v1/widgets/posts');
echo '<div class="aksrt-main-posts style-' . esc_attr($style) . ' ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -161,6 +244,7 @@ class WidgetMainPosts extends \WP_Widget {
<option value="modified" <?php selected(($instance['orderby'] ?? ''), 'modified'); ?>><?php esc_html_e('更新', 'aksrt'); ?></option>
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
<option value="comments" <?php selected(($instance['orderby'] ?? ''), 'comments'); ?>><?php esc_html_e('评论', 'aksrt'); ?></option>
<option value="likes" <?php selected(($instance['orderby'] ?? ''), 'likes'); ?>><?php esc_html_e('点赞', 'aksrt'); ?></option>
<option value="rand" <?php selected(($instance['orderby'] ?? ''), 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
</select>
</label>
@@ -175,6 +259,14 @@ class WidgetMainPosts extends \WP_Widget {
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('limit_day')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['limit_day'] ?? 0))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('分页', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
<option value="none" <?php selected(($instance['paginate'] ?? 'none'), 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
<option value="ajax" <?php selected(($instance['paginate'] ?? ''), 'ajax'); ?>><?php esc_html_e('AJAX 加载更多', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_thumb')); ?>" value="1" <?php checked(!isset($instance['show_thumb']) || !empty($instance['show_thumb'])); ?>> <?php esc_html_e('显示缩略图', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_meta')); ?>" value="1" <?php checked(!isset($instance['show_meta']) || !empty($instance['show_meta'])); ?>> <?php esc_html_e('显示 meta', 'aksrt'); ?></label><br>
@@ -189,7 +281,7 @@ class WidgetMainPosts extends \WP_Widget {
$style = 'list';
}
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
if (!in_array($orderby, ['date', 'modified', 'views', 'comments', 'rand'], true)) {
if (!in_array($orderby, ['date', 'modified', 'views', 'comments', 'likes', 'rand'], true)) {
$orderby = 'date';
}
return [
@@ -199,6 +291,7 @@ class WidgetMainPosts extends \WP_Widget {
'orderby' => $orderby,
'cat' => max(0, (int) ($new_instance['cat'] ?? 0)),
'limit_day' => max(0, (int) ($new_instance['limit_day'] ?? 0)),
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'none')) === 'ajax' ? 'ajax' : 'none',
'show_thumb' => !empty($new_instance['show_thumb']) ? 1 : 0,
'show_meta' => !empty($new_instance['show_meta']) ? 1 : 0,
'show_number' => !empty($new_instance['show_number']) ? 1 : 0,
+88 -21
View File
@@ -1,6 +1,6 @@
<?php
/**
* Paid posts list (zibll zib_widget_ui_posts_pay lite).
* Paid posts list (zibll zib_widget_ui_posts_pay + ajax).
*
* @package AKSRT
*/
@@ -16,20 +16,20 @@ class WidgetPaidPosts extends \WP_Widget {
'aksrt_paid_posts',
esc_html__('AKSRT 付费内容列表', 'aksrt'),
[
'description' => esc_html__('展示开启付费/积分可见的文章。', 'aksrt'),
'description' => esc_html__('展示开启付费/积分可见的文章(支持 AJAX)。', 'aksrt'),
'classname' => 'widget_aksrt_paid_posts',
]
);
}
public function widget($args, $instance): void {
public static function build_query_args(array $instance, int $paged = 1): array {
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
$query = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $count,
'paged' => max(1, $paged),
'order' => 'DESC',
'meta_query' => [
'relation' => 'OR',
@@ -53,7 +53,7 @@ class WidgetPaidPosts extends \WP_Widget {
],
];
if ($orderby === 'views') {
$query['meta_key'] = '_aksrt_views';
$query['meta_key'] = 'aksrt_post_views';
$query['orderby'] = 'meta_value_num';
} elseif ($orderby === 'price') {
$query['meta_key'] = '_aksrt_pay_price';
@@ -61,26 +61,20 @@ class WidgetPaidPosts extends \WP_Widget {
} else {
$query['orderby'] = 'date';
}
return $query;
}
$q = new \WP_Query($query);
public static function render_items_html(\WP_Query $q): string {
if (!$q->have_posts()) {
wp_reset_postdata();
return;
return '';
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '<ul class="aksrt-paid-posts">';
ob_start();
while ($q->have_posts()) {
$q->the_post();
$price = (float) get_post_meta(get_the_ID(), '_aksrt_pay_price', true);
$credits = (int) get_post_meta(get_the_ID(), '_aksrt_credits_cost', true);
$thumb = get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');
echo '<li class="aksrt-paid-posts__item">';
echo '<li class="aksrt-paid-posts__item ajax-item">';
if ($thumb) {
echo '<a class="aksrt-paid-posts__thumb" href="' . esc_url(get_permalink()) . '"><img src="' . esc_url($thumb) . '" alt="" loading="lazy" /></a>';
}
@@ -96,8 +90,72 @@ class WidgetPaidPosts extends \WP_Widget {
}
echo '</div></div></li>';
}
echo '</ul>';
wp_reset_postdata();
return (string) ob_get_clean();
}
/**
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
*/
public static function query_payload(array $instance, int $page = 1): array {
$page = max(1, $page);
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
$q = new \WP_Query(self::build_query_args($instance, $page));
$items = self::render_items_html($q);
$html = $items !== '' ? '<ul class="aksrt-paid-posts">' . $items . '</ul>' : '';
if ($html === '' && $page === 1) {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无付费内容', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无付费内容', 'aksrt') . '</p>';
}
$max_pages = max(1, (int) $q->max_num_pages);
$total = (int) $q->found_posts;
$has_more = $page < $max_pages;
$paginate_html = '';
if ($paginate === 'ajax' && $has_more) {
$next = add_query_arg([
'page' => $page + 1,
'count' => $count,
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
'paginate' => 'ajax',
], rest_url('aksrt/v1/widgets/paid-posts'));
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
? aksrt_get_ajax_next_paginate($total, $page, $count, $next)
: '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
} else {
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
}
return [
'html' => $html,
'paginate_html' => $paginate_html,
'has_more' => $has_more,
'page' => $page,
'max_pages' => $max_pages,
'total' => $total,
];
}
public function widget($args, $instance): void {
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$instance['paginate'] = $paginate;
$payload = self::query_payload($instance, 1);
if ($payload['html'] === '' && empty($payload['total'])) {
return;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$rest = rest_url('aksrt/v1/widgets/paid-posts');
echo '<div class="aksrt-paid-posts-wrap ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
@@ -123,6 +181,14 @@ class WidgetPaidPosts extends \WP_Widget {
</select>
</label>
</p>
<p>
<label><?php esc_html_e('分页', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
<option value="ajax" <?php selected(($instance['paginate'] ?? 'ajax'), 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
<option value="none" <?php selected(($instance['paginate'] ?? ''), 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
</select>
</label>
</p>
<?php
}
@@ -132,9 +198,10 @@ class WidgetPaidPosts extends \WP_Widget {
$orderby = 'date';
}
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
'orderby' => $orderby,
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
'orderby' => $orderby,
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) === 'none' ? 'none' : 'ajax',
];
}
}
+55 -3
View File
@@ -23,7 +23,8 @@ class WidgetUserRanking extends \WP_Widget {
$count = max(1, min(20, (int) ($instance['count'] ?? 5)));
$show_avatar = !empty($instance['show_avatar']);
$users = $this->get_ranking($rank_by, $count);
$desc_mode = sanitize_key((string) ($instance['desc'] ?? 'desc'));
$users = $this->get_ranking($rank_by, $count, $desc_mode);
if (empty($users)) return;
echo $args['before_widget'];
@@ -60,8 +61,13 @@ class WidgetUserRanking extends \WP_Widget {
echo '</a>';
}
// 昵称
// 昵称 + 可选签名/附加信息
echo '<div class="aksrt-user-ranking__meta">';
echo '<a href="' . esc_url($user_url) . '" class="aksrt-user-ranking__name">' . esc_html($user['name']) . '</a>';
if (!empty($user['desc'])) {
echo '<div class="aksrt-user-ranking__desc em09 muted-2-color">' . esc_html((string) $user['desc']) . '</div>';
}
echo '</div>';
// 数值
echo '<span class="aksrt-user-ranking__value">' . esc_html($user['value']) . '</span>';
@@ -73,7 +79,7 @@ class WidgetUserRanking extends \WP_Widget {
echo $args['after_widget'];
}
private function get_ranking(string $rank_by, int $count): array {
private function get_ranking(string $rank_by, int $count, string $desc_mode = 'desc'): array {
$users = [];
switch ($rank_by) {
@@ -93,6 +99,7 @@ class WidgetUserRanking extends \WP_Widget {
'id' => (int) $row['user_id'],
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['credits']) . ' 积分',
'desc' => $this->user_extra_desc((int) $row['user_id'], $user, $desc_mode),
];
}
}
@@ -112,6 +119,7 @@ class WidgetUserRanking extends \WP_Widget {
'id' => $user->ID,
'name' => $user->display_name,
'value' => number_format_i18n($post_count) . ' 篇',
'desc' => $this->user_extra_desc($user->ID, $user, $desc_mode),
];
}
}
@@ -137,6 +145,7 @@ class WidgetUserRanking extends \WP_Widget {
'id' => $uid,
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['val']) . ' 天',
'desc' => $this->user_extra_desc($uid, $user, $desc_mode),
];
}
if (count($users) >= $count) break;
@@ -163,6 +172,7 @@ class WidgetUserRanking extends \WP_Widget {
'id' => $uid,
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['val']) . ' 天',
'desc' => $this->user_extra_desc($uid, $user, $desc_mode),
];
}
if (count($users) >= $count) break;
@@ -189,6 +199,7 @@ class WidgetUserRanking extends \WP_Widget {
'id' => (int) $row['following_id'],
'name' => $user->display_name,
'value' => number_format_i18n((int) $row['cnt']) . ' 粉丝',
'desc' => $this->user_extra_desc((int) $row['following_id'], $user, $desc_mode),
];
}
}
@@ -200,6 +211,37 @@ class WidgetUserRanking extends \WP_Widget {
return $users;
}
private function user_extra_desc(int $user_id, $user, string $mode): string {
switch ($mode) {
case 'user_registered':
if ($user && !empty($user->user_registered)) {
$days = max(0, (int) floor((time() - strtotime($user->user_registered)) / DAY_IN_SECONDS));
return sprintf(__('加入 %d 天', 'aksrt'), $days);
}
return '';
case 'last_login':
$t = (string) get_user_meta($user_id, 'aksrt_last_login', true);
if ($t === '') {
$t = (string) get_user_meta($user_id, 'last_login', true);
}
return $t !== '' ? sprintf(__('最近登录 %s', 'aksrt'), $t) : '';
case 'last_checkin':
$t = (string) get_user_meta($user_id, 'aksrt_last_checkin', true);
if ($t === '') {
$t = (string) get_user_meta($user_id, 'last_checkin', true);
}
return $t !== '' ? sprintf(__('最近签到 %s', 'aksrt'), $t) : '';
case 'desc':
default:
$bio = get_user_meta($user_id, 'description', true);
if (!is_string($bio) || $bio === '') {
$bio = $user ? (string) $user->description : '';
}
return $bio !== '' ? wp_trim_words($bio, 12) : '';
}
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('排行榜', 'aksrt');
$rank_by = !empty($instance['rank_by']) ? $instance['rank_by'] : 'credits';
@@ -230,6 +272,15 @@ class WidgetUserRanking extends \WP_Widget {
<?php esc_html_e('显示头像', 'aksrt'); ?>
</label>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('desc')); ?>"><?php esc_html_e('附加显示:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('desc')); ?>" name="<?php echo esc_attr($this->get_field_name('desc')); ?>">
<option value="desc" <?php selected(($instance['desc'] ?? 'desc'), 'desc'); ?>><?php esc_html_e('用户签名', 'aksrt'); ?></option>
<option value="user_registered" <?php selected(($instance['desc'] ?? ''), 'user_registered'); ?>><?php esc_html_e('加入天数', 'aksrt'); ?></option>
<option value="last_login" <?php selected(($instance['desc'] ?? ''), 'last_login'); ?>><?php esc_html_e('最后登录', 'aksrt'); ?></option>
<option value="last_checkin" <?php selected(($instance['desc'] ?? ''), 'last_checkin'); ?>><?php esc_html_e('最后签到', 'aksrt'); ?></option>
</select>
</p>
<p>
<label>
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('checkin_btn')); ?>" value="1" <?php checked(!empty($instance['checkin_btn'])); ?>>
@@ -246,6 +297,7 @@ class WidgetUserRanking extends \WP_Widget {
$instance['count'] = max(1, min(20, (int) ($new_instance['count'] ?? 5)));
$instance['show_avatar'] = !empty($new_instance['show_avatar']);
$instance['checkin_btn'] = !empty($new_instance['checkin_btn']);
$instance['desc'] = in_array($new_instance['desc'] ?? '', ['desc', 'user_registered', 'last_login', 'last_checkin'], true) ? $new_instance['desc'] : 'desc';
return $instance;
}
}