feat(comments): AJAX fragment list/submit + load-more parity
Add REST comment/list + comment/submit HTML fragments, shared query renderer for order/author/hottest, fix load-more policy and DOM targets, and stop full-page scrapes for order/author/reload.
This commit is contained in:
@@ -268,41 +268,28 @@ class AKSRT_Admin {
|
||||
wp_send_json_error(['message' => __('无效的文章 ID', 'aksrt')]);
|
||||
}
|
||||
|
||||
$settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_comment_display_settings() : [];
|
||||
$per_page = max(1, (int) get_option('comments_per_page', 10));
|
||||
$comment_query = new WP_Comment_Query([
|
||||
'post_id' => $post_id,
|
||||
'status' => 'approve',
|
||||
'number' => $per_page,
|
||||
'offset' => ($page - 1) * $per_page,
|
||||
'orderby' => 'comment_date_gmt',
|
||||
'order' => !empty($settings['order']) && strtoupper((string) $settings['order']) === 'ASC' ? 'ASC' : 'DESC',
|
||||
]);
|
||||
$comments = $comment_query->comments;
|
||||
$order = sanitize_key((string) ($_POST['comment_order'] ?? $_POST['order'] ?? 'newest'));
|
||||
$author_only = !empty($_POST['author_only']) || (isset($_POST['aksrt_author_only']) && (int) $_POST['aksrt_author_only'] === 1);
|
||||
|
||||
ob_start();
|
||||
if (!empty($comments) && function_exists('aksrt_comment_callback')) {
|
||||
set_query_var('aksrt_comment_settings', $settings);
|
||||
wp_list_comments([
|
||||
'callback' => 'aksrt_comment_callback',
|
||||
'style' => 'ul',
|
||||
'avatar_size' => max(24, (int) ($settings['avatar_size'] ?? 48)),
|
||||
'max_depth' => max(1, (int) ($settings['nested_depth'] ?? 3)),
|
||||
], $comments);
|
||||
if (!function_exists('aksrt_comments_query_for_post') || !function_exists('aksrt_comments_render_items_html')) {
|
||||
wp_send_json_error(['message' => __('评论加载不可用', 'aksrt')]);
|
||||
}
|
||||
$html = ob_get_clean();
|
||||
|
||||
$total = get_comments([
|
||||
'post_id' => $post_id,
|
||||
'status' => 'approve',
|
||||
'count' => true,
|
||||
$result = aksrt_comments_query_for_post($post_id, [
|
||||
'page' => $page,
|
||||
'order' => $order,
|
||||
'author_only' => $author_only,
|
||||
]);
|
||||
$max_pages = (int) ceil(((int) $total) / $per_page);
|
||||
|
||||
$html = aksrt_comments_render_items_html($result['comments'], $result['settings'], $result['order']);
|
||||
|
||||
wp_send_json_success([
|
||||
'html' => $html,
|
||||
'has_more' => $page < $max_pages,
|
||||
'has_more' => $page < (int) $result['max_pages'],
|
||||
'page' => $page,
|
||||
'order' => $result['order'],
|
||||
'total' => (int) $result['total'],
|
||||
'max_pages'=> (int) $result['max_pages'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user