Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetForumTopicTerms.php
T
kate522 8b5ca8f287 feat(widgets): ranking identity, moderators follow, plate chrome
Align UserRanking to zibll user-ranking-item + aksrt_get_user_name + badg,
render moderators via shared rows with follow, deepen plate info cover/stats,
and clean forum tab/terms empty shells.
2026-07-21 10:39:17 +08:00

174 lines
6.6 KiB
PHP

<?php
/**
* Forum topic tags list (zibll topic_lists + remote HTML).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetForumTopicTerms extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_topic_terms',
esc_html__('论坛 - 话题标签列表', 'aksrt'),
[
'description' => esc_html__('展示论坛话题标签(aksrt_forum_tag)。', 'aksrt'),
'classname' => 'widget_aksrt_forum_topic_terms',
]
);
}
/** @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 '';
}
$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'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无话题', 'aksrt') . '</p>';
}
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);
if (is_wp_error($link)) {
continue;
}
$icon = (string) get_term_meta($term->term_id, '_aksrt_icon', true);
echo '<a class="aksrt-ftag-item but p2-10' . ($style === 'card' ? ' is-card' : '') . '" href="' . esc_url($link) . '">';
if ($icon !== '') {
echo '<i class="' . esc_attr($icon) . '" aria-hidden="true"></i> ';
}
echo '<span class="aksrt-ftag-item__name">' . esc_html($term->name) . '</span>';
echo '<span class="aksrt-ftag-item__count">' . esc_html((string) (int) $term->count) . '</span>';
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 aksrt-ftag-wrap">';
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '</div>';
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 {
?>
<p>
<label><?php esc_html_e('标题', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? __('话题', 'aksrt'))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('数量', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="40" name="<?php echo esc_attr($this->get_field_name('number')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['number'] ?? 12))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
<option value="count" <?php selected(($instance['orderby'] ?? 'count'), 'count'); ?>><?php esc_html_e('帖子数', 'aksrt'); ?></option>
<option value="name" <?php selected(($instance['orderby'] ?? ''), 'name'); ?>><?php esc_html_e('名称', 'aksrt'); ?></option>
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('样式', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
<option value="mini" <?php selected(($instance['style'] ?? 'mini'), 'mini'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
<option value="card" <?php selected(($instance['style'] ?? ''), 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
</select>
</label>
</p>
<p>
<label><?php esc_html_e('包含 term ID', 'aksrt'); ?>
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('include')); ?>" value="<?php echo esc_attr((string) ($instance['include'] ?? '')); ?>">
</label>
</p>
<p>
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('hide_empty')); ?>" value="1" <?php checked(!isset($instance['hide_empty']) || !empty($instance['hide_empty'])); ?>> <?php esc_html_e('隐藏空标签', 'aksrt'); ?></label>
</p>
<?php
}
public function update($new_instance, $old_instance): array {
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'count'));
if (!in_array($orderby, ['count', 'name', 'views'], true)) {
$orderby = 'count';
}
return [
'title' => 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,
];
}
}