PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 425 lines | 281 code | 49 blank | 95 comment | 28 complexity | d2435f8c6efd345d686301b7f933bb3f MD5 | raw file
  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(
  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. *
  177. * @param WP_Taxonomy $taxonomy Taxonomy data.
  178. * @param WP_REST_Request $request Full details about the request.
  179. * @return WP_REST_Response Response object.
  180. */
  181. public function prepare_item_for_response( $taxonomy, $request ) {
  182. $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  183. $fields = $this->get_fields_for_response( $request );
  184. $data = array();
  185. if ( in_array( 'name', $fields, true ) ) {
  186. $data['name'] = $taxonomy->label;
  187. }
  188. if ( in_array( 'slug', $fields, true ) ) {
  189. $data['slug'] = $taxonomy->name;
  190. }
  191. if ( in_array( 'capabilities', $fields, true ) ) {
  192. $data['capabilities'] = $taxonomy->cap;
  193. }
  194. if ( in_array( 'description', $fields, true ) ) {
  195. $data['description'] = $taxonomy->description;
  196. }
  197. if ( in_array( 'labels', $fields, true ) ) {
  198. $data['labels'] = $taxonomy->labels;
  199. }
  200. if ( in_array( 'types', $fields, true ) ) {
  201. $data['types'] = array_values( $taxonomy->object_type );
  202. }
  203. if ( in_array( 'show_cloud', $fields, true ) ) {
  204. $data['show_cloud'] = $taxonomy->show_tagcloud;
  205. }
  206. if ( in_array( 'hierarchical', $fields, true ) ) {
  207. $data['hierarchical'] = $taxonomy->hierarchical;
  208. }
  209. if ( in_array( 'rest_base', $fields, true ) ) {
  210. $data['rest_base'] = $base;
  211. }
  212. if ( in_array( 'visibility', $fields, true ) ) {
  213. $data['visibility'] = array(
  214. 'public' => (bool) $taxonomy->public,
  215. 'publicly_queryable' => (bool) $taxonomy->publicly_queryable,
  216. 'show_admin_column' => (bool) $taxonomy->show_admin_column,
  217. 'show_in_nav_menus' => (bool) $taxonomy->show_in_nav_menus,
  218. 'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,
  219. 'show_ui' => (bool) $taxonomy->show_ui,
  220. );
  221. }
  222. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  223. $data = $this->add_additional_fields_to_object( $data, $request );
  224. $data = $this->filter_response_by_context( $data, $context );
  225. // Wrap the data in a response object.
  226. $response = rest_ensure_response( $data );
  227. $response->add_links(
  228. array(
  229. 'collection' => array(
  230. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  231. ),
  232. 'https://api.w.org/items' => array(
  233. 'href' => rest_url( sprintf( 'wp/v2/%s', $base ) ),
  234. ),
  235. )
  236. );
  237. /**
  238. * Filters a taxonomy returned from the REST API.
  239. *
  240. * Allows modification of the taxonomy data right before it is returned.
  241. *
  242. * @since 4.7.0
  243. *
  244. * @param WP_REST_Response $response The response object.
  245. * @param WP_Taxonomy $item The original taxonomy object.
  246. * @param WP_REST_Request $request Request used to generate the response.
  247. */
  248. return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
  249. }
  250. /**
  251. * Retrieves the taxonomy's schema, conforming to JSON Schema.
  252. *
  253. * @since 4.7.0
  254. *
  255. * @return array Item schema data.
  256. */
  257. public function get_item_schema() {
  258. if ( $this->schema ) {
  259. return $this->add_additional_fields_schema( $this->schema );
  260. }
  261. $schema = array(
  262. '$schema' => 'http://json-schema.org/draft-04/schema#',
  263. 'title' => 'taxonomy',
  264. 'type' => 'object',
  265. 'properties' => array(
  266. 'capabilities' => array(
  267. 'description' => __( 'All capabilities used by the taxonomy.' ),
  268. 'type' => 'object',
  269. 'context' => array( 'edit' ),
  270. 'readonly' => true,
  271. ),
  272. 'description' => array(
  273. 'description' => __( 'A human-readable description of the taxonomy.' ),
  274. 'type' => 'string',
  275. 'context' => array( 'view', 'edit' ),
  276. 'readonly' => true,
  277. ),
  278. 'hierarchical' => array(
  279. 'description' => __( 'Whether or not the taxonomy should have children.' ),
  280. 'type' => 'boolean',
  281. 'context' => array( 'view', 'edit' ),
  282. 'readonly' => true,
  283. ),
  284. 'labels' => array(
  285. 'description' => __( 'Human-readable labels for the taxonomy for various contexts.' ),
  286. 'type' => 'object',
  287. 'context' => array( 'edit' ),
  288. 'readonly' => true,
  289. ),
  290. 'name' => array(
  291. 'description' => __( 'The title for the taxonomy.' ),
  292. 'type' => 'string',
  293. 'context' => array( 'view', 'edit', 'embed' ),
  294. 'readonly' => true,
  295. ),
  296. 'slug' => array(
  297. 'description' => __( 'An alphanumeric identifier for the taxonomy.' ),
  298. 'type' => 'string',
  299. 'context' => array( 'view', 'edit', 'embed' ),
  300. 'readonly' => true,
  301. ),
  302. 'show_cloud' => array(
  303. 'description' => __( 'Whether or not the term cloud should be displayed.' ),
  304. 'type' => 'boolean',
  305. 'context' => array( 'edit' ),
  306. 'readonly' => true,
  307. ),
  308. 'types' => array(
  309. 'description' => __( 'Types associated with the taxonomy.' ),
  310. 'type' => 'array',
  311. 'items' => array(
  312. 'type' => 'string',
  313. ),
  314. 'context' => array( 'view', 'edit' ),
  315. 'readonly' => true,
  316. ),
  317. 'rest_base' => array(
  318. 'description' => __( 'REST base route for the taxonomy.' ),
  319. 'type' => 'string',
  320. 'context' => array( 'view', 'edit', 'embed' ),
  321. 'readonly' => true,
  322. ),
  323. 'visibility' => array(
  324. 'description' => __( 'The visibility settings for the taxonomy.' ),
  325. 'type' => 'object',
  326. 'context' => array( 'edit' ),
  327. 'readonly' => true,
  328. 'properties' => array(
  329. 'public' => array(
  330. 'description' => __( 'Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.' ),
  331. 'type' => 'boolean',
  332. ),
  333. 'publicly_queryable' => array(
  334. 'description' => __( 'Whether the taxonomy is publicly queryable.' ),
  335. 'type' => 'boolean',
  336. ),
  337. 'show_ui' => array(
  338. 'description' => __( 'Whether to generate a default UI for managing this taxonomy.' ),
  339. 'type' => 'boolean',
  340. ),
  341. 'show_admin_column' => array(
  342. 'description' => __( 'Whether to allow automatic creation of taxonomy columns on associated post-types table.' ),
  343. 'type' => 'boolean',
  344. ),
  345. 'show_in_nav_menus' => array(
  346. 'description' => __( 'Whether to make the taxonomy available for selection in navigation menus.' ),
  347. 'type' => 'boolean',
  348. ),
  349. 'show_in_quick_edit' => array(
  350. 'description' => __( 'Whether to show the taxonomy in the quick/bulk edit panel.' ),
  351. 'type' => 'boolean',
  352. ),
  353. ),
  354. ),
  355. ),
  356. );
  357. $this->schema = $schema;
  358. return $this->add_additional_fields_schema( $this->schema );
  359. }
  360. /**
  361. * Retrieves the query params for collections.
  362. *
  363. * @since 4.7.0
  364. *
  365. * @return array Collection parameters.
  366. */
  367. public function get_collection_params() {
  368. $new_params = array();
  369. $new_params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  370. $new_params['type'] = array(
  371. 'description' => __( 'Limit results to taxonomies associated with a specific post type.' ),
  372. 'type' => 'string',
  373. );
  374. return $new_params;
  375. }
  376. }