class-wp-rest-post-types-controller.php 9.6 KB

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