feat(forum/shop): hot/recent topics AJAX + term lists HTML

Forum hot/recent share query_payload with load-more; topic terms
remote HTML; product oneline forces oneline style on shared list.
This commit is contained in:
2026-07-20 21:58:33 +08:00
parent 6b8d7a9d0a
commit 7f051148e4
4 changed files with 278 additions and 212 deletions
+51 -38
View File
@@ -1,6 +1,6 @@
<?php
/**
* Forum topic tags / topic taxonomy list (zibll zib_bbs_widget_ui_topic_lists).
* Forum topic tags list (zibll topic_lists + remote HTML).
*
* @package AKSRT
*/
@@ -22,52 +22,60 @@ class WidgetForumTopicTerms 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 {
$tax = taxonomy_exists('aksrt_forum_tag') ? 'aksrt_forum_tag' : (taxonomy_exists('aksrt_forum_topic_cat') ? 'aksrt_forum_topic_cat' : '');
if ($tax === '') {
return;
return '';
}
$number = max(1, min(40, (int) ($instance['number'] ?? 12)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'count'));
$hide_empty = !isset($instance['hide_empty']) || !empty($instance['hide_empty']);
$include = $this->parse_ids((string) ($instance['include'] ?? ''));
$include = self::parse_ids_static((string) ($instance['include'] ?? ''));
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
$term_args = [
'taxonomy' => $tax,
'number' => $number,
'hide_empty' => $hide_empty,
'hide_empty' => (bool) $hide_empty,
];
if ($include) {
$term_args['include'] = $include;
$term_args['orderby'] = 'include';
} elseif ($orderby === 'name') {
$term_args['orderby'] = 'name';
$term_args['order'] = 'ASC';
} elseif ($orderby === 'views') {
$term_args['meta_key'] = '_aksrt_views';
$term_args['orderby'] = 'meta_value_num';
$term_args['order'] = 'DESC';
} else {
if ($orderby === 'name') {
$term_args['orderby'] = 'name';
$term_args['order'] = 'ASC';
} elseif ($orderby === 'views') {
$term_args['meta_key'] = '_aksrt_views';
$term_args['orderby'] = 'meta_value_num';
$term_args['order'] = 'DESC';
} else {
$term_args['orderby'] = 'count';
$term_args['order'] = 'DESC';
}
$term_args['orderby'] = 'count';
$term_args['order'] = 'DESC';
}
$terms = get_terms($term_args);
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>';
}
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
}
$list_cls = $style === 'card' ? 'aksrt-ftag-list is-card' : 'aksrt-ftag-list is-mini';
ob_start();
$list_cls = $style === 'card' ? 'aksrt-ftag-list is-card ajax-item' : 'aksrt-ftag-list is-mini ajax-item';
echo '<div class="' . esc_attr($list_cls) . '">';
foreach ($terms as $term) {
$link = get_term_link($term);
@@ -84,23 +92,28 @@ class WidgetForumTopicTerms extends \WP_Widget {
echo '</a>';
}
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
}
// IAS-style wrap for remote refresh.
echo '<div class="zib-widget padding-6 ajaxpager" data-aksrt-ajaxpager>';
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
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 {