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

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