forked from CalderaWP/searchwp-api-route
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.php
235 lines (207 loc) · 4.51 KB
/
route.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Adds a SearchWP Endpoint to the WordPress REST API
*
* @package swp_api
* @author Josh Pollock <Josh@JoshPress.net>
* @license GPL-2.0+
* @link
* @copyright 2015 Josh Pollock
*/
namespace calderawp\swp_api;
class route extends \WP_REST_Posts_Controller {
/**
* Define the route
*
* @uses "rest_api_init"
*
* @since 0.1.0
*/
public function the_route() {
register_rest_route( 'swp_api', '/search',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'the_search' ),
'args' => array(
's' => array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
),
'engine' => array(
'default' => 'default',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => array( $this, 'validate_engine' ),
),
'posts_per_page' => array(
'default' => get_option( 'posts_per_page', 15 ),
'sanitize_callback' => 'absint',
'validate_callback' => array( $this, 'limit_posts_per_page')
),
'nopaging' => array(
'default' => 0,
'sanitize_callback' => 'absint',
),
'load_posts' => array(
'default' => 1,
'sanitize_callback' => 'absint',
),
'page' => array(
'default' => 1,
'sanitize_callback' => 'absint',
),
'post__in' => array(
'default' => array(),
'sanitize_callback' => 'absint',
),
'post__not_in' => array(
'default' => array(),
'sanitize_callback' => '',
),
'tax_query' => array(
'default' => null,
'sanitize_callback' => array( $this, 'sanatize_array'),
'validate_callback' => array( $this, 'validate_tax_query' ),
),
'meta_query' => array(
'default' => null,
'sanitize_callback' => array( $this, 'sanatize_array'),
'validate_callback' => array( $this, 'validate_meta_query' ),
),
)
)
);
}
/**
* Do search and respond.
*
* @since 0.0.1
*
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response|WP_Error
*/
public function the_search( $request ) {
$args = (array) $request->get_params();
$search = new \SWP_Query( $args );
$query_result = $search->posts;
$posts = array();
foreach ( $query_result as $post ) {
$data = $this->prepare_item_for_response( $post, $request );
$posts[] = $this->prepare_response_for_collection( $data );
}
$response = rest_ensure_response( $posts );
return $response;
}
/**
* Ensure that this engine exists.
*
* @since 0.1.0
*
* @param string $engine
*
* @return bool
*/
public function validate_engine( $engine ){
return SWP()->is_valid_engine( $engine );
}
/**
* Cap posts_per_page at 50 to prevent huge requests functioning as DDOS.
*
* @since 0.1.0
*
* @param $posts_per_page
*
* @return int
*/
public function limit_posts_per_page( $posts_per_page ) {
if ( $posts_per_page > 50 ) {
$posts_per_page = 50;
}
return $posts_per_page;
}
/**
* Apply strip_tags to each key of an array.
*
* @since 0.1.0
*
* @param $array
*
* @return array
*/
public function sanatize_array( $array ) {
$array = array_map( "strip_tags", $array );
return $array;
}
/**
* Ensure that the meta query is valid
*
* @since 0.1.0
*
* @param array $query
*
* @return bool
*/
public function validate_meta_query( $query ) {
$required_keys = array(
'key',
'value',
'compare'
);
$valid = $this->validate_query_keys( $query, $required_keys );
return $valid;
}
/**
* Ensure that the tax query is valid
*
* @since 0.1.0
*
* @param array $query
*
* @return bool
*/
public function validate_tax_query( $query ) {
$required_keys = array(
'taxonomy',
'field',
'terms'
);
$valid = $this->validate_query_keys( $query, $required_keys );
return $valid;
}
/**
* Ensure that the date query is valid
*
* @since 0.1.0
*
* @param array $query
*
* @return bool
*/
public function validate_date_query( $query ) {
$required_keys = array(
'year',
'month',
'day'
);
$valid = $this->validate_query_keys( $query, $required_keys );
return $valid;
}
/**
* Ensure an array, in this case meta/tax/date query has the right keys.
*
* @since 0.1.0
*
* @param array $query
* @param array $required_keys
*
* @return bool
*/
protected function validate_query_keys( $query, $required_keys ) {
foreach( $required_keys as $key ) {
if ( ! array_key_exists( $key, $query ) ) {
return false;
}
}
return true;
}
}