PluginProbe
ElasticPress / 5.0.2
ElasticPress v5.0.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / REST / SearchOrdering.php

SearchOrdering.php in ElasticPress 5.0.2, at includes/classes/REST/SearchOrdering.php

127 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Custom Results REST API Controller
4 *
5 * @since 5.0.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\REST;
10
11 use ElasticPress\Features;
12 use ElasticPress\Utils;
13
14 /**
15 * Custom Results API controller class.
16 *
17 * @since 5.0.0
18 * @package elasticpress
19 */
20 class SearchOrdering {
21
22 /**
23 * Register routes.
24 *
25 * @return void
26 */
27 public function register_routes() {
28 register_rest_route(
29 'elasticpress/v1',
30 'pointer_search',
31 [
32 'args' => $this->get_args(),
33 'callback' => [ $this, 'get_posts' ],
34 'methods' => 'GET',
35 'permission_callback' => [ $this, 'check_permission' ],
36 ]
37 );
38
39 register_rest_route(
40 'elasticpress/v1',
41 'pointer_preview',
42 [
43 'args' => $this->get_args(),
44 'callback' => [ $this, 'get_preview' ],
45 'methods' => 'GET',
46 'permission_callback' => [ $this, 'check_permission' ],
47 ]
48 );
49 }
50
51
52 /**
53 * Get args schema.
54 *
55 * @return array
56 */
57 public function get_args() {
58 return [
59 's' => [
60 'description' => __( 'Search query.', 'elasticpress' ),
61 'required' => true,
62 'type' => 'string',
63 'validate_callback' => fn( $param ) => ! empty( $param ),
64 ],
65 ];
66 }
67
68 /**
69 * Check that the request has permission to sync.
70 *
71 * @return boolean
72 */
73 public function check_permission() {
74 $capability = Utils\get_capability();
75
76 return current_user_can( $capability );
77 }
78
79 /**
80 * Handles the search for posts from the admin interface for the post
81 * type.
82 *
83 * @param \WP_REST_Request $request Full details about the request.
84 * @return array
85 */
86 public function get_posts( \WP_REST_Request $request ) {
87 $search = $request->get_param( 's' );
88
89 /** Features Class @var Features $features */
90 $features = Features::factory();
91
92 /** Search Feature @var Feature\Search\Search $search */
93 $search_feature = $features->get_registered_feature( 'search' );
94
95 $post_types = $search_feature->get_searchable_post_types();
96
97 $query = new \WP_Query(
98 [
99 'post_type' => $post_types,
100 'post_status' => 'publish',
101 's' => $search,
102 ]
103 );
104
105 return $query->posts;
106 }
107
108 /**
109 * Handles the search preview on the pointer edit screen.
110 *
111 * @param \WP_REST_Request $request Full details about the request.
112 * @return array
113 */
114 public function get_preview( $request ) {
115 $search = $request->get_param( 's' );
116
117 $query = new \WP_Query(
118 [
119 's' => $search,
120 'exclude_pointers' => true,
121 ]
122 );
123
124 return $query->posts;
125 }
126 }
127