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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 taxonomies.
  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<taxonomy>[\w-]+)',
  50. array(
  51. 'args' => array(
  52. 'taxonomy' => array(
  53. 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  54. 'type' => 'string',
  55. ),
  56. ),
  57. array(
  58. 'methods' => WP_REST_Server::READABLE,
  59. 'callback' => array( $this, 'get_item' ),
  60. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  61. 'args' => array(
  62. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  63. ),
  64. ),
  65. 'schema' => array( $this, 'get_public_item_schema' ),
  66. )
  67. );
  68. }
  69. /**
  70. * Checks whether a given request has permission to read taxonomies.
  71. *
  72. * @since 4.7.0
  73. *
  74. * @param WP_REST_Request $request Full details about the request.
  75. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  76. */
  77. public function get_items_permissions_check( $request ) {
  78. if ( 'edit' === $request['context'] ) {
  79. if ( ! empty( $request['type'] ) ) {
  80. $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  81. } else {
  82. $taxonomies = get_taxonomies( '', 'objects' );
  83. }
  84. foreach ( $taxonomies as $taxonomy ) {
  85. if ( ! empty( $taxonomy->show_in_rest ) && current_user_can( $taxonomy->cap->assign_terms ) ) {
  86. return true;
  87. }
  88. }
  89. return new WP_Error(
  90. 'rest_cannot_view',
  91. __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ),
  92. array( 'status' => rest_authorization_required_code() )
  93. );
  94. }
  95. return true;
  96. }
  97. /**
  98. * Retrieves all public taxonomies.
  99. *
  100. * @since 4.7.0
  101. *
  102. * @param WP_REST_Request $request Full details about the request.
  103. * @return WP_REST_Response Response object on success, or WP_Error object on failure.
  104. */
  105. public function get_items( $request ) {
  106. // Retrieve the list of registered collection query parameters.
  107. $registered = $this->get_collection_params();
  108. if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
  109. $taxonomies = get_object_taxonomies( $request['type'], 'objects' );
  110. } else {
  111. $taxonomies = get_taxonomies( '', 'objects' );
  112. }
  113. $data = array();
  114. foreach ( $taxonomies as $tax_type => $value ) {
  115. if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
  116. continue;
  117. }
  118. $tax = $this->prepare_item_for_response( $value, $request );
  119. $tax = $this->prepare_response_for_collection( $tax );
  120. $data[ $tax_type ] = $tax;
  121. }
  122. if ( empty( $data ) ) {
  123. // Response should still be returned as a JSON object when it is empty.
  124. $data = (object) $data;
  125. }
  126. return rest_ensure_response( $data );
  127. }
  128. /**
  129. * Checks if a given request has access to a taxonomy.
  130. *
  131. * @since 4.7.0
  132. *
  133. * @param WP_REST_Request $request Full details about the request.
  134. * @return true|WP_Error True if the request has read access for the item, otherwise false or WP_Error object.
  135. */
  136. public function get_item_permissions_check( $request ) {
  137. $tax_obj = get_taxonomy( $request['taxonomy'] );
  138. if ( $tax_obj ) {
  139. if ( empty( $tax_obj->show_in_rest ) ) {
  140. return false;
  141. }
  142. if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->assign_terms ) ) {
  143. return new WP_Error(
  144. 'rest_forbidden_context',
  145. __( 'Sorry, you are not allowed to manage terms in this taxonomy.' ),
  146. array( 'status' => rest_authorization_required_code() )
  147. );
  148. }
  149. }
  150. return true;
  151. }
  152. /**
  153. * Retrieves a specific taxonomy.
  154. *
  155. * @since 4.7.0
  156. *
  157. * @param WP_REST_Request $request Full details about the request.
  158. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  159. */
  160. public function get_item( $request ) {
  161. $tax_obj = get_taxonomy( $request['taxonomy'] );
  162. if ( empty( $tax_obj ) ) {
  163. return new WP_Error(
  164. 'rest_taxonomy_invalid',
  165. __( 'Invalid taxonomy.' ),
  166. array( 'status' => 404 )
  167. );
  168. }
  169. $data = $this->prepare_item_for_response( $tax_obj, $request );
  170. return rest_ensure_response( $data );
  171. }
  172. /**
  173. * Prepares a taxonomy object for serialization.
  174. *
  175. * @since 4.7.0
  176. * @since 5.9.0 Renamed `$taxonomy` to `$item` to match parent class for PHP 8 named parameter support.
  177. *
  178. * @param WP_Taxonomy $item Taxonomy data.
  179. * @param WP_REST_Request $request Full details about the request.
  180. * @return WP_REST_Response Response object.
  181. */
  182. public function prepare_item_for_response( $item, $request ) {
  183. // Restores the more descriptive, specific name for use within this method.
  184. $taxonomy = $item;
  185. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  186. $fields = $this->get_fields_for_response( $request );
  187. $data = array();
  188. if ( in_array( 'name', $fields, true ) ) {
  189. $data['name'] = $taxonomy->label;
  190. }
  191. if ( in_array( 'slug', $fields, true ) ) {
  192. $data['slug'] = $taxonomy->name;
  193. }
  194. if ( in_array( 'capabilities', $fields, true ) ) {
  195. $data['capabilities'] = $taxonomy->cap;
  196. }
  197. if ( in_array( 'description', $fields, true ) ) {
  198. $data['description'] = $taxonomy->description;
  199. }
  200. if ( in_array( 'labels', $fields, true ) ) {
  201. $data['labels'] = $taxonomy->labels;
  202. }
  203. if ( in_array( 'types', $fields, true ) ) {
  204. $data['types'] = array_values( $taxonomy->object_type );
  205. }
  206. if ( in_array( 'show_cloud', $fields, true ) ) {
  207. $data['show_cloud'] = $taxonomy->show_tagcloud;
  208. }
  209. if ( in_array( 'hierarchical', $fields, true ) ) {
  210. $data['hierarchical'] = $taxonomy->hierarchical;
  211. }
  212. if ( in_array( 'rest_base', $fields, true ) ) {
  213. $data['rest_base'] = $base;
  214. }
  215. if ( in_array( 'rest_namespace', $fields, true ) ) {
  216. $data['rest_namespace'] = $taxonomy->rest_namespace;
  217. }
  218. if ( in_array( 'visibility', $fields, true ) ) {
  219. $data['visibility'] = array(
  220. 'public' => (bool) $taxonomy->public,
  221. 'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
  222. 'show_admin_column' => (bool) $taxonomy->show_admin_column,
  223. 'show_in_nav_menus' => (bool) $taxonomy->show_in_nav_menus,
  224. 'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
  225. 'show_ui' => (bool) $taxonomy->show_ui,
  226. );
  227. }
  228. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  229. $data = $this->add_additional_fields_to_object( $data, $request );
  230. $data = $this->filter_response_by_context( $data, $context );
  231. // Wrap the data in a response object.
  232. $response = rest_ensure_response( $data );
  233. if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
  234. $response->add_links( $this->prepare_links( $taxonomy ) );
  235. }
  236. /**
  237. * Filters a taxonomy returned from the REST API.
  238. *
  239. * Allows modification of the taxonomy data right before it is returned.
  240. *
  241. * @since 4.7.0
  242. *
  243. * @param WP_REST_Response $response The response object.
  244. * @param WP_Taxonomy $item The original taxonomy object.
  245. * @param WP_REST_Request $request Request used to generate the response.
  246. */
  247. return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
  248. }
  249. /**
  250. * Prepares links for the request.
  251. *
  252. * @since 6.1.0
  253. *
  254. * @param WP_Taxonomy $taxonomy The taxonomy.
  255. * @return array Links for the given taxonomy.
  256. */
  257. protected function prepare_links( $taxonomy ) {
  258. return array(
  259. 'collection' => array(
  260. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  261. ),
  262. 'https://api.w.org/items' => array(
  263. 'href' => rest_url( rest_get_route_for_taxonomy_items( $taxonomy->name ) ),
  264. ),
  265. );
  266. }
  267. /**
  268. * Retrieves the taxonomy's schema, conforming to JSON Schema.
  269. *
  270. * @since 4.7.0
  271. * @since 5.0.0 The `visibility` property was added.
  272. * @since 5.9.0 The `rest_namespace` property was added.
  273. *
  274. * @return array Item schema data.
  275. */
  276. public function get_item_schema() {
  277. if ( $this->schema ) {
  278. return $this->add_additional_fields_schema( $this->schema );
  279. }
  280. $schema = array(
  281. '$schema' => 'http://json-schema.org/draft-04/schema#',
  282. 'title' => 'taxonomy',
  283. 'type' => 'object',
  284. 'properties' => array(
  285. 'capabilities' => array(
  286. 'description' => __( 'All capabilities used by the taxonomy.' ),
  287. 'type' => 'object',
  288. 'context' => array( 'edit' ),
  289. 'readonly' => true,
  290. ),
  291. 'description' => array(
  292. 'description' => __( 'A human-readable description of the taxonomy.' ),
  293. 'type' => 'string',
  294. 'context' => array( 'view', 'edit' ),
  295. 'readonly' => true,
  296. ),
  297. 'hierarchical' => array(
  298. 'description' => __( 'Whether or not the taxonomy should have children.' ),
  299. 'type' => 'boolean',
  300. 'context' => array( 'view', 'edit' ),
  301. 'readonly' => true,
  302. ),
  303. 'labels' => array(
  304. 'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
  305. 'type' => 'object',
  306. 'context' => array( 'edit' ),
  307. 'readonly' => true,
  308. ),
  309. 'name' => array(
  310. 'description' => __( 'The title for the taxonomy.' ),
  311. 'type' => 'string',
  312. 'context' => array( 'view', 'edit', 'embed' ),
  313. 'readonly' => true,
  314. ),
  315. 'slug' => array(
  316. 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  317. 'type' => 'string',
  318. 'context' => array( 'view', 'edit', 'embed' ),
  319. 'readonly' => true,
  320. ),
  321. 'show_cloud' => array(
  322. 'description' => __( 'Whether or not the term cloud should be displayed.' ),
  323. 'type' => 'boolean',
  324. 'context' => array( 'edit' ),
  325. 'readonly' => true,
  326. ),
  327. 'types' => array(
  328. 'description' => __( 'Types associated with the taxonomy.' ),
  329. 'type' => 'array',
  330. 'items' => array(
  331. 'type' => 'string',
  332. ),
  333. 'context' => array( 'view', 'edit' ),
  334. 'readonly' => true,
  335. ),
  336. 'rest_base' => array(
  337. 'description' => __( 'REST base route for the taxonomy.' ),
  338. 'type' => 'string',
  339. 'context' => array( 'view', 'edit', 'embed' ),
  340. 'readonly' => true,
  341. ),
  342. 'rest_namespace' => array(
  343. 'description' => __( 'REST namespace route for the taxonomy.' ),
  344. 'type' => 'string',
  345. 'context' => array( 'view', 'edit', 'embed' ),
  346. 'readonly' => true,
  347. ),
  348. 'visibility' => array(
  349. 'description' => __( 'The visibility settings for the taxonomy.' ),
  350. 'type' => 'object',
  351. 'context' => array( 'edit' ),
  352. 'readonly' => true,
  353. 'properties' => array(
  354. 'public' => array(
  355. 'description' => __( 'Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.' ),
  356. 'type' => 'boolean',
  357. ),
  358. 'publicly_queryable' => array(
  359. 'description' => __( 'Whether the taxonomy is publicly queryable.' ),
  360. 'type' => 'boolean',
  361. ),
  362. 'show_ui' => array(
  363. 'description' => __( 'Whether to generate a default UI for managing this taxonomy.' ),
  364. 'type' => 'boolean',
  365. ),
  366. 'show_admin_column' => array(
  367. 'description' => __( 'Whether to allow automatic creation of taxonomy columns on associated post-types table.' ),
  368. 'type' => 'boolean',
  369. ),
  370. 'show_in_nav_menus' => array(
  371. 'description' => __( 'Whether to make the taxonomy available for selection in navigation menus.' ),
  372. 'type' => 'boolean',
  373. ),
  374. 'show_in_quick_edit' => array(
  375. 'description' => __( 'Whether to show the taxonomy in the quick/bulk edit panel.' ),
  376. 'type' => 'boolean',
  377. ),
  378. ),
  379. ),
  380. ),
  381. );
  382. $this->schema = $schema;
  383. return $this->add_additional_fields_schema( $this->schema );
  384. }
  385. /**
  386. * Retrieves the query params for collections.
  387. *
  388. * @since 4.7.0
  389. *
  390. * @return array Collection parameters.
  391. */
  392. public function get_collection_params() {
  393. $new_params = array();
  394. $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  395. $new_params['type'] = array(
  396. 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
  397. 'type' => 'string',
  398. );
  399. return $new_params;
  400. }
  401. }