Geen omschrijving

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

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