feat(forum): plate info follow/compose + mods grid + board depth

Sidebar plate widgets gain follow/compose actions, super-mod roles,
board list sorting/styles, and matching CSS polish.
This commit is contained in:
kate522 committed 2026-07-20 22:57:10 +08:00
1 parent 2dd3b33cb7
commit 2396ca98fb
4 files changed
+424 -149

No files matched your search

+164 -85
View File
@@ -1,5 +1,7 @@
<?php
/**
* Forum Board List Widget (board taxonomy + plate CPT fallback).
*
* @package AKSRT
*/
@@ -7,97 +9,174 @@ namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
/**
* Forum Board List Widget
*/
class WidgetForumBoardList extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_board_list',
__('论坛 - 版块列表', 'aksrt'),
['description' => __('显示论坛版块列表及话题数量。', 'aksrt')]
);
}
public function __construct() {
parent::__construct(
'aksrt_forum_board_list',
__('论坛 - 版块列表', 'aksrt'),
['description' => __('显示论坛版块列表及话题数量。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
public static function render_html(array $instance): string {
$limit = max(1, min(50, (int) ($instance['count'] ?? $instance['showposts'] ?? 30)));
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'menu_order'));
$style = sanitize_key((string) ($instance['style'] ?? 'list'));
if (!in_array($style, ['list', 'mini'], true)) {
$style = 'list';
}
echo $args['before_widget'];
echo $args['before_title'] . esc_html($title) . $args['after_title'];
// Prefer plate CPT (same as plate_lists lite).
if (post_type_exists('aksrt_forum_plate')) {
$query_args = [
'post_type' => 'aksrt_forum_plate',
'post_status' => 'publish',
'posts_per_page' => $limit,
'order' => 'ASC',
'no_found_rows' => true,
];
switch ($orderby) {
case 'posts':
$query_args['meta_key'] = '_aksrt_posts_count';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'DESC';
break;
case 'views':
$query_args['meta_key'] = '_aksrt_views';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'DESC';
break;
case 'date':
$query_args['orderby'] = 'date';
$query_args['order'] = 'DESC';
break;
default:
$query_args['orderby'] = 'menu_order title';
}
$plates = get_posts($query_args);
if ($plates) {
ob_start();
echo '<ul class="aksrt-forum-widget__boards style-' . esc_attr($style) . ' ajax-item">';
foreach ($plates as $plate) {
$icon = (string) get_post_meta($plate->ID, '_aksrt_icon', true);
$count = (int) get_post_meta($plate->ID, '_aksrt_posts_count', true);
$thumb = get_the_post_thumbnail_url($plate->ID, 'thumbnail');
echo '<li class="aksrt-forum-widget__board">';
echo '<a href="' . esc_url(get_permalink($plate)) . '">';
if ($style === 'mini' && $thumb) {
echo '<img class="aksrt-forum-widget__thumb" src="' . esc_url($thumb) . '" alt="" width="28" height="28" loading="lazy" />';
} elseif ($icon !== '') {
echo '<i class="' . esc_attr($icon) . '"></i> ';
}
echo '<span class="aksrt-forum-widget__name">' . esc_html(get_the_title($plate)) . '</span>';
echo '<span class="aksrt-forum-widget__count">' . esc_html((string) $count) . '</span>';
echo '</a></li>';
}
echo '</ul>';
return (string) ob_get_clean();
}
}
$rendered = false;
if (post_type_exists('aksrt_forum_plate')) {
$plates = get_posts([
'post_type' => 'aksrt_forum_plate',
'post_status' => 'publish',
'posts_per_page' => 30,
'orderby' => 'menu_order title',
'order' => 'ASC',
]);
if ($plates) {
$rendered = true;
echo '<ul class="aksrt-forum-widget__boards">';
foreach ($plates as $plate) {
$icon = (string) get_post_meta($plate->ID, '_aksrt_icon', true);
$count = (int) get_post_meta($plate->ID, '_aksrt_posts_count', true);
echo '<li class="aksrt-forum-widget__board">';
echo '<a href="' . esc_url(get_permalink($plate)) . '">';
if ($icon !== '') {
echo '<i class="' . esc_attr($icon) . '"></i> ';
}
echo esc_html(get_the_title($plate));
echo '<span class="aksrt-forum-widget__count">' . $count . '</span>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
}
if (!$rendered) {
$boards = get_terms([
'taxonomy' => 'aksrt_forum_board',
'hide_empty' => true,
'orderby' => 'meta_value_num',
'meta_key' => '_aksrt_sort_order',
'order' => 'ASC',
]);
if (!is_wp_error($boards) && !empty($boards)) {
echo '<ul class="aksrt-forum-widget__boards">';
foreach ($boards as $board) {
$icon = get_term_meta($board->term_id, '_aksrt_icon', true);
echo '<li class="aksrt-forum-widget__board">';
echo '<a href="' . esc_url(get_term_link($board)) . '">';
if ($icon) {
echo '<i class="' . esc_attr($icon) . '"></i> ';
}
echo esc_html($board->name);
echo '<span class="aksrt-forum-widget__count">' . (int) $board->count . '</span>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
} else {
echo '<p class="aksrt-forum-widget__empty">' . esc_html__('暂无版块', 'aksrt') . '</p>';
}
}
// Taxonomy fallback.
$term_args = [
'taxonomy' => 'aksrt_forum_board',
'hide_empty' => !isset($instance['hide_empty']) || !empty($instance['hide_empty']),
'number' => $limit,
'order' => 'ASC',
];
if ($orderby === 'count') {
$term_args['orderby'] = 'count';
$term_args['order'] = 'DESC';
} elseif ($orderby === 'name') {
$term_args['orderby'] = 'name';
} else {
$term_args['orderby'] = 'meta_value_num';
$term_args['meta_key'] = '_aksrt_sort_order';
}
$boards = get_terms($term_args);
if (is_wp_error($boards) || empty($boards)) {
return function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无版块', 'aksrt'))
: '<p class="aksrt-forum-widget__empty ajax-item">' . esc_html__('暂无版块', 'aksrt') . '</p>';
}
ob_start();
echo '<ul class="aksrt-forum-widget__boards style-' . esc_attr($style) . ' ajax-item">';
foreach ($boards as $board) {
$icon = get_term_meta($board->term_id, '_aksrt_icon', true);
echo '<li class="aksrt-forum-widget__board">';
echo '<a href="' . esc_url(get_term_link($board)) . '">';
if ($icon) {
echo '<i class="' . esc_attr($icon) . '"></i> ';
}
echo '<span class="aksrt-forum-widget__name">' . esc_html($board->name) . '</span>';
echo '<span class="aksrt-forum-widget__count">' . esc_html((string) (int) $board->count) . '</span>';
echo '</a></li>';
}
echo '</ul>';
return (string) ob_get_clean();
}
echo $args['after_widget'];
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
$html = self::render_html($instance);
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
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
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
return $instance;
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : __('版块列表', 'aksrt');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label><?php esc_html_e('数量', 'aksrt'); ?>
<input class="tiny-text" type="number" min="1" max="50" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 30))); ?>">
</label>
</p>
<p>
<label><?php esc_html_e('排序', 'aksrt'); ?>
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
<option value="menu_order" <?php selected(($instance['orderby'] ?? 'menu_order'), 'menu_order'); ?>><?php esc_html_e('菜单顺序', 'aksrt'); ?></option>
<option value="posts" <?php selected(($instance['orderby'] ?? ''), 'posts'); ?>><?php esc_html_e('帖子数', 'aksrt'); ?></option>
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览量', 'aksrt'); ?></option>
<option value="date" <?php selected(($instance['orderby'] ?? ''), 'date'); ?>><?php esc_html_e('创建时间', 'aksrt'); ?></option>
<option value="name" <?php selected(($instance['orderby'] ?? ''), 'name'); ?>><?php esc_html_e('名称', 'aksrt'); ?></option>
<option value="count" <?php selected(($instance['orderby'] ?? ''), 'count'); ?>><?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="list" <?php selected(($instance['style'] ?? 'list'), 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
<option value="mini" <?php selected(($instance['style'] ?? ''), 'mini'); ?>><?php esc_html_e('迷你图', 'aksrt'); ?></option>
</select>
</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'] ?? 'menu_order'));
$allowed = ['menu_order', 'posts', 'views', 'date', 'name', 'count'];
if (!in_array($orderby, $allowed, true)) {
$orderby = 'menu_order';
}
return [
'title' => sanitize_text_field($new_instance['title'] ?? ''),
'count' => max(1, min(50, (int) ($new_instance['count'] ?? 30))),
'orderby' => $orderby,
'style' => ($new_instance['style'] ?? '') === 'mini' ? 'mini' : 'list',
'hide_empty' => !empty($new_instance['hide_empty']) ? 1 : 0,
];
}
}