class.wpcom-json-api-edit-media-v1-2-endpoint.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. jetpack_require_lib( 'class.media' );
  3. define( 'REVISION_HISTORY_MAXIMUM_AMOUNT', 0 );
  4. define( 'WP_ATTACHMENT_IMAGE_ALT', '_wp_attachment_image_alt' );
  5. new WPCOM_JSON_API_Edit_Media_v1_2_Endpoint( array(
  6. 'description' => 'Edit a media item.',
  7. 'group' => 'media',
  8. 'stat' => 'media:1:POST',
  9. 'min_version' => '1',
  10. 'max_version' => '1.2',
  11. 'method' => 'POST',
  12. 'path' => '/sites/%s/media/%d/edit',
  13. 'path_labels' => array(
  14. '$site' => '(int|string) Site ID or domain',
  15. '$media_ID' => '(int) The ID of the media item',
  16. ),
  17. 'request_format' => array(
  18. 'parent_id' => '(int) ID of the post this media is attached to',
  19. 'title' => '(string) The file name.',
  20. 'caption' => '(string) File caption.',
  21. 'description' => '(HTML) Description of the file.',
  22. 'alt' => "(string) Alternative text for image files.",
  23. 'artist' => "(string) Audio Only. Artist metadata for the audio track.",
  24. 'album' => "(string) Audio Only. Album metadata for the audio track.",
  25. 'media' => "(object) An object file to attach to the post. To upload media, " .
  26. "the entire request should be multipart/form-data encoded. " .
  27. "Multiple media items will be displayed in a gallery. Accepts " .
  28. "jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. " .
  29. "Audio and Video may also be available. See <code>allowed_file_types</code> " .
  30. "in the options response of the site endpoint. " .
  31. "<br /><br /><strong>Example</strong>:<br />" .
  32. "<code>curl \<br />--form 'title=Image' \<br />--form 'media=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
  33. 'attrs' => "(object) An Object of attributes (`title`, `description` and `caption`) " .
  34. "are supported to assign to the media uploaded via the `media` or `media_url`",
  35. 'media_url' => "(string) An URL of the image to attach to a post.",
  36. ),
  37. 'response_format' => array(
  38. 'ID' => '(int) The ID of the media item',
  39. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  40. 'post_ID' => '(int) ID of the post this media is attached to',
  41. 'author_ID' => '(int) ID of the user who uploaded the media',
  42. 'URL' => '(string) URL to the file',
  43. 'guid' => '(string) Unique identifier',
  44. 'file' => '(string) File name',
  45. 'extension' => '(string) File extension',
  46. 'mime_type' => '(string) File mime type',
  47. 'title' => '(string) File name',
  48. 'caption' => '(string) User provided caption of the file',
  49. 'description' => '(string) Description of the file',
  50. 'alt' => '(string) Alternative text for image files.',
  51. 'thumbnails' => '(object) Media item thumbnail URL options',
  52. 'height' => '(int) (Image & video only) Height of the media item',
  53. 'width' => '(int) (Image & video only) Width of the media item',
  54. 'length' => '(int) (Video & audio only) Duration of the media item, in seconds',
  55. 'exif' => '(array) (Image & audio only) Exif (meta) information about the media item',
  56. 'videopress_guid' => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
  57. 'videopress_processing_done' => '(bool) (Video only) If the video is uploaded on a blog with VideoPress, this will return the status of processing on the video.',
  58. 'revision_history' => '(object) An object with `items` and `original` keys. ' .
  59. '`original` is an object with data about the original image. ' .
  60. '`items` is an array of snapshots of the previous images of this Media. ' .
  61. 'Each item has the `URL`, `file, `extension`, `date`, and `mime_type` fields.'
  62. ),
  63. 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/82974409/media/446',
  64. 'example_request_data' => array(
  65. 'headers' => array(
  66. 'authorization' => 'Bearer YOUR_API_TOKEN'
  67. ),
  68. 'body' => array(
  69. 'title' => 'Updated Title'
  70. )
  71. )
  72. ) );
  73. class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Media_v1_1_Endpoint {
  74. /**
  75. * Return an array of mime_type items allowed when the media file is uploaded.
  76. *
  77. * @return {Array} mime_type array
  78. */
  79. static function get_allowed_mime_types( $default_mime_types ) {
  80. return array_unique( array_merge( $default_mime_types, array(
  81. 'application/msword', // .doc
  82. 'application/vnd.ms-powerpoint', // .ppt, .pps
  83. 'application/vnd.ms-excel', // .xls
  84. 'application/vnd.openxmlformats-officedocument.presentationml.presentation', // .pptx
  85. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', // .ppsx
  86. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // .xlsx
  87. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // .docx
  88. 'application/vnd.oasis.opendocument.text', // .odt
  89. 'application/pdf', // .pdf
  90. ) ) );
  91. }
  92. /**
  93. * Update the media post grabbing the post values from
  94. * the `attrs` parameter
  95. *
  96. * @param {Number} $media_id - post media ID
  97. * @param {Object} $attrs - `attrs` parameter sent from the client in the request body
  98. * @return
  99. */
  100. private function update_by_attrs_parameter( $media_id, $attrs ) {
  101. $insert = array();
  102. // Attributes: Title, Caption, Description
  103. if ( isset( $attrs['title'] ) ) {
  104. $insert['post_title'] = $attrs['title'];
  105. }
  106. if ( isset( $attrs['caption'] ) ) {
  107. $insert['post_excerpt'] = $attrs['caption'];
  108. }
  109. if ( isset( $attrs['description'] ) ) {
  110. $insert['post_content'] = $attrs['description'];
  111. }
  112. if ( ! empty( $insert ) ) {
  113. $insert['ID'] = $media_id;
  114. $update_action = wp_update_post( (object) $insert );
  115. if ( is_wp_error( $update_action ) ) {
  116. return $update_action;
  117. }
  118. }
  119. // Attributes: Alt
  120. if ( isset( $attrs['alt'] ) ) {
  121. $alt = wp_strip_all_tags( $attrs['alt'], true );
  122. $post_update_action = update_post_meta( $media_id, WP_ATTACHMENT_IMAGE_ALT, $alt );
  123. if ( is_wp_error( $post_update_action ) ) {
  124. return $post_update_action;
  125. }
  126. }
  127. // Attributes: Artist, Album
  128. $id3_meta = array();
  129. foreach ( array( 'artist', 'album' ) as $key ) {
  130. if ( isset( $attrs[ $key ] ) ) {
  131. $id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
  132. }
  133. }
  134. if ( ! empty( $id3_meta ) ) {
  135. // Before updating metadata, ensure that the item is audio
  136. $item = $this->get_media_item_v1_1( $media_id );
  137. if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
  138. $update_action = wp_update_attachment_metadata( $media_id, $id3_meta );
  139. if ( is_wp_error( $update_action ) ) {
  140. return $update_action;
  141. }
  142. }
  143. }
  144. return $post_update_action;
  145. }
  146. /**
  147. * Return an object to be used to store into the revision_history
  148. *
  149. * @param {Object} $media_item - media post object
  150. * @return {Object} the snapshot object
  151. */
  152. private function get_snapshot( $media_item ) {
  153. $current_file = get_attached_file( $media_item->ID );
  154. $file_paths = pathinfo( $current_file );
  155. $snapshot = array(
  156. 'date' => (string) $this->format_date( $media_item->post_modified_gmt, $media_item->post_modified ),
  157. 'URL' => (string) wp_get_attachment_url( $media_item->ID ),
  158. 'file' => (string) $file_paths['basename'],
  159. 'extension' => (string) $file_paths['extension'],
  160. 'mime_type' => (string) $media_item->post_mime_type,
  161. 'size' => (int) filesize( $current_file )
  162. );
  163. return (object) $snapshot;
  164. }
  165. /**
  166. * Try to remove the temporal file from the given file array.
  167. *
  168. * @param {Array} $file_array - Array with data about the temporal file
  169. * @return {Boolean} `true` if the file has been removed.
  170. * `false` either the file doesn't exist or it couldn't be removed.
  171. */
  172. private function remove_tmp_file( $file_array ) {
  173. if ( ! file_exists ( $file_array['tmp_name'] ) ) {
  174. return false;
  175. }
  176. return @unlink( $file_array['tmp_name'] );
  177. }
  178. /**
  179. * Save the given temporal file in a local folder.
  180. *
  181. * @param {Array} $file_array
  182. * @param {Number} $media_id
  183. * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
  184. */
  185. private function save_temporary_file( $file_array, $media_id ) {
  186. $tmp_filename = $file_array['tmp_name'];
  187. if ( ! file_exists( $tmp_filename ) ) {
  188. return new WP_Error( 'invalid_input', 'No media provided in input.' );
  189. }
  190. // add additional mime_types through of the `jetpack_supported_media_sideload_types` filter
  191. $mime_type_static_filter = array(
  192. 'WPCOM_JSON_API_Edit_Media_v1_2_Endpoint',
  193. 'get_allowed_mime_types'
  194. );
  195. add_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter );
  196. if (
  197. ! $this->is_file_supported_for_sideloading( $tmp_filename ) &&
  198. ! file_is_displayable_image( $tmp_filename )
  199. ) {
  200. @unlink( $tmp_filename );
  201. return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
  202. }
  203. remove_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter );
  204. // generate a new file name
  205. $tmp_new_filename = Jetpack_Media::generate_new_filename( $media_id, $file_array[ 'name' ] );
  206. // start to create the parameters to move the temporal file
  207. $overrides = array( 'test_form' => false );
  208. $time = $this->get_time_string_from_guid( $media_id );
  209. $file_array['name'] = $tmp_new_filename;
  210. $file = wp_handle_sideload( $file_array, $overrides, $time );
  211. $this->remove_tmp_file( $file_array );
  212. if ( isset( $file['error'] ) ) {
  213. return new WP_Error( 'upload_error', $file['error'] );
  214. }
  215. return $file;
  216. }
  217. /**
  218. * File urls use the post date to generate a folder path.
  219. * Post dates can change, so we use the original date used in the guid
  220. * url so edits can remain in the same folder. In the following function
  221. * we capture a string in the format of `YYYY/MM` from the guid.
  222. *
  223. * For example with a guid of
  224. * "http://test.files.wordpress.com/2016/10/test.png" the resulting string
  225. * would be: "2016/10"
  226. *
  227. * @param $media_id
  228. *
  229. * @return string
  230. */
  231. private function get_time_string_from_guid( $media_id ) {
  232. $time = date( "Y/m", strtotime( current_time( 'mysql' ) ) );
  233. if ( $media = get_post( $media_id ) ) {
  234. $pattern = '/\/(\d{4}\/\d{2})\//';
  235. preg_match( $pattern, $media->guid, $matches );
  236. if ( count( $matches ) > 1 ) {
  237. $time = $matches[1];
  238. }
  239. }
  240. return $time;
  241. }
  242. /**
  243. * Get the image from a remote url and then save it locally.
  244. *
  245. * @param {Number} $media_id - media post ID
  246. * @param {String} $url - image URL to save locally
  247. * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
  248. */
  249. private function build_file_array_from_url( $media_id, $url ) {
  250. if ( ! $url ) {
  251. return null;
  252. }
  253. // if we didn't get a URL, let's bail
  254. $parsed = @parse_url( $url );
  255. if ( empty( $parsed ) ) {
  256. return new WP_Error( 'invalid_url', 'No media provided in url.' );
  257. }
  258. // save the remote image into a tmp file
  259. $tmp = download_url( wpcom_get_private_file( $url ) );
  260. if ( is_wp_error( $tmp ) ) {
  261. return $tmp;
  262. }
  263. return array(
  264. 'name' => basename( $url ),
  265. 'tmp_name' => $tmp
  266. );
  267. }
  268. /**
  269. * Add a new item into revision_history array.
  270. *
  271. * @param {Object} $media_item - media post
  272. * @param {file} $file - file recentrly added
  273. * @param {Boolean} $has_original_media - condition is the original media has been already added
  274. * @return {Boolean} `true` if the item has been added. Otherwise `false`.
  275. */
  276. private function register_revision( $media_item, $file, $has_original_media ) {
  277. if (
  278. is_wp_error( $file ) ||
  279. ! $has_original_media
  280. ) {
  281. return false;
  282. }
  283. add_post_meta( $media_item->ID, Jetpack_Media::$WP_REVISION_HISTORY, $this->get_snapshot( $media_item ) );
  284. }
  285. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  286. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  287. if ( is_wp_error( $blog_id ) ) {
  288. return $blog_id;
  289. }
  290. $media_item = get_post( $media_id );
  291. if ( ! $media_item || is_wp_error( $media_item ) ) {
  292. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  293. }
  294. if ( is_wp_error( $media_item ) ) {
  295. return $media_item;
  296. }
  297. if ( ! current_user_can( 'upload_files', $media_id ) ) {
  298. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  299. }
  300. $input = $this->input( true );
  301. // images
  302. $media_file = $input['media'] ? (array) $input['media'] : null;
  303. $media_url = $input['media_url'];
  304. $media_attrs = $input['attrs'] ? (array) $input['attrs'] : null;
  305. if ( isset( $media_url ) || $media_file ) {
  306. $user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
  307. if ( ! $user_can_upload_files ) {
  308. return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
  309. }
  310. $has_original_media = Jetpack_Media::get_original_media( $media_id );
  311. if ( ! $has_original_media ) {
  312. // The first time that the media is updated
  313. // the original media is stored into the revision_history
  314. $snapshot = $this->get_snapshot( $media_item );
  315. add_post_meta( $media_id, Jetpack_Media::$WP_ORIGINAL_MEDIA, $snapshot, true );
  316. }
  317. // save the temporal file locally
  318. $temporal_file = $media_file ? $media_file : $this->build_file_array_from_url( $media_id, $media_url );
  319. if ( is_wp_error( $temporal_file ) ) {
  320. return $temporal_file;
  321. }
  322. $uploaded_file = $this->save_temporary_file( $temporal_file, $media_id );
  323. if ( is_wp_error( $uploaded_file ) ) {
  324. return $uploaded_file;
  325. }
  326. // revision_history control
  327. $this->register_revision( $media_item, $uploaded_file, $has_original_media );
  328. $uploaded_path = $uploaded_file['file'];
  329. $udpated_mime_type = $uploaded_file['type'];
  330. $was_updated = update_attached_file( $media_id, $uploaded_path );
  331. if ( $was_updated ) {
  332. $new_metadata = wp_generate_attachment_metadata( $media_id, $uploaded_path );
  333. wp_update_attachment_metadata( $media_id, $new_metadata );
  334. // check maximum amount of revision_history
  335. Jetpack_Media::limit_revision_history( $media_id, REVISION_HISTORY_MAXIMUM_AMOUNT );
  336. wp_update_post( (object) array(
  337. 'ID' => $media_id,
  338. 'post_mime_type' => $udpated_mime_type
  339. ) );
  340. }
  341. unset( $input['media'] );
  342. unset( $input['media_url'] );
  343. unset( $input['attrs'] );
  344. }
  345. // update media through of `attrs` value it it's defined
  346. if ( ( $media_file || isset( $media_url ) ) && $media_attrs ) {
  347. $was_updated = $this->update_by_attrs_parameter( $media_id, $media_attrs );
  348. if ( is_wp_error( $was_updated ) ) {
  349. return $was_updated;
  350. }
  351. }
  352. // call parent method
  353. $response = parent::callback( $path, $blog_id, $media_id );
  354. // expose `revision_history` object
  355. $response->revision_history = (object) array(
  356. 'items' => (array) Jetpack_Media::get_revision_history( $media_id ),
  357. 'original' => (object) Jetpack_Media::get_original_media( $media_id )
  358. );
  359. return $response;
  360. }
  361. }