PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
← All changes | includes/api/search-controller.php +133 -0 2.7.62.13.1 View file →
@@ -1,0 +1,133 @@
1 +<?php
2 +
3 +namespace ABlocks\API;
4 +
5 +use ABlocks\Helper;
6 +use WP_Query;
7 +use WP_REST_Server;
8 +use WP_REST_Request;
9 +use WP_REST_Response;
10 +use WP_Error;
11 +
12 +if ( ! defined( 'ABSPATH' ) ) {
13 + exit;
14 +}
15 +
16 +class SearchController {
17 +
18 + public function __construct() {
19 + add_action( 'rest_api_init', array( $this, 'register_routes' ) );
20 + }
21 +
22 + public function register_routes() {
23 + register_rest_route(
24 + ABLOCKS_REST_NAMESPACE,
25 + '/search',
26 + array(
27 + 'methods' => WP_REST_Server::READABLE,
28 + 'callback' => array( $this, 'search_blocks' ),
29 + 'permission_callback' => '__return_true',
30 + 'args' => array(
31 + 'current_page_id' => array(
32 + 'required' => true,
33 + 'type' => 'string',
34 + 'description' => 'Current page ID to exclude from results.',
35 + 'sanitize_callback' => 'sanitize_text_field',
36 + ),
37 +
38 + 'searchQuery' => array(
39 + 'required' => true,
40 + 'type' => 'string',
41 + 'description' => 'Search keyword.',
42 + 'sanitize_callback' => 'sanitize_text_field',
43 + 'validate_callback' => function( $param ) {
44 + return is_string( $param ) && strlen( trim( $param ) ) >= 1;
45 + },
46 + ),
47 +
48 + 'source' => array(
49 + 'required' => true,
50 + 'type' => 'string',
51 + 'description' => 'Post type to search.',
52 + 'sanitize_callback' => 'sanitize_key',
53 + // Public route: only a post type visitors can already
54 + // see may be searched, never an internal one.
55 + 'validate_callback' => function( $param ) {
56 + return 'any' === $param || is_post_type_viewable( (string) $param );
57 + },
58 + ),
59 + ),
60 + )
61 + );
62 + }
63 +
64 + public function search_blocks( WP_REST_Request $request ) {
65 +
66 + $searchQuery = $request->get_param( 'searchQuery' );
67 + $source = $request->get_param( 'source' );
68 + $current_page_id = $request->get_param( 'current_page_id' );
69 +
70 + $args = array(
71 + 's' => $searchQuery,
72 + // A public, repeatable request must not render every matching post.
73 + 'posts_per_page' => max( 1, (int) apply_filters( 'ablocks/search/max_results', 20, $source ) ),
74 + 'no_found_rows' => true,
75 + 'post_type' => $source,
76 + 'post_status' => 'publish',
77 + );
78 +
79 + if ( $current_page_id ) {
80 + $args['post__not_in'] = array( $current_page_id );
81 + }
82 +
83 + $query = new WP_Query( $args );
84 +
85 + if ( ! $query->have_posts() ) {
86 + return new WP_REST_Response(
87 + array(
88 + 'html' => '',
89 + 'data' => array(),
90 + ),
91 + 200
92 + );
93 + }
94 +
95 + $results = array();
96 + $results_html = '';
97 +
98 + while ( $query->have_posts() ) {
99 + $query->the_post();
100 +
101 + $title = get_the_title();
102 + $link = get_permalink();
103 + $thumbnail = get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' );
104 +
105 + ob_start();
106 + Helper::get_template(
107 + 'search-block/search-results.php',
108 + array(
109 + 'title' => $title,
110 + 'link' => $link,
111 + 'thumbnail' => $thumbnail,
112 + )
113 + );
114 + $results_html .= ob_get_clean();
115 +
116 + $results[] = array(
117 + 'title' => $title,
118 + 'link' => $link,
119 + 'thumbnail' => $thumbnail,
120 + );
121 + }//end while
122 +
123 + wp_reset_postdata();
124 +
125 + return new WP_REST_Response(
126 + array(
127 + 'html' => $results_html,
128 + 'data' => $results,
129 + ),
130 + 200
131 + );
132 + }
133 +}