PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
← All changes | classes/RestAPI/ObjectSearch.php +132 -124 2.0.42.6.1 View file →
@@ -7,9 +7,9 @@
7 7 */
8 8
9 9 namespace ContentControl\RestAPI;
10 10
11 -use WP_User_Query, WP_Rest_Controller, WP_REST_Response, WP_REST_Server, WP_Error;
11 +use WP_User_Query, WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error;
12 12
13 13 defined( 'ABSPATH' ) || exit;
14 14
15 15 /**
@@ -32,8 +32,10 @@
32 32 protected $base = 'objectSearch';
33 33
34 34 /**
35 35 * Register API endpoint routes.
36 + *
37 + * @return void
36 38 */
37 39 public function register_routes() {
38 40 register_rest_route(
39 41 $this->namespace,
@@ -92,9 +94,9 @@
92 94
93 95 /**
94 96 * Get block type list.
95 97 *
96 - * @param \WP_REST_Request $request Request object.
98 + * @param \WP_REST_Request<array<string,mixed>> $request Request object.
97 99 *
98 100 * @return WP_Error|WP_REST_Response
99 101 */
100 102 public function object_search( $request ) {
@@ -104,38 +106,60 @@
104 106 if ( ! isset( $nonce ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $nonce ) ), 'content_control_object_search_nonce' ) ) {
105 107 wp_send_json_error();
106 108 }
107 109
108 - $results = [
109 - 'hash' => $request->get_param( 'hash' ),
110 - 'items' => [],
111 - 'totalCount' => 0,
112 - ];
110 + try {
111 + $results = [
112 + 'hash' => $request->get_param( 'hash' ),
113 + 'items' => [],
114 + 'totalCount' => 0,
115 + ];
113 116
114 - $object_kind = isset( $params['entityKind'] ) ? sanitize_text_field( wp_unslash( $params['entityKind'] ) ) : '';
115 - $object_type = isset( $params['entityType'] ) ? sanitize_text_field( wp_unslash( $params['entityType'] ) ) : '';
116 - $include = ! empty( $params['include'] ) ? wp_parse_id_list( wp_unslash( $params['include'] ) ) : [];
117 - $exclude = ! empty( $params['exclude'] ) ? wp_parse_id_list( wp_unslash( $params['exclude'] ) ) : [];
117 + $object_kind = isset( $params['entityKind'] ) ? sanitize_text_field( wp_unslash( $params['entityKind'] ) ) : '';
118 + $object_type = isset( $params['entityType'] ) ? sanitize_text_field( wp_unslash( $params['entityType'] ) ) : '';
119 + $include = ! empty( $params['include'] ) ? wp_parse_id_list( wp_unslash( $params['include'] ) ) : [];
120 + $exclude = ! empty( $params['exclude'] ) ? wp_parse_id_list( wp_unslash( $params['exclude'] ) ) : [];
118 121
119 - if ( ! empty( $include ) ) {
120 - $exclude = array_merge( $include, $exclude );
121 - }
122 + if ( ! empty( $include ) ) {
123 + $exclude = array_merge( $include, $exclude );
124 + }
122 125
123 - switch ( $object_kind ) {
124 - case 'post_type':
125 - $post_type = ! empty( $object_type ) ? $object_type : 'post';
126 + switch ( $object_kind ) {
127 + case 'post_type':
128 + $post_type = ! empty( $object_type ) ? $object_type : 'post';
126 129
127 - if ( ! empty( $include ) ) {
128 - $include_query = $this->post_type_selectlist_query(
130 + if ( ! empty( $include ) ) {
131 + $include_query = $this->post_type_selectlist_query(
132 + $post_type,
133 + [
134 + 'post__in' => $include,
135 + 'posts_per_page' => - 1,
136 + ],
137 + true
138 + );
139 +
140 + foreach ( $include_query['items'] as $id => $name ) {
141 + $results['items'][] = [
142 + 'id' => $id,
143 + 'text' => "$name (ID: $id)",
144 + ];
145 + }
146 +
147 + $results['totalCount'] += (int) $include_query['totalCount'];
148 + }
149 +
150 + $query = $this->post_type_selectlist_query(
129 151 $post_type,
130 152 [
131 - 'post__in' => $include,
132 - 'posts_per_page' => - 1,
153 + 's' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
154 + 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
155 + 'post__not_in' => $exclude,
156 + 'posts_per_page' => 10,
133 157 ],
134 158 true
135 159 );
136 160
137 - foreach ( $include_query['items'] as $id => $name ) {
161 + foreach ( $query['items'] as $id => $name ) {
138 162 $results['items'][] = [
139 163 'id' => $id,
140 164 'text' => "$name (ID: $id)",
141 165 ];
@@ -140,46 +164,46 @@
140 164 'text' => "$name (ID: $id)",
141 165 ];
142 166 }
143 167
144 - $results['totalCount'] += (int) $include_query['totalCount'];
145 - }
168 + $results['totalCount'] += (int) $query['totalCount'];
169 + break;
146 170
147 - $query = $this->post_type_selectlist_query(
148 - $post_type,
149 - [
150 - 's' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
151 - 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
152 - 'post__not_in' => $exclude,
153 - 'posts_per_page' => 10,
154 - ],
155 - true
156 - );
171 + case 'taxonomy':
172 + $taxonomy = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : 'category';
157 173
158 - foreach ( $query['items'] as $id => $name ) {
159 - $results['items'][] = [
160 - 'id' => $id,
161 - 'text' => "$name (ID: $id)",
162 - ];
163 - }
174 + if ( ! empty( $include ) ) {
175 + $include_query = $this->taxonomy_selectlist_query(
176 + $taxonomy,
177 + [
178 + 'include' => $include,
179 + 'number' => 0,
180 + ],
181 + true
182 + );
164 183
165 - $results['totalCount'] += (int) $query['totalCount'];
166 - break;
184 + foreach ( $include_query['items'] as $id => $name ) {
185 + $results['items'][] = [
186 + 'id' => $id,
187 + 'text' => "$name (ID: $id)",
188 + ];
189 + }
167 190
168 - case 'taxonomy':
169 - $taxonomy = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : 'category';
191 + $results['totalCount'] += (int) $include_query['totalCount'];
192 + }
170 193
171 - if ( ! empty( $include ) ) {
172 - $include_query = $this->taxonomy_selectlist_query(
194 + $query = $this->taxonomy_selectlist_query(
173 195 $taxonomy,
174 196 [
175 - 'include' => $include,
176 - 'number' => 0,
197 + 'search' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
198 + 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
199 + 'exclude' => $exclude,
200 + 'number' => 10,
177 201 ],
178 202 true
179 203 );
180 204
181 - foreach ( $include_query['items'] as $id => $name ) {
205 + foreach ( $query['items'] as $id => $name ) {
182 206 $results['items'][] = [
183 207 'id' => $id,
184 208 'text' => "$name (ID: $id)",
185 209 ];
@@ -184,49 +208,49 @@
184 208 'text' => "$name (ID: $id)",
185 209 ];
186 210 }
187 211
188 - $results['totalCount'] += (int) $include_query['totalCount'];
189 - }
212 + $results['totalCount'] += (int) $query['totalCount'];
213 + break;
214 + case 'user':
215 + if ( ! current_user_can( 'list_users' ) ) {
216 + wp_send_json_error();
217 + }
190 218
191 - $query = $this->taxonomy_selectlist_query(
192 - $taxonomy,
193 - [
194 - 'search' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
195 - 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
196 - 'exclude' => $exclude,
197 - 'number' => 10,
198 - ],
199 - true
200 - );
219 + $user_role = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : null;
201 220
202 - foreach ( $query['items'] as $id => $name ) {
203 - $results['items'][] = [
204 - 'id' => $id,
205 - 'text' => "$name (ID: $id)",
206 - ];
207 - }
221 + if ( ! empty( $include ) ) {
222 + $include_query = $this->user_selectlist_query(
223 + [
224 + 'role' => $user_role,
225 + 'include' => $include,
226 + 'number' => - 1,
227 + ],
228 + true
229 + );
208 230
209 - $results['totalCount'] += (int) $query['totalCount'];
210 - break;
211 - case 'user':
212 - if ( ! current_user_can( 'list_users' ) ) {
213 - wp_send_json_error();
214 - }
231 + foreach ( $include_query['items'] as $id => $name ) {
232 + $results['items'][] = [
233 + 'id' => $id,
234 + 'text' => "$name (ID: $id)",
235 + ];
236 + }
215 237
216 - $user_role = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : null;
238 + $results['totalCount'] += (int) $include_query['totalCount'];
239 + }
217 240
218 - if ( ! empty( $include ) ) {
219 - $include_query = $this->user_selectlist_query(
241 + $query = $this->user_selectlist_query(
220 242 [
221 243 'role' => $user_role,
222 - 'include' => $include,
223 - 'number' => - 1,
244 + 'search' => ! empty( $params['s'] ) ? '*' . sanitize_text_field( wp_unslash( $params['s'] ) ) . '*' : null,
245 + 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
246 + 'exclude' => $exclude,
247 + 'number' => 10,
224 248 ],
225 249 true
226 250 );
227 251
228 - foreach ( $include_query['items'] as $id => $name ) {
252 + foreach ( $query['items'] as $id => $name ) {
229 253 $results['items'][] = [
230 254 'id' => $id,
231 255 'text' => "$name (ID: $id)",
232 256 ];
@@ -231,40 +255,18 @@
231 255 'text' => "$name (ID: $id)",
232 256 ];
233 257 }
234 258
235 - $results['totalCount'] += (int) $include_query['totalCount'];
236 - }
259 + $results['totalCount'] += (int) $query['totalCount'];
260 + break;
261 + }
237 262
238 - $query = $this->user_selectlist_query(
239 - [
240 - 'role' => $user_role,
241 - 'search' => ! empty( $params['s'] ) ? '*' . sanitize_text_field( wp_unslash( $params['s'] ) ) . '*' : null,
242 - 'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
243 - 'exclude' => $exclude,
244 - 'number' => 10,
245 - ],
246 - true
247 - );
263 + // Take out keys which were only used to deduplicate.
264 + $results['items'] = array_values( $results['items'] );
248 265
249 - foreach ( $query['items'] as $id => $name ) {
250 - $results['items'][] = [
251 - 'id' => $id,
252 - 'text' => "$name (ID: $id)",
253 - ];
254 - }
255 -
256 - $results['totalCount'] += (int) $query['totalCount'];
257 - break;
258 - }
259 -
260 - // Take out keys which were only used to deduplicate.
261 - $results['items'] = array_values( $results['items'] );
262 -
263 - if ( $results ) {
264 266 return new WP_REST_Response( $results, 200 );
265 - } else {
266 - return new WP_Error( '404', __( 'Something went wrong, the results could not be returned.', 'content-control' ), [ 'status' => 404 ] );
267 + } catch ( \Exception $e ) {
268 + return new WP_Error( '500', __( 'Something went wrong, the results could not be returned.', 'content-control' ), [ 'status' => 500 ] );
267 269 }
268 270 }
269 271
270 272
@@ -270,14 +272,14 @@
270 272
271 273 /**
272 274 * Get a list of posts for a select list.
273 275 *
274 - * @param array $post_type Post type(s) to query.
275 - * @param array $args Query arguments.
276 - * @param boolean $include_total Whether to include the total count in the response.
277 - * @return array
276 + * @param string $post_type Post type(s) to query.
277 + * @param array<string,mixed> $args Query arguments.
278 + * @param boolean $include_total Whether to include the total count in the response.
279 + * @return array{items:array<int,string>,totalCount:int}|array<int,string>
278 280 */
279 - public function post_type_selectlist_query( $post_type = [], $args = [], $include_total = false ) {
281 + public function post_type_selectlist_query( $post_type, $args = [], $include_total = false ) {
280 282 if ( empty( $post_type ) ) {
281 283 $post_type = [ 'any' ];
282 284 }
283 285
@@ -293,8 +295,9 @@
293 295 'update_post_term_cache' => false,
294 296 'update_post_meta_cache' => false,
295 297 ] );
296 298
299 + // $post_type should always be single string?
297 300 if ( 'attachment' === $post_type ) {
298 301 $args['post_status'] = 'inherit';
299 302 }
300 303
@@ -307,8 +310,13 @@
307 310 $query = new \WP_Query( $args );
308 311
309 312 $posts = [];
310 313 foreach ( $query->posts as $post ) {
314 + /**
315 + * The post object.
316 + *
317 + * @var \WP_Post $post
318 + */
311 319 $posts[ $post->ID ] = $post->post_title;
312 320 }
313 321
314 322 $results = [
@@ -326,16 +334,16 @@
326 334
327 335 /**
328 336 * Get a list of terms for a select list.
329 337 *
330 - * @param array $taxonomies Taxonomy(s) to query.
331 - * @param array $args Query arguments.
332 - * @param boolean $include_total Whether to include the total count in the response.
333 - * @return array
338 + * @param string $taxonomy Taxonomy(s) to query.
339 + * @param array<string,mixed> $args Query arguments.
340 + * @param boolean $include_total Whether to include the total count in the response.
341 + * @return array{items:array<int,string>,totalCount:int}|array<int,string>
334 342 */
335 - public function taxonomy_selectlist_query( $taxonomies = [], $args = [], $include_total = false ) {
336 - if ( empty( $taxonomies ) ) {
337 - $taxonomies = [ 'category' ];
343 + public function taxonomy_selectlist_query( $taxonomy, $args = [], $include_total = false ) {
344 + if ( empty( $taxonomy ) ) {
345 + $taxonomy = [ 'category' ];
338 346 }
339 347
340 348 $args = wp_parse_args( $args, [
341 349 'hide_empty' => false,
@@ -344,9 +352,9 @@
344 352 'include' => null,
345 353 'exclude' => null,
346 354 'offset' => 0,
347 355 'page' => null,
348 - 'taxonomy' => $taxonomies,
356 + 'taxonomy' => $taxonomy,
349 357 ] );
350 358
351 359 if ( $args['page'] ) {
352 360 $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
@@ -384,12 +392,12 @@
384 392
385 393 /**
386 394 * Get a list of users for a select list.
387 395 *
388 - * @param array $args Query arguments.
389 - * @param bool $include_total Whether to include the total count in the response.
396 + * @param array<string,mixed> $args Query arguments.
397 + * @param bool $include_total Whether to include the total count in the response.
390 398 *
391 - * @return array|mixed
399 + * @return array{items:array<int,string>,totalCount:int}|array<int,string>
392 400 */
393 401 public function user_selectlist_query( $args = [], $include_total = false ) {
394 402 $args = wp_parse_args(
395 403 $args,