No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

class-wp-rest-taxonomies-controller.php 9.9 KiB

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