ObjectSearch.php
11.3 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* RestAPI Global Settings Endpoint.
*
* @copyright (c) 2021, Code Atlantic LLC.
* @package ContentControl
*/
namespace ContentControl\RestAPI;
use WP_User_Query, WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error;
defined( 'ABSPATH' ) || exit;
/**
* Rest API Object Search Controller Class.
*/
class ObjectSearch extends WP_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'content-control/v2';
/**
* Route base.
*
* @var string
*/
protected $base = 'objectSearch';
/**
* Register API endpoint routes.
*
* @return void
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->base,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'object_search' ],
'permission_callback' => '__return_true', // Read only, so anyone can view.
'args' => [
'nonce' => [
'description' => __( 'Nonce', 'content-control' ),
'type' => 'string',
'required' => true,
],
'hash' => [
'description' => __( 'Hash', 'content-control' ),
'type' => 'string',
'required' => true,
],
'entityKind' => [
'description' => __( 'Object kind, (postType, taxonomy)', 'content-control' ),
'type' => 'string',
'required' => true,
],
'entityType' => [
'description' => __( 'Object type (post, category)', 'content-control' ),
'type' => 'string',
'required' => true,
],
'paged' => [
'description' => __( 'Page number', 'content-control' ),
'type' => 'integer',
'required' => false,
],
's' => [
'description' => __( 'Search term', 'content-control' ),
'type' => 'string',
'required' => false,
],
'include' => [
'description' => __( 'Include IDs', 'content-control' ),
'type' => 'string',
'required' => false,
],
'exclude' => [
'description' => __( 'Exclude IDs', 'content-control' ),
'type' => 'string',
'required' => false,
],
],
],
]
);
}
/**
* Get block type list.
*
* @param \WP_REST_Request<array<string,mixed>> $request Request object.
*
* @return WP_Error|WP_REST_Response
*/
public function object_search( $request ) {
$nonce = $request->get_param( 'nonce' );
$params = $request->get_params();
if ( ! isset( $nonce ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $nonce ) ), 'content_control_object_search_nonce' ) ) {
wp_send_json_error();
}
try {
$results = [
'hash' => $request->get_param( 'hash' ),
'items' => [],
'totalCount' => 0,
];
$object_kind = isset( $params['entityKind'] ) ? sanitize_text_field( wp_unslash( $params['entityKind'] ) ) : '';
$object_type = isset( $params['entityType'] ) ? sanitize_text_field( wp_unslash( $params['entityType'] ) ) : '';
$include = ! empty( $params['include'] ) ? wp_parse_id_list( wp_unslash( $params['include'] ) ) : [];
$exclude = ! empty( $params['exclude'] ) ? wp_parse_id_list( wp_unslash( $params['exclude'] ) ) : [];
if ( ! empty( $include ) ) {
$exclude = array_merge( $include, $exclude );
}
switch ( $object_kind ) {
case 'post_type':
$post_type = ! empty( $object_type ) ? $object_type : 'post';
if ( ! empty( $include ) ) {
$include_query = $this->post_type_selectlist_query(
$post_type,
[
'post__in' => $include,
'posts_per_page' => - 1,
],
true
);
foreach ( $include_query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $include_query['totalCount'];
}
$query = $this->post_type_selectlist_query(
$post_type,
[
's' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
'post__not_in' => $exclude,
'posts_per_page' => 10,
],
true
);
foreach ( $query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $query['totalCount'];
break;
case 'taxonomy':
$taxonomy = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : 'category';
if ( ! empty( $include ) ) {
$include_query = $this->taxonomy_selectlist_query(
$taxonomy,
[
'include' => $include,
'number' => 0,
],
true
);
foreach ( $include_query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $include_query['totalCount'];
}
$query = $this->taxonomy_selectlist_query(
$taxonomy,
[
'search' => ! empty( $params['s'] ) ? sanitize_text_field( wp_unslash( $params['s'] ) ) : null,
'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
'exclude' => $exclude,
'number' => 10,
],
true
);
foreach ( $query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $query['totalCount'];
break;
case 'user':
if ( ! current_user_can( 'list_users' ) ) {
wp_send_json_error();
}
$user_role = ! empty( $params['object_key'] ) ? sanitize_text_field( wp_unslash( $params['object_key'] ) ) : null;
if ( ! empty( $include ) ) {
$include_query = $this->user_selectlist_query(
[
'role' => $user_role,
'include' => $include,
'number' => - 1,
],
true
);
foreach ( $include_query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $include_query['totalCount'];
}
$query = $this->user_selectlist_query(
[
'role' => $user_role,
'search' => ! empty( $params['s'] ) ? '*' . sanitize_text_field( wp_unslash( $params['s'] ) ) . '*' : null,
'paged' => ! empty( $params['paged'] ) ? absint( $params['paged'] ) : null,
'exclude' => $exclude,
'number' => 10,
],
true
);
foreach ( $query['items'] as $id => $name ) {
$results['items'][] = [
'id' => $id,
'text' => "$name (ID: $id)",
];
}
$results['totalCount'] += (int) $query['totalCount'];
break;
}
// Take out keys which were only used to deduplicate.
$results['items'] = array_values( $results['items'] );
return new WP_REST_Response( $results, 200 );
} catch ( \Exception $e ) {
return new WP_Error( '500', __( 'Something went wrong, the results could not be returned.', 'content-control' ), [ 'status' => 500 ] );
}
}
/**
* Get a list of posts for a select list.
*
* @param string $post_type Post type(s) to query.
* @param array<string,mixed> $args Query arguments.
* @param boolean $include_total Whether to include the total count in the response.
* @return array{items:array<int,string>,totalCount:int}|array<int,string>
*/
public function post_type_selectlist_query( $post_type, $args = [], $include_total = false ) {
if ( empty( $post_type ) ) {
$post_type = [ 'any' ];
}
$args = wp_parse_args( $args, [
'posts_per_page' => 10,
'post_type' => $post_type,
'post__in' => null,
'post__not_in' => null,
'post_status' => null,
'page' => 1,
// Performance Optimization.
'no_found_rows' => ! $include_total ? true : false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
] );
// $post_type should always be single string?
if ( 'attachment' === $post_type ) {
$args['post_status'] = 'inherit';
}
// Query Caching.
static $queries = [];
$key = md5( maybe_serialize( $args ) );
if ( ! isset( $queries[ $key ] ) ) {
$query = new \WP_Query( $args );
$posts = [];
foreach ( $query->posts as $post ) {
/**
* The post object.
*
* @var \WP_Post $post
*/
$posts[ $post->ID ] = $post->post_title;
}
$results = [
'items' => $posts,
'totalCount' => $query->found_posts,
];
$queries[ $key ] = $results;
} else {
$results = $queries[ $key ];
}
return ! $include_total ? $results['items'] : $results;
}
/**
* Get a list of terms for a select list.
*
* @param string $taxonomy Taxonomy(s) to query.
* @param array<string,mixed> $args Query arguments.
* @param boolean $include_total Whether to include the total count in the response.
* @return array{items:array<int,string>,totalCount:int}|array<int,string>
*/
public function taxonomy_selectlist_query( $taxonomy, $args = [], $include_total = false ) {
if ( empty( $taxonomy ) ) {
$taxonomy = [ 'category' ];
}
$args = wp_parse_args( $args, [
'hide_empty' => false,
'number' => 10,
'search' => '',
'include' => null,
'exclude' => null,
'offset' => 0,
'page' => null,
'taxonomy' => $taxonomy,
] );
if ( $args['page'] ) {
$args['offset'] = ( $args['page'] - 1 ) * $args['number'];
}
// Query Caching.
static $queries = [];
$key = md5( maybe_serialize( $args ) );
if ( ! isset( $queries[ $key ] ) ) {
$terms = [];
foreach ( get_terms( $args ) as $term ) {
$terms[ $term->term_id ] = $term->name;
}
$total_args = $args;
$total_args['fields'] = 'count';
unset( $total_args['number'] );
unset( $total_args['offset'] );
$results = [
'items' => $terms,
'totalCount' => $include_total ? get_terms( $total_args ) : null,
];
$queries[ $key ] = $results;
} else {
$results = $queries[ $key ];
}
return ! $include_total ? $results['items'] : $results;
}
/**
* Get a list of users for a select list.
*
* @param array<string,mixed> $args Query arguments.
* @param bool $include_total Whether to include the total count in the response.
*
* @return array{items:array<int,string>,totalCount:int}|array<int,string>
*/
public function user_selectlist_query( $args = [], $include_total = false ) {
$args = wp_parse_args(
$args,
[
'role' => null,
'count_total' => ! $include_total ? true : false,
]
);
// Query Caching.
static $queries = [];
$key = md5( maybe_serialize( $args ) );
if ( ! isset( $queries[ $key ] ) ) {
$query = new WP_User_Query( $args );
$users = [];
foreach ( $query->get_results() as $user ) {
$users[ $user->ID ] = $user->display_name;
}
$results = [
'items' => $users,
'totalCount' => $query->get_total(),
];
$queries[ $key ] = $results;
} else {
$results = $queries[ $key ];
}
return ! $include_total ? $results['items'] : $results;
}
}