diff --git a/src/AKSRT/Widgets/WidgetProductTab.php b/src/AKSRT/Widgets/WidgetProductTab.php index a54e4a9d..8bece9e6 100644 --- a/src/AKSRT/Widgets/WidgetProductTab.php +++ b/src/AKSRT/Widgets/WidgetProductTab.php @@ -8,163 +8,188 @@ namespace AKSRT\Widgets; defined('ABSPATH') || exit; /** - * Multi-tab product widget — equivalent to zibll `zib_shop_widget_ui_tab_product`. - * Each tab has independent category/discount filter. + * 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 独立筛选)。', 'aksrt')] - ); - } + public function __construct() { + parent::__construct( + 'aksrt_product_tab', + __('多TAB商品', 'aksrt'), + ['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选 + AJAX)。', 'aksrt')] + ); + } - public function widget($args, $instance): void { - $title = apply_filters('widget_title', $instance['title'] ?? ''); - $tabs_raw = (string) ($instance['tabs'] ?? ''); - $per_page = max(1, min(20, (int) ($instance['per_page'] ?? 8))); + /** + * @return list + */ + 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; + } - // Parse tabs: "Tab Name|cat_ids|discount_ids\n..." - $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] ?? ''), - ]; - } - } - if (empty($tabs)) { - return; - } + 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) { + return; + } - echo $args['before_widget']; - if ($title) { - echo $args['before_title'] . esc_html($title) . $args['after_title']; - } + 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 + } - echo '
'; + $rest = rest_url('aksrt/v1/shop/frontend/widgets/products'); + echo '
'; - // Tab headers - echo '
'; - foreach ($tabs as $i => $tab) { - $active = $i === 0 ? ' is-active' : ''; - echo ''; - } - echo '
'; + echo '
'; + foreach ($tabs as $i => $tab) { + $active = $i === 0 ? ' is-active' : ''; + echo ''; + } + echo '
'; - // Tab panels - foreach ($tabs as $i => $tab) { - $hidden = $i > 0 ? ' style="display:none"' : ''; - echo '
'; + foreach ($tabs as $i => $tab) { + $hidden = $i > 0 ? ' style="display:none"' : ''; + $loaded = $i === 0 ? '1' : '0'; + echo '
'; - $query_args = [ - 'post_type' => 'aksrt_product', - 'post_status' => 'publish', - 'posts_per_page' => $per_page, - 'orderby' => 'date', - 'order' => 'DESC', - ]; + 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 '
'; + echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo ''; + echo '
'; + } else { + echo '
'; + echo '
'; + echo '
'; + echo '
'; + } + echo '
'; + } - $tax_query = []; - $cat_ids = array_filter(array_map('intval', explode(',', $tab['cat_ids']))); - $discount_ids = array_filter(array_map('intval', explode(',', $tab['discount_ids']))); - if (!empty($cat_ids)) { - $tax_query[] = ['taxonomy' => 'aksrt_product_cat', 'field' => 'term_id', 'terms' => $cat_ids]; - } - if (!empty($discount_ids)) { - $tax_query[] = ['taxonomy' => 'shop_discount', 'field' => 'term_id', 'terms' => $discount_ids]; - } - if (!empty($tax_query)) { - $query_args['tax_query'] = $tax_query; - } + // Lightweight tab switch + lazy REST load (no inline product HTML duplication). + echo ''; - $products = new \WP_Query($query_args); - if ($products->have_posts()) { - echo '
'; - while ($products->have_posts()) { - $products->the_post(); - $pid = get_the_ID(); - $card = class_exists('\AKSRT\Shop\Shop') - ? \AKSRT\Shop\Shop::get_instance()->build_product_card_view($pid) - : []; - $price = (float) ($card['price'] ?? 0); - $image = $card['image'] ?? ''; - echo '
'; - if ($image) { - echo ''; - echo '' . esc_attr(get_the_title($pid)) . ''; - echo ''; - } - echo '
'; - echo '' . esc_html(get_the_title($pid)) . ''; - echo '¥' . esc_html(number_format_i18n($price, 2)) . ''; - echo '
'; - } - echo '
'; - } else { - echo '

' . esc_html__('暂无商品', 'aksrt') . '

'; - } + echo '
'; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } - echo '
'; - wp_reset_postdata(); - } + public function form($instance): void { + $defaults = [ + 'title' => __('商品精选', 'aksrt'), + 'tabs' => "热门推荐|\n最新上架|", + 'per_page' => 8, + 'style' => 'default', + 'paginate' => 'ajax', + ]; + $instance = wp_parse_args($instance, $defaults); + ?> +

+ + +

+

+ + +

+

+ + +

+

+ + +

+

+ + +

+ '; - echo '(function(){'; - echo 'var w=document.currentScript.parentElement;'; - echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(btn){'; - echo 'btn.addEventListener("click",function(){'; - echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(b){b.classList.remove("is-active")});'; - echo 'w.querySelectorAll(".aksrt-product-tab__panel").forEach(function(p){p.style.display="none"});'; - echo 'btn.classList.add("is-active");'; - echo 'var idx=btn.getAttribute("data-tab-index");'; - echo 'var panel=w.querySelector(".aksrt-product-tab__panel[data-tab-index=\'\"+idx+\"\']");'; - echo 'if(panel)panel.style.display="";'; - echo '});'; - echo '});'; - echo '})();'; - echo ''; - - echo '
'; - echo $args['after_widget']; - } - - public function form($instance): void { - $defaults = [ - 'title' => __('商品精选', 'aksrt'), - 'tabs' => "热门推荐|\n最新上架|", - 'per_page' => 8, - ]; - $instance = wp_parse_args($instance, $defaults); - ?> -

- - -

-

- - -

-

- - -

- sanitize_text_field($new_instance['title'] ?? ''), - 'tabs' => sanitize_textarea_field($new_instance['tabs'] ?? ''), - 'per_page' => max(1, min(20, (int) ($new_instance['per_page'] ?? 8))), - ]; - } + 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' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')), + ]; + } }