feat(widgets): main posts AJAX pager + public REST

Add WidgetMainPosts query_payload and /aksrt/v1/widgets/posts for
ajax-list load-more parity with zibll main post modules.
This commit is contained in:
kate522 committed 2026-07-20 20:50:57 +08:00
1 parent 19de0b85a6
commit 46c5fec180
3 files changed
+219 -30

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
<?php
/**
* Public widget list REST endpoints (posts / tab-posts).
*
* @package AKSRT
*/
namespace AKSRT\Core;
defined('ABSPATH') || exit;
class WidgetAjax {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
add_action('rest_api_init', [$this, 'register_routes']);
}
public function register_routes(): void {
register_rest_route('aksrt/v1', '/widgets/posts', [
'methods' => 'GET',
'callback' => [$this, 'rest_main_posts'],
'permission_callback' => '__return_true',
]);
register_rest_route('aksrt/v1', '/widgets/tab-posts', [
'methods' => 'GET',
'callback' => [$this, 'rest_tab_posts'],
'permission_callback' => '__return_true',
]);
}
public function rest_main_posts(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\WidgetMainPosts')) {
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
}
$instance = [
'count' => max(1, min(30, (int) ($request->get_param('count') ?? 6))),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
'cat' => max(0, (int) ($request->get_param('cat') ?? 0)),
'limit_day' => max(0, (int) ($request->get_param('limit_day') ?? 0)),
'style' => sanitize_key((string) ($request->get_param('style') ?? 'list')),
'show_thumb' => $request->get_param('show_thumb') === null ? 1 : (int) $request->get_param('show_thumb'),
'show_meta' => $request->get_param('show_meta') === null ? 1 : (int) $request->get_param('show_meta'),
'show_number' => (int) ($request->get_param('show_number') ?? 0),
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
];
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
$payload = \AKSRT\Widgets\WidgetMainPosts::query_payload($instance, $page);
return new \WP_REST_Response([
'success' => true,
'html' => (string) ($payload['html'] ?? ''),
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
'has_more' => !empty($payload['has_more']),
'page' => (int) ($payload['page'] ?? $page),
'max_pages' => (int) ($payload['max_pages'] ?? 1),
'total' => (int) ($payload['total'] ?? 0),
], 200);
}
public function rest_tab_posts(\WP_REST_Request $request) {
if (!class_exists('\AKSRT\Widgets\TabPostsWidget')) {
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
}
$default_count = max(1, min(20, (int) ($request->get_param('count') ?? 5)));
$tab = [
'title' => '',
'category_id' => max(0, (int) ($request->get_param('cat') ?? $request->get_param('category_id') ?? 0)),
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
'count' => max(0, min(20, (int) ($request->get_param('tab_count') ?? 0))),
'enabled' => true,
];
$html = \AKSRT\Widgets\TabPostsWidget::render_tab_html($tab, $default_count);
if ($html === '') {
$html = function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null(__('暂无文章', 'aksrt'))
: '<p class="widget-empty ajax-item">' . esc_html__('暂无文章', 'aksrt') . '</p>';
} else {
$html = '<div class="widget-tab-posts__list widget-tab-posts__list--mini aksrt-main-posts style-mini">' . $html . '</div>';
}
return new \WP_REST_Response([
'success' => true,
'html' => $html,
], 200);
}
}