esc_html__('展示论坛话题标签(aksrt_forum_tag)。', 'aksrt'), 'classname' => 'widget_aksrt_forum_topic_terms', ] ); } /** @return array */ 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 ''; } $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 = self::parse_ids_static((string) ($instance['include'] ?? '')); $style = sanitize_key((string) ($instance['style'] ?? 'mini')); $term_args = [ 'taxonomy' => $tax, 'number' => $number, '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 { $term_args['orderby'] = 'count'; $term_args['order'] = 'DESC'; } $terms = get_terms($term_args); if (is_wp_error($terms) || !$terms) { return function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无话题', 'aksrt')) : '

' . esc_html__('暂无话题', 'aksrt') . '

'; } ob_start(); $list_cls = $style === 'card' ? 'aksrt-ftag-list is-card ajax-item' : 'aksrt-ftag-list is-mini ajax-item'; echo '
'; foreach ($terms as $term) { $link = get_term_link($term); if (is_wp_error($link)) { continue; } $icon = (string) get_term_meta($term->term_id, '_aksrt_icon', true); echo ''; if ($icon !== '') { echo ' '; } echo '' . esc_html($term->name) . ''; echo '' . esc_html((string) (int) $term->count) . ''; echo ''; } echo '
'; 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 '
'; echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } private function parse_ids(string $raw): array { return self::parse_ids_static($raw); } public function form($instance): void { ?>

sanitize_text_field((string) ($new_instance['title'] ?? '')), 'number' => max(1, min(40, (int) ($new_instance['number'] ?? 12))), 'orderby' => $orderby, 'style' => ($new_instance['style'] ?? '') === 'card' ? 'card' : 'mini', 'include' => sanitize_text_field((string) ($new_instance['include'] ?? '')), 'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0, ]; } }