Files
wordpress-theme-aksrt/src/AKSRT/Widgets/CategoryRecommendWidget.php
T
kate522 4368a3c586 feat(widgets): category recommend/tabs residual + batch22-24 CSS
Use graphic_card heroes and mini post strips for category recommend,
prefer shared mini list in category block sidebar fallback, admin empty
for product tabs, and forum/user residual styles.
2026-07-21 10:39:17 +08:00

263 lines
13 KiB
PHP

<?php
/**
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
/**
* Homepage category recommend cards widget.
*/
class CategoryRecommendWidget extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_category_recommend',
esc_html__('AKSRT 分类推荐卡片', 'aksrt'),
[
'classname' => 'widget_aksrt_category_recommend',
'description' => esc_html__('分类推荐卡片区块,可挂载到首页内容挂载区。', 'aksrt'),
'customize_selective_refresh' => true,
]
);
}
public static function render_html(array $instance): string {
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 4);
$posts_per_cat = max(1, min(8, (int) ($instance['posts_per_category'] ?? 5)));
$columns = max(1, min(4, (int) ($instance['columns'] ?? 2)));
$show_views = ! empty($instance['show_views']);
$show_description = ! empty($instance['show_description']);
$show_dates = ! empty($instance['show_dates']);
$show_more = ! empty($instance['show_more']);
if (empty($category_ids)) {
$category_ids = aksrt_home_widget_fallback_category_ids(4);
}
$cards = self::build_cards_static($category_ids, $posts_per_cat, $show_views, $show_description);
if (empty($cards)) {
return function_exists('aksrt_get_widget_null')
? aksrt_get_widget_null(__('暂无推荐分类', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无推荐分类', 'aksrt') . '</p>';
}
$lazy = aksrt_home_widget_get_lazyload_args();
ob_start();
printf('<div class="aksrt-category-recommend__grid ajax-item" style="--aksrt-recommend-columns:%d;">', (int) $columns);
foreach ($cards as $card) {
$cover = (string) ($card['cover'] ?? '');
$link = (string) ($card['link'] ?? '');
$name = (string) ($card['name'] ?? '');
$badge = sprintf(_n('%d 篇文章', '%d 篇文章', (int) $card['count'], 'aksrt'), (int) $card['count']);
$more = '';
if ($show_views && (int) $card['views'] > 0) {
$v = function_exists('aksrt_format_number') ? aksrt_format_number((int) $card['views']) : number_format_i18n((int) $card['views']);
$more = '<span class="aksrt-category-recommend__views"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $v) . '</span>';
}
echo '<article class="aksrt-category-recommend__card" data-aksrt-animate="fade-up">';
if (function_exists('aksrt_graphic_card') && $cover !== '') {
echo aksrt_graphic_card([ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'type' => 'style-2',
'class' => 'aksrt-category-recommend__hero mb10',
'img' => $cover,
'alt' => $name,
'link' => ['url' => $link],
'text1' => $name,
'text2' => $show_description ? (string) $card['description'] : '',
'text3' => $badge,
'more' => $more,
'lazy' => true,
]);
} else {
echo '<div class="aksrt-category-recommend__hero">';
echo '<a href="' . esc_url($link) . '">';
if ($lazy['enabled']) {
echo '<span class="aksrt-skeleton"></span>';
echo '<img class="aksrt-category-recommend__cover aksrt-lazy" data-src="' . esc_url($cover) . '" src="' . esc_url($lazy['placeholder_url']) . '" alt="' . esc_attr($name) . '">';
} else {
echo '<img class="aksrt-category-recommend__cover" src="' . esc_url($cover) . '" alt="' . esc_attr($name) . '">';
}
echo '</a>';
echo '<span class="aksrt-category-recommend__badge">' . esc_html($badge) . '</span>';
echo $more; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
echo '<div class="aksrt-category-recommend__meta">';
echo '<h4 class="aksrt-category-recommend__name"><a href="' . esc_url($link) . '">' . esc_html($name) . '</a></h4>';
if ($show_description && $card['description'] !== '') {
echo '<p class="aksrt-category-recommend__desc">' . esc_html((string) $card['description']) . '</p>';
}
echo '</div>';
}
echo '<div class="aksrt-category-recommend__body">';
// Prefer shared mini list when post IDs available.
if (!empty($card['post_ids']) && function_exists('aksrt_render_mini_list')) {
$q = new \WP_Query([
'post_type' => 'any',
'post__in' => array_map('intval', (array) $card['post_ids']),
'orderby' => 'post__in',
'posts_per_page' => count((array) $card['post_ids']),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
]);
echo '<div class="aksrt-category-recommend__posts posts-mini-wrap">';
aksrt_render_mini_list($q, ['context' => 'widget', 'show_thumb' => true, 'show_meta' => $show_dates]);
echo '</div>';
wp_reset_postdata();
} else {
echo '<ul class="aksrt-category-recommend__posts">';
foreach ($card['posts'] as $post_item) {
echo '<li class="aksrt-category-recommend__post">';
echo '<a class="aksrt-category-recommend__post-link" href="' . esc_url($post_item['link']) . '">' . esc_html($post_item['title']) . '</a>';
if ($show_dates && $post_item['date'] !== '') {
echo '<span class="aksrt-category-recommend__date">' . esc_html($post_item['date']) . '</span>';
}
echo '</li>';
}
echo '</ul>';
}
if ($show_more) {
echo '<div class="aksrt-home-widget__footer">';
echo '<a class="aksrt-home-widget__more" href="' . esc_url($link) . '">' . esc_html__('查看更多', 'aksrt') . '</a>';
echo '</div>';
}
echo '</div></article>';
}
echo '</div>';
return (string) ob_get_clean();
}
public function widget($args, $instance): void {
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
$title = $title !== '' ? $title : __('分类推荐', 'aksrt');
$html = self::render_html($instance);
echo $args['before_widget'];
echo '<section class="aksrt-home-widget aksrt-home-widget--recommend">';
aksrt_home_widget_render_header($title);
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</section>';
echo $args['after_widget'];
}
public function form($instance): void {
$title = (string) ($instance['title'] ?? __('分类推荐', 'aksrt'));
$category_ids = aksrt_home_widget_normalize_category_ids($instance['category_ids'] ?? [], 4);
$posts_per_cat = (int) ($instance['posts_per_category'] ?? 5);
$columns = (int) ($instance['columns'] ?? 2);
$show_views = ! empty($instance['show_views']);
$show_description = ! array_key_exists('show_description', $instance) || ! empty($instance['show_description']);
$show_dates = ! array_key_exists('show_dates', $instance) || ! empty($instance['show_dates']);
$show_more = ! array_key_exists('show_more', $instance) || ! empty($instance['show_more']);
$categories = aksrt_home_widget_available_categories();
?>
<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($title); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('category_ids')); ?>"><?php esc_html_e('分类(最多 4 个):', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('category_ids')); ?>[]" multiple size="6">
<?php foreach ($categories as $category) : ?>
<option value="<?php echo (int) $category->term_id; ?>" <?php selected(in_array((int) $category->term_id, $category_ids, true)); ?>>
<?php echo esc_html($category->name); ?>
</option>
<?php endforeach; ?>
</select>
<small><?php esc_html_e('留空时自动选择热门分类。', 'aksrt'); ?></small>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('posts_per_category')); ?>"><?php esc_html_e('每个分类展示文章数:', 'aksrt'); ?></label>
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('posts_per_category')); ?>" name="<?php echo esc_attr($this->get_field_name('posts_per_category')); ?>" type="number" min="1" max="8" value="<?php echo (int) $posts_per_cat; ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('columns')); ?>"><?php esc_html_e('桌面列数:', 'aksrt'); ?></label>
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('columns')); ?>" name="<?php echo esc_attr($this->get_field_name('columns')); ?>" type="number" min="1" max="4" value="<?php echo (int) $columns; ?>">
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_views); ?> id="<?php echo esc_attr($this->get_field_id('show_views')); ?>" name="<?php echo esc_attr($this->get_field_name('show_views')); ?>" value="1">
<label for="<?php echo esc_attr($this->get_field_id('show_views')); ?>"><?php esc_html_e('显示分类总浏览量', 'aksrt'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_description); ?> id="<?php echo esc_attr($this->get_field_id('show_description')); ?>" name="<?php echo esc_attr($this->get_field_name('show_description')); ?>" value="1">
<label for="<?php echo esc_attr($this->get_field_id('show_description')); ?>"><?php esc_html_e('显示分类描述', 'aksrt'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_dates); ?> id="<?php echo esc_attr($this->get_field_id('show_dates')); ?>" name="<?php echo esc_attr($this->get_field_name('show_dates')); ?>" value="1">
<label for="<?php echo esc_attr($this->get_field_id('show_dates')); ?>"><?php esc_html_e('显示文章时间', 'aksrt'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_more); ?> id="<?php echo esc_attr($this->get_field_id('show_more')); ?>" name="<?php echo esc_attr($this->get_field_name('show_more')); ?>" value="1">
<label for="<?php echo esc_attr($this->get_field_id('show_more')); ?>"><?php esc_html_e('显示“查看更多”链接', 'aksrt'); ?></label>
</p>
<?php if (function_exists('aksrt_widget_render_location_help')) { aksrt_widget_render_location_help('homepage'); } ?>
<?php
}
public function update($new_instance, $old_instance): array {
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'category_ids' => aksrt_home_widget_normalize_category_ids($new_instance['category_ids'] ?? [], 4),
'posts_per_category' => max(1, min(8, absint($new_instance['posts_per_category'] ?? 5))),
'columns' => max(1, min(4, absint($new_instance['columns'] ?? 2))),
'show_views' => ! empty($new_instance['show_views']) ? 1 : 0,
'show_description' => ! empty($new_instance['show_description']) ? 1 : 0,
'show_dates' => ! empty($new_instance['show_dates']) ? 1 : 0,
'show_more' => ! empty($new_instance['show_more']) ? 1 : 0,
];
}
/**
* Build category recommend cards.
*
* @param array<int, int> $category_ids Category ids.
* @param int $posts_per_cat Post count per category.
* @param bool $show_views Whether to calculate views.
* @param bool $show_description Whether to fetch description.
* @return array<int, array<string, mixed>>
*/
public static function build_cards_static(array $category_ids, int $posts_per_cat, bool $show_views, bool $show_description): array {
$cards = [];
foreach ($category_ids as $category_id) {
$term = get_term($category_id, 'category');
if (! $term || is_wp_error($term)) {
continue;
}
$posts = get_posts([
'post_type' => aksrt_home_widget_post_types(),
'cat' => $category_id,
'posts_per_page' => $posts_per_cat,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
]);
$post_items = [];
foreach ($posts as $post) {
$post_items[] = [
'title' => get_the_title($post->ID),
'link' => get_permalink($post->ID),
'date' => function_exists('aksrt_time_ago') ? aksrt_time_ago($post->post_date) : get_the_date('Y-m-d', $post->ID),
];
}
$cards[] = [
'name' => $term->name,
'link' => get_category_link($category_id),
'cover' => class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_category_cover_url($category_id) : (get_template_directory_uri() . '/assets/img/placeholder.svg'),
'description' => $show_description && class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_category_recommend_desc($category_id) : ($show_description ? (string) $term->description : ''),
'count' => (int) $term->count,
'views' => $show_views && function_exists('aksrt_get_category_total_views') ? aksrt_get_category_total_views($category_id) : 0,
'posts' => $post_items,
];
}
return $cards;
}
}