WPCOM_JSON_API_Get_Taxonomies_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Get Taxonomies Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-get-taxonomies-endpoint.php  
  1. class WPCOM_JSON_API_Get_Taxonomies_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. // /sites/%s/tags -> $blog_id 
  3. // /sites/%s/categories -> $blog_id 
  4. function callback( $path = '', $blog_id = 0 ) { 
  5. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  6. if ( is_wp_error( $blog_id ) ) { 
  7. return $blog_id; 
  8.  
  9. $args = $this->query_args(); 
  10. $args = $this->process_args( $args ); 
  11.  
  12. if ( preg_match( '#/tags#i', $path ) ) { 
  13. return $this->tags( $args ); 
  14. } else { 
  15. return $this->categories( $args ); 
  16.  
  17. function process_args( $args ) { 
  18. if ( $args['number'] < 1 ) { 
  19. $args['number'] = 100; 
  20. } elseif ( 1000 < $args['number'] ) { 
  21. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 1000.', 400 ); 
  22.  
  23. if ( isset( $args['page'] ) ) { 
  24. if ( $args['page'] < 1 ) { 
  25. $args['page'] = 1; 
  26.  
  27. $args['offset'] = ( $args['page'] - 1 ) * $args['number']; 
  28. unset( $args['page'] ); 
  29.  
  30. if ( $args['offset'] < 0 ) { 
  31. $args['offset'] = 0; 
  32.  
  33. $args['orderby'] = $args['order_by']; 
  34. unset( $args['order_by'] ); 
  35.  
  36. unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] ); 
  37. return $args; 
  38.  
  39. function categories( $args ) { 
  40. $args['get'] = 'all'; 
  41.  
  42. $cats = get_categories( $args ); 
  43. unset( $args['offset'] ); 
  44. $found = wp_count_terms( 'category', $args ); 
  45.  
  46. $cats_obj = array(); 
  47. foreach ( $cats as $cat ) { 
  48. $cats_obj[] = $this->format_taxonomy( $cat, 'category', 'display' ); 
  49.  
  50. return array( 
  51. 'found' => (int) $found,  
  52. 'categories' => $cats_obj 
  53. ); 
  54.  
  55. function tags( $args ) { 
  56. $args['get'] = 'all'; 
  57.  
  58. $tags = (array) get_tags( $args ); 
  59. unset( $args['offset'] ); 
  60. $found = wp_count_terms( 'post_tag', $args ); 
  61.  
  62. $tags_obj = array(); 
  63. foreach ( $tags as $tag ) { 
  64. $tags_obj[] = $this->format_taxonomy( $tag, 'post_tag', 'display' ); 
  65.  
  66. return array( 
  67. 'found' => (int) $found,  
  68. 'tags' => $tags_obj 
  69. );