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:
@@ -22,18 +22,32 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
/** @return array<int,int> */
|
||||
public static function parse_ids_static(string $raw): array {
|
||||
if ($raw === '') {
|
||||
return [];
|
||||
}
|
||||
$ids = [];
|
||||
foreach (preg_split('/[\s,]+/', $raw) ?: [] as $p) {
|
||||
$id = (int) $p;
|
||||
if ($id > 0) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
}
|
||||
|
||||
public static function render_html(array $instance): string {
|
||||
$taxonomy = sanitize_key((string) ($instance['taxonomy'] ?? 'category'));
|
||||
if (!taxonomy_exists($taxonomy)) {
|
||||
$taxonomy = 'category';
|
||||
}
|
||||
$term_ids = $this->parse_ids((string) ($instance['term_ids'] ?? ''));
|
||||
$term_ids = self::parse_ids_static((string) ($instance['term_ids'] ?? ''));
|
||||
$count = max(1, min(12, (int) ($instance['count'] ?? 4)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
$columns = max(1, min(3, (int) ($instance['columns'] ?? 1)));
|
||||
|
||||
if (!$term_ids) {
|
||||
// Fallback: top terms by count.
|
||||
$terms = get_terms([
|
||||
'taxonomy' => $taxonomy,
|
||||
'number' => max(1, min(4, (int) ($instance['auto_terms'] ?? 2))),
|
||||
@@ -42,18 +56,15 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
'hide_empty' => true,
|
||||
]);
|
||||
if (is_wp_error($terms) || !$terms) {
|
||||
return;
|
||||
return function_exists('aksrt_get_ajax_null')
|
||||
? aksrt_get_ajax_null(__('暂无分类', 'aksrt'))
|
||||
: '<p class="widget-empty ajax-item">' . esc_html__('暂无分类', 'aksrt') . '</p>';
|
||||
}
|
||||
$term_ids = array_map(static fn($t) => (int) $t->term_id, $terms);
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-term-agg" style="--aksrt-ta-cols:' . esc_attr((string) $columns) . '">';
|
||||
ob_start();
|
||||
echo '<div class="aksrt-term-agg ajax-item" style="--aksrt-ta-cols:' . esc_attr((string) $columns) . '">';
|
||||
foreach ($term_ids as $term_id) {
|
||||
$term = get_term($term_id, $taxonomy);
|
||||
if (!$term || is_wp_error($term)) {
|
||||
@@ -118,23 +129,26 @@ class WidgetTermAggregation extends \WP_Widget {
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$html = self::render_html($instance);
|
||||
if ($html === '') {
|
||||
return;
|
||||
}
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/** @return array<int,int> */
|
||||
private function parse_ids(string $raw): array {
|
||||
if ($raw === '') {
|
||||
return [];
|
||||
}
|
||||
$ids = [];
|
||||
foreach (preg_split('/[\s,]+/', $raw) ?: [] as $p) {
|
||||
$id = (int) $p;
|
||||
if ($id > 0) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
return array_values(array_unique($ids));
|
||||
return self::parse_ids_static($raw);
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
|
||||
Reference in New Issue
Block a user