feat(widgets): hot posts AJAX load-more + REST expansion
HotPostsWidget query_payload; WidgetAjax routes for hot/oneline/ comments/users/forum topics/terms.
This commit is contained in:
1 parent
6a6154900b
commit
75e046ee7f
2 files changed
+324
-190
No files matched your search
+131
-49
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Public widget list REST endpoints (posts / tab-posts).
|
||||
* Public widget list REST endpoints.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -24,31 +24,30 @@ class WidgetAjax {
|
||||
}
|
||||
|
||||
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',
|
||||
]);
|
||||
register_rest_route('aksrt/v1', '/widgets/paid-posts', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'rest_paid_posts'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
register_rest_route('aksrt/v1', '/widgets/category-tabs', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'rest_category_tabs'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
$routes = [
|
||||
'/widgets/posts' => 'rest_main_posts',
|
||||
'/widgets/tab-posts' => 'rest_tab_posts',
|
||||
'/widgets/paid-posts' => 'rest_paid_posts',
|
||||
'/widgets/category-tabs' => 'rest_category_tabs',
|
||||
'/widgets/hot-posts' => 'rest_hot_posts',
|
||||
'/widgets/oneline-posts' => 'rest_oneline_posts',
|
||||
'/widgets/recent-comments'=> 'rest_recent_comments',
|
||||
'/widgets/user-lists' => 'rest_user_lists',
|
||||
'/widgets/forum-topics' => 'rest_forum_topics',
|
||||
'/widgets/forum-terms' => 'rest_forum_terms',
|
||||
];
|
||||
foreach ($routes as $path => $cb) {
|
||||
register_rest_route('aksrt/v1', $path, [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, $cb],
|
||||
'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);
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(30, (int) ($request->get_param('count') ?? 6))),
|
||||
@@ -62,21 +61,12 @@ class WidgetAjax {
|
||||
'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);
|
||||
return $this->payload(\AKSRT\Widgets\WidgetMainPosts::query_payload($instance, $page), $page);
|
||||
}
|
||||
|
||||
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);
|
||||
return $this->fail();
|
||||
}
|
||||
$default_count = max(1, min(20, (int) ($request->get_param('count') ?? 5)));
|
||||
$tab = [
|
||||
@@ -94,15 +84,12 @@ class WidgetAjax {
|
||||
} 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);
|
||||
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
|
||||
}
|
||||
|
||||
public function rest_paid_posts(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetPaidPosts')) {
|
||||
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 6))),
|
||||
@@ -110,16 +97,7 @@ class WidgetAjax {
|
||||
'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\WidgetPaidPosts::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);
|
||||
return $this->payload(\AKSRT\Widgets\WidgetPaidPosts::query_payload($instance, $page), $page);
|
||||
}
|
||||
|
||||
public function rest_category_tabs(\WP_REST_Request $request) {
|
||||
@@ -152,7 +130,111 @@ class WidgetAjax {
|
||||
echo '</ul>';
|
||||
}
|
||||
wp_reset_postdata();
|
||||
$html = (string) ob_get_clean();
|
||||
return new \WP_REST_Response(['success' => true, 'html' => (string) ob_get_clean()], 200);
|
||||
}
|
||||
|
||||
public function rest_hot_posts(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\HotPostsWidget')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 5))),
|
||||
'order_by' => sanitize_key((string) ($request->get_param('order_by') ?? 'views')),
|
||||
'days' => max(0, (int) ($request->get_param('days') ?? 30)),
|
||||
'show_thumb' => $request->get_param('show_thumb') === null ? 1 : (int) $request->get_param('show_thumb'),
|
||||
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
|
||||
];
|
||||
$page = max(1, (int) ($request->get_param('page') ?? 1));
|
||||
return $this->payload(\AKSRT\Widgets\HotPostsWidget::query_payload($instance, $page), $page);
|
||||
}
|
||||
|
||||
public function rest_oneline_posts(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetOnelinePosts')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 8))),
|
||||
'style' => sanitize_key((string) ($request->get_param('style') ?? 'card')),
|
||||
'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)),
|
||||
];
|
||||
$html = \AKSRT\Widgets\WidgetOnelinePosts::render_html($instance);
|
||||
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
|
||||
}
|
||||
|
||||
public function rest_recent_comments(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\RecentCommentsWidget')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 5))),
|
||||
'show_avatar' => $request->get_param('show_avatar') === null ? 1 : (int) $request->get_param('show_avatar'),
|
||||
'outpost' => max(0, (int) ($request->get_param('outpost') ?? 0)),
|
||||
'outer' => sanitize_text_field((string) ($request->get_param('outer') ?? '')),
|
||||
];
|
||||
$html = \AKSRT\Widgets\RecentCommentsWidget::render_html($instance);
|
||||
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
|
||||
}
|
||||
|
||||
public function rest_user_lists(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetUserLists')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'number' => max(1, min(50, (int) ($request->get_param('number') ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'user_registered')),
|
||||
'order' => sanitize_key((string) ($request->get_param('order') ?? 'DESC')),
|
||||
'include' => sanitize_text_field((string) ($request->get_param('include') ?? '')),
|
||||
'exclude' => sanitize_text_field((string) ($request->get_param('exclude') ?? '')),
|
||||
'hide_box' => (int) ($request->get_param('hide_box') ?? 0),
|
||||
];
|
||||
$html = \AKSRT\Widgets\WidgetUserLists::render_html($instance);
|
||||
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
|
||||
}
|
||||
|
||||
public function rest_forum_topics(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetForumHotTopics')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(20, (int) ($request->get_param('count') ?? 5))),
|
||||
'period' => sanitize_key((string) ($request->get_param('period') ?? 'week')),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'views')),
|
||||
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
|
||||
];
|
||||
$page = max(1, (int) ($request->get_param('page') ?? 1));
|
||||
return $this->payload(\AKSRT\Widgets\WidgetForumHotTopics::query_payload($instance, $page), $page);
|
||||
}
|
||||
|
||||
public function rest_forum_terms(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetForumTopicTerms')) {
|
||||
return $this->fail();
|
||||
}
|
||||
$instance = [
|
||||
'number' => max(1, min(40, (int) ($request->get_param('number') ?? 12))),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'count')),
|
||||
'hide_empty' => $request->get_param('hide_empty') === null ? 1 : (int) $request->get_param('hide_empty'),
|
||||
'include' => sanitize_text_field((string) ($request->get_param('include') ?? '')),
|
||||
'style' => sanitize_key((string) ($request->get_param('style') ?? 'mini')),
|
||||
];
|
||||
$html = \AKSRT\Widgets\WidgetForumTopicTerms::render_html($instance);
|
||||
return new \WP_REST_Response(['success' => true, 'html' => $html], 200);
|
||||
}
|
||||
|
||||
private function fail(): \WP_REST_Response {
|
||||
return new \WP_REST_Response(['success' => false, 'message' => 'unavailable'], 500);
|
||||
}
|
||||
|
||||
private function payload(array $payload, int $page): \WP_REST_Response {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user