Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetProductTab.php
T
kate522 1a92adbaf1 feat(widgets): number paginate for hot/paid/forum lists
Extend aksrt_get_ajax_number_paginate to HotPosts, PaidPosts, forum
hot/topic lists and product tabs; REST accepts number + paid filters.
2026-07-21 12:59:05 +08:00

174 lines
8.5 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
/**
* Multi-tab product widget — zibll `zib_shop_widget_ui_tab_product` lite.
* First tab renders immediately via shared payload; other tabs lazy-load via REST.
*/
class WidgetProductTab extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_product_tab',
__('多TAB商品', 'aksrt'),
['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选 + AJAX)。', 'aksrt')]
);
}
/**
* @return list<array{label:string,cat_ids:string,discount_ids:string,tag_ids:string,orderby:string}>
*/
private static function parse_tabs(string $tabs_raw): array {
$tabs = [];
foreach (array_filter(explode("\n", $tabs_raw)) as $line) {
$parts = explode('|', trim($line));
if (count($parts) >= 1 && trim($parts[0]) !== '') {
$tabs[] = [
'label' => trim($parts[0]),
'cat_ids' => trim($parts[1] ?? ''),
'discount_ids' => trim($parts[2] ?? ''),
'tag_ids' => trim($parts[3] ?? ''),
'orderby' => trim($parts[4] ?? 'date'),
];
}
}
return $tabs;
}
public function widget($args, $instance): void {
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
$tabs_raw = (string) ($instance['tabs'] ?? '');
$per_page = max(1, min(20, (int) ($instance['per_page'] ?? 8)));
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
$tabs = self::parse_tabs($tabs_raw);
if (!$tabs) {
if (current_user_can('manage_options')) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
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;
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($title !== '') {
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$rest = rest_url('aksrt/v1/shop/frontend/widgets/products');
echo '<div class="aksrt-product-tab-widget" data-aksrt-product-tab data-per-page="' . esc_attr((string) $per_page) . '" data-style="' . esc_attr($style) . '" data-paginate="' . esc_attr($paginate) . '" data-rest="' . esc_url($rest) . '">';
echo '<div class="aksrt-product-tab__headers index-tab">';
foreach ($tabs as $i => $tab) {
$active = $i === 0 ? ' is-active' : '';
echo '<button type="button" class="aksrt-product-tab__header' . esc_attr($active) . '" data-tab-index="' . esc_attr((string) $i) . '"';
echo ' data-cat-ids="' . esc_attr($tab['cat_ids']) . '"';
echo ' data-discount-ids="' . esc_attr($tab['discount_ids']) . '"';
echo ' data-tag-ids="' . esc_attr($tab['tag_ids']) . '"';
echo ' data-orderby="' . esc_attr(sanitize_key($tab['orderby'] ?: 'date')) . '"';
echo '>' . esc_html($tab['label']) . '</button>';
}
echo '</div>';
foreach ($tabs as $i => $tab) {
$hidden = $i > 0 ? ' style="display:none"' : '';
$loaded = $i === 0 ? '1' : '0';
echo '<div class="aksrt-product-tab__panel" data-tab-index="' . esc_attr((string) $i) . '" data-loaded="' . esc_attr($loaded) . '"' . $hidden . '>';
if ($i === 0) {
$inst = [
'per_page' => $per_page,
'orderby' => sanitize_key($tab['orderby'] ?: 'date'),
'order' => 'DESC',
'cat_ids' => $tab['cat_ids'],
'tag_ids' => $tab['tag_ids'],
'discount_ids' => $tab['discount_ids'],
'style' => $style,
'paginate' => $paginate,
];
$payload = class_exists('\AKSRT\Widgets\WidgetProductList')
? WidgetProductList::query_payload($inst, 1)
: ['html' => '', 'paginate_html' => ''];
echo '<div class="ajaxpager aksrt-product-widget__grid product-lists-row aksrt-product-widget--' . esc_attr($style) . '" data-aksrt-ajaxpager>';
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>';
} else {
echo '<div class="ajaxpager aksrt-product-widget__grid product-lists-row aksrt-product-widget--' . esc_attr($style) . ' is-lazy" data-aksrt-ajaxpager>';
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader>';
echo '<div class="aksrt-product-tab__skeleton">';
for ($si = 0; $si < min(4, $per_page); $si++) {
echo '<div class="placeholder-row product-card-placeholder"><div class="placeholder thumb"></div><div class="placeholder k1"></div><div class="placeholder s1"></div></div>';
}
echo '</div></div>';
echo '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
echo '</div>';
}
echo '</div>';
}
echo '</div>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public function form($instance): void {
$defaults = [
'title' => __('商品精选', 'aksrt'),
'tabs' => "热门推荐|\n最新上架|",
'per_page' => 8,
'style' => 'default',
'paginate' => 'ajax',
];
$instance = wp_parse_args($instance, $defaults);
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) $instance['title']); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('tabs')); ?>"><?php esc_html_e('TAB配置(每行: 名称|分类ID|优惠ID|标签ID|排序):', 'aksrt'); ?></label>
<textarea class="widefat" rows="5" id="<?php echo esc_attr($this->get_field_id('tabs')); ?>" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>"><?php echo esc_textarea((string) $instance['tabs']); ?></textarea>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('样式:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
<option value="default" <?php selected($instance['style'], 'default'); ?>><?php esc_html_e('默认', 'aksrt'); ?></option>
<option value="card" <?php selected($instance['style'], 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('paginate')); ?>"><?php esc_html_e('分页:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('paginate')); ?>" name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
<option value="ajax" <?php selected($instance['paginate'], 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
<option value="none" <?php selected($instance['paginate'], 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
</select>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
'tabs' => sanitize_textarea_field((string) ($new_instance['tabs'] ?? '')),
'per_page' => max(1, min(20, (int) ($new_instance['per_page'] ?? 8))),
'style' => sanitize_key((string) ($new_instance['style'] ?? 'default')),
'paginate' => in_array(sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')), ['none', 'ajax', 'number'], true) ? sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')) : 'ajax',
];
}
}