PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.0
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 +129 -0 2.7.42.13.0 View file →
@@ -1,0 +1,129 @@
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 + 'validate_callback' => function( $param ) {
54 + return $param === 'any' || post_type_exists( $param );
55 + },
56 + ),
57 + ),
58 + )
59 + );
60 + }
61 +
62 + public function search_blocks( WP_REST_Request $request ) {
63 +
64 + $searchQuery = $request->get_param( 'searchQuery' );
65 + $source = $request->get_param( 'source' );
66 + $current_page_id = $request->get_param( 'current_page_id' );
67 +
68 + $args = array(
69 + 's' => $searchQuery,
70 + 'posts_per_page' => -1,
71 + 'post_type' => $source,
72 + 'post_status' => 'publish',
73 + );
74 +
75 + if ( $current_page_id ) {
76 + $args['post__not_in'] = array( $current_page_id );
77 + }
78 +
79 + $query = new WP_Query( $args );
80 +
81 + if ( ! $query->have_posts() ) {
82 + return new WP_REST_Response(
83 + array(
84 + 'html' => '',
85 + 'data' => array(),
86 + ),
87 + 200
88 + );
89 + }
90 +
91 + $results = array();
92 + $results_html = '';
93 +
94 + while ( $query->have_posts() ) {
95 + $query->the_post();
96 +
97 + $title = get_the_title();
98 + $link = get_permalink();
99 + $thumbnail = get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' );
100 +
101 + ob_start();
102 + Helper::get_template(
103 + 'search-block/search-results.php',
104 + array(
105 + 'title' => $title,
106 + 'link' => $link,
107 + 'thumbnail' => $thumbnail,
108 + )
109 + );
110 + $results_html .= ob_get_clean();
111 +
112 + $results[] = array(
113 + 'title' => $title,
114 + 'link' => $link,
115 + 'thumbnail' => $thumbnail,
116 + );
117 + }//end while
118 +
119 + wp_reset_postdata();
120 +
121 + return new WP_REST_Response(
122 + array(
123 + 'html' => $results_html,
124 + 'data' => $results,
125 + ),
126 + 200
127 + );
128 + }
129 +}