-
Notifications
You must be signed in to change notification settings - Fork 20
/
class-post-select-controller.php
331 lines (283 loc) · 9.14 KB
/
class-post-select-controller.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
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
<?php
namespace HM\GutenbergTools\Endpoints;
use WP_Query;
use WP_REST_Controller;
use WP_REST_Server;
class Post_Select_Controller extends WP_REST_Controller {
/**
* Type property name.
*/
const PROP_TYPE = 'type';
/**
* Search property name.
*/
const PROP_SEARCH = 'search';
/**
* Include property name.
*/
const PROP_INCLUDE = 'include';
/**
* Per page property name.
*/
const PROP_PER_PAGE = 'per_page';
/**
* Page property name.
*/
const PROP_PAGE = 'page';
/**
* Constructor.
*/
public function __construct() {
$this->namespace = 'hm-gb-tools/v1';
$this->rest_base = 'post-select';
}
/**
* Registers the routes for the objects of the controller.
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permission_check' ],
'args' => $this->get_collection_params(),
],
]
);
}
/**
* Checks if a given request has access to search content.
*
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has search access, WP_Error object otherwise.
*/
public function get_items_permission_check( $request ) {
return current_user_can( 'edit_posts' );
}
/**
* Retrieves a collection of objects.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$search = $request->get_param( self::PROP_SEARCH );
$include = $request->get_param( self::PROP_INCLUDE );
if ( empty( $request->get_param( self::PROP_TYPE ) ) ) {
return [];
}
$query_args = [
'post_type' => $request->get_param( self::PROP_TYPE ),
'posts_per_page' => $request->get_param( self::PROP_PER_PAGE ),
'paged' => $request->get_param( self::PROP_PAGE ),
'tax_query' => [],
'filter_bundles' => true,
];
if ( ! empty( $search ) ) {
$query_args['s'] = $search;
}
foreach ( $this->get_allowed_tax_filters() as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$query = $request->get_param( $base );
if ( ! empty( $query ) ) {
$query_args['tax_query'][] = [
'taxonomy' => $taxonomy->name,
'field' => 'term_id',
'terms' => $query,
'include_children' => false,
];
}
}
if ( $include ) {
$query_args['post__in'] = $include;
$query_args['orderby'] = 'post__in';
}
$query_args = apply_filters( 'hm_gb_tools_post_select_query_args', $query_args );
$query = new WP_Query( $query_args );
$posts = [];
foreach ( $query->posts as $post ) {
$posts[] = $this->prepare_item_for_response( $post, $request );
}
$response = rest_ensure_response( $posts );
$total_posts = $query->found_posts;
$max_pages = ceil( $total_posts / (int) $query->query_vars['posts_per_page'] );
$response->header( 'X-WP-Total', (int) $total_posts );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
return $response;
}
/**
* Prepares a single result for response.
*
* @param int $id ID of the item to prepare.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $post, $request ) {
$data = [
'id' => $post->ID,
'title' => [
'raw' => $post->post_title,
'rendered' => $post->post_title,
],
'type' => $post->post_type,
'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
'slug' => $post->post_name,
'status' => $post->post_status,
'link' => get_permalink( $post->ID ),
'author' => absint( $post->post_author ),
'thumbnail' => apply_filters( 'hm_gb_tools_item_thumbnail_id', null, $post, $request ),
];
// For drafts, `post_date_gmt` may not be set, indicating that the
// date of the draft should be updated each time it is saved (see
// #38883). In this case, shim the value based on the `post_date`
// field with the site's timezone offset applied.
if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) {
$post_date_gmt = get_gmt_from_date( $post->post_date );
} else {
$post_date_gmt = $post->post_date_gmt;
}
$data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
return $data;
}
/**
* Checks the post_date_gmt or modified_gmt and prepare any post or
* modified date for single post output.
*
* @param string $date_gmt GMT publication time.
* @param string|null $date Optional. Local publication time. Default null.
* @return string|null ISO8601/RFC3339 formatted datetime.
*/
protected function prepare_date_response( $date_gmt, $date = null ) {
// Use the date if passed.
if ( isset( $date ) ) {
return mysql_to_rfc3339( $date ); // phpcs:ignore -- this is an internal WordPress function not mysql lib
}
// Return null if $date_gmt is empty/zeros.
if ( '0000-00-00 00:00:00' === $date_gmt ) {
return null;
}
// Return the formatted datetime.
return mysql_to_rfc3339( $date_gmt ); // phpcs:ignore -- this is an internal WordPress function not mysql lib
}
/**
* Retrieves the query params for the search results collection.
*
* @return array Collection parameters.
*/
public function get_collection_params() {
$query_params = parent::get_collection_params();
$allowed_types = $this->get_allowed_post_types();
$query_params[ self::PROP_TYPE ] = [
'description' => __( 'Limit results to items of an object type.', 'hm-gb-tools' ),
'type' => 'array',
'items' => [
'type' => 'string',
],
'sanitize_callback' => [ $this, 'sanitize_post_types' ],
'validate_callback' => function( $value ) {
return is_array( $value );
},
'default' => $allowed_types,
];
$query_params[ self::PROP_SEARCH ] = [
'description' => __( 'Limit results to items that match search query.', 'hm-gb-tools' ),
'type' => 'string',
];
$query_params[ self::PROP_INCLUDE ] = [
'description' => __( 'Include posts by ID.', 'hm-gb-tools' ),
'type' => 'array',
'validate_callback' => function( $ids ) {
return count( $ids ) > 0;
},
'sanitize_callback' => function( $ids ) {
return array_map( 'absint', $ids );
},
];
$query_params[ self::PROP_PER_PAGE ] = [
'description' => __( 'Number of results to return.', 'hm-gb-tools' ),
'type' => 'number',
'sanitize_callback' => function( $val ) {
return min( absint( $val ), 100 );
},
'default' => 25,
];
$query_params[ self::PROP_PAGE ] = [
'description' => __( 'Page of results to return.', 'hm-gb-tools' ),
'type' => 'number',
'sanitize_callback' => function( $val ) {
return absint( $val );
},
'default' => 1,
];
foreach ( $this->get_allowed_tax_filters() as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$query_params[ $base ] = [
/* translators: %s: taxonomy name */
'description' => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ),
'type' => 'array',
'items' => [
'type' => 'integer',
],
'default' => [],
];
}
return $query_params;
}
/**
* Sanitizes the list of subtypes, to ensure only subtypes of the passed type are included.
*
* @param string|array $subtypes One or more subtypes.
* @param WP_REST_Request $request Full details about the request.
* @param string $parameter Parameter name.
* @return array|WP_Error List of valid subtypes, or WP_Error object on failure.
*/
public function sanitize_post_types( $post_types, $request ) {
$allowed_types = $this->get_allowed_post_types();
return array_unique( array_intersect( $post_types, $allowed_types ) );
}
/**
* Get allowed post types.
*
* By default this is only post types that have show_in_rest set to true.
* You can filter this to support more post types if required.
*
* @return array
*/
public function get_allowed_post_types() {
$allowed_types = array_values( get_post_types( [
'show_in_rest' => true,
] ) );
$key = array_search( 'attachment', $allowed_types, true );
if ( false !== $key ) {
unset( $allowed_types[ $key ] );
}
/**
* Filter the allowed post types.
*
* Note that if you allow this for posts that are not otherwise public,
* this data will be accessible using this endpoint for any logged in user with the edit_post capability.
*/
return apply_filters( 'hm_gb_tools_post_select_allowed_post_types', $allowed_types );
}
/**
* Get allowed tax filters.
*
* @return array
*/
public function get_allowed_tax_filters() {
$taxonomies = [];
foreach ( $this->get_allowed_post_types() as $post_type ) {
$taxonomies = array_merge(
$taxonomies,
wp_list_filter( get_object_taxonomies( $post_type, 'objects' ), [ 'show_in_rest' => true ] )
);
}
return $taxonomies;
}
}