WPCOM_JSON_API_List_Media_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Media Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-list-media-endpoint.php  
  1. class WPCOM_JSON_API_List_Media_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. function callback( $path = '', $blog_id = 0 ) { 
  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. //upload_files can probably be used for other endpoints but we want contributors to be able to use media too 
  9. if ( !current_user_can( 'edit_posts' ) ) { 
  10. return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); 
  11.  
  12. $args = $this->query_args(); 
  13.  
  14. if ( $args['number'] < 1 ) { 
  15. $args['number'] = 20; 
  16. } elseif ( 100 < $args['number'] ) { 
  17. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); 
  18.  
  19. $media = get_posts( array( 
  20. 'post_type' => 'attachment',  
  21. 'post_parent' => $args['parent_id'],  
  22. 'offset' => $args['offset'],  
  23. 'numberposts' => $args['number'],  
  24. 'post_mime_type' => $args['mime_type'] 
  25. ) ); 
  26.  
  27. $response = array(); 
  28. foreach ( $media as $item ) { 
  29. $response[] = $this->get_media_item( $item->ID ); 
  30.  
  31. $_num = (array) wp_count_attachments(); 
  32. $_total_media = array_sum( $_num ) - $_num['trash']; 
  33.  
  34. $return = array( 
  35. 'found' => $_total_media,  
  36. 'media' => $response 
  37. ); 
  38.  
  39. return $return; 
  40.