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 '
'; if ($thumb) { $ph = get_template_directory_uri() . '/assets/img/placeholder.svg'; echo '
'; } elseif ($icon !== '') { echo '
'; } echo '
'; echo '' . esc_html(get_the_title($plate)) . ''; if ($desc !== '') { echo '
' . esc_html($desc) . '
'; } if ($show_stats) { // zibll count-mates: label above big number. echo '
'; echo '
' . esc_html__('帖子', 'aksrt') . '
' . esc_html((string) $posts) . '
'; echo '
' . esc_html__('回复', 'aksrt') . '
' . esc_html((string) $reply) . '
'; echo '
' . esc_html__('阅读', 'aksrt') . '
' . esc_html((string) $views) . '
'; echo '
' . esc_html__('关注', 'aksrt') . '
' . esc_html((string) $follow) . '
'; echo '
'; } $actions = []; if ($show_follow && $uid > 0) { $actions[] = sprintf( '', $is_followed ? ' is-followed' : '', $id, $is_followed ? '1' : '0', esc_html($is_followed ? __('已关注', 'aksrt') : __('关注', 'aksrt')) ); } if ($show_compose) { $actions[] = ' ' . esc_html__('发帖', 'aksrt') . ''; } if ($actions) { echo '
' . implode('', $actions) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } echo '
'; 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 { ?>



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, ]; } }