Files
wordpress-theme-aksrt/inc/helpers/aksrt-forum-widgets.php
T
kate522 9c5ac01119 feat(widgets): shared forum topic/plate renderers for list depth
Extract aksrt_render_forum_topic_mini/detail and plate row/card helpers,
then wire TopicLists, PlateLists, and BoardList for zibll-aligned DOM,
lazy thumbs, and identity-aware topic rows.
2026-07-21 10:39:16 +08:00

364 lines
15 KiB
PHP

<?php
/**
* Shared forum widget renderers (zibll BBS posts/plate mini parity).
*
* Used by WidgetForumTopicLists, WidgetForumTabTopics, WidgetForumPlateLists,
* WidgetForumBoardList, WidgetForumPlateModerator.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
if (!function_exists('aksrt_forum_topic_meta_flags')) :
/**
* @return array{topping:bool,essence:bool,views:int,replies:int,plate_id:int}
*/
function aksrt_forum_topic_meta_flags(int $post_id): array {
return [
'topping' => (string) get_post_meta($post_id, '_aksrt_topping', true) === '1'
|| (string) get_post_meta($post_id, '_aksrt_is_top', true) === '1',
'essence' => (string) get_post_meta($post_id, '_aksrt_essence', true) === '1',
'views' => (int) get_post_meta($post_id, '_aksrt_views', true),
'replies' => (int) get_comments_number($post_id),
'plate_id' => (int) get_post_meta($post_id, '_aksrt_plate_id', true),
];
}
endif;
if (!function_exists('aksrt_forum_topic_badges_html')) :
/**
* Topping / essence badges for forum topic rows.
*/
function aksrt_forum_topic_badges_html(int $post_id, bool $show_topping = true): string {
$flags = aksrt_forum_topic_meta_flags($post_id);
$html = '';
if ($show_topping && !empty($flags['topping'])) {
$html .= '<badge class="img-badge left jb-red"><i>' . esc_html__('置顶', 'aksrt') . '</i></badge>';
}
if (!empty($flags['essence'])) {
$html .= '<badge class="img-badge right jb-yellow"><i>' . esc_html__('精华', 'aksrt') . '</i></badge>';
}
return $html;
}
endif;
if (!function_exists('aksrt_render_forum_topic_mini')) :
/**
* Mini forum topic row (zibll zib_bbs_get_posts_mini_list parity).
*
* @param int|null $post_id Null = current post in loop.
* @param string $class Extra classes on outer element.
* @param bool $show_topping Whether to show topping badge.
*/
function aksrt_render_forum_topic_mini(?int $post_id = null, string $class = 'ajax-item', bool $show_topping = true): string {
$post_id = $post_id ?: (int) get_the_ID();
if ($post_id <= 0) {
return '';
}
$post = get_post($post_id);
if (!$post || $post->post_status !== 'publish') {
return '';
}
$author_id = (int) $post->post_author;
$flags = aksrt_forum_topic_meta_flags($post_id);
$permalink = get_permalink($post_id);
$title = get_the_title($post_id);
$avatar_url = function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($author_id, ['size' => 48])
: get_avatar_url($author_id, ['size' => 48]);
$home = function_exists('aksrt_get_user_home_url')
? aksrt_get_user_home_url($author_id)
: get_author_posts_url($author_id);
$name = function_exists('aksrt_get_user_name')
? aksrt_get_user_name([
'id' => $author_id,
'class' => 'flex ac flex1',
'follow' => false,
'medal' => false,
'level' => false,
])
: '<a href="' . esc_url($home) . '">' . esc_html(get_the_author_meta('display_name', $author_id)) . '</a>';
// Prefer last reply time when available.
$time_raw = (string) get_post_meta($post_id, '_aksrt_last_reply_time', true);
if ($time_raw === '' || !strtotime($time_raw)) {
$time_raw = (string) $post->post_date;
}
$time_txt = function_exists('aksrt_time_ago')
? aksrt_time_ago($time_raw)
: human_time_diff(strtotime($time_raw), current_time('timestamp')) . __('前', 'aksrt');
$views_txt = function_exists('aksrt_format_number')
? aksrt_format_number($flags['views'])
: (string) $flags['views'];
$cls = trim('forum-posts mini aksrt-ftopic-mini relative ' . $class);
$html = '<posts class="' . esc_attr($cls) . '">';
$html .= aksrt_forum_topic_badges_html($post_id, $show_topping);
$html .= '<div class="forum-posts-header flex ac">';
$html .= '<a class="avatar-img mr6" href="' . esc_url($home) . '"><img class="avatar aksrt-lazy lazyload" data-src="' . esc_url($avatar_url) . '" src="' . esc_url(get_template_directory_uri() . '/assets/img/placeholder.svg') . '" alt="" loading="lazy" width="28" height="28" /></a>';
$html .= '<div class="flex1 min-w0">';
$html .= '<div class="forum-title text-ellipsis"><a class="icon-circle" href="' . esc_url($permalink) . '">' . esc_html($title) . '</a></div>';
$html .= '<div class="item-meta em09 muted-2-color flex ac flex-wrap">';
$html .= '<span class="forum-user-name">' . $name . '</span>';
$html .= '<span class="ml6">' . esc_html($time_txt) . '</span>';
$html .= '<span class="ml6"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views_txt) . '</span>';
if ($flags['replies'] > 0) {
$html .= '<span class="ml6"><i class="fa fa-comment-o" aria-hidden="true"></i> ' . esc_html((string) $flags['replies']) . '</span>';
}
$html .= '</div></div></div></posts>';
return $html;
}
endif;
if (!function_exists('aksrt_render_forum_topic_detail')) :
/**
* Detail forum topic row (richer entry meta).
*
* @param int|null $post_id
* @param string $class
* @param bool $show_topping
*/
function aksrt_render_forum_topic_detail(?int $post_id = null, string $class = 'ajax-item', bool $show_topping = true): string {
$post_id = $post_id ?: (int) get_the_ID();
if ($post_id <= 0) {
return '';
}
$post = get_post($post_id);
if (!$post || $post->post_status !== 'publish') {
return '';
}
$author_id = (int) $post->post_author;
$flags = aksrt_forum_topic_meta_flags($post_id);
$permalink = get_permalink($post_id);
$title = get_the_title($post_id);
$excerpt = wp_trim_words(wp_strip_all_tags((string) $post->post_content), 36, '…');
$avatar_url = function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($author_id, ['size' => 64])
: get_avatar_url($author_id, ['size' => 64]);
$home = function_exists('aksrt_get_user_home_url')
? aksrt_get_user_home_url($author_id)
: get_author_posts_url($author_id);
$name = function_exists('aksrt_get_user_name')
? aksrt_get_user_name([
'id' => $author_id,
'class' => 'flex ac',
'follow' => false,
'medal' => true,
'level' => true,
])
: '<a href="' . esc_url($home) . '">' . esc_html(get_the_author_meta('display_name', $author_id)) . '</a>';
$time_raw = (string) get_post_meta($post_id, '_aksrt_last_reply_time', true);
if ($time_raw === '' || !strtotime($time_raw)) {
$time_raw = (string) $post->post_date;
}
$time_txt = function_exists('aksrt_time_ago')
? aksrt_time_ago($time_raw)
: human_time_diff(strtotime($time_raw), current_time('timestamp')) . __('前', 'aksrt');
$views_txt = function_exists('aksrt_format_number')
? aksrt_format_number($flags['views'])
: (string) $flags['views'];
// Optional plate name chip.
$plate_chip = '';
if ($flags['plate_id'] > 0) {
$plate = get_post($flags['plate_id']);
if ($plate instanceof WP_Post) {
$plate_chip = '<a class="but p2-10 em09 mr6" href="' . esc_url(get_permalink($plate)) . '">' . esc_html(get_the_title($plate)) . '</a>';
}
}
$cls = trim('forum-posts detail aksrt-ftopic-detail relative ' . $class);
$html = '<posts class="' . esc_attr($cls) . '">';
$html .= aksrt_forum_topic_badges_html($post_id, $show_topping);
$html .= '<div class="forum-posts-header flex ac">';
$html .= '<a class="avatar-img mr10" href="' . esc_url($home) . '"><img class="avatar aksrt-lazy lazyload" data-src="' . esc_url($avatar_url) . '" src="' . esc_url(get_template_directory_uri() . '/assets/img/placeholder.svg') . '" alt="" loading="lazy" width="40" height="40" /></a>';
$html .= '<div class="flex1 min-w0">' . $name . '<div class="em09 muted-2-color mt3">' . esc_html($time_txt) . '</div></div>';
$html .= '</div>';
$html .= '<h2 class="forum-title mt10"><a href="' . esc_url($permalink) . '">' . esc_html($title) . '</a></h2>';
if ($excerpt !== '') {
$html .= '<div class="forum-excerpt muted-2-color em09 mt6">' . esc_html($excerpt) . '</div>';
}
$html .= '<div class="item-meta flex ac flex-wrap mt10 em09 muted-2-color">';
$html .= $plate_chip;
$html .= '<span class="mr10"><i class="fa fa-eye" aria-hidden="true"></i> ' . esc_html((string) $views_txt) . '</span>';
$html .= '<span class="mr10"><i class="fa fa-comment-o" aria-hidden="true"></i> ' . esc_html((string) $flags['replies']) . '</span>';
$html .= '</div></posts>';
return $html;
}
endif;
if (!function_exists('aksrt_render_plate_thumb_html')) :
/**
* Shared plate thumb with lazy pattern.
*/
function aksrt_render_plate_thumb_html(int $plate_id, string $size = 'thumbnail'): string {
$thumb = get_the_post_thumbnail_url($plate_id, $size);
$icon = (string) get_post_meta($plate_id, '_aksrt_icon', true);
$ph = get_template_directory_uri() . '/assets/img/placeholder.svg';
if ($thumb) {
return '<span class="plate-thumb aksrt-plate-item__thumb"><img class="aksrt-lazy lazyload fit-cover" data-src="' . esc_url($thumb) . '" src="' . esc_url($ph) . '" alt="" loading="lazy" width="48" height="48" /></span>';
}
if ($icon !== '') {
return '<span class="plate-thumb aksrt-plate-item__thumb is-icon"><i class="' . esc_attr($icon) . '" aria-hidden="true"></i></span>';
}
return '<span class="plate-thumb aksrt-plate-item__thumb is-empty"><i class="fa fa-comments" aria-hidden="true"></i></span>';
}
endif;
if (!function_exists('aksrt_render_plate_row')) :
/**
* Plate list row (zibll plate-item flex).
*
* @param WP_Post|int $plate
*/
function aksrt_render_plate_row($plate): string {
$post = $plate instanceof WP_Post ? $plate : get_post((int) $plate);
if (!$post instanceof WP_Post) {
return '';
}
$id = (int) $post->ID;
$url = get_permalink($id);
$count = (int) get_post_meta($id, '_aksrt_posts_count', true);
$follow = (int) get_post_meta($id, '_aksrt_follow_count', true);
$desc = $post->post_excerpt !== '' ? $post->post_excerpt : wp_trim_words(wp_strip_all_tags((string) $post->post_content), 18);
$html = '<a class="plate-item flex aksrt-plate-item ajax-item" href="' . esc_url($url) . '">';
$html .= aksrt_render_plate_thumb_html($id, 'thumbnail');
$html .= '<div class="item-info flex1 aksrt-plate-item__body">';
$html .= '<div class="info-header"><h2 class="forum-title aksrt-plate-item__title">' . esc_html(get_the_title($post)) . '</h2></div>';
if ($desc !== '') {
$html .= '<div class="muted-2-color em09 mt3 text-ellipsis aksrt-plate-item__desc">' . esc_html($desc) . '</div>';
}
$html .= '<div class="item-meta em09 muted-2-color mt3 aksrt-plate-item__meta">';
$html .= '<span>' . esc_html(sprintf(/* translators: %d topic count */ __('%d 帖', 'aksrt'), $count)) . '</span>';
if ($follow > 0) {
$html .= '<span class="ml10">' . esc_html(sprintf(/* translators: %d follow count */ __('%d 关注', 'aksrt'), $follow)) . '</span>';
}
$html .= '</div></div></a>';
return $html;
}
endif;
if (!function_exists('aksrt_render_plate_card')) :
/**
* Plate card slide item.
*
* @param WP_Post|int $plate
*/
function aksrt_render_plate_card($plate): string {
$post = $plate instanceof WP_Post ? $plate : get_post((int) $plate);
if (!$post instanceof WP_Post) {
return '';
}
$id = (int) $post->ID;
$url = get_permalink($id);
$thumb = get_the_post_thumbnail_url($id, 'medium');
$count = (int) get_post_meta($id, '_aksrt_posts_count', true);
$follow = (int) get_post_meta($id, '_aksrt_follow_count', true);
$ph = get_template_directory_uri() . '/assets/img/placeholder.svg';
$html = '<a class="plate-card aksrt-plate-card ajax-item" href="' . esc_url($url) . '">';
$html .= '<div class="plate-thumb aksrt-plate-card__cover">';
if ($thumb) {
$html .= '<img class="aksrt-lazy lazyload fit-cover" data-src="' . esc_url($thumb) . '" src="' . esc_url($ph) . '" alt="" loading="lazy" />';
} else {
$html .= '<span class="is-empty"><i class="fa fa-comments" aria-hidden="true"></i></span>';
}
$html .= '</div>';
$html .= '<div class="aksrt-plate-card__title forum-title mt10">' . esc_html(get_the_title($post)) . '</div>';
$html .= '<div class="aksrt-plate-card__meta muted-2-color em09 mt6">';
$html .= '<span>' . esc_html(sprintf(__('%d 帖', 'aksrt'), $count)) . '</span>';
$html .= '<span class="ml10">' . esc_html(sprintf(__('%d 关注', 'aksrt'), $follow)) . '</span>';
$html .= '</div></a>';
return $html;
}
endif;
if (!function_exists('aksrt_render_moderator_row')) :
/**
* Moderator identity row with optional follow.
*
* @param int $user_id
* @param string $role author|super|moderator
* @param bool $show_avatar
* @param string $style list|grid
*/
function aksrt_render_moderator_row(int $user_id, string $role = 'moderator', bool $show_avatar = true, string $style = 'list'): string {
if ($user_id <= 0) {
return '';
}
$user = get_userdata($user_id);
if (!$user) {
return '';
}
$avatar = function_exists('aksrt_get_avatar_url')
? aksrt_get_avatar_url($user_id, ['size' => 64])
: get_avatar_url($user_id, ['size' => 64]);
$home = function_exists('aksrt_get_user_home_url')
? aksrt_get_user_home_url($user_id)
: get_author_posts_url($user_id);
$ph = get_template_directory_uri() . '/assets/img/placeholder.svg';
if ($role === 'author' || $role === 'super') {
$role_label = __('超级版主', 'aksrt');
} else {
$role_label = __('版主', 'aksrt');
}
$name = function_exists('aksrt_get_user_name')
? aksrt_get_user_name([
'id' => $user_id,
'class' => 'flex ac flex1',
'follow' => false,
'medal' => true,
'level' => true,
])
: '<a href="' . esc_url($home) . '">' . esc_html($user->display_name) . '</a>';
// Follow control (not for self).
$follow_html = '';
$cuid = get_current_user_id();
if ($cuid > 0 && $cuid !== $user_id && class_exists('\AKSRT\User\Follow')) {
$following = \AKSRT\User\Follow::get_instance()->is_following($cuid, $user_id);
$label = $following ? __('已关注', 'aksrt') : __('关注', 'aksrt');
$follow_html = '<button type="button" class="but hollow c-blue p2-10 em09 ml10 flex0' . ($following ? ' is-following' : '') . '" data-aksrt-follow="' . (int) $user_id . '">' . esc_html($label) . '</button>';
}
if ($style === 'grid') {
$html = '<div class="aksrt-plate-mods__item role-' . esc_attr($role) . ' is-grid">';
if ($show_avatar) {
$html .= '<a href="' . esc_url($home) . '"><img class="aksrt-plate-mods__avatar aksrt-lazy lazyload" data-src="' . esc_url($avatar) . '" src="' . esc_url($ph) . '" alt="" loading="lazy" width="48" height="48" /></a>';
}
$html .= '<div class="aksrt-plate-mods__name">' . $name . '</div>';
$html .= '<div class="aksrt-plate-mods__role muted-2-color em09">' . esc_html($role_label) . '</div>';
$html .= $follow_html;
$html .= '</div>';
return $html;
}
$html = '<div class="aksrt-plate-mods__item role-' . esc_attr($role) . ' flex ac">';
if ($show_avatar) {
$html .= '<a class="avatar-img mr10" href="' . esc_url($home) . '"><img class="aksrt-plate-mods__avatar aksrt-lazy lazyload" data-src="' . esc_url($avatar) . '" src="' . esc_url($ph) . '" alt="" loading="lazy" width="40" height="40" /></a>';
}
$html .= '<div class="flex1 min-w0">';
$html .= '<div class="aksrt-plate-mods__name">' . $name . '</div>';
$html .= '<div class="aksrt-plate-mods__role muted-2-color em09">' . esc_html($role_label) . '</div>';
$html .= '</div>';
$html .= $follow_html;
$html .= '</div>';
return $html;
}
endif;