feat(forum): frontend 1:1 list/tabs/urls/enqueue
Wire Zibll-style board sort tabs, shared topic rows, page-template URLs, forum asset enqueue on home/plate, home synthesis load-more, and topic last_reply meta for last-reply ordering. No dual-read.
This commit is contained in:
1 parent
df6b224dd3
commit
780c8cb4f7
23 files changed
+843
-481
No files matched your search
@@ -34,35 +34,55 @@ class AKSRT_Forum_View_Builder {
|
||||
$visibility = sanitize_key((string) ($args['visibility'] ?? ''));
|
||||
$review_status = sanitize_key((string) ($args['review_status'] ?? ''));
|
||||
$author_id = (int) ($args['author_id'] ?? 0);
|
||||
$orderby = sanitize_key((string) ($args['orderby'] ?? 'date'));
|
||||
$orderby = sanitize_key((string) ($args['orderby'] ?? ($args['sort'] ?? 'date')));
|
||||
$viewer_user_id = (int) ($args['viewer_user_id'] ?? get_current_user_id());
|
||||
$include_hidden = !empty($args['include_hidden']);
|
||||
|
||||
// Settings tab aliases: hot→views, essence/featured→featured filter.
|
||||
if ($orderby === 'hot') {
|
||||
$orderby = 'views';
|
||||
}
|
||||
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'posts_per_page' => $limit,
|
||||
'offset' => $offset,
|
||||
'post_status' => $this->forum->normalize_post_status_query($status),
|
||||
'order' => 'DESC',
|
||||
'ignore_sticky_posts' => true,
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'posts_per_page' => $limit,
|
||||
'offset' => $offset,
|
||||
'post_status' => $this->forum->normalize_post_status_query($status),
|
||||
'order' => 'DESC',
|
||||
'ignore_sticky_posts'=> true,
|
||||
];
|
||||
|
||||
$meta_query = [];
|
||||
|
||||
if ($orderby === 'views') {
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['meta_key'] = '_aksrt_views';
|
||||
} elseif ($orderby === 'recommend') {
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['meta_key'] = '_aksrt_recommend_score';
|
||||
} elseif ($orderby === 'comment_count') {
|
||||
$query_args['orderby'] = 'comment_count';
|
||||
} elseif ($orderby === 'score') {
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['meta_key'] = '_aksrt_score';
|
||||
} elseif ($orderby === 'favorites') {
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['orderby'] = 'meta_value_num';
|
||||
$query_args['meta_key'] = '_aksrt_favorite_count';
|
||||
} elseif ($orderby === 'last_reply') {
|
||||
$query_args['orderby'] = 'meta_value';
|
||||
$query_args['meta_key'] = '_aksrt_last_reply';
|
||||
$query_args['meta_type']= 'DATETIME';
|
||||
} elseif ($orderby === 'featured' || $orderby === 'essence') {
|
||||
// 推荐 / 精华 share _aksrt_featured (Zibll essence = featured).
|
||||
$meta_query[] = [
|
||||
'key' => '_aksrt_featured',
|
||||
'value' => '1',
|
||||
'compare' => '=',
|
||||
];
|
||||
$query_args['orderby'] = 'date';
|
||||
} else {
|
||||
$query_args['orderby'] = 'date';
|
||||
$orderby = 'date';
|
||||
}
|
||||
|
||||
if ($keyword !== '') {
|
||||
@@ -76,14 +96,14 @@ class AKSRT_Forum_View_Builder {
|
||||
$plate_id = (int) ($args['plate_id'] ?? 0);
|
||||
$plate_ids = $args['plate_ids'] ?? [];
|
||||
if (!empty($plate_ids) && is_array($plate_ids)) {
|
||||
$query_args['meta_query'][] = [
|
||||
$meta_query[] = [
|
||||
'key' => '_aksrt_plate_id',
|
||||
'value' => array_map('intval', $plate_ids),
|
||||
'type' => 'NUMERIC',
|
||||
'compare' => 'IN',
|
||||
];
|
||||
} elseif ($plate_id > 0) {
|
||||
$query_args['meta_query'][] = [
|
||||
$meta_query[] = [
|
||||
'key' => '_aksrt_plate_id',
|
||||
'value' => $plate_id,
|
||||
'type' => 'NUMERIC',
|
||||
@@ -92,29 +112,31 @@ class AKSRT_Forum_View_Builder {
|
||||
} elseif ($board_id > 0) {
|
||||
$query_args['tax_query'] = [[
|
||||
'taxonomy' => 'aksrt_forum_board',
|
||||
'field' => 'term_id',
|
||||
'terms' => $board_id,
|
||||
'field' => 'term_id',
|
||||
'terms' => $board_id,
|
||||
]];
|
||||
}
|
||||
|
||||
$meta_query = [];
|
||||
if ($visibility !== '') {
|
||||
$meta_query[] = [
|
||||
'key' => '_aksrt_visibility',
|
||||
'value' => $visibility,
|
||||
'key' => '_aksrt_visibility',
|
||||
'value' => $visibility,
|
||||
'compare' => '=',
|
||||
];
|
||||
}
|
||||
|
||||
if ($review_status !== '') {
|
||||
$meta_query[] = [
|
||||
'key' => '_aksrt_review_status',
|
||||
'value' => $review_status,
|
||||
'key' => '_aksrt_review_status',
|
||||
'value' => $review_status,
|
||||
'compare' => '=',
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($meta_query)) {
|
||||
if (count($meta_query) > 1) {
|
||||
$meta_query['relation'] = 'AND';
|
||||
}
|
||||
$query_args['meta_query'] = $meta_query;
|
||||
}
|
||||
|
||||
@@ -129,7 +151,7 @@ class AKSRT_Forum_View_Builder {
|
||||
}
|
||||
|
||||
$topic = $this->forum->build_topic_payload($post, $viewer_user_id);
|
||||
if (!$include_hidden && !$topic['can_view']) {
|
||||
if (!$include_hidden && empty($topic['can_view'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -139,10 +161,11 @@ class AKSRT_Forum_View_Builder {
|
||||
wp_reset_postdata();
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => (int) $query->found_posts,
|
||||
'items' => $items,
|
||||
'total' => (int) $query->found_posts,
|
||||
'max_pages' => (int) $query->max_num_pages,
|
||||
'page' => $page,
|
||||
'page' => $page,
|
||||
'orderby' => $orderby,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -152,16 +175,12 @@ class AKSRT_Forum_View_Builder {
|
||||
public function get_home_view(int $user_id = 0): array {
|
||||
$boards = $this->forum->get_boards();
|
||||
$stats = $this->get_stats();
|
||||
$recent = $this->get_topic_list([
|
||||
'limit' => 10,
|
||||
'status' => 'publish',
|
||||
'viewer_user_id' => $user_id,
|
||||
]);
|
||||
$synthesis = $this->get_synthesis_stream(1, 15, $user_id);
|
||||
$hot = $this->get_topic_list([
|
||||
'limit' => 8,
|
||||
'status' => 'publish',
|
||||
'limit' => 8,
|
||||
'status' => 'publish',
|
||||
'viewer_user_id' => $user_id,
|
||||
'orderby' => 'views',
|
||||
'orderby' => 'views',
|
||||
]);
|
||||
|
||||
$applications = [];
|
||||
@@ -181,20 +200,23 @@ class AKSRT_Forum_View_Builder {
|
||||
$quick_posts = !empty($config['homepage']['quick_posts']);
|
||||
|
||||
return [
|
||||
'boards' => $boards,
|
||||
'recent_topics' => $recent['items'],
|
||||
'hot_topics' => $hot['items'],
|
||||
'stats' => $stats,
|
||||
'permissions' => [
|
||||
'can_compose' => $user_id > 0 && $this->forum->forum_user_can($user_id, 'compose_post'),
|
||||
'can_manage' => $this->forum->is_forum_admin($user_id),
|
||||
'can_create_plate' => $this->forum->forum_user_can($user_id, 'plate_create'),
|
||||
'user_tier' => $user_id > 0 ? $this->forum->get_user_plate_tier($user_id, 0) : AKSRT_Forum::TIER_NONE,
|
||||
'boards' => $boards,
|
||||
'recent_topics' => $synthesis['items'],
|
||||
'synthesis' => $synthesis,
|
||||
'hot_topics' => $hot['items'],
|
||||
'stats' => $stats,
|
||||
'permissions' => [
|
||||
'can_compose' => $user_id > 0 && $this->forum->forum_user_can($user_id, 'compose_post'),
|
||||
'can_manage' => $this->forum->is_forum_admin($user_id),
|
||||
'can_create_plate'=> $this->forum->forum_user_can($user_id, 'plate_create'),
|
||||
'user_tier' => $user_id > 0 ? $this->forum->get_user_plate_tier($user_id, 0) : AKSRT_Forum::TIER_NONE,
|
||||
],
|
||||
'applications' => $applications,
|
||||
'home_tabs' => $home_tabs,
|
||||
'active_index' => $active_index,
|
||||
'quick_posts' => $quick_posts,
|
||||
'applications' => $applications,
|
||||
'home_tabs' => $home_tabs,
|
||||
'active_index' => $active_index,
|
||||
'quick_posts' => $quick_posts,
|
||||
'compose_url' => function_exists('aksrt_get_forum_compose_url') ? aksrt_get_forum_compose_url() : home_url('/'),
|
||||
'home_url' => function_exists('aksrt_get_forum_home_url') ? aksrt_get_forum_home_url() : home_url('/'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -274,13 +296,17 @@ class AKSRT_Forum_View_Builder {
|
||||
$result = $this->get_pending_stream($user_id, $page);
|
||||
break;
|
||||
case 'plate':
|
||||
ob_start();
|
||||
$this->render_board_cards($this->forum->get_boards());
|
||||
$html = ob_get_clean();
|
||||
wp_send_json_success([
|
||||
'html' => '',
|
||||
'html' => $html,
|
||||
'type' => 'plate',
|
||||
'boards' => $this->forum->get_boards(),
|
||||
'total' => 0,
|
||||
'total' => count($this->forum->get_boards()),
|
||||
'max_pages' => 1,
|
||||
'page' => 1,
|
||||
'has_more' => false,
|
||||
]);
|
||||
return;
|
||||
default:
|
||||
@@ -293,7 +319,7 @@ class AKSRT_Forum_View_Builder {
|
||||
$this->render_topic_row($topic);
|
||||
}
|
||||
} else {
|
||||
echo '<div class="aksrt-forum__empty"><i class="far fa-comment-dots"></i><p>' . esc_html__('暂无内容', 'aksrt') . '</p></div>';
|
||||
echo '<div class="aksrt-forum__empty"><i class="far fa-comment-dots"></i><p>' . esc_html__('内容空空如也', 'aksrt') . '</p></div>';
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
@@ -307,14 +333,127 @@ class AKSRT_Forum_View_Builder {
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Public AJAX: plate/board topic list with sort tabs.
|
||||
*/
|
||||
public function ajax_board_topics(): void {
|
||||
check_ajax_referer('aksrt_public_nonce', 'nonce');
|
||||
|
||||
$plate_id = (int) ($_POST['plate_id'] ?? $_GET['plate_id'] ?? 0);
|
||||
$board_id = (int) ($_POST['board_id'] ?? $_GET['board_id'] ?? 0);
|
||||
$page = max(1, (int) ($_POST['page'] ?? $_GET['page'] ?? 1));
|
||||
$orderby = sanitize_key((string) ($_POST['orderby'] ?? $_POST['sort'] ?? $_GET['orderby'] ?? $_GET['sort'] ?? 'date'));
|
||||
$limit = max(1, min(50, (int) ($_POST['limit'] ?? $_GET['limit'] ?? 20)));
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
$allowed = ['date', 'featured', 'essence', 'last_reply', 'hot', 'views', 'recommend', 'comment_count', 'score', 'favorites'];
|
||||
if (!in_array($orderby, $allowed, true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
|
||||
$list_args = [
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'status' => 'publish',
|
||||
'viewer_user_id' => $user_id,
|
||||
'orderby' => $orderby,
|
||||
];
|
||||
if ($plate_id > 0) {
|
||||
$list_args['plate_id'] = $plate_id;
|
||||
} elseif ($board_id > 0) {
|
||||
$list_args['board_id'] = $board_id;
|
||||
}
|
||||
|
||||
$result = $this->get_topic_list($list_args);
|
||||
|
||||
ob_start();
|
||||
if (!empty($result['items'])) {
|
||||
foreach ($result['items'] as $topic) {
|
||||
$this->render_topic_row($topic);
|
||||
}
|
||||
} else {
|
||||
$compose = function_exists('aksrt_get_forum_compose_url')
|
||||
? aksrt_get_forum_compose_url($plate_id > 0 ? ['board' => $plate_id] : ($board_id > 0 ? ['board' => $board_id] : []))
|
||||
: '';
|
||||
echo '<div class="aksrt-forum-board__empty aksrt-forum__empty">';
|
||||
echo '<i class="fas fa-comments"></i>';
|
||||
echo '<p>' . esc_html__('内容空空如也', 'aksrt') . '</p>';
|
||||
if ($compose !== '') {
|
||||
echo '<a href="' . esc_url($compose) . '">' . esc_html__('发布第一个主题', 'aksrt') . '</a>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
wp_send_json_success([
|
||||
'html' => $html,
|
||||
'total' => $result['total'],
|
||||
'max_pages' => $result['max_pages'],
|
||||
'page' => $page,
|
||||
'orderby' => $orderby,
|
||||
'has_more' => $page < $result['max_pages'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render plate/board cards grid (home plate tab).
|
||||
*
|
||||
* @param array<int, array<string, mixed>> $boards
|
||||
*/
|
||||
public function render_board_cards(array $boards): void {
|
||||
$names = $this->forum->get_config()['names'] ?? [];
|
||||
$name_topic = $names['posts'] ?? __('主题', 'aksrt');
|
||||
if (empty($boards)) {
|
||||
echo '<div class="aksrt-forum__empty"><i class="far fa-folder-open"></i><p>' . esc_html__('内容空空如也', 'aksrt') . '</p></div>';
|
||||
return;
|
||||
}
|
||||
echo '<div class="aksrt-forum-board-card__grid">';
|
||||
foreach ($boards as $board) {
|
||||
$url = (string) ($board['url'] ?? '#');
|
||||
$name = (string) ($board['name'] ?? '');
|
||||
$desc = (string) ($board['description'] ?? '');
|
||||
$count = (int) ($board['post_count'] ?? $board['topic_count'] ?? 0);
|
||||
$cover = (string) ($board['cover_url'] ?? $board['thumbnail_url'] ?? '');
|
||||
?>
|
||||
<a class="aksrt-forum-board-card" href="<?php echo esc_url($url); ?>">
|
||||
<?php if ($cover !== '') : ?>
|
||||
<span class="aksrt-forum-board-card__cover" style="background-image:url(<?php echo esc_url($cover); ?>)"></span>
|
||||
<?php else : ?>
|
||||
<span class="aksrt-forum-board-card__cover aksrt-forum-board-card__cover--empty"><i class="fas fa-comments"></i></span>
|
||||
<?php endif; ?>
|
||||
<span class="aksrt-forum-board-card__body">
|
||||
<span class="aksrt-forum-board-card__title"><?php echo esc_html($name); ?></span>
|
||||
<?php if ($desc !== '') : ?>
|
||||
<span class="aksrt-forum-board-card__desc"><?php echo esc_html(wp_trim_words($desc, 18, '…')); ?></span>
|
||||
<?php endif; ?>
|
||||
<span class="aksrt-forum-board-card__meta">
|
||||
<i class="fas fa-list-alt"></i>
|
||||
<?php printf(esc_html__('%1$d 个%2$s', 'aksrt'), $count, esc_html($name_topic)); ?>
|
||||
</span>
|
||||
</span>
|
||||
</a>
|
||||
<?php
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a single topic row (reusable for AJAX and initial render).
|
||||
*/
|
||||
public function render_topic_row(array $topic): void {
|
||||
$board = !empty($topic['boards'][0]) ? $topic['boards'][0] : null;
|
||||
$names = $this->forum->get_config()['names'] ?? [];
|
||||
$sticky_level = (int) ($topic['sticky_level'] ?? (!empty($topic['sticky']) ? 1 : 0));
|
||||
$row_class = 'aksrt-forum-topic-row';
|
||||
if ($sticky_level > 0) {
|
||||
$row_class .= ' is-sticky is-sticky-' . $sticky_level;
|
||||
}
|
||||
if (!empty($topic['featured'])) {
|
||||
$row_class .= ' is-featured';
|
||||
}
|
||||
$cover = $topic['cover'] ?? null;
|
||||
?>
|
||||
<article class="aksrt-forum-topic-row">
|
||||
<article class="<?php echo esc_attr($row_class); ?>">
|
||||
<div class="aksrt-forum-topic-row__avatar">
|
||||
<?php if (!empty($topic['author']['avatar_url'])) : ?>
|
||||
<img src="<?php echo esc_url((string) $topic['author']['avatar_url']); ?>" alt="" width="40" height="40" loading="lazy">
|
||||
@@ -322,14 +461,19 @@ class AKSRT_Forum_View_Builder {
|
||||
</div>
|
||||
<div class="aksrt-forum-topic-row__body">
|
||||
<div class="aksrt-forum-topic-row__title-wrap">
|
||||
<?php if (!empty($topic['sticky'])) : ?>
|
||||
<span class="aksrt-forum-topic-row__badge aksrt-forum-topic-row__badge--sticky"><?php esc_html_e('置顶', 'aksrt'); ?></span>
|
||||
<?php if ($sticky_level > 0) : ?>
|
||||
<span class="aksrt-forum-topic-row__badge aksrt-forum-topic-row__badge--sticky"><?php echo esc_html((string) ($topic['sticky_label'] ?? __('置顶', 'aksrt'))); ?></span>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($topic['featured'])) : ?>
|
||||
<span class="aksrt-forum-topic-row__badge aksrt-forum-topic-row__badge--featured"><?php esc_html_e('精华', 'aksrt'); ?></span>
|
||||
<?php endif; ?>
|
||||
<?php if (($topic['bbs_type'] ?? '') === 'question') : ?>
|
||||
<span class="aksrt-forum-topic-row__badge aksrt-forum-topic-row__badge--question"><?php echo esc_html($names['question_badge'] ?? __('提问', 'aksrt')); ?></span>
|
||||
<?php $q_status = (int) ($topic['question_status'] ?? 0); ?>
|
||||
<span class="aksrt-forum-topic-row__badge aksrt-forum-topic-row__badge--question<?php echo $q_status === 1 ? ' is-solved' : ''; ?>">
|
||||
<?php echo $q_status === 1
|
||||
? esc_html($names['question_ok_badge'] ?? __('已解决', 'aksrt'))
|
||||
: esc_html($names['question_badge'] ?? __('提问', 'aksrt')); ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
<h3 class="aksrt-forum-topic-row__title">
|
||||
<a href="<?php echo esc_url((string) ($topic['permalink'] ?? '#')); ?>"><?php echo esc_html((string) ($topic['title'] ?? '')); ?></a>
|
||||
@@ -345,6 +489,21 @@ class AKSRT_Forum_View_Builder {
|
||||
<span><i class="fas fa-comment"></i> <?php echo esc_html(number_format_i18n((int) ($topic['comment_count'] ?? 0))); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
<?php if (is_array($cover) && !empty($cover['type'])) : ?>
|
||||
<div class="aksrt-forum-topic-row__cover">
|
||||
<?php if (($cover['type'] ?? '') === 'video') : ?>
|
||||
<?php if (!empty($cover['poster'])) : ?>
|
||||
<img src="<?php echo esc_url((string) $cover['poster']); ?>" alt="" loading="lazy">
|
||||
<?php endif; ?>
|
||||
<i class="fas fa-play-circle"></i>
|
||||
<?php elseif (($cover['type'] ?? '') === 'slide' && !empty($cover['images'][0])) : ?>
|
||||
<img src="<?php echo esc_url((string) $cover['images'][0]); ?>" alt="" loading="lazy">
|
||||
<span class="aksrt-forum-topic-row__cover-count"><i class="fas fa-images"></i></span>
|
||||
<?php elseif (!empty($cover['url'])) : ?>
|
||||
<img src="<?php echo esc_url((string) $cover['url']); ?>" alt="" loading="lazy">
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</article>
|
||||
<?php
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user