3 * REST API: WP_REST_Post_Statuses_Controller class
11 * Core class used to access post statuses via the REST API.
15 * @see WP_REST_Controller
17 class WP_REST_Post_Statuses_Controller extends WP_REST_Controller {
25 public function __construct() {
26 $this->namespace = 'wp/v2';
27 $this->rest_base = 'statuses';
31 * Registers the routes for the objects of the controller.
36 * @see register_rest_route()
38 public function register_routes() {
40 register_rest_route( $this->namespace, '/' . $this->rest_base, array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get_items' ),
44 'permission_callback' => array( $this, 'get_items_permissions_check' ),
45 'args' => $this->get_collection_params(),
47 'schema' => array( $this, 'get_public_item_schema' ),
50 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<status>[\w-]+)', array(
52 'methods' => WP_REST_Server::READABLE,
53 'callback' => array( $this, 'get_item' ),
54 'permission_callback' => array( $this, 'get_item_permissions_check' ),
56 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
59 'schema' => array( $this, 'get_public_item_schema' ),
64 * Checks whether a given request has permission to read post statuses.
69 * @param WP_REST_Request $request Full details about the request.
70 * @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
72 public function get_items_permissions_check( $request ) {
73 if ( 'edit' === $request['context'] ) {
74 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
76 foreach ( $types as $type ) {
77 if ( current_user_can( $type->cap->edit_posts ) ) {
81 return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to edit posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
88 * Retrieves all post statuses, depending on user context.
93 * @param WP_REST_Request $request Full details about the request.
94 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
96 public function get_items( $request ) {
98 $statuses = get_post_stati( array( 'internal' => false ), 'object' );
99 $statuses['trash'] = get_post_status_object( 'trash' );
101 foreach ( $statuses as $slug => $obj ) {
102 $ret = $this->check_read_permission( $obj );
108 $status = $this->prepare_item_for_response( $obj, $request );
109 $data[ $obj->name ] = $this->prepare_response_for_collection( $status );
112 return rest_ensure_response( $data );
116 * Checks if a given request has access to read a post status.
121 * @param WP_REST_Request $request Full details about the request.
122 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise.
124 public function get_item_permissions_check( $request ) {
125 $status = get_post_status_object( $request['status'] );
127 if ( empty( $status ) ) {
128 return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
131 $check = $this->check_read_permission( $status );
134 return new WP_Error( 'rest_cannot_read_status', __( 'Cannot view status.' ), array( 'status' => rest_authorization_required_code() ) );
141 * Checks whether a given post status should be visible.
146 * @param object $status Post status.
147 * @return bool True if the post status is visible, otherwise false.
149 protected function check_read_permission( $status ) {
150 if ( true === $status->public ) {
154 if ( false === $status->internal || 'trash' === $status->name ) {
155 $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
157 foreach ( $types as $type ) {
158 if ( current_user_can( $type->cap->edit_posts ) ) {
168 * Retrieves a specific post status.
173 * @param WP_REST_Request $request Full details about the request.
174 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
176 public function get_item( $request ) {
177 $obj = get_post_status_object( $request['status'] );
179 if ( empty( $obj ) ) {
180 return new WP_Error( 'rest_status_invalid', __( 'Invalid status.' ), array( 'status' => 404 ) );
183 $data = $this->prepare_item_for_response( $obj, $request );
185 return rest_ensure_response( $data );
189 * Prepares a post status object for serialization.
194 * @param stdClass $status Post status data.
195 * @param WP_REST_Request $request Full details about the request.
196 * @return WP_REST_Response Post status data.
198 public function prepare_item_for_response( $status, $request ) {
201 'name' => $status->label,
202 'private' => (bool) $status->private,
203 'protected' => (bool) $status->protected,
204 'public' => (bool) $status->public,
205 'queryable' => (bool) $status->publicly_queryable,
206 'show_in_list' => (bool) $status->show_in_admin_all_list,
207 'slug' => $status->name,
210 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
211 $data = $this->add_additional_fields_to_object( $data, $request );
212 $data = $this->filter_response_by_context( $data, $context );
214 $response = rest_ensure_response( $data );
216 if ( 'publish' === $status->name ) {
217 $response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
219 $response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
223 * Filters a status returned from the REST API.
225 * Allows modification of the status data right before it is returned.
229 * @param WP_REST_Response $response The response object.
230 * @param object $status The original status object.
231 * @param WP_REST_Request $request Request used to generate the response.
233 return apply_filters( 'rest_prepare_status', $response, $status, $request );
237 * Retrieves the post status' schema, conforming to JSON Schema.
242 * @return array Item schema data.
244 public function get_item_schema() {
246 '$schema' => 'http://json-schema.org/schema#',
249 'properties' => array(
251 'description' => __( 'The title for the status.' ),
253 'context' => array( 'embed', 'view', 'edit' ),
257 'description' => __( 'Whether posts with this status should be private.' ),
259 'context' => array( 'edit' ),
262 'protected' => array(
263 'description' => __( 'Whether posts with this status should be protected.' ),
265 'context' => array( 'edit' ),
269 'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ),
271 'context' => array( 'view', 'edit' ),
274 'queryable' => array(
275 'description' => __( 'Whether posts with this status should be publicly-queryable.' ),
277 'context' => array( 'view', 'edit' ),
280 'show_in_list' => array(
281 'description' => __( 'Whether to include posts in the edit listing for their post type.' ),
283 'context' => array( 'edit' ),
287 'description' => __( 'An alphanumeric identifier for the status.' ),
289 'context' => array( 'embed', 'view', 'edit' ),
295 return $this->add_additional_fields_schema( $schema );
299 * Retrieves the query params for collections.
304 * @return array Collection parameters.
306 public function get_collection_params() {
308 'context' => $this->get_context_param( array( 'default' => 'view' ) ),