411 lines
20 KiB
PHP
411 lines
20 KiB
PHP
<?php
|
|
/**
|
|
* Shop product widgets (§6.9)
|
|
*
|
|
* - AKSRT_Widget_Product_List: product list widget (filter/sort/paginate/AJAX)
|
|
* - AKSRT_Widget_Product_Oneline: horizontal swiper product widget
|
|
* - AKSRT_Widget_Product_Tab: multi-tab product list widget
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Product list widget — equivalent to zibll `zib_shop_widget_ui_product_lists`.
|
|
*
|
|
* Supports: include/exclude category, discount, tag; sort order; list style;
|
|
* AJAX pagination; per-page count.
|
|
*/
|
|
class AKSRT_Widget_Product_List extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_product_list',
|
|
__('商品列表', 'aksrt'),
|
|
['description' => __('AKSRT 商品列表组件:筛选、排序、AJAX 翻页。', 'aksrt')]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
$title = apply_filters('widget_title', $instance['title'] ?? '');
|
|
$per_page = max(1, min(50, (int) ($instance['per_page'] ?? 8)));
|
|
$orderby = sanitize_key($instance['orderby'] ?? 'date');
|
|
$order = in_array(strtoupper($instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($instance['order']) : 'DESC';
|
|
$cat_ids = array_filter(array_map('intval', explode(',', (string) ($instance['cat_ids'] ?? ''))));
|
|
$tag_ids = array_filter(array_map('intval', explode(',', (string) ($instance['tag_ids'] ?? ''))));
|
|
$discount_ids = array_filter(array_map('intval', explode(',', (string) ($instance['discount_ids'] ?? ''))));
|
|
$exclude_cat = !empty($instance['exclude_cat']);
|
|
$style = sanitize_key($instance['style'] ?? 'default');
|
|
$paged = max(1, (int) ($_GET['aksrt_product_page'] ?? 1));
|
|
|
|
$query_args = [
|
|
'post_type' => 'aksrt_product',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => $per_page,
|
|
'paged' => $paged,
|
|
'orderby' => $orderby === 'sales' ? 'meta_value_num' : $orderby,
|
|
'order' => $order,
|
|
];
|
|
|
|
if ($orderby === 'sales') {
|
|
$query_args['meta_key'] = '_aksrt_sales';
|
|
}
|
|
|
|
// Taxonomy filters
|
|
$tax_query = [];
|
|
if (!empty($cat_ids)) {
|
|
$tax_query[] = [
|
|
'taxonomy' => 'aksrt_product_cat',
|
|
'field' => 'term_id',
|
|
'terms' => $cat_ids,
|
|
'operator' => $exclude_cat ? 'NOT IN' : 'IN',
|
|
];
|
|
}
|
|
if (!empty($tag_ids)) {
|
|
$tax_query[] = [
|
|
'taxonomy' => 'aksrt_product_tag',
|
|
'field' => 'term_id',
|
|
'terms' => $tag_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;
|
|
}
|
|
|
|
$products = new WP_Query($query_args);
|
|
|
|
echo $args['before_widget'];
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
|
}
|
|
|
|
$style_class = 'aksrt-product-widget--' . $style;
|
|
echo '<div class="aksrt-product-widget ' . esc_attr($style_class) . '" data-aksrt-product-widget>';
|
|
|
|
if ($products->have_posts()) {
|
|
echo '<div class="aksrt-product-widget__grid">';
|
|
while ($products->have_posts()) {
|
|
$products->the_post();
|
|
$pid = get_the_ID();
|
|
$card = class_exists('AKSRT_Shop')
|
|
? AKSRT_Shop::get_instance()->build_product_card_view($pid)
|
|
: [];
|
|
$price = (float) ($card['price'] ?? 0);
|
|
$image = $card['image'] ?? '';
|
|
echo '<div class="aksrt-product-widget__item">';
|
|
if ($image) {
|
|
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
|
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
|
echo '</a>';
|
|
}
|
|
echo '<div class="aksrt-product-widget__info">';
|
|
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
|
echo '<div class="aksrt-product-widget__meta">';
|
|
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
|
if (!empty($card['sales'])) {
|
|
echo '<span class="aksrt-product-widget__sales">' . esc_html($card['sales']) . __(' 已售', 'aksrt') . '</span>';
|
|
}
|
|
echo '</div></div></div>';
|
|
}
|
|
echo '</div>';
|
|
|
|
// Pagination
|
|
if ($products->max_num_pages > 1) {
|
|
echo '<div class="aksrt-product-widget__pagination">';
|
|
echo paginate_links([
|
|
'base' => add_query_arg('aksrt_product_page', '%#%'),
|
|
'format' => '',
|
|
'current' => $paged,
|
|
'total' => $products->max_num_pages,
|
|
'prev_text' => '«',
|
|
'next_text' => '»',
|
|
]);
|
|
echo '</div>';
|
|
}
|
|
} else {
|
|
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
|
}
|
|
|
|
echo '</div>';
|
|
echo $args['after_widget'];
|
|
|
|
wp_reset_postdata();
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$defaults = [
|
|
'title' => __('商品推荐', 'aksrt'),
|
|
'per_page' => 8,
|
|
'orderby' => 'date',
|
|
'order' => 'DESC',
|
|
'cat_ids' => '',
|
|
'tag_ids' => '',
|
|
'discount_ids' => '',
|
|
'exclude_cat' => false,
|
|
'style' => 'default',
|
|
];
|
|
$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($instance['title']); ?>">
|
|
</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="50" 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('orderby')); ?>"><?php esc_html_e('排序:', 'aksrt'); ?></label>
|
|
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby')); ?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
|
<option value="date" <?php selected($instance['orderby'], 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
|
<option value="sales" <?php selected($instance['orderby'], 'sales'); ?>><?php esc_html_e('销量', 'aksrt'); ?></option>
|
|
<option value="title" <?php selected($instance['orderby'], 'title'); ?>><?php esc_html_e('标题', 'aksrt'); ?></option>
|
|
<option value="rand" <?php selected($instance['orderby'], 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
|
</select>
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php esc_html_e('方向:', 'aksrt'); ?></label>
|
|
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
|
|
<option value="DESC" <?php selected($instance['order'], 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
|
|
<option value="ASC" <?php selected($instance['order'], 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
|
|
</select>
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>"><?php esc_html_e('分类ID(逗号分隔):', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_ids')); ?>" value="<?php echo esc_attr($instance['cat_ids']); ?>">
|
|
</p>
|
|
<p>
|
|
<label>
|
|
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('exclude_cat')); ?>" value="1" <?php checked($instance['exclude_cat']); ?>>
|
|
<?php esc_html_e('排除上述分类', 'aksrt'); ?>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>"><?php esc_html_e('标签ID(逗号分隔):', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('tag_ids')); ?>" value="<?php echo esc_attr($instance['tag_ids']); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>"><?php esc_html_e('优惠活动ID(逗号分隔):', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('discount_ids')); ?>" value="<?php echo esc_attr($instance['discount_ids']); ?>">
|
|
</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="list" <?php selected($instance['style'], 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
|
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
|
</select>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
return [
|
|
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
|
'per_page' => max(1, min(50, (int) ($new_instance['per_page'] ?? 8))),
|
|
'orderby' => sanitize_key($new_instance['orderby'] ?? 'date'),
|
|
'order' => in_array(strtoupper($new_instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($new_instance['order']) : 'DESC',
|
|
'cat_ids' => sanitize_text_field($new_instance['cat_ids'] ?? ''),
|
|
'tag_ids' => sanitize_text_field($new_instance['tag_ids'] ?? ''),
|
|
'discount_ids' => sanitize_text_field($new_instance['discount_ids'] ?? ''),
|
|
'exclude_cat' => !empty($new_instance['exclude_cat']),
|
|
'style' => sanitize_key($new_instance['style'] ?? 'default'),
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Horizontal swiper product widget — equivalent to zibll `zib_shop_widget_ui_oneline_product_lists`.
|
|
*/
|
|
class AKSRT_Widget_Product_Oneline extends AKSRT_Widget_Product_List {
|
|
|
|
public function __construct() {
|
|
WP_Widget::__construct(
|
|
'aksrt_product_oneline',
|
|
__('横滑商品', 'aksrt'),
|
|
['description' => __('AKSRT 单行横滑商品 Swiper 组件。', 'aksrt')]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
$instance['style'] = 'oneline';
|
|
parent::widget($args, $instance);
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$instance = wp_parse_args($instance, ['style' => 'oneline']);
|
|
parent::form($instance);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Multi-tab product widget — equivalent to zibll `zib_shop_widget_ui_tab_product`.
|
|
* Each tab has independent category/discount filter.
|
|
*/
|
|
class AKSRT_Widget_Product_Tab extends WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_product_tab',
|
|
__('多TAB商品', 'aksrt'),
|
|
['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选)。', '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)));
|
|
|
|
// 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;
|
|
}
|
|
|
|
echo $args['before_widget'];
|
|
if ($title) {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
|
}
|
|
|
|
echo '<div class="aksrt-product-tab-widget" data-aksrt-product-tab>';
|
|
|
|
// Tab headers
|
|
echo '<div class="aksrt-product-tab__headers">';
|
|
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 esc_html($tab['label']);
|
|
echo '</button>';
|
|
}
|
|
echo '</div>';
|
|
|
|
// Tab panels
|
|
foreach ($tabs as $i => $tab) {
|
|
$hidden = $i > 0 ? ' style="display:none"' : '';
|
|
echo '<div class="aksrt-product-tab__panel" data-tab-index="' . esc_attr((string) $i) . '"' . $hidden . '>';
|
|
|
|
$query_args = [
|
|
'post_type' => 'aksrt_product',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => $per_page,
|
|
'orderby' => 'date',
|
|
'order' => 'DESC',
|
|
];
|
|
|
|
$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;
|
|
}
|
|
|
|
$products = new WP_Query($query_args);
|
|
if ($products->have_posts()) {
|
|
echo '<div class="aksrt-product-widget__grid">';
|
|
while ($products->have_posts()) {
|
|
$products->the_post();
|
|
$pid = get_the_ID();
|
|
$card = class_exists('AKSRT_Shop')
|
|
? AKSRT_Shop::get_instance()->build_product_card_view($pid)
|
|
: [];
|
|
$price = (float) ($card['price'] ?? 0);
|
|
$image = $card['image'] ?? '';
|
|
echo '<div class="aksrt-product-widget__item">';
|
|
if ($image) {
|
|
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
|
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
|
echo '</a>';
|
|
}
|
|
echo '<div class="aksrt-product-widget__info">';
|
|
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
|
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
|
echo '</div></div>';
|
|
}
|
|
echo '</div>';
|
|
} else {
|
|
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
|
}
|
|
|
|
echo '</div>';
|
|
wp_reset_postdata();
|
|
}
|
|
|
|
// Tab switching JS
|
|
echo '<script>';
|
|
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 '</script>';
|
|
|
|
echo '</div>';
|
|
echo $args['after_widget'];
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$defaults = [
|
|
'title' => __('商品精选', 'aksrt'),
|
|
'tabs' => "热门推荐|\n最新上架|",
|
|
'per_page' => 8,
|
|
];
|
|
$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($instance['title']); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('tabs')); ?>"><?php esc_html_e('TAB配置(每行: 名称|分类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($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>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
return [
|
|
'title' => 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))),
|
|
];
|
|
}
|
|
}
|