WPCOM_JSON_API_Edit_Media_v1_2_Endpoint

The WordPress Core WPCOM JSON API Edit Media v1 2 Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-edit-media-v1-2-endpoint.php  
  1. class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Media_v1_1_Endpoint { 
  2. /** 
  3. * Update the media post grabbing the post values from 
  4. * the `attrs` parameter 
  5. * @param {Number} $media_id - post media ID 
  6. * @param {Object} $attrs - `attrs` parameter sent from the client in the request body 
  7. * @return bool|WP_Error `WP_Error` on failure. `true` on success. 
  8. */ 
  9. private function update_by_attrs_parameter( $media_id, $attrs ) { 
  10. $insert = array(); 
  11.  
  12. // Attributes: Title, Caption, Description 
  13. if ( isset( $attrs['title'] ) ) { 
  14. $insert['post_title'] = $attrs['title']; 
  15.  
  16. if ( isset( $attrs['caption'] ) ) { 
  17. $insert['post_excerpt'] = $attrs['caption']; 
  18.  
  19. if ( isset( $attrs['description'] ) ) { 
  20. $insert['post_content'] = $attrs['description']; 
  21.  
  22. if ( ! empty( $insert ) ) { 
  23. $insert['ID'] = $media_id; 
  24. $update_action = wp_update_post( (object) $insert ); 
  25. if ( is_wp_error( $update_action ) ) { 
  26. $update_action; 
  27.  
  28. // Attributes: Alt 
  29. if ( isset( $attrs['alt'] ) ) { 
  30. $alt = wp_strip_all_tags( $attrs['alt'], true ); 
  31. update_post_meta( $media_id, Jetpack_Media::$WP_ATTACHMENT_IMAGE_ALT, $alt ); 
  32.  
  33. // Attributes: Artist, Album 
  34. $id3_meta = array(); 
  35.  
  36. foreach ( array( 'artist', 'album' ) as $key ) { 
  37. if ( isset( $attrs[ $key ] ) ) { 
  38. $id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true ); 
  39.  
  40. if ( ! empty( $id3_meta ) ) { 
  41. // Before updating metadata, ensure that the item is audio 
  42. $item = $this->get_media_item_v1_1( $media_id ); 
  43. if ( 0 === strpos( $item->mime_type, 'audio/' ) ) { 
  44. wp_update_attachment_metadata( $media_id, $id3_meta ); 
  45.  
  46. return $update_action; 
  47.  
  48. /** 
  49. * Get the image from a remote url and then save it locally. 
  50. * @param {Number} $media_id - media post ID 
  51. * @param {String} $url - image URL to save locally 
  52. * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong. 
  53. */ 
  54. private function build_file_array_from_url( $media_id, $url ) { 
  55. if ( ! $url ) { 
  56. return null; 
  57.  
  58. // if we didn't get a URL, let's bail 
  59. $parsed = @parse_url( $url ); 
  60. if ( empty( $parsed ) ) { 
  61. return new WP_Error( 'invalid_url', 'No media provided in url.' ); 
  62.  
  63. // save the remote image into a tmp file 
  64. $tmp = download_url( $url ); 
  65. if ( is_wp_error( $tmp ) ) { 
  66. return $tmp; 
  67.  
  68. return array( 
  69. 'name' => basename( $url ),  
  70. 'tmp_name' => $tmp 
  71. ); 
  72.  
  73. function callback( $path = '', $blog_id = 0, $media_id = 0 ) { 
  74. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  75. if ( is_wp_error( $blog_id ) ) { 
  76. return $blog_id; 
  77.  
  78. $media_item = get_post( $media_id ); 
  79.  
  80. if ( ! $media_item ) { 
  81. return new WP_Error( 'unknown_media', 'Unknown Media', 404 ); 
  82.  
  83. if ( is_wp_error( $media_item ) ) { 
  84. return $media_item; 
  85.  
  86. if ( ! current_user_can( 'upload_files', $media_id ) ) { 
  87. return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); 
  88.  
  89. $input = $this->input( true ); 
  90.  
  91. // images 
  92. $media_url = $input['media_url']; 
  93. $media_attrs = $input['attrs'] ? (array) $input['attrs'] : null; 
  94.  
  95. if ( isset( $media_url ) ) { 
  96. $user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token(); 
  97.  
  98. if ( ! $user_can_upload_files ) { 
  99. return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 ); 
  100.  
  101. // save the temporal file locally  
  102. $temporal_file = $this->build_file_array_from_url( $media_id, $media_url ); 
  103.  
  104. $edited_media_item = Jetpack_Media::edit_media_file( $media_id, $temporal_file ); 
  105.  
  106. if ( is_wp_error( $edited_media_item ) ) { 
  107. return $edited_media_item; 
  108.  
  109. unset( $input['media'] ); 
  110. unset( $input['media_url'] ); 
  111. unset( $input['attrs'] ); 
  112.  
  113. // update media through of `attrs` value it it's defined 
  114. if ( $media_attrs ) { 
  115. $updated_by_attrs = $this->update_by_attrs_parameter( $media_id, $media_attrs ); 
  116.  
  117. if ( is_wp_error( $updated_by_attrs ) ) { 
  118. return $updated_by_attrs; 
  119.  
  120. // call parent method 
  121. $response = parent::callback( $path, $blog_id, $media_id ); 
  122.  
  123. // expose `revision_history` object 
  124. $response->revision_history = (object) array( 
  125. 'items' => (array) Jetpack_Media::get_revision_history( $media_id ),  
  126. 'original' => (object) Jetpack_Media::get_original_media( $media_id ) 
  127. ); 
  128.  
  129. return $response;