WPCOM_JSON_API_Update_Media_v1_1_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-update-media-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_Update_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2. function callback( $path = '', $blog_id = 0, $media_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', $media_id ) ) { 
  8. return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); 
  9.  
  10. $item = $this->get_media_item_v1_1( $media_id ); 
  11.  
  12. if ( is_wp_error( $item ) ) { 
  13. return new WP_Error( 'unknown_media', 'Unknown Media', 404 ); 
  14.  
  15. $input = $this->input( true ); 
  16. $insert = array(); 
  17.  
  18. if ( isset( $input['title'] ) ) { 
  19. $insert['post_title'] = $input['title']; 
  20.  
  21. if ( isset( $input['caption'] ) ) { 
  22. $insert['post_excerpt'] = $input['caption']; 
  23.  
  24. if ( isset( $input['description'] ) ) { 
  25. $insert['post_content'] = $input['description']; 
  26.  
  27. if ( isset( $input['parent_id'] ) ) { 
  28. $insert['post_parent'] = $input['parent_id']; 
  29.  
  30. if ( isset( $input['alt'] ) ) { 
  31. $alt = wp_strip_all_tags( $input['alt'], true ); 
  32. update_post_meta( $media_id, '_wp_attachment_image_alt', $alt ); 
  33.  
  34. // audio only artist/album info 
  35. if ( 0 === strpos( $item->mime_type, 'audio/' ) ) { 
  36. $changed = false; 
  37. $id3data = wp_get_attachment_metadata( $media_id ); 
  38.  
  39. if ( ! is_array( $id3data ) ) { 
  40. $changed = true; 
  41. $id3data = array(); 
  42.  
  43. $id3_keys = array( 
  44. 'artist' => __( 'Artist', 'jetpack' ),  
  45. 'album' => __( 'Album', 'jetpack' ) 
  46. ); 
  47.  
  48. foreach ( $id3_keys as $key => $label ) { 
  49. if ( isset( $input[ $key ] ) ) { 
  50. $changed = true; 
  51. $id3data[ $key ] = wp_strip_all_tags( $input[ $key ], true ); 
  52.  
  53. if ( $changed ) { 
  54. wp_update_attachment_metadata( $media_id, $id3data ); 
  55.  
  56. $insert['ID'] = $media_id; 
  57. wp_update_post( (object) $insert ); 
  58.  
  59. $item = $this->get_media_item_v1_1( $media_id ); 
  60. return $item;