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:
1 parent
2dd3b33cb7
commit
2396ca98fb
4 files changed
+424
-149
No files matched your search
@@ -22,41 +22,96 @@ class WidgetForumPlateInfo extends \WP_Widget {
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$plate = $this->resolve_current_plate();
|
||||
/**
|
||||
* @return array{html:string}
|
||||
*/
|
||||
public static function render_payload(array $instance, ?\WP_Post $plate = null): array {
|
||||
$plate = $plate ?: self::resolve_current_plate_static();
|
||||
if (!$plate) {
|
||||
return;
|
||||
return ['html' => ''];
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$show_follow = !isset($instance['show_follow']) || !empty($instance['show_follow']);
|
||||
$show_compose = !isset($instance['show_compose']) || !empty($instance['show_compose']);
|
||||
$show_stats = !isset($instance['show_stats']) || !empty($instance['show_stats']);
|
||||
|
||||
$id = (int) $plate->ID;
|
||||
$url = get_permalink($id);
|
||||
$thumb = get_the_post_thumbnail_url($id, 'medium');
|
||||
$posts = (int) get_post_meta($id, '_aksrt_posts_count', true);
|
||||
$reply = (int) get_post_meta($id, '_aksrt_reply_count', true);
|
||||
$id = (int) $plate->ID;
|
||||
$url = get_permalink($id);
|
||||
$thumb = get_the_post_thumbnail_url($id, 'medium');
|
||||
$posts = (int) get_post_meta($id, '_aksrt_posts_count', true);
|
||||
$reply = (int) get_post_meta($id, '_aksrt_reply_count', true);
|
||||
$follow = (int) get_post_meta($id, '_aksrt_follow_count', true);
|
||||
$desc = $plate->post_excerpt !== '' ? $plate->post_excerpt : wp_trim_words(wp_strip_all_tags($plate->post_content), 30);
|
||||
$views = (int) get_post_meta($id, '_aksrt_views', true);
|
||||
$desc = $plate->post_excerpt !== '' ? $plate->post_excerpt : wp_trim_words(wp_strip_all_tags($plate->post_content), 30);
|
||||
$icon = (string) get_post_meta($id, '_aksrt_icon', true);
|
||||
|
||||
echo '<div class="aksrt-plate-info">';
|
||||
$forum = class_exists('\AKSRT\Forum\Forum') ? \AKSRT\Forum\Forum::get_instance() : null;
|
||||
$uid = get_current_user_id();
|
||||
$is_followed = $forum && $uid > 0 ? $forum->is_user_following_plate($uid, $id) : false;
|
||||
|
||||
$compose_url = function_exists('aksrt_get_forum_compose_url')
|
||||
? aksrt_get_forum_compose_url(['board' => $id])
|
||||
: home_url('/');
|
||||
|
||||
ob_start();
|
||||
echo '<div class="aksrt-plate-info ajax-item">';
|
||||
if ($thumb) {
|
||||
echo '<div class="aksrt-plate-info__cover" style="background-image:url(' . esc_url($thumb) . ')"></div>';
|
||||
} elseif ($icon !== '') {
|
||||
echo '<div class="aksrt-plate-info__cover is-icon"><i class="' . esc_attr($icon) . '" aria-hidden="true"></i></div>';
|
||||
}
|
||||
echo '<div class="aksrt-plate-info__body">';
|
||||
echo '<a class="aksrt-plate-info__title" href="' . esc_url($url) . '">' . esc_html(get_the_title($plate)) . '</a>';
|
||||
if ($desc !== '') {
|
||||
echo '<div class="aksrt-plate-info__desc">' . esc_html($desc) . '</div>';
|
||||
}
|
||||
echo '<div class="aksrt-plate-info__stats">';
|
||||
echo '<span><i class="fas fa-file-alt" aria-hidden="true"></i> ' . esc_html((string) $posts) . '</span>';
|
||||
echo '<span><i class="fas fa-comment" aria-hidden="true"></i> ' . esc_html((string) $reply) . '</span>';
|
||||
echo '<span><i class="fas fa-heart" aria-hidden="true"></i> ' . esc_html((string) $follow) . '</span>';
|
||||
echo '</div></div></div>';
|
||||
if ($show_stats) {
|
||||
echo '<div class="aksrt-plate-info__stats">';
|
||||
echo '<span title="' . esc_attr__('帖子', 'aksrt') . '"><i class="fas fa-file-alt" aria-hidden="true"></i> ' . esc_html((string) $posts) . '</span>';
|
||||
echo '<span title="' . esc_attr__('回复', 'aksrt') . '"><i class="fas fa-comment" aria-hidden="true"></i> ' . esc_html((string) $reply) . '</span>';
|
||||
if ($views > 0) {
|
||||
echo '<span title="' . esc_attr__('浏览', 'aksrt') . '"><i class="fas fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views) . '</span>';
|
||||
}
|
||||
echo '<span title="' . esc_attr__('关注', 'aksrt') . '"><i class="fas fa-heart" aria-hidden="true"></i> ' . esc_html((string) $follow) . '</span>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
$actions = [];
|
||||
if ($show_follow && $uid > 0) {
|
||||
$actions[] = sprintf(
|
||||
'<button type="button" class="aksrt-plate-info__btn aksrt-plate-info__follow js-aksrt-follow-plate%s" data-plate-id="%d" data-followed="%s"><i class="fas fa-heart" aria-hidden="true"></i> %s</button>',
|
||||
$is_followed ? ' is-followed' : '',
|
||||
$id,
|
||||
$is_followed ? '1' : '0',
|
||||
esc_html($is_followed ? __('已关注', 'aksrt') : __('关注', 'aksrt'))
|
||||
);
|
||||
}
|
||||
if ($show_compose) {
|
||||
$actions[] = '<a class="aksrt-plate-info__btn aksrt-plate-info__compose" href="' . esc_url($compose_url) . '"><i class="fas fa-pen" aria-hidden="true"></i> ' . esc_html__('发帖', 'aksrt') . '</a>';
|
||||
}
|
||||
if ($actions) {
|
||||
echo '<div class="aksrt-plate-info__actions">' . implode('', $actions) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
echo '</div></div>';
|
||||
return ['html' => (string) ob_get_clean()];
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$payload = self::render_payload($instance);
|
||||
if ($payload['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
|
||||
}
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
private function resolve_current_plate(): ?\WP_Post {
|
||||
public static function resolve_current_plate_static(): ?\WP_Post {
|
||||
if (is_singular('aksrt_forum_plate')) {
|
||||
$p = get_queried_object();
|
||||
return $p instanceof \WP_Post ? $p : null;
|
||||
@@ -65,7 +120,6 @@ class WidgetForumPlateInfo extends \WP_Widget {
|
||||
$topic_id = (int) get_queried_object_id();
|
||||
$plate_id = (int) get_post_meta($topic_id, '_aksrt_plate_id', true);
|
||||
if ($plate_id <= 0) {
|
||||
// Fallback: first linked board term → plate meta, or parent.
|
||||
$boards = get_the_terms($topic_id, 'aksrt_forum_board');
|
||||
if (!is_wp_error($boards) && !empty($boards[0])) {
|
||||
$linked = (int) get_term_meta((int) $boards[0]->term_id, '_aksrt_plate_id', true);
|
||||
@@ -84,11 +138,32 @@ class WidgetForumPlateInfo extends \WP_Widget {
|
||||
return null;
|
||||
}
|
||||
|
||||
private function resolve_current_plate(): ?\WP_Post {
|
||||
return self::resolve_current_plate_static();
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
echo '<p class="description">' . esc_html__('无需配置。仅在版块页或帖子页显示对应版块信息。', 'aksrt') . '</p>';
|
||||
?>
|
||||
<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'] ?? '')); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_stats')); ?>" value="1" <?php checked(!isset($instance['show_stats']) || !empty($instance['show_stats'])); ?>> <?php esc_html_e('显示统计', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_follow')); ?>" value="1" <?php checked(!isset($instance['show_follow']) || !empty($instance['show_follow'])); ?>> <?php esc_html_e('显示关注按钮', 'aksrt'); ?></label><br>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('show_compose')); ?>" value="1" <?php checked(!isset($instance['show_compose']) || !empty($instance['show_compose'])); ?>> <?php esc_html_e('显示发帖按钮', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p class="description"><?php esc_html_e('仅在版块页或帖子页显示对应版块信息。', 'aksrt'); ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [];
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'show_stats' => !empty($new_instance['show_stats']) ? 1 : 0,
|
||||
'show_follow' => !empty($new_instance['show_follow']) ? 1 : 0,
|
||||
'show_compose' => !empty($new_instance['show_compose']) ? 1 : 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user