-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-rest-posts-controller.php
More file actions
293 lines (261 loc) · 7.13 KB
/
class-rest-posts-controller.php
File metadata and controls
293 lines (261 loc) · 7.13 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
<?php
/**
* Class REST_Posts_Controller
*
* @package FuxtApi
*/
namespace FuxtApi;
use FuxtApi\Utils\Post as PostUtils;
use FuxtApi\Utils\Utils;
/**
* Class REST_Posts_Controller
*
* @package FuxtApi
*/
class REST_Posts_Controller {
const REST_NAMESPACE = 'fuxt/v1';
const ROUTE = '/posts';
/**
* Init function.
*/
public function init() {
add_action( 'rest_api_init', array( $this, 'register_endpoint' ) );
}
/**
* Register post endpoint.
*/
public function register_endpoint() {
register_rest_route(
self::REST_NAMESPACE,
self::ROUTE,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'array',
'title' => 'fuxt_posts',
'items' => array(
'type' => 'object',
'properties' => ( REST_Post_Controller::get_item_schema() )['properties'],
),
);
return $schema;
}
/**
* Checks if a given request has access to read posts.
*
* @param \WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_item_permissions_check( $request ) {
return true;
}
/**
* Retrieves the query params for the collection.
*
* @return array Collection parameters.
*/
public function get_collection_params() {
return array(
'post_parent_uri' => array(
'description' => __( 'Parent post slug', 'fuxt-api' ),
'type' => 'string',
),
'term_slug' => array(
'description' => __( 'Terms slug', 'fuxt-api' ),
'type' => 'string',
),
'orderby' => array(
'description' => __( 'orderby', 'fuxt-api' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'author',
'date',
'id',
'include',
'modified',
'parent',
'relevance',
'slug',
'include_slugs',
'title',
'menu_order',
),
),
'order' => array(
'description' => __( 'order', 'fuxt-api' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
),
'per_page' => array(
'description' => __( 'Per page', 'fuxt-api' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
),
'page' => array(
'description' => __( 'Page number', 'fuxt-api' ),
'type' => 'integer',
),
'post_type' => array(
'description' => __( 'Post type, or comma-separated list of post types', 'fuxt-api' ),
'type' => 'string',
),
'fields' => array(
'description' => __( 'Additional fields to return. Comma separated string of fields.', 'fuxt-api' ),
'type' => 'string',
'items' => array(
'type' => 'string',
'enum' => array(
'acf',
'terms',
'siblings',
'children',
'parent',
'ancestors',
'next',
'prev',
),
),
),
);
}
/**
* Retrieves a collection of posts.
*
* @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_item( $request ) {
$additional_fields = $this->get_additional_fields_for_response( $request );
$posts = PostUtils::get_posts( $request, $additional_fields );
if ( empty( $posts ) ) {
$posts = array(
'list' => array(),
'total' => 0,
'total_pages' => 0,
);
}
$response = rest_ensure_response( $posts['list'] );
$response->header( 'X-WP-Total', (int) $posts['total'] );
$response->header( 'X-WP-TotalPages', (int) $posts['total_pages'] );
return $response;
}
/**
* Gets an array of additional fields to be included on the response.
*
* Included fields are based on item schema and `fields=` request argument.
*
* @param WP_REST_Request $request Full details about the request.
* @return string[] Fields to be included in the response.
*/
public function get_additional_fields_for_response( $request ) {
if ( ! isset( $request['fields'] ) ) {
return array();
}
$requested_fields = wp_parse_list( $request['fields'] );
if ( 0 === count( $requested_fields ) ) {
return array();
}
$fields = array(
'acf',
'terms',
'siblings',
'children',
'parent',
'ancestors',
'next',
'prev',
);
// Trim off outside whitespace from the comma delimited list.
$requested_fields = array_map( 'trim', $requested_fields );
// Always persist 'id'.
$requested_fields[] = 'id';
// Return the list of all requested fields which appear in the schema.
return array_reduce(
$requested_fields,
static function ( $response_fields, $field ) use ( $fields ) {
if ( in_array( $field, $fields, true ) ) {
$response_fields[] = $field;
return $response_fields;
}
// Check for nested fields if $field is not a direct match.
$nested_fields = explode( '.', $field );
/*
* A nested field is included so long as its top-level property
* is present in the schema.
*/
if ( in_array( $nested_fields[0], $fields, true ) ) {
$response_fields[] = $field;
}
return $response_fields;
},
array()
);
}
/**
* Checks if a post can be read.
*
* Correctly handles posts with the inherit status.
*
* @param WP_Post $post Post object.
* @return bool Whether the post can be read.
*/
public function check_read_permission( $post ) {
$post_type = get_post_type_object( $post->post_type );
if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
return false;
}
// Is the post readable?
if ( 'publish' === $post->post_status || current_user_can( 'read_post', $post->ID ) ) {
return true;
}
$post_status_obj = get_post_status_object( $post->post_status );
if ( $post_status_obj && $post_status_obj->public ) {
return true;
}
// Can we read the parent if we're inheriting?
if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
$parent = get_post( $post->post_parent );
if ( $parent ) {
return $this->check_read_permission( $parent );
}
}
/*
* If there isn't a parent, but the status is set to inherit, assume
* it's published (as per get_post_status()).
*/
if ( 'inherit' === $post->post_status ) {
return true;
}
return false;
}
/**
* Checks if a given post type can be viewed or managed.
*
* @param WP_Post_Type|string $post_type Post type name or object.
* @return bool Whether the post type is allowed in REST.
*/
protected function check_is_post_type_allowed( $post_type ) {
if ( ! is_object( $post_type ) ) {
$post_type = get_post_type_object( $post_type );
}
if ( ! empty( $post_type ) && ! empty( $post_type->show_in_rest ) ) {
return true;
}
return false;
}
}