class.wpcom-json-api-list-terms-endpoint.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. new WPCOM_JSON_API_List_Terms_Endpoint( array(
  3. 'description' => 'Get a list of a site\'s terms by taxonomy.',
  4. 'group' => 'taxonomy',
  5. 'stat' => 'terms',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/taxonomies/%s/terms',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$taxonomy' => '(string) Taxonomy',
  11. ),
  12. 'query_parameters' => array(
  13. 'number' => '(int=100) The number of terms to return. Limit: 1000.',
  14. 'offset' => '(int=0) 0-indexed offset.',
  15. 'page' => '(int) Return the Nth 1-indexed page of terms. Takes precedence over the <code>offset</code> parameter.',
  16. 'search' => '(string) Limit response to include only terms whose names or slugs match the provided search query.',
  17. 'order' => array(
  18. 'ASC' => 'Return terms in ascending order.',
  19. 'DESC' => 'Return terms in descending order.',
  20. ),
  21. 'order_by' => array(
  22. 'name' => 'Order by the name of each tag.',
  23. 'count' => 'Order by the number of posts in each tag.',
  24. ),
  25. ),
  26. 'response_format' => array(
  27. 'found' => '(int) The number of terms returned.',
  28. 'terms' => '(array) Array of tag objects.',
  29. ),
  30. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tags/terms?number=5'
  31. ) );
  32. class WPCOM_JSON_API_List_Terms_Endpoint extends WPCOM_JSON_API_Endpoint {
  33. // /sites/%s/taxonomies/%s/terms -> $blog_id, $taxonomy
  34. function callback( $path = '', $blog_id = 0, $taxonomy = 'category' ) {
  35. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  36. if ( is_wp_error( $blog_id ) ) {
  37. return $blog_id;
  38. }
  39. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  40. $this->load_theme_functions();
  41. }
  42. $taxonomy_meta = get_taxonomy( $taxonomy );
  43. if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&
  44. ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
  45. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
  46. }
  47. $args = $this->query_args();
  48. $args = $this->process_args( $args );
  49. $formatted_terms = $this->get_formatted_terms( $taxonomy, $args );
  50. if ( ! empty( $formatted_terms ) ) {
  51. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  52. do_action( 'wpcom_json_api_objects', 'terms', count( $formatted_terms ) );
  53. }
  54. return array(
  55. 'found' => (int) $this->get_found( $taxonomy, $args ),
  56. 'terms' => (array) $formatted_terms
  57. );
  58. }
  59. function process_args( $args ) {
  60. $args['get'] = 'all';
  61. if ( $args['number'] < 1 ) {
  62. $args['number'] = 100;
  63. } elseif ( 1000 < $args['number'] ) {
  64. return new WP_Error( 'invalid_number', 'The number parameter must be less than or equal to 1000.', 400 );
  65. }
  66. if ( isset( $args['page'] ) ) {
  67. if ( $args['page'] < 1 ) {
  68. $args['page'] = 1;
  69. }
  70. $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
  71. unset( $args['page'] );
  72. }
  73. if ( $args['offset'] < 0 ) {
  74. $args['offset'] = 0;
  75. }
  76. $args['orderby'] = $args['order_by'];
  77. unset( $args['order_by'] );
  78. unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
  79. return $args;
  80. }
  81. function get_found( $taxonomy, $args ) {
  82. unset( $args['offset'] );
  83. return wp_count_terms( $taxonomy, $args );
  84. }
  85. function get_formatted_terms( $taxonomy, $args ) {
  86. $terms = get_terms( $taxonomy, $args );
  87. $formatted_terms = array();
  88. foreach ( $terms as $term ) {
  89. $formatted_terms[] = $this->format_taxonomy( $term, $taxonomy, 'display' );
  90. }
  91. return $formatted_terms;
  92. }
  93. }