'rest_main_posts', '/widgets/tab-posts' => 'rest_tab_posts', '/widgets/paid-posts' => 'rest_paid_posts', '/widgets/category-tabs' => 'rest_category_tabs', '/widgets/category-block' => 'rest_category_block', '/widgets/category-recommend' => 'rest_category_recommend', '/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', '/widgets/forum-boards' => 'rest_forum_boards', '/widgets/tag-cloud' => 'rest_tag_cloud', '/widgets/categories' => 'rest_categories', '/widgets/term-cards' => 'rest_term_cards', '/widgets/term-aggregation' => 'rest_term_aggregation', ]; 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 $this->fail(); } $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)), 'topics' => sanitize_text_field((string) ($request->get_param('topics') ?? '')), 'pay_type' => sanitize_key((string) ($request->get_param('pay_type') ?? '')), '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' => in_array(sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')), ['none','ajax','number'], true) ? sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')) : 'ajax', ]; $page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1)); 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 $this->fail(); } $default_count = max(1, min(20, (int) ($request->get_param('count') ?? 5))); $style = sanitize_key((string) ($request->get_param('style') ?? 'mini')); if (!in_array($style, ['list', 'card', 'mini'], true)) { $style = 'mini'; } $paginate = sanitize_key((string) ($request->get_param('paginate') ?? 'none')); if (!in_array($paginate, ['none', 'ajax', 'number'], true)) { $paginate = 'none'; } $page = max(1, (int) ($request->get_param('page') ?? 1)); $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, ]; $opts = [ 'style' => $style, 'paginate' => $paginate, '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), ]; $payload = \AKSRT\Widgets\TabPostsWidget::render_tab_payload($tab, $default_count, $opts, $page); $html = (string) ($payload['html'] ?? ''); if ($html === '') { $html = function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) : '

' . esc_html__('暂无文章', 'aksrt') . '

'; } else { $html = '
' . $html . '
'; } return new \WP_REST_Response([ 'success' => true, 'html' => $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_paid_posts(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\WidgetPaidPosts')) { return $this->fail(); } $instance = [ 'count' => max(1, min(20, (int) ($request->get_param('count') ?? 6))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')), 'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')), ]; $page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1)); return $this->payload(\AKSRT\Widgets\WidgetPaidPosts::query_payload($instance, $page), $page); } public function rest_category_tabs(\WP_REST_Request $request) { $cat = max(0, (int) ($request->get_param('cat') ?? 0)); $count = max(1, min(12, (int) ($request->get_param('count') ?? 6))); $layout = sanitize_key((string) ($request->get_param('layout') ?? 'card')); $columns = max(1, min(4, (int) ($request->get_param('columns') ?? 3))); $context = sanitize_key((string) ($request->get_param('context') ?? 'homepage')); if ($cat <= 0) { return new \WP_REST_Response(['success' => true, 'html' => ''], 200); } $post_types = function_exists('aksrt_home_widget_post_types') ? aksrt_home_widget_post_types() : ['post']; $query = new \WP_Query([ 'post_type' => $post_types, 'post_status' => 'publish', 'posts_per_page' => $count, 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'cat' => $cat, ]); ob_start(); if (function_exists('aksrt_home_widget_render_post_grid')) { aksrt_home_widget_render_post_grid($query, $layout, $columns, $context); } elseif ($query->have_posts()) { if (function_exists('aksrt_render_mini_list')) { echo '
'; aksrt_render_mini_list($query, ['context' => 'widget', 'show_thumb' => true]); echo '
'; } else { echo ''; } } else { echo function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped : '

' . esc_html__('暂无文章', 'aksrt') . '

'; } wp_reset_postdata(); $html = (string) ob_get_clean(); if ($html === '') { $html = function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('暂无文章', 'aksrt')) : '

' . esc_html__('暂无文章', 'aksrt') . '

'; } return new \WP_REST_Response(['success' => true, 'html' => $html], 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)), 'topics' => sanitize_text_field((string) ($request->get_param('topics') ?? '')), 'pay_type' => sanitize_key((string) ($request->get_param('pay_type') ?? '')), ]; $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' => sanitize_text_field((string) ($request->get_param('outpost') ?? '')), '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), 'blur_bg' => (int) ($request->get_param('blur_bg') ?? 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(); } $style = sanitize_key((string) ($request->get_param('style') ?? 'mini')); if (!in_array($style, ['mini', 'rank'], true)) { $style = 'mini'; } $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')), 'style' => $style, 'plate' => max(0, (int) ($request->get_param('plate') ?? $request->get_param('plate_id') ?? 0)), '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); } public function rest_category_block(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\CategoryBlockWidget')) { return $this->fail(); } $instance = [ 'category_id' => max(0, (int) ($request->get_param('cat') ?? $request->get_param('category_id') ?? 0)), 'count' => max(1, min(12, (int) ($request->get_param('count') ?? 6))), 'columns' => max(1, min(4, (int) ($request->get_param('columns') ?? 3))), 'layout' => sanitize_key((string) ($request->get_param('layout') ?? 'card')), 'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')), ]; $context = sanitize_key((string) ($request->get_param('context') ?? 'homepage')); $page = max(1, (int) ($request->get_param('page') ?? 1)); return $this->payload(\AKSRT\Widgets\CategoryBlockWidget::query_payload($instance, $page, $context), $page); } public function rest_category_recommend(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\CategoryRecommendWidget')) { return $this->fail(); } $raw_ids = $request->get_param('category_ids'); if (is_string($raw_ids)) { $raw_ids = array_filter(array_map('intval', preg_split('/[\s,]+/', $raw_ids) ?: [])); } $instance = [ 'category_ids' => is_array($raw_ids) ? $raw_ids : [], 'posts_per_category' => max(1, min(8, (int) ($request->get_param('posts_per_category') ?? 5))), 'columns' => max(1, min(4, (int) ($request->get_param('columns') ?? 2))), 'show_views' => (int) ($request->get_param('show_views') ?? 0), 'show_description' => $request->get_param('show_description') === null ? 1 : (int) $request->get_param('show_description'), 'show_dates' => $request->get_param('show_dates') === null ? 1 : (int) $request->get_param('show_dates'), 'show_more' => $request->get_param('show_more') === null ? 1 : (int) $request->get_param('show_more'), ]; $html = \AKSRT\Widgets\CategoryRecommendWidget::render_html($instance); return new \WP_REST_Response(['success' => true, 'html' => $html], 200); } public function rest_forum_boards(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\WidgetForumBoardList')) { return $this->fail(); } $instance = [ 'count' => max(1, min(50, (int) ($request->get_param('count') ?? 30))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'menu_order')), 'style' => sanitize_key((string) ($request->get_param('style') ?? 'list')), 'hide_empty' => $request->get_param('hide_empty') === null ? 1 : (int) $request->get_param('hide_empty'), ]; $html = \AKSRT\Widgets\WidgetForumBoardList::render_html($instance); return new \WP_REST_Response(['success' => true, 'html' => $html], 200); } public function rest_tag_cloud(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\TagCloudWidget')) { return $this->fail(); } $instance = [ 'taxonomy' => sanitize_key((string) ($request->get_param('taxonomy') ?? 'post_tag')), 'count' => max(1, min(80, (int) ($request->get_param('count') ?? 15))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'count')), 'color' => sanitize_html_class((string) ($request->get_param('color') ?? 'rand')), 'show_count' => (int) ($request->get_param('show_count') ?? 0), 'blank' => (int) ($request->get_param('blank') ?? 0), 'fixed_width' => (int) ($request->get_param('fixed_width') ?? 0), 'hide_empty' => (int) ($request->get_param('hide_empty') ?? 0), ]; $html = \AKSRT\Widgets\TagCloudWidget::render_html($instance); return new \WP_REST_Response(['success' => true, 'html' => $html], 200); } public function rest_categories(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\CategoriesWidget')) { return $this->fail(); } $instance = [ 'count' => max(1, min(50, (int) ($request->get_param('count') ?? 8))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'count')), 'order' => sanitize_key((string) ($request->get_param('order') ?? 'DESC')), 'parent' => max(0, (int) ($request->get_param('parent') ?? 0)), 'include' => sanitize_text_field((string) ($request->get_param('include') ?? '')), 'exclude' => sanitize_text_field((string) ($request->get_param('exclude') ?? '')), 'show_count' => $request->get_param('show_count') === null ? 1 : (int) $request->get_param('show_count'), 'hide_empty' => $request->get_param('hide_empty') === null ? 1 : (int) $request->get_param('hide_empty'), 'hierarchical' => (int) ($request->get_param('hierarchical') ?? 0), ]; $html = \AKSRT\Widgets\CategoriesWidget::render_html($instance); return new \WP_REST_Response(['success' => true, 'html' => $html], 200); } public function rest_term_cards(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\WidgetTermCards')) { return $this->fail(); } $instance = [ 'taxonomy' => sanitize_key((string) ($request->get_param('taxonomy') ?? 'category')), 'number' => max(1, min(30, (int) ($request->get_param('number') ?? 8))), 'columns' => max(1, min(4, (int) ($request->get_param('columns') ?? 2))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'count')), 'include' => sanitize_text_field((string) ($request->get_param('include') ?? '')), 'hide_empty' => $request->get_param('hide_empty') === null ? 1 : (int) $request->get_param('hide_empty'), ]; $html = \AKSRT\Widgets\WidgetTermCards::render_html($instance); return new \WP_REST_Response(['success' => true, 'html' => $html], 200); } public function rest_term_aggregation(\WP_REST_Request $request) { if (!class_exists('\AKSRT\Widgets\WidgetTermAggregation')) { return $this->fail(); } $instance = [ 'taxonomy' => sanitize_key((string) ($request->get_param('taxonomy') ?? 'category')), 'term_ids' => sanitize_text_field((string) ($request->get_param('term_ids') ?? '')), 'count' => max(1, min(12, (int) ($request->get_param('count') ?? 4))), 'columns' => max(1, min(3, (int) ($request->get_param('columns') ?? 1))), 'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')), 'auto_terms' => max(1, min(4, (int) ($request->get_param('auto_terms') ?? 2))), ]; $html = \AKSRT\Widgets\WidgetTermAggregation::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); } }