feat(widgets): number paginate + main/tab query depth
Add shared aksrt_get_ajax_number_paginate, MainPosts topics/pay_type/ favorites filters and number pager, TabPosts style/paginate/skeleton payload, REST wiring, product number AJAX parity.
This commit is contained in:
@@ -93,30 +93,72 @@ class TabPostsWidget extends \WP_Widget {
|
||||
* @param int $default_count
|
||||
* @return array
|
||||
*/
|
||||
public static function tab_to_main_instance(array $tab, int $default_count): array {
|
||||
public static function tab_to_main_instance(array $tab, int $default_count, array $opts = []): array {
|
||||
$count = !empty($tab['count']) ? (int) $tab['count'] : $default_count;
|
||||
$style = sanitize_key((string) ($opts['style'] ?? $tab['style'] ?? 'mini'));
|
||||
if (!in_array($style, ['list', 'card', 'mini'], true)) {
|
||||
$style = 'mini';
|
||||
}
|
||||
$paginate = sanitize_key((string) ($opts['paginate'] ?? $tab['paginate'] ?? 'none'));
|
||||
if (!in_array($paginate, ['none', 'ajax', 'number'], true)) {
|
||||
$paginate = 'none';
|
||||
}
|
||||
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',
|
||||
'count' => max(1, min(30, $count)),
|
||||
'orderby' => sanitize_key((string) ($tab['orderby'] ?? 'date')),
|
||||
'cat' => absint($tab['category_id'] ?? 0),
|
||||
'style' => $style,
|
||||
'show_thumb' => !isset($opts['show_thumb']) ? 1 : (int) $opts['show_thumb'],
|
||||
'show_meta' => !isset($opts['show_meta']) ? 1 : (int) $opts['show_meta'],
|
||||
'show_number' => !empty($opts['show_number']) ? 1 : 0,
|
||||
'paginate' => $paginate,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* REST helper: render one tab list HTML.
|
||||
*
|
||||
* @param array<string,mixed> $opts
|
||||
*/
|
||||
public static function render_tab_html(array $tab, int $default_count): string {
|
||||
$inst = self::tab_to_main_instance($tab, $default_count);
|
||||
public static function render_tab_html(array $tab, int $default_count, array $opts = []): string {
|
||||
$payload = self::render_tab_payload($tab, $default_count, $opts, 1);
|
||||
return (string) ($payload['html'] ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Full tab payload (html + pager) for REST/ajax.
|
||||
*
|
||||
* @param array<string,mixed> $opts
|
||||
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
|
||||
*/
|
||||
public static function render_tab_payload(array $tab, int $default_count, array $opts = [], int $page = 1): array {
|
||||
$inst = self::tab_to_main_instance($tab, $default_count, $opts);
|
||||
if (class_exists('\AKSRT\Widgets\WidgetMainPosts')) {
|
||||
$payload = WidgetMainPosts::query_payload($inst, 1);
|
||||
return (string) ($payload['html'] ?? '');
|
||||
return WidgetMainPosts::query_payload($inst, $page);
|
||||
}
|
||||
return '';
|
||||
return [
|
||||
'html' => '',
|
||||
'paginate_html' => '',
|
||||
'has_more' => false,
|
||||
'page' => 1,
|
||||
'max_pages' => 1,
|
||||
'total' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
/** Skeleton rows for lazy tab panels. */
|
||||
public static function skeleton_html(int $count = 5, string $style = 'mini'): string {
|
||||
$count = max(1, min(12, $count));
|
||||
ob_start();
|
||||
echo '<div class="widget-tab-posts__skeleton posts-mini-lists style-' . esc_attr($style) . '">';
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
echo '<div class="posts-mini placeholder-row">';
|
||||
echo '<div class="placeholder radius thumb-placeholder"></div>';
|
||||
echo '<div class="flex1"><div class="placeholder k1"></div><div class="placeholder s1 mt6"></div></div>';
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
@@ -124,6 +166,14 @@ class TabPostsWidget extends \WP_Widget {
|
||||
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
$count = absint($instance['count'] ?? 5);
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
if (!in_array($style, ['list', 'card', 'mini'], true)) {
|
||||
$style = 'mini';
|
||||
}
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'none'));
|
||||
if (!in_array($paginate, ['none', 'ajax', 'number'], true)) {
|
||||
$paginate = 'none';
|
||||
}
|
||||
$tabs = $this->normalize_tabs($instance['tabs'] ?? 'latest,comments,random');
|
||||
$tabs = array_values(array_filter($tabs, static fn($t) => !empty($t['enabled'])));
|
||||
if (!$tabs) {
|
||||
@@ -147,7 +197,9 @@ class TabPostsWidget extends \WP_Widget {
|
||||
<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); ?>">
|
||||
data-count="<?php echo esc_attr((string) $count); ?>"
|
||||
data-style="<?php echo esc_attr($style); ?>"
|
||||
data-paginate="<?php echo esc_attr($paginate); ?>">
|
||||
<ul class="widget-tab-posts__nav" role="tablist">
|
||||
<?php $first = true; foreach ($tabs as $i => $tab) :
|
||||
$tab_key = 'tab-' . $i;
|
||||
@@ -175,24 +227,20 @@ class TabPostsWidget extends \WP_Widget {
|
||||
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>';
|
||||
$opts = ['style' => $style, 'paginate' => $paginate];
|
||||
$payload = self::render_tab_payload($tab, $count, $opts, 1);
|
||||
$html = (string) ($payload['html'] ?? '');
|
||||
if ($html !== '') {
|
||||
echo '<div class="widget-tab-posts__list widget-tab-posts__list--' . esc_attr($style) . ' aksrt-main-posts style-' . esc_attr($style) . '">' . $html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
if (!empty($payload['paginate_html'])) {
|
||||
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
} 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; ?>
|
||||
else : ?>
|
||||
<div class="post_ajax_loader" data-aksrt-ajax-loader><?php echo self::skeleton_html($count, $style); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php $first = false; endforeach; ?>
|
||||
</div>
|
||||
@@ -223,6 +271,24 @@ class TabPostsWidget extends \WP_Widget {
|
||||
<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>
|
||||
<p>
|
||||
<label><?php esc_html_e('列表样式', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="mini" <?php selected(($instance['style'] ?? 'mini'), 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
<option value="list" <?php selected(($instance['style'] ?? ''), 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected(($instance['style'] ?? ''), 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</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>
|
||||
<option value="number" <?php selected(($instance['paginate'] ?? ''), 'number'); ?>><?php esc_html_e('数字分页', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<hr style="margin:12px 0;">
|
||||
<p style="font-weight:600;margin-bottom:8px;"><?php esc_html_e('Tab 配置', 'aksrt'); ?></p>
|
||||
<?php
|
||||
@@ -262,9 +328,19 @@ class TabPostsWidget extends \WP_Widget {
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$style = sanitize_key((string) ($new_instance['style'] ?? 'mini'));
|
||||
if (!in_array($style, ['list', 'card', 'mini'], true)) {
|
||||
$style = 'mini';
|
||||
}
|
||||
$paginate = sanitize_key((string) ($new_instance['paginate'] ?? 'none'));
|
||||
if (!in_array($paginate, ['none', 'ajax', 'number'], true)) {
|
||||
$paginate = 'none';
|
||||
}
|
||||
$result = [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 5))),
|
||||
'style' => $style,
|
||||
'paginate' => $paginate,
|
||||
];
|
||||
$tabs = [];
|
||||
if (isset($new_instance['tabs']) && is_array($new_instance['tabs'])) {
|
||||
|
||||
Reference in New Issue
Block a user