class.wpcom-json-api-get-taxonomies-endpoint.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. new WPCOM_JSON_API_Get_Taxonomies_Endpoint( array(
  3. 'description' => "Get a list of a site's categories.",
  4. 'group' => 'taxonomy',
  5. 'stat' => 'categories',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/categories',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain'
  10. ),
  11. 'query_parameters' => array(
  12. 'number' => '(int=100) The number of categories to return. Limit: 1000.',
  13. 'offset' => '(int=0) 0-indexed offset.',
  14. 'page' => '(int) Return the Nth 1-indexed page of categories. Takes precedence over the <code>offset</code> parameter.',
  15. 'search' => '(string) Limit response to include only categories whose names or slugs match the provided search query.',
  16. 'order' => array(
  17. 'ASC' => 'Return categories in ascending order.',
  18. 'DESC' => 'Return categories in descending order.',
  19. ),
  20. 'order_by' => array(
  21. 'name' => 'Order by the name of each category.',
  22. 'count' => 'Order by the number of posts in each category.',
  23. ),
  24. ),
  25. 'response_format' => array(
  26. 'found' => '(int) The number of categories returned.',
  27. 'categories' => '(array) Array of category objects.',
  28. ),
  29. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/categories/?number=5'
  30. ) );
  31. new WPCOM_JSON_API_Get_Taxonomies_Endpoint( array(
  32. 'description' => "Get a list of a site's tags.",
  33. 'group' => 'taxonomy',
  34. 'stat' => 'tags',
  35. 'method' => 'GET',
  36. 'path' => '/sites/%s/tags',
  37. 'path_labels' => array(
  38. '$site' => '(int|string) Site ID or domain'
  39. ),
  40. 'query_parameters' => array(
  41. 'number' => '(int=100) The number of tags to return. Limit: 1000.',
  42. 'offset' => '(int=0) 0-indexed offset.',
  43. 'page' => '(int) Return the Nth 1-indexed page of tags. Takes precedence over the <code>offset</code> parameter.',
  44. 'search' => '(string) Limit response to include only tags whose names or slugs match the provided search query.',
  45. 'order' => array(
  46. 'ASC' => 'Return tags in ascending order.',
  47. 'DESC' => 'Return tags in descending order.',
  48. ),
  49. 'order_by' => array(
  50. 'name' => 'Order by the name of each tag.',
  51. 'count' => 'Order by the number of posts in each tag.',
  52. ),
  53. ),
  54. 'response_format' => array(
  55. 'found' => '(int) The number of tags returned.',
  56. 'tags' => '(array) Array of tag objects.',
  57. ),
  58. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/tags/?number=5'
  59. ) );
  60. class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint {
  61. // /sites/%s/tags -> $blog_id
  62. // /sites/%s/categories -> $blog_id
  63. function callback( $path = '', $blog_id = 0 ) {
  64. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  65. if ( is_wp_error( $blog_id ) ) {
  66. return $blog_id;
  67. }
  68. $args = $this->query_args();
  69. $args = $this->process_args( $args );
  70. if ( preg_match( '#/tags#i', $path ) ) {
  71. return $this->tags( $args );
  72. } else {
  73. return $this->categories( $args );
  74. }
  75. }
  76. function process_args( $args ) {
  77. if ( $args['number'] < 1 ) {
  78. $args['number'] = 100;
  79. } elseif ( 1000 < $args['number'] ) {
  80. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 );
  81. }
  82. if ( isset( $args['page'] ) ) {
  83. if ( $args['page'] < 1 ) {
  84. $args['page'] = 1;
  85. }
  86. $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
  87. unset( $args['page'] );
  88. }
  89. if ( $args['offset'] < 0 ) {
  90. $args['offset'] = 0;
  91. }
  92. $args['orderby'] = $args['order_by'];
  93. unset( $args['order_by'] );
  94. unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] );
  95. return $args;
  96. }
  97. function categories( $args ) {
  98. $args['get'] = 'all';
  99. $cats = get_categories( $args );
  100. unset( $args['offset'] );
  101. $found = wp_count_terms( 'category', $args );
  102. $cats_obj = array();
  103. foreach ( $cats as $cat ) {
  104. $cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' );
  105. }
  106. return array(
  107. 'found' => (int) $found,
  108. 'categories' => $cats_obj
  109. );
  110. }
  111. function tags( $args ) {
  112. $args['get'] = 'all';
  113. $tags = (array) get_tags( $args );
  114. unset( $args['offset'] );
  115. $found = wp_count_terms( 'post_tag', $args );
  116. $tags_obj = array();
  117. foreach ( $tags as $tag ) {
  118. $tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' );
  119. }
  120. return array(
  121. 'found' => (int) $found,
  122. 'tags' => $tags_obj
  123. );
  124. }
  125. }