WPCOM_JSON_API_List_Terms_Endpoint

The WordPress Core WPCOM JSON API List Terms Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-list-terms-endpoint.php  
  1. class WPCOM_JSON_API_List_Terms_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. // /sites/%s/taxonomies/%s/terms -> $blog_id, $taxonomy 
  3. function callback( $path = '', $blog_id = 0, $taxonomy = 'category' ) { 
  4. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  5. if ( is_wp_error( $blog_id ) ) { 
  6. return $blog_id; 
  7.  
  8. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  9. $this->load_theme_functions(); 
  10.  
  11. $taxonomy_meta = get_taxonomy( $taxonomy ); 
  12. if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public &&  
  13. ! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) { 
  14. return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 ); 
  15.  
  16. $args = $this->query_args(); 
  17. $args = $this->process_args( $args ); 
  18.  
  19. $formatted_terms = $this->get_formatted_terms( $taxonomy, $args ); 
  20.  
  21. if ( ! empty( $formatted_terms ) ) { 
  22. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ 
  23. do_action( 'wpcom_json_api_objects', 'terms', count( $formatted_terms ) ); 
  24.  
  25. return array( 
  26. 'found' => (int) $this->get_found( $taxonomy, $args ),  
  27. 'terms' => (array) $formatted_terms 
  28. ); 
  29.  
  30. function process_args( $args ) { 
  31. $args['get'] = 'all'; 
  32.  
  33. if ( $args['number'] < 1 ) { 
  34. $args['number'] = 100; 
  35. } elseif ( 1000 < $args['number'] ) { 
  36. return new WP_Error( 'invalid_number', 'The number parameter must be less than or equal to 1000.', 400 ); 
  37.  
  38. if ( isset( $args['page'] ) ) { 
  39. if ( $args['page'] < 1 ) { 
  40. $args['page'] = 1; 
  41.  
  42. $args['offset'] = ( $args['page'] - 1 ) * $args['number']; 
  43. unset( $args['page'] ); 
  44.  
  45. if ( $args['offset'] < 0 ) { 
  46. $args['offset'] = 0; 
  47.  
  48. $args['orderby'] = $args['order_by']; 
  49. unset( $args['order_by'] ); 
  50.  
  51. unset( $args['context'], $args['pretty'], $args['http_envelope'], $args['fields'] ); 
  52. return $args; 
  53.  
  54. function get_found( $taxonomy, $args ) { 
  55. unset( $args['offset'] ); 
  56. return wp_count_terms( $taxonomy, $args ); 
  57.  
  58. function get_formatted_terms( $taxonomy, $args ) { 
  59. $terms = get_terms( $taxonomy, $args ); 
  60.  
  61. $formatted_terms = array(); 
  62. foreach ( $terms as $term ) { 
  63. $formatted_terms[] = $this->format_taxonomy( $term, $taxonomy, 'display' ); 
  64.  
  65. return $formatted_terms;