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

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