Files
wordpress-theme-aksrt/src/AKSRT/Widgets/WidgetForumPlateInfo.php
T
kate522 152772ea60 feat(widgets): feature intro shared JS + plate info count-mates
Move FeatureIntro tab/media script into widget-set.js; align plate
info stats with zibll count-mates layout and residual CSS.
2026-07-21 12:59:05 +08:00

170 lines
7.6 KiB
PHP

<?php
/**
* Current plate info mini-box (zibll zib_bbs_widget_ui_plate_info).
*
* @package AKSRT
*/
namespace AKSRT\Widgets;
defined('ABSPATH') || exit;
class WidgetForumPlateInfo extends \WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_forum_plate_info',
esc_html__('论坛 - 当前版块信息', 'aksrt'),
[
'description' => esc_html__('在版块/帖子页侧栏显示当前 plate 信息。', 'aksrt'),
'classname' => 'widget_aksrt_forum_plate_info',
]
);
}
/**
* @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 ['html' => ''];
}
$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);
$follow = (int) get_post_meta($id, '_aksrt_follow_count', true);
$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);
$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="forum-header plate-info-sidebar aksrt-plate-info ajax-item">';
if ($thumb) {
$ph = get_template_directory_uri() . '/assets/img/placeholder.svg';
echo '<div class="aksrt-plate-info__cover plate-thumb"><img class="aksrt-lazy lazyload fit-cover" data-src="' . esc_url($thumb) . '" src="' . esc_url($ph) . '" alt="" loading="lazy" /></div>';
} elseif ($icon !== '') {
echo '<div class="aksrt-plate-info__cover plate-thumb 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>';
}
if ($show_stats) {
// zibll count-mates: label above big number.
echo '<div class="aksrt-plate-info__stats count-mates text-center flex jsb">';
echo '<item class="mate-views"><div class="em09 opacity5 mb6">' . esc_html__('帖子', 'aksrt') . '</div><div class="em14">' . esc_html((string) $posts) . '</div></item>';
echo '<item class="mate-reply"><div class="em09 opacity5 mb6">' . esc_html__('回复', 'aksrt') . '</div><div class="em14">' . esc_html((string) $reply) . '</div></item>';
echo '<item class="mate-views"><div class="em09 opacity5 mb6">' . esc_html__('阅读', 'aksrt') . '</div><div class="em14">' . esc_html((string) $views) . '</div></item>';
echo '<item class="mate-follow"><div class="em09 opacity5 mb6">' . esc_html__('关注', 'aksrt') . '</div><div class="em14">' . esc_html((string) $follow) . '</div></item>';
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
}
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;
}
if (is_singular('aksrt_forum_topic')) {
$topic_id = (int) get_queried_object_id();
$plate_id = (int) get_post_meta($topic_id, '_aksrt_plate_id', true);
if ($plate_id <= 0) {
$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);
if ($linked > 0) {
$plate_id = $linked;
}
}
}
if ($plate_id > 0) {
$p = get_post($plate_id);
if ($p instanceof \WP_Post && $p->post_type === 'aksrt_forum_plate') {
return $p;
}
}
}
return null;
}
private function resolve_current_plate(): ?\WP_Post {
return self::resolve_current_plate_static();
}
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'] ?? '')); ?>">
</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 [
'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,
];
}
}