feat(widgets): author card, forum tags, multi-tab topic lists
Add post-author card, forum tag term list, and multi-tab forum topic switcher widgets to close remaining zibll user/bbs widget gaps.
This commit is contained in:
1 parent
ee2042f936
commit
bf28ab64b8
6 files changed
+600
No files matched your search
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Forum topic tags / topic taxonomy list (zibll zib_bbs_widget_ui_topic_lists).
|
||||
*
|
||||
* @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',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$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 = $this->parse_ids((string) ($instance['include'] ?? ''));
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
|
||||
$term_args = [
|
||||
'taxonomy' => $tax,
|
||||
'number' => $number,
|
||||
'hide_empty' => $hide_empty,
|
||||
];
|
||||
if ($include) {
|
||||
$term_args['include'] = $include;
|
||||
$term_args['orderby'] = 'include';
|
||||
} 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';
|
||||
}
|
||||
}
|
||||
|
||||
$terms = get_terms($term_args);
|
||||
if (is_wp_error($terms) || !$terms) {
|
||||
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
|
||||
}
|
||||
|
||||
$list_cls = $style === 'card' ? 'aksrt-ftag-list is-card' : 'aksrt-ftag-list is-mini';
|
||||
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" 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>';
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user