WP_REST_Post_Types_Controller

Core class to access post types via the REST API.

Defined (1)

The class is defined in the following location(s).

/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php  
  1. class WP_REST_Post_Types_Controller extends WP_REST_Controller { 
  2.  
  3. /** 
  4. * Constructor. 
  5. * @since 4.7.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $this->namespace = 'wp/v2'; 
  10. $this->rest_base = 'types'; 
  11.  
  12. /** 
  13. * Registers the routes for the objects of the controller. 
  14. * @since 4.7.0 
  15. * @access public 
  16. * @see register_rest_route() 
  17. */ 
  18. public function register_routes() { 
  19.  
  20. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  21. array( 
  22. 'methods' => WP_REST_Server::READABLE,  
  23. 'callback' => array( $this, 'get_items' ),  
  24. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  25. 'args' => $this->get_collection_params(),  
  26. ),  
  27. 'schema' => array( $this, 'get_public_item_schema' ),  
  28. ) ); 
  29.  
  30. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<type>[\w-]+)', array( 
  31. 'args' => array( 
  32. 'type' => array( 
  33. 'description' => __( 'An alphanumeric identifier for the post type.' ),  
  34. 'type' => 'string',  
  35. ),  
  36. ),  
  37. array( 
  38. 'methods' => WP_REST_Server::READABLE,  
  39. 'callback' => array( $this, 'get_item' ),  
  40. 'args' => array( 
  41. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  42. ),  
  43. ),  
  44. 'schema' => array( $this, 'get_public_item_schema' ),  
  45. ) ); 
  46.  
  47. /** 
  48. * Checks whether a given request has permission to read types. 
  49. * @since 4.7.0 
  50. * @access public 
  51. * @param WP_REST_Request $request Full details about the request. 
  52. * @return WP_Error|true True if the request has read access, WP_Error object otherwise. 
  53. */ 
  54. public function get_items_permissions_check( $request ) { 
  55. if ( 'edit' === $request['context'] ) { 
  56. foreach ( get_post_types( array(), 'object' ) as $post_type ) { 
  57. if ( ! empty( $post_type->show_in_rest ) && current_user_can( $post_type->cap->edit_posts ) ) { 
  58. return true; 
  59.  
  60. 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() ) ); 
  61.  
  62. return true; 
  63.  
  64. /** 
  65. * Retrieves all public post types. 
  66. * @since 4.7.0 
  67. * @access public 
  68. * @param WP_REST_Request $request Full details about the request. 
  69. * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 
  70. */ 
  71. public function get_items( $request ) { 
  72. $data = array(); 
  73.  
  74. foreach ( get_post_types( array(), 'object' ) as $obj ) { 
  75. if ( empty( $obj->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) ) { 
  76. continue; 
  77.  
  78. $post_type = $this->prepare_item_for_response( $obj, $request ); 
  79. $data[ $obj->name ] = $this->prepare_response_for_collection( $post_type ); 
  80.  
  81. return rest_ensure_response( $data ); 
  82.  
  83. /** 
  84. * Retrieves a specific post type. 
  85. * @since 4.7.0 
  86. * @access public 
  87. * @param WP_REST_Request $request Full details about the request. 
  88. * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. 
  89. */ 
  90. public function get_item( $request ) { 
  91. $obj = get_post_type_object( $request['type'] ); 
  92.  
  93. if ( empty( $obj ) ) { 
  94. return new WP_Error( 'rest_type_invalid', __( 'Invalid post type.' ), array( 'status' => 404 ) ); 
  95.  
  96. if ( empty( $obj->show_in_rest ) ) { 
  97. return new WP_Error( 'rest_cannot_read_type', __( 'Cannot view post type.' ), array( 'status' => rest_authorization_required_code() ) ); 
  98.  
  99. if ( 'edit' === $request['context'] && ! current_user_can( $obj->cap->edit_posts ) ) { 
  100. 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() ) ); 
  101.  
  102. $data = $this->prepare_item_for_response( $obj, $request ); 
  103.  
  104. return rest_ensure_response( $data ); 
  105.  
  106. /** 
  107. * Prepares a post type object for serialization. 
  108. * @since 4.7.0 
  109. * @access public 
  110. * @param stdClass $post_type Post type data. 
  111. * @param WP_REST_Request $request Full details about the request. 
  112. * @return WP_REST_Response Response object. 
  113. */ 
  114. public function prepare_item_for_response( $post_type, $request ) { 
  115. $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) ); 
  116. $taxonomies = wp_list_pluck( $taxonomies, 'name' ); 
  117. $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; 
  118. $data = array( 
  119. 'capabilities' => $post_type->cap,  
  120. 'description' => $post_type->description,  
  121. 'hierarchical' => $post_type->hierarchical,  
  122. 'labels' => $post_type->labels,  
  123. 'name' => $post_type->label,  
  124. 'slug' => $post_type->name,  
  125. 'taxonomies' => array_values( $taxonomies ),  
  126. 'rest_base' => $base,  
  127. ); 
  128. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  129. $data = $this->add_additional_fields_to_object( $data, $request ); 
  130. $data = $this->filter_response_by_context( $data, $context ); 
  131.  
  132. // Wrap the data in a response object. 
  133. $response = rest_ensure_response( $data ); 
  134.  
  135. $response->add_links( array( 
  136. 'collection' => array( 
  137. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),  
  138. ),  
  139. 'https://api.w.org/items' => array( 
  140. 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),  
  141. ),  
  142. ) ); 
  143.  
  144. /** 
  145. * Filters a post type returned from the API. 
  146. * Allows modification of the post type data right before it is returned. 
  147. * @since 4.7.0 
  148. * @param WP_REST_Response $response The response object. 
  149. * @param object $item The original post type object. 
  150. * @param WP_REST_Request $request Request used to generate the response. 
  151. */ 
  152. return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request ); 
  153.  
  154. /** 
  155. * Retrieves the post type's schema, conforming to JSON Schema. 
  156. * @since 4.7.0 
  157. * @access public 
  158. * @return array Item schema data. 
  159. */ 
  160. public function get_item_schema() { 
  161. $schema = array( 
  162. '$schema' => 'http://json-schema.org/schema#',  
  163. 'title' => 'type',  
  164. 'type' => 'object',  
  165. 'properties' => array( 
  166. 'capabilities' => array( 
  167. 'description' => __( 'All capabilities used by the post type.' ),  
  168. 'type' => 'object',  
  169. 'context' => array( 'edit' ),  
  170. 'readonly' => true,  
  171. ),  
  172. 'description' => array( 
  173. 'description' => __( 'A human-readable description of the post type.' ),  
  174. 'type' => 'string',  
  175. 'context' => array( 'view', 'edit' ),  
  176. 'readonly' => true,  
  177. ),  
  178. 'hierarchical' => array( 
  179. 'description' => __( 'Whether or not the post type should have children.' ),  
  180. 'type' => 'boolean',  
  181. 'context' => array( 'view', 'edit' ),  
  182. 'readonly' => true,  
  183. ),  
  184. 'labels' => array( 
  185. 'description' => __( 'Human-readable labels for the post type for various contexts.' ),  
  186. 'type' => 'object',  
  187. 'context' => array( 'edit' ),  
  188. 'readonly' => true,  
  189. ),  
  190. 'name' => array( 
  191. 'description' => __( 'The title for the post type.' ),  
  192. 'type' => 'string',  
  193. 'context' => array( 'view', 'edit', 'embed' ),  
  194. 'readonly' => true,  
  195. ),  
  196. 'slug' => array( 
  197. 'description' => __( 'An alphanumeric identifier for the post type.' ),  
  198. 'type' => 'string',  
  199. 'context' => array( 'view', 'edit', 'embed' ),  
  200. 'readonly' => true,  
  201. ),  
  202. 'taxonomies' => array( 
  203. 'description' => __( 'Taxonomies associated with post type.' ),  
  204. 'type' => 'array',  
  205. 'items' => array( 
  206. 'type' => 'string',  
  207. ),  
  208. 'context' => array( 'view', 'edit' ),  
  209. 'readonly' => true,  
  210. ),  
  211. 'rest_base' => array( 
  212. 'description' => __( 'REST base route for the post type.' ),  
  213. 'type' => 'string',  
  214. 'context' => array( 'view', 'edit', 'embed' ),  
  215. 'readonly' => true,  
  216. ),  
  217. ),  
  218. ); 
  219. return $this->add_additional_fields_schema( $schema ); 
  220.  
  221. /** 
  222. * Retrieves the query params for collections. 
  223. * @since 4.7.0 
  224. * @access public 
  225. * @return array Collection parameters. 
  226. */ 
  227. public function get_collection_params() { 
  228. return array( 
  229. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  230. ); 
  231.