feat(widgets): category block AJAX + recommend/term render_html
Expose static HTML/query payloads for homepage category widgets, tag cloud, categories, and term card/aggregation remote refresh.
This commit is contained in:
1 parent
2396ca98fb
commit
5a900d7385
6 files changed
+227
-105
No files matched your search
@@ -24,9 +24,7 @@ class CategoryRecommendWidget extends \WP_Widget {
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '', $instance, $this->id_base);
|
||||
$title = $title !== '' ? $title : __('分类推荐', 'aksrt');
|
||||
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)));
|
||||
@@ -39,23 +37,16 @@ class CategoryRecommendWidget extends \WP_Widget {
|
||||
$category_ids = aksrt_home_widget_fallback_category_ids(4);
|
||||
}
|
||||
|
||||
$cards = $this->build_cards($category_ids, $posts_per_cat, $show_views, $show_description);
|
||||
|
||||
echo $args['before_widget'];
|
||||
echo '<section class="aksrt-home-widget aksrt-home-widget--recommend">';
|
||||
|
||||
aksrt_home_widget_render_header($title);
|
||||
|
||||
$cards = self::build_cards_static($category_ids, $posts_per_cat, $show_views, $show_description);
|
||||
if (empty($cards)) {
|
||||
echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无推荐分类', 'aksrt')) : '<p class="widget-empty">' . esc_html__('暂无推荐分类', 'aksrt') . '</p>';
|
||||
echo '</section>';
|
||||
echo $args['after_widget'];
|
||||
return;
|
||||
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();
|
||||
printf('<div class="aksrt-category-recommend__grid" style="--aksrt-recommend-columns:%d;">', (int) $columns);
|
||||
|
||||
ob_start();
|
||||
printf('<div class="aksrt-category-recommend__grid ajax-item" style="--aksrt-recommend-columns:%d;">', (int) $columns);
|
||||
foreach ($cards as $card) {
|
||||
?>
|
||||
<article class="aksrt-category-recommend__card">
|
||||
@@ -108,8 +99,19 @@ class CategoryRecommendWidget extends \WP_Widget {
|
||||
</article>
|
||||
<?php
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
@@ -190,7 +192,7 @@ class CategoryRecommendWidget extends \WP_Widget {
|
||||
* @param bool $show_description Whether to fetch description.
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function build_cards(array $category_ids, int $posts_per_cat, bool $show_views, bool $show_description): array {
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user