76 * Checks whether a given request has permission to read post statuses. |
76 * Checks whether a given request has permission to read post statuses. |
77 * |
77 * |
78 * @since 4.7.0 |
78 * @since 4.7.0 |
79 * |
79 * |
80 * @param WP_REST_Request $request Full details about the request. |
80 * @param WP_REST_Request $request Full details about the request. |
81 * @return WP_Error|bool True if the request has read access, WP_Error object otherwise. |
81 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
82 */ |
82 */ |
83 public function get_items_permissions_check( $request ) { |
83 public function get_items_permissions_check( $request ) { |
84 if ( 'edit' === $request['context'] ) { |
84 if ( 'edit' === $request['context'] ) { |
85 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); |
85 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); |
86 |
86 |
87 foreach ( $types as $type ) { |
87 foreach ( $types as $type ) { |
88 if ( current_user_can( $type->cap->edit_posts ) ) { |
88 if ( current_user_can( $type->cap->edit_posts ) ) { |
89 return true; |
89 return true; |
90 } |
90 } |
91 } |
91 } |
92 return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage post statuses.' ), array( 'status' => rest_authorization_required_code() ) ); |
92 |
|
93 return new WP_Error( |
|
94 'rest_cannot_view', |
|
95 __( 'Sorry, you are not allowed to manage post statuses.' ), |
|
96 array( 'status' => rest_authorization_required_code() ) |
|
97 ); |
93 } |
98 } |
94 |
99 |
95 return true; |
100 return true; |
96 } |
101 } |
97 |
102 |
99 * Retrieves all post statuses, depending on user context. |
104 * Retrieves all post statuses, depending on user context. |
100 * |
105 * |
101 * @since 4.7.0 |
106 * @since 4.7.0 |
102 * |
107 * |
103 * @param WP_REST_Request $request Full details about the request. |
108 * @param WP_REST_Request $request Full details about the request. |
104 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
109 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
105 */ |
110 */ |
106 public function get_items( $request ) { |
111 public function get_items( $request ) { |
107 $data = array(); |
112 $data = array(); |
108 $statuses = get_post_stati( array( 'internal' => false ), 'object' ); |
113 $statuses = get_post_stati( array( 'internal' => false ), 'object' ); |
109 $statuses['trash'] = get_post_status_object( 'trash' ); |
114 $statuses['trash'] = get_post_status_object( 'trash' ); |
126 * Checks if a given request has access to read a post status. |
131 * Checks if a given request has access to read a post status. |
127 * |
132 * |
128 * @since 4.7.0 |
133 * @since 4.7.0 |
129 * |
134 * |
130 * @param WP_REST_Request $request Full details about the request. |
135 * @param WP_REST_Request $request Full details about the request. |
131 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. |
136 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
132 */ |
137 */ |
133 public function get_item_permissions_check( $request ) { |
138 public function get_item_permissions_check( $request ) { |
134 $status = get_post_status_object( $request['status'] ); |
139 $status = get_post_status_object( $request['status'] ); |
135 |
140 |
136 if ( empty( $status ) ) { |
141 if ( empty( $status ) ) { |
137 return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) ); |
142 return new WP_Error( |
|
143 'rest_status_invalid', |
|
144 __( 'Invalid status.' ), |
|
145 array( 'status' => 404 ) |
|
146 ); |
138 } |
147 } |
139 |
148 |
140 $check = $this->check_read_permission( $status ); |
149 $check = $this->check_read_permission( $status ); |
141 |
150 |
142 if ( ! $check ) { |
151 if ( ! $check ) { |
143 return new WP_Error( 'rest_cannot_read_status', __( 'Cannot view status.' ), array( 'status' => rest_authorization_required_code() ) ); |
152 return new WP_Error( |
|
153 'rest_cannot_read_status', |
|
154 __( 'Cannot view status.' ), |
|
155 array( 'status' => rest_authorization_required_code() ) |
|
156 ); |
144 } |
157 } |
145 |
158 |
146 return true; |
159 return true; |
147 } |
160 } |
148 |
161 |
176 * Retrieves a specific post status. |
189 * Retrieves a specific post status. |
177 * |
190 * |
178 * @since 4.7.0 |
191 * @since 4.7.0 |
179 * |
192 * |
180 * @param WP_REST_Request $request Full details about the request. |
193 * @param WP_REST_Request $request Full details about the request. |
181 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
194 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
182 */ |
195 */ |
183 public function get_item( $request ) { |
196 public function get_item( $request ) { |
184 $obj = get_post_status_object( $request['status'] ); |
197 $obj = get_post_status_object( $request['status'] ); |
185 |
198 |
186 if ( empty( $obj ) ) { |
199 if ( empty( $obj ) ) { |
187 return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) ); |
200 return new WP_Error( |
|
201 'rest_status_invalid', |
|
202 __( 'Invalid status.' ), |
|
203 array( 'status' => 404 ) |
|
204 ); |
188 } |
205 } |
189 |
206 |
190 $data = $this->prepare_item_for_response( $obj, $request ); |
207 $data = $this->prepare_item_for_response( $obj, $request ); |
191 |
208 |
192 return rest_ensure_response( $data ); |
209 return rest_ensure_response( $data ); |
230 $data['show_in_list'] = (bool) $status->show_in_admin_all_list; |
247 $data['show_in_list'] = (bool) $status->show_in_admin_all_list; |
231 } |
248 } |
232 |
249 |
233 if ( in_array( 'slug', $fields, true ) ) { |
250 if ( in_array( 'slug', $fields, true ) ) { |
234 $data['slug'] = $status->name; |
251 $data['slug'] = $status->name; |
|
252 } |
|
253 |
|
254 if ( in_array( 'date_floating', $fields, true ) ) { |
|
255 $data['date_floating'] = $status->date_floating; |
235 } |
256 } |
236 |
257 |
237 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
258 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
238 $data = $this->add_additional_fields_to_object( $data, $request ); |
259 $data = $this->add_additional_fields_to_object( $data, $request ); |
239 $data = $this->filter_response_by_context( $data, $context ); |
260 $data = $this->filter_response_by_context( $data, $context ); |
266 * @since 4.7.0 |
287 * @since 4.7.0 |
267 * |
288 * |
268 * @return array Item schema data. |
289 * @return array Item schema data. |
269 */ |
290 */ |
270 public function get_item_schema() { |
291 public function get_item_schema() { |
|
292 if ( $this->schema ) { |
|
293 return $this->add_additional_fields_schema( $this->schema ); |
|
294 } |
|
295 |
271 $schema = array( |
296 $schema = array( |
272 '$schema' => 'http://json-schema.org/draft-04/schema#', |
297 '$schema' => 'http://json-schema.org/draft-04/schema#', |
273 'title' => 'status', |
298 'title' => 'status', |
274 'type' => 'object', |
299 'type' => 'object', |
275 'properties' => array( |
300 'properties' => array( |
276 'name' => array( |
301 'name' => array( |
277 'description' => __( 'The title for the status.' ), |
302 'description' => __( 'The title for the status.' ), |
278 'type' => 'string', |
303 'type' => 'string', |
279 'context' => array( 'embed', 'view', 'edit' ), |
304 'context' => array( 'embed', 'view', 'edit' ), |
280 'readonly' => true, |
305 'readonly' => true, |
281 ), |
306 ), |
282 'private' => array( |
307 'private' => array( |
283 'description' => __( 'Whether posts with this status should be private.' ), |
308 'description' => __( 'Whether posts with this status should be private.' ), |
284 'type' => 'boolean', |
309 'type' => 'boolean', |
285 'context' => array( 'edit' ), |
310 'context' => array( 'edit' ), |
286 'readonly' => true, |
311 'readonly' => true, |
287 ), |
312 ), |
288 'protected' => array( |
313 'protected' => array( |
289 'description' => __( 'Whether posts with this status should be protected.' ), |
314 'description' => __( 'Whether posts with this status should be protected.' ), |
290 'type' => 'boolean', |
315 'type' => 'boolean', |
291 'context' => array( 'edit' ), |
316 'context' => array( 'edit' ), |
292 'readonly' => true, |
317 'readonly' => true, |
293 ), |
318 ), |
294 'public' => array( |
319 'public' => array( |
295 'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ), |
320 'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ), |
296 'type' => 'boolean', |
321 'type' => 'boolean', |
297 'context' => array( 'view', 'edit' ), |
322 'context' => array( 'view', 'edit' ), |
298 'readonly' => true, |
323 'readonly' => true, |
299 ), |
324 ), |
300 'queryable' => array( |
325 'queryable' => array( |
301 'description' => __( 'Whether posts with this status should be publicly-queryable.' ), |
326 'description' => __( 'Whether posts with this status should be publicly-queryable.' ), |
302 'type' => 'boolean', |
327 'type' => 'boolean', |
303 'context' => array( 'view', 'edit' ), |
328 'context' => array( 'view', 'edit' ), |
304 'readonly' => true, |
329 'readonly' => true, |
305 ), |
330 ), |
306 'show_in_list' => array( |
331 'show_in_list' => array( |
307 'description' => __( 'Whether to include posts in the edit listing for their post type.' ), |
332 'description' => __( 'Whether to include posts in the edit listing for their post type.' ), |
308 'type' => 'boolean', |
333 'type' => 'boolean', |
309 'context' => array( 'edit' ), |
334 'context' => array( 'edit' ), |
310 'readonly' => true, |
335 'readonly' => true, |
311 ), |
336 ), |
312 'slug' => array( |
337 'slug' => array( |
313 'description' => __( 'An alphanumeric identifier for the status.' ), |
338 'description' => __( 'An alphanumeric identifier for the status.' ), |
314 'type' => 'string', |
339 'type' => 'string', |
315 'context' => array( 'embed', 'view', 'edit' ), |
340 'context' => array( 'embed', 'view', 'edit' ), |
316 'readonly' => true, |
341 'readonly' => true, |
317 ), |
342 ), |
|
343 'date_floating' => array( |
|
344 'description' => __( 'Whether posts of this status may have floating published dates.' ), |
|
345 'type' => 'boolean', |
|
346 'context' => array( 'view', 'edit' ), |
|
347 'readonly' => true, |
|
348 ), |
318 ), |
349 ), |
319 ); |
350 ); |
320 |
351 |
321 return $this->add_additional_fields_schema( $schema ); |
352 $this->schema = $schema; |
|
353 |
|
354 return $this->add_additional_fields_schema( $this->schema ); |
322 } |
355 } |
323 |
356 |
324 /** |
357 /** |
325 * Retrieves the query params for collections. |
358 * Retrieves the query params for collections. |
326 * |
359 * |