WPCOM_JSON_API_Upload_Media_v1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Upload Media v1 1 Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_Upload_Media_v1_1_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. if ( ! current_user_can( 'upload_files' ) ) { 
  9. return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 ); 
  10.  
  11. $input = $this->input( true ); 
  12.  
  13. $media_files = ! empty( $input['media'] ) ? $input['media'] : array(); 
  14. $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array(); 
  15. $media_attrs = ! empty( $input['attrs'] ) ? $input['attrs'] : array(); 
  16. if ( empty( $media_files ) && empty( $media_urls ) ) { 
  17. return new WP_Error( 'invalid_input', 'No media provided in input.' ); 
  18.  
  19. $create_media = $this->handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs ); 
  20. $media_ids = $create_media['media_ids']; 
  21. $errors = $create_media['errors']; 
  22.  
  23. $results = array(); 
  24. if ( count( $media_ids ) <= 0 ) { 
  25. $this->api->output_early( 400, array( 'errors' => $errors ) ); 
  26. } else { 
  27. foreach ( $media_ids as $media_id ) { 
  28. $result = $this->get_media_item_v1_1( $media_id ); 
  29. if ( is_wp_error( $result ) ) { 
  30. $errors[] = array( 'file' => $media_id, 'error' => $result->get_error_code(), 'message' => $result->get_error_message() ); 
  31. } else { 
  32. $results[] = $result; 
  33. if ( count( $errors ) > 0 ) { 
  34. return array( 'media' => $results, 'errors' => $errors ); 
  35. } else { 
  36. return array( 'media' => $results ); 
  37.