WPCOM_JSON_API_Upload_Media_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-upload-media-endpoint.php  
  1. class WPCOM_JSON_API_Upload_Media_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. function callback( $path = '', $blog_id = 0 ) { 
  3. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  4. if ( is_wp_error( $blog_id ) ) { 
  5. return $blog_id; 
  6.  
  7. if ( ! current_user_can( 'upload_files' ) ) { 
  8. return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 ); 
  9.  
  10. $input = $this->input( true ); 
  11.  
  12. $has_media = isset( $input['media'] ) && $input['media'] ? count( $input['media'] ) : false; 
  13. $has_media_urls = isset( $input['media_urls'] ) && $input['media_urls'] ? count( $input['media_urls'] ) : false; 
  14.  
  15. $media_ids = $files = $errors = array(); 
  16.  
  17. if ( $has_media ) { 
  18. $this->api->trap_wp_die( 'upload_error' ); 
  19. foreach ( $input['media'] as $index => $media_item ) { 
  20. $_FILES['.api.media.item.'] = $media_item; 
  21. // check for WP_Error if we ever actually need $media_id 
  22. $media_id = media_handle_upload( '.api.media.item.', 0 ); 
  23. if ( is_wp_error( $media_id ) ) { 
  24. if ( 1 === count( $input['media'] ) && ! $has_media_urls ) { 
  25. unset( $_FILES['.api.media.item.'] ); 
  26. return $media_id; 
  27. $errors[ $index ]['error'] = $media_id->get_error_code(); 
  28. $errors[ $index ]['message'] = $media_id->get_error_message(); 
  29. } else { 
  30. $media_ids[ $index ] = $media_id; 
  31. $files[] = $media_item; 
  32. $this->api->trap_wp_die( null ); 
  33.  
  34. unset( $_FILES['.api.media.item.'] ); 
  35.  
  36. if ( $has_media_urls ) { 
  37. foreach ( $input['media_urls'] as $url ) { 
  38. $id = $this->handle_media_sideload( $url ); 
  39. if ( ! empty( $id ) && is_int( $id ) ) 
  40. $media_ids[] = $id; 
  41.  
  42. $results = array(); 
  43. foreach ( $media_ids as $media_id ) { 
  44. $results[] = $this->get_media_item( $media_id ); 
  45.  
  46. return array( 'media' => $results, 'errors' => $errors );