3 * REST API: WP_REST_Post_Types_Controller class
11 * Core class to access post types via the REST API.
15 * @see WP_REST_Controller
17 class WP_REST_Post_Types_Controller extends WP_REST_Controller {
25 public function __construct() {
26 $this->namespace = 'wp/v2';
27 $this->rest_base = 'types';
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<type>[\w-]+)', array(
53 'description' => __( 'An alphanumeric identifier for the post type.' ),
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => array( $this, 'get_item' ),
61 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
64 'schema' => array( $this, 'get_public_item_schema' ),
69 * Checks whether a given request has permission to read types.
74 * @param WP_REST_Request $request Full details about the request.
75 * @return WP_Error|true True if the request has read access, WP_Error object otherwise.
77 public function get_items_permissions_check( $request ) {
78 if ( 'edit' === $request['context'] ) {
79 foreach ( get_post_types( array(), 'object' ) as $post_type ) {
80 if ( ! empty( $post_type->show_in_rest ) && current_user_can( $post_type->cap->edit_posts ) ) {
85 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() ) );
92 * Retrieves all public post types.
97 * @param WP_REST_Request $request Full details about the request.
98 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
100 public function get_items( $request ) {
103 foreach ( get_post_types( array(), 'object' ) as $obj ) {
104 if ( empty( $obj->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) ) {
108 $post_type = $this->prepare_item_for_response( $obj, $request );
109 $data[ $obj->name ] = $this->prepare_response_for_collection( $post_type );
112 return rest_ensure_response( $data );
116 * Retrieves a specific post type.
121 * @param WP_REST_Request $request Full details about the request.
122 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
124 public function get_item( $request ) {
125 $obj = get_post_type_object( $request['type'] );
127 if ( empty( $obj ) ) {
128 return new WP_Error( 'rest_type_invalid', __( 'Invalid post type.' ), array( 'status' => 404 ) );
131 if ( empty( $obj->show_in_rest ) ) {
132 return new WP_Error( 'rest_cannot_read_type', __( 'Cannot view post type.' ), array( 'status' => rest_authorization_required_code() ) );
135 if ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) {
136 return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit posts in this post type.' ), array( 'status' => rest_authorization_required_code() ) );
139 $data = $this->prepare_item_for_response( $obj, $request );
141 return rest_ensure_response( $data );
145 * Prepares a post type object for serialization.
150 * @param stdClass $post_type Post type data.
151 * @param WP_REST_Request $request Full details about the request.
152 * @return WP_REST_Response Response object.
154 public function prepare_item_for_response( $post_type, $request ) {
155 $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
156 $taxonomies = wp_list_pluck( $taxonomies, 'name' );
157 $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
159 'capabilities' => $post_type->cap,
160 'description' => $post_type->description,
161 'hierarchical' => $post_type->hierarchical,
162 'labels' => $post_type->labels,
163 'name' => $post_type->label,
164 'slug' => $post_type->name,
165 'taxonomies' => array_values( $taxonomies ),
166 'rest_base' => $base,
168 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
169 $data = $this->add_additional_fields_to_object( $data, $request );
170 $data = $this->filter_response_by_context( $data, $context );
172 // Wrap the data in a response object.
173 $response = rest_ensure_response( $data );
175 $response->add_links( array(
176 'collection' => array(
177 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
179 'https://api.w.org/items' => array(
180 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
185 * Filters a post type returned from the API.
187 * Allows modification of the post type data right before it is returned.
191 * @param WP_REST_Response $response The response object.
192 * @param object $item The original post type object.
193 * @param WP_REST_Request $request Request used to generate the response.
195 return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
199 * Retrieves the post type's schema, conforming to JSON Schema.
204 * @return array Item schema data.
206 public function get_item_schema() {
208 '$schema' => 'http://json-schema.org/schema#',
211 'properties' => array(
212 'capabilities' => array(
213 'description' => __( 'All capabilities used by the post type.' ),
215 'context' => array( 'edit' ),
218 'description' => array(
219 'description' => __( 'A human-readable description of the post type.' ),
221 'context' => array( 'view', 'edit' ),
224 'hierarchical' => array(
225 'description' => __( 'Whether or not the post type should have children.' ),
227 'context' => array( 'view', 'edit' ),
231 'description' => __( 'Human-readable labels for the post type for various contexts.' ),
233 'context' => array( 'edit' ),
237 'description' => __( 'The title for the post type.' ),
239 'context' => array( 'view', 'edit', 'embed' ),
243 'description' => __( 'An alphanumeric identifier for the post type.' ),
245 'context' => array( 'view', 'edit', 'embed' ),
248 'taxonomies' => array(
249 'description' => __( 'Taxonomies associated with post type.' ),
254 'context' => array( 'view', 'edit' ),
257 'rest_base' => array(
258 'description' => __( 'REST base route for the post type.' ),
260 'context' => array( 'view', 'edit', 'embed' ),
265 return $this->add_additional_fields_schema( $schema );
269 * Retrieves the query params for collections.
274 * @return array Collection parameters.
276 public function get_collection_params() {
278 'context' => $this->get_context_param( array( 'default' => 'view' ) ),