diff --git a/src/AKSRT/Core/WidgetAjax.php b/src/AKSRT/Core/WidgetAjax.php index 5cd0166d..82145a25 100644 --- a/src/AKSRT/Core/WidgetAjax.php +++ b/src/AKSRT/Core/WidgetAjax.php @@ -25,17 +25,24 @@ class WidgetAjax { public function register_routes(): void { $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', - ]; + '/widgets/posts' => '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', @@ -222,6 +229,127 @@ class WidgetAjax { 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); } diff --git a/src/AKSRT/Widgets/RecentCommentsWidget.php b/src/AKSRT/Widgets/RecentCommentsWidget.php index bfaa45d7..2a382d95 100644 --- a/src/AKSRT/Widgets/RecentCommentsWidget.php +++ b/src/AKSRT/Widgets/RecentCommentsWidget.php @@ -78,7 +78,10 @@ class RecentCommentsWidget extends \WP_Widget { if ($title !== '') { echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } + $rest = rest_url('aksrt/v1/widgets/recent-comments'); + echo '