@@ -231,6 +231,7 @@ public function requirements_not_met_notice() {
231231 */
232232 public function hooks () {
233233 add_filter ( 'wp_enqueue_scripts ' , array ( $ this , 'enqueue_scripts ' ), 1 );
234+ add_action ( 'rest_api_init ' , array ( $ this , 'rest_api_init ' ), 10 , 2 );
234235 add_action ( 'rest_post_query ' , array ( $ this , 'add_post_types_to_query ' ), 10 , 2 );
235236 }
236237
@@ -248,7 +249,7 @@ public function enqueue_scripts() {
248249 wp_enqueue_style ( 'wds-react-post-search-styles ' );
249250
250251 wp_localize_script ( 'wds-react-post-search ' , 'wds_react_post_search ' , array (
251- 'rest_search_posts ' => rest_url ( 'wp/v2/posts? ' . $ this -> get_post_types_to_search () . ' search=%s ' ),
252+ 'rest_search_posts ' => rest_url ( 'wds-react-post-search/v1/ search ' ),
252253 'loading_text ' => apply_filters ( 'wds_react_post_search_loading_text ' , esc_html__ ( 'Loading results... ' , 'wds-react-post-search ' ) ),
253254 'no_results_text ' => apply_filters ( 'wds_react_post_search_no_results_text ' , esc_html__ ( 'No results found. ' , 'wds-react-post-search ' ) ),
254255 'length_error ' => apply_filters ( 'wds_react_post_search_length_error_text ' , esc_html__ ( 'Please enter at least 3 characters. ' , 'wds-react-post-search ' ) ),
@@ -308,41 +309,105 @@ public function add_post_types_to_query( $args, $request ) {
308309 * }
309310 * add_filter( 'wds_react_post_search_filter_post_types', '_s_filter_post_types_to_query' );
310311 *
311- * @param array $post_types Array of post types to be used.
312312 * @author Corey Collins
313313 */
314- public function post_types_to_search ( $ post_types = array () ) {
314+ public function post_types_to_search () {
315315
316- $ default_post_types = array (
317- 'post ' ,
316+ $ post_types = array (
317+ 'any ' ,
318318 );
319319
320- $ all_post_types = array_merge ( $ default_post_types , apply_filters ( 'wds_react_post_search_filter_post_types ' , $ post_types ) );
320+ $ all_post_types = apply_filters ( 'wds_react_post_search_filter_post_types ' , $ post_types );
321321
322322 return $ all_post_types ;
323323 }
324324
325325 /**
326- * Get the string of post types to search for our REST API query .
326+ * Register REST API search results route .
327327 *
328- * @return void Bail if we don't even have filtered post types – we'll just search posts.
329- * @author Corey Collins
328+ * @return void
330329 */
331- public function get_post_types_to_search () {
332-
333- $ post_types = $ this ->post_types_to_search ();
330+ public function rest_api_init () {
331+ register_rest_route ('wds-react-post-search/v1 ' , '/search ' , [
332+ 'methods ' => WP_REST_Server::READABLE ,
333+ 'callback ' => array ( $ this , 'search_posts ' ),
334+ 'args ' => $ this ->get_search_args (),
335+ ]);
336+ }
334337
335- if ( ! $ post_types ) {
336- return ;
337- }
338+ /**
339+ * Define the arguments our endpoint receives.
340+ */
341+ public function get_search_args () {
342+ $ args = [];
343+
344+ $ args ['s ' ] = [
345+ 'description ' => esc_html__ ( 'The search term. ' , 'wds-react-post-search ' ),
346+ 'type ' => 'string ' ,
347+ ];
348+
349+ $ args ['type ' ] = [
350+ 'description ' => esc_html__ ( 'Post Types. ' , 'wds-react-post-search ' ),
351+ 'type ' => 'array ' ,
352+ 'default ' => [],
353+ 'items ' => [
354+ 'type ' => 'string ' ,
355+ ],
356+ ];
338357
339- $ post_types_string = '' ;
358+ return $ args ;
359+ }
340360
341- foreach ( $ post_types as $ post_type ) {
342- $ post_types_string .= 'type[]= ' . esc_attr ( $ post_type ) . '& ' ;
361+ /**
362+ * Get search post types.
363+ *
364+ * @param array $request HTTP request variables.
365+ * @return mixed
366+ */
367+ public function get_valid_search_post_types ( $ request ) {
368+ if ( is_array ( $ request ['type ' ] ) && ! empty ( $ request ['type ' ] ) ) {
369+ return array_map ( 'sanitize_text_field ' , $ request ['type ' ] );
370+ } elseif ( $ request ['type ' ] ) {
371+ return sanitize_text_field ( $ request ['type ' ] );
372+ } else {
373+ return $ this ->post_types_to_search ();
343374 }
375+ }
344376
345- return $ post_types_string ;
377+ /**
378+ * Search posts.
379+ *
380+ * @param array $request HTTP request variables.
381+ * @return array Search results or error.
382+ */
383+ public function search_posts ( $ request ) {
384+ $ posts = [];
385+ $ results = [];
386+
387+ if ( isset ( $ request ['s ' ] ) ) :
388+ $ filter = [
389+ 'posts_per_page ' => -1 ,
390+ 'post_type ' => $ this ->get_valid_search_post_types ( $ request ),
391+ 's ' => sanitize_text_field ( $ request ['s ' ] ),
392+ ];
393+
394+ // Get posts.
395+ $ posts = get_posts ( $ filter );
396+
397+ // Return title and link.
398+ foreach ( $ posts as $ post ) :
399+ $ results [] = [
400+ 'title ' => $ post ->post_title ,
401+ 'link ' => get_permalink ( $ post ->ID ),
402+ ];
403+ endforeach ;
404+ endif ;
405+
406+ if ( empty ( $ results ) ) :
407+ return new WP_Error ( 'wds-react-post-search-results ' , apply_filters ( 'wds_react_post_search_no_results_text ' , esc_html__ ( 'No results found. ' , 'wds-react-post-search ' ) ) );
408+ endif ;
409+
410+ return rest_ensure_response ( $ results );
346411 }
347412}
348413
0 commit comments