class-wp-rest-taxonomies-controller.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * REST API: WP_REST_Taxonomies_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to manage taxonomies via the REST API.
  11. *
  12. * @since 4.7.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Taxonomies_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 = 'taxonomies';
  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<taxonomy>[\w-]+)', array(
  44. 'args' => array(
  45. 'taxonomy' => array(
  46. 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  47. 'type' => 'string',
  48. ),
  49. ),
  50. array(
  51. 'methods' => WP_REST_Server::READABLE,
  52. 'callback' => array( $this, 'get_item' ),
  53. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  54. 'args' => array(
  55. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  56. ),
  57. ),
  58. 'schema' => array( $this, 'get_public_item_schema' ),
  59. ) );
  60. }
  61. /**
  62. * Checks whether a given request has permission to read taxonomies.
  63. *
  64. * @since 4.7.0
  65. *
  66. * @param WP_REST_Request $request Full details about the request.
  67. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  68. */
  69. public function get_items_permissions_check( $request ) {
  70. if ( 'edit' === $request['context'] ) {
  71. if ( ! empty( $request['type'] ) ) {
  72. $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  73. } else {
  74. $taxonomies = get_taxonomies( '', 'objects' );
  75. }
  76. foreach ( $taxonomies as $taxonomy ) {
  77. if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) {
  78. return true;
  79. }
  80. }
  81. return new WP_Error( 'rest_cannot_view', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
  82. }
  83. return true;
  84. }
  85. /**
  86. * Retrieves all public taxonomies.
  87. *
  88. * @since 4.7.0
  89. *
  90. * @param WP_REST_Request $request Full details about the request.
  91. * @return WP_REST_Response Response object on success, or WP_Error object on failure.
  92. */
  93. public function get_items( $request ) {
  94. // Retrieve the list of registered collection query parameters.
  95. $registered = $this->get_collection_params();
  96. if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
  97. $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  98. } else {
  99. $taxonomies = get_taxonomies( '', 'objects' );
  100. }
  101. $data = array();
  102. foreach ( $taxonomies as $tax_type => $value ) {
  103. if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
  104. continue;
  105. }
  106. $tax = $this->prepare_item_for_response( $value, $request );
  107. $tax = $this->prepare_response_for_collection( $tax );
  108. $data[ $tax_type ] = $tax;
  109. }
  110. if ( empty( $data ) ) {
  111. // Response should still be returned as a JSON object when it is empty.
  112. $data = (object) $data;
  113. }
  114. return rest_ensure_response( $data );
  115. }
  116. /**
  117. * Checks if a given request has access to a taxonomy.
  118. *
  119. * @since 4.7.0
  120. *
  121. * @param WP_REST_Request $request Full details about the request.
  122. * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
  123. */
  124. public function get_item_permissions_check( $request ) {
  125. $tax_obj = get_taxonomy( $request['taxonomy'] );
  126. if ( $tax_obj ) {
  127. if ( empty( $tax_obj->show_in_rest ) ) {
  128. return false;
  129. }
  130. if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) {
  131. return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ), array( 'status' => rest_authorization_required_code() ) );
  132. }
  133. }
  134. return true;
  135. }
  136. /**
  137. * Retrieves a specific taxonomy.
  138. *
  139. * @since 4.7.0
  140. *
  141. * @param WP_REST_Request $request Full details about the request.
  142. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  143. */
  144. public function get_item( $request ) {
  145. $tax_obj = get_taxonomy( $request['taxonomy'] );
  146. if ( empty( $tax_obj ) ) {
  147. return new WP_Error( 'rest_taxonomy_invalid', __( 'Invalid taxonomy.' ), array( 'status' => 404 ) );
  148. }
  149. $data = $this->prepare_item_for_response( $tax_obj, $request );
  150. return rest_ensure_response( $data );
  151. }
  152. /**
  153. * Prepares a taxonomy object for serialization.
  154. *
  155. * @since 4.7.0
  156. *
  157. * @param stdClass $taxonomy Taxonomy data.
  158. * @param WP_REST_Request $request Full details about the request.
  159. * @return WP_REST_Response Response object.
  160. */
  161. public function prepare_item_for_response( $taxonomy, $request ) {
  162. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  163. $fields = $this->get_fields_for_response( $request );
  164. $data = array();
  165. if ( in_array( 'name', $fields, true ) ) {
  166. $data['name'] = $taxonomy->label;
  167. }
  168. if ( in_array( 'slug', $fields, true ) ) {
  169. $data['slug'] = $taxonomy->name;
  170. }
  171. if ( in_array( 'capabilities', $fields, true ) ) {
  172. $data['capabilities'] = $taxonomy->cap;
  173. }
  174. if ( in_array( 'description', $fields, true ) ) {
  175. $data['description'] = $taxonomy->description;
  176. }
  177. if ( in_array( 'labels', $fields, true ) ) {
  178. $data['labels'] = $taxonomy->labels;
  179. }
  180. if ( in_array( 'types', $fields, true ) ) {
  181. $data['types'] = $taxonomy->object_type;
  182. }
  183. if ( in_array( 'show_cloud', $fields, true ) ) {
  184. $data['show_cloud'] = $taxonomy->show_tagcloud;
  185. }
  186. if ( in_array( 'hierarchical', $fields, true ) ) {
  187. $data['hierarchical'] = $taxonomy->hierarchical;
  188. }
  189. if ( in_array( 'rest_base', $fields, true ) ) {
  190. $data['rest_base'] = $base;
  191. }
  192. if ( in_array( 'visibility', $fields, true ) ) {
  193. $data['visibility'] = array(
  194. 'public' => (bool) $taxonomy->public,
  195. 'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
  196. 'show_admin_column' => (bool) $taxonomy->show_admin_column,
  197. 'show_in_nav_menus' => (bool) $taxonomy->show_in_nav_menus,
  198. 'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
  199. 'show_ui' => (bool) $taxonomy->show_ui,
  200. );
  201. }
  202. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  203. $data = $this->add_additional_fields_to_object( $data, $request );
  204. $data = $this->filter_response_by_context( $data, $context );
  205. // Wrap the data in a response object.
  206. $response = rest_ensure_response( $data );
  207. $response->add_links( array(
  208. 'collection' => array(
  209. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  210. ),
  211. 'https://api.w.org/items' => array(
  212. 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
  213. ),
  214. ) );
  215. /**
  216. * Filters a taxonomy returned from the REST API.
  217. *
  218. * Allows modification of the taxonomy data right before it is returned.
  219. *
  220. * @since 4.7.0
  221. *
  222. * @param WP_REST_Response $response The response object.
  223. * @param object $item The original taxonomy object.
  224. * @param WP_REST_Request $request Request used to generate the response.
  225. */
  226. return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
  227. }
  228. /**
  229. * Retrieves the taxonomy's schema, conforming to JSON Schema.
  230. *
  231. * @since 4.7.0
  232. *
  233. * @return array Item schema data.
  234. */
  235. public function get_item_schema() {
  236. $schema = array(
  237. '$schema' => 'http://json-schema.org/draft-04/schema#',
  238. 'title' => 'taxonomy',
  239. 'type' => 'object',
  240. 'properties' => array(
  241. 'capabilities' => array(
  242. 'description' => __( 'All capabilities used by the taxonomy.' ),
  243. 'type' => 'object',
  244. 'context' => array( 'edit' ),
  245. 'readonly' => true,
  246. ),
  247. 'description' => array(
  248. 'description' => __( 'A human-readable description of the taxonomy.' ),
  249. 'type' => 'string',
  250. 'context' => array( 'view', 'edit' ),
  251. 'readonly' => true,
  252. ),
  253. 'hierarchical' => array(
  254. 'description' => __( 'Whether or not the taxonomy should have children.' ),
  255. 'type' => 'boolean',
  256. 'context' => array( 'view', 'edit' ),
  257. 'readonly' => true,
  258. ),
  259. 'labels' => array(
  260. 'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
  261. 'type' => 'object',
  262. 'context' => array( 'edit' ),
  263. 'readonly' => true,
  264. ),
  265. 'name' => array(
  266. 'description' => __( 'The title for the taxonomy.' ),
  267. 'type' => 'string',
  268. 'context' => array( 'view', 'edit', 'embed' ),
  269. 'readonly' => true,
  270. ),
  271. 'slug' => array(
  272. 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  273. 'type' => 'string',
  274. 'context' => array( 'view', 'edit', 'embed' ),
  275. 'readonly' => true,
  276. ),
  277. 'show_cloud' => array(
  278. 'description' => __( 'Whether or not the term cloud should be displayed.' ),
  279. 'type' => 'boolean',
  280. 'context' => array( 'edit' ),
  281. 'readonly' => true,
  282. ),
  283. 'types' => array(
  284. 'description' => __( 'Types associated with the taxonomy.' ),
  285. 'type' => 'array',
  286. 'items' => array(
  287. 'type' => 'string',
  288. ),
  289. 'context' => array( 'view', 'edit' ),
  290. 'readonly' => true,
  291. ),
  292. 'rest_base' => array(
  293. 'description' => __( 'REST base route for the taxonomy.' ),
  294. 'type' => 'string',
  295. 'context' => array( 'view', 'edit', 'embed' ),
  296. 'readonly' => true,
  297. ),
  298. ),
  299. );
  300. return $this->add_additional_fields_schema( $schema );
  301. }
  302. /**
  303. * Retrieves the query params for collections.
  304. *
  305. * @since 4.7.0
  306. *
  307. * @return array Collection parameters.
  308. */
  309. public function get_collection_params() {
  310. $new_params = array();
  311. $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  312. $new_params['type'] = array(
  313. 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
  314. 'type' => 'string',
  315. );
  316. return $new_params;
  317. }
  318. }