395 lines
17 KiB
PHP
395 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* Multi-tab posts (zibll zib_widget_ui_tab_post + lazy REST).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
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,
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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 [];
|
|
}
|
|
|
|
private function normalize_tabs($tabs): array {
|
|
return self::normalize_tabs_static($tabs);
|
|
}
|
|
|
|
/**
|
|
* 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 $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),
|
|
'topics' => sanitize_text_field((string) ($tab['topics'] ?? '')),
|
|
'limit_day' => max(0, (int) ($tab['limit_day'] ?? 0)),
|
|
'pay_type' => sanitize_key((string) ($tab['pay_type'] ?? '')),
|
|
'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, 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')) {
|
|
return WidgetMainPosts::query_payload($inst, $page);
|
|
}
|
|
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 {
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
|
|
$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) {
|
|
if (current_user_can('manage_options')) {
|
|
echo function_exists('aksrt_get_widget_null')
|
|
? aksrt_get_widget_null(__('请至少启用一个 Tab', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
: '<p class="widget-empty">' . esc_html__('请至少启用一个 Tab', 'aksrt') . '</p>';
|
|
}
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
return;
|
|
}
|
|
|
|
if ($title !== '') {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
$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); ?>"
|
|
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;
|
|
$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']; ?>"
|
|
data-topics="<?php echo esc_attr((string) ($tab['topics'] ?? '')); ?>"
|
|
data-limit-day="<?php echo esc_attr((string) ((int) ($tab['limit_day'] ?? 0))); ?>"
|
|
data-pay-type="<?php echo esc_attr((string) ($tab['pay_type'] ?? '')); ?>"
|
|
aria-selected="<?php echo $first ? 'true' : 'false'; ?>"
|
|
><?php echo esc_html($tab_title); ?></button>
|
|
</li>
|
|
<?php $first = false; endforeach; ?>
|
|
</ul>
|
|
|
|
<?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) :
|
|
$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
|
|
}
|
|
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>
|
|
<?php
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
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>
|
|
<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
|
|
$max_tabs = 8;
|
|
$tabs = array_slice($tabs, 0, $max_tabs);
|
|
while (count($tabs) < 3) {
|
|
$tabs[] = ['title' => '', 'category_id' => 0, 'orderby' => 'date', 'count' => 0, 'topics' => '', 'limit_day' => 0, 'pay_type' => '', '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>
|
|
<p style="margin:0 0 6px;">
|
|
<label><?php esc_html_e('专题 topics ID(逗号):', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name("tabs[{$i}][topics]")); ?>" value="<?php echo esc_attr((string) ($tab['topics'] ?? '')); ?>">
|
|
</p>
|
|
<p style="margin:0 0 6px;">
|
|
<label><?php esc_html_e('最近 N 天(0=不限):', 'aksrt'); ?></label>
|
|
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name("tabs[{$i}][limit_day]")); ?>" value="<?php echo (int) ($tab['limit_day'] ?? 0); ?>">
|
|
</p>
|
|
<p style="margin:0;">
|
|
<label><?php esc_html_e('付费类型:', 'aksrt'); ?></label>
|
|
<select class="widefat" name="<?php echo esc_attr($this->get_field_name("tabs[{$i}][pay_type]")); ?>">
|
|
<option value="" <?php selected(($tab['pay_type'] ?? ''), ''); ?>><?php esc_html_e('不限', 'aksrt'); ?></option>
|
|
<option value="paid" <?php selected(($tab['pay_type'] ?? ''), 'paid'); ?>><?php esc_html_e('任意付费', 'aksrt'); ?></option>
|
|
<option value="reading" <?php selected(($tab['pay_type'] ?? ''), 'reading'); ?>><?php esc_html_e('付费阅读', 'aksrt'); ?></option>
|
|
<option value="download" <?php selected(($tab['pay_type'] ?? ''), 'download'); ?>><?php esc_html_e('付费下载', 'aksrt'); ?></option>
|
|
</select>
|
|
</p>
|
|
</div>
|
|
<?php endforeach;
|
|
}
|
|
|
|
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))),
|
|
'style' => $style,
|
|
'paginate' => $paginate,
|
|
];
|
|
$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))),
|
|
'topics' => sanitize_text_field((string) ($tab['topics'] ?? '')),
|
|
'limit_day' => max(0, (int) ($tab['limit_day'] ?? 0)),
|
|
'pay_type' => sanitize_key((string) ($tab['pay_type'] ?? '')),
|
|
'enabled' => !empty($tab['enabled']),
|
|
];
|
|
}
|
|
}
|
|
$result['tabs'] = !empty($tabs) ? $tabs : 'latest,comments,random';
|
|
return $result;
|
|
}
|
|
}
|