Ei kuvausta

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. 'allow_fallback_to_jetpack_blog_token' => true,
  27. 'response_format' => array(
  28. 'found' => '(int) The number of terms returned.',
  29. 'terms' => '(array) Array of tag objects.',
  30. ),
  31. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tags/terms?number=5'
  32. ) );
  33. class WPCOM_JSON_API_List_Terms_Endpoint extends WPCOM_JSON_API_Endpoint {
  34. // /sites/%s/taxonomies/%s/terms -> $blog_id, $taxonomy
  35. function callback( $path = '', $blog_id = 0, $taxonomy = 'category' ) {
  36. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  37. if ( is_wp_error( $blog_id ) ) {
  38. return $blog_id;
  39. }
  40. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  41. $this->load_theme_functions();
  42. }
  43. $taxonomy_meta = get_taxonomy( $taxonomy );
  44. if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&
  45. ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
  46. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
  47. }
  48. $args = $this->query_args();
  49. $args = $this->process_args( $args );
  50. $formatted_terms = $this->get_formatted_terms( $taxonomy, $args );
  51. if ( ! empty( $formatted_terms ) ) {
  52. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  53. do_action( 'wpcom_json_api_objects', 'terms', count( $formatted_terms ) );
  54. }
  55. return array(
  56. 'found' => (int) $this->get_found( $taxonomy, $args ),
  57. 'terms' => (array) $formatted_terms
  58. );
  59. }
  60. function process_args( $args ) {
  61. $args['get'] = 'all';
  62. if ( $args['number'] < 1 ) {
  63. $args['number'] = 100;
  64. } elseif ( 1000 < $args['number'] ) {
  65. return new WP_Error( 'invalid_number', 'The number parameter must be less than or equal to 1000.', 400 );
  66. }
  67. if ( isset( $args['page'] ) ) {
  68. if ( $args['page'] < 1 ) {
  69. $args['page'] = 1;
  70. }
  71. $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
  72. unset( $args['page'] );
  73. }
  74. if ( $args['offset'] < 0 ) {
  75. $args['offset'] = 0;
  76. }
  77. $args['orderby'] = $args['order_by'];
  78. unset( $args['order_by'] );
  79. unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
  80. return $args;
  81. }
  82. function get_found( $taxonomy, $args ) {
  83. unset( $args['offset'] );
  84. return wp_count_terms( $taxonomy, $args );
  85. }
  86. function get_formatted_terms( $taxonomy, $args ) {
  87. $terms = get_terms( $taxonomy, $args );
  88. $formatted_terms = array();
  89. foreach ( $terms as $term ) {
  90. $formatted_terms[] = $this->format_taxonomy( $term, $taxonomy, 'display' );
  91. }
  92. return $formatted_terms;
  93. }
  94. }