Keine Beschreibung

class.wpcom-json-api-update-media-v1-1-endpoint.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. new WPCOM_JSON_API_Update_Media_v1_1_Endpoint(
  3. array(
  4. 'description' => 'Edit basic information about a media item.',
  5. 'group' => 'media',
  6. 'stat' => 'media:1:POST',
  7. 'min_version' => '1.1',
  8. 'max_version' => '1.1',
  9. 'method' => 'POST',
  10. 'path' => '/sites/%s/media/%d',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. '$media_ID' => '(int) The ID of the media item',
  14. ),
  15. 'request_format' => array(
  16. 'parent_id' => '(int) ID of the post this media is attached to',
  17. 'title' => '(string) The file name.',
  18. 'caption' => '(string) File caption.',
  19. 'description' => '(HTML) Description of the file.',
  20. 'alt' => '(string) Alternative text for image files.',
  21. 'rating' => '(string) Video only. Video rating.',
  22. 'display_embed' => '(string) Video only. Whether to share or not the video.',
  23. 'artist' => '(string) Audio Only. Artist metadata for the audio track.',
  24. 'album' => '(string) Audio Only. Album metadata for the audio track.',
  25. ),
  26. 'response_format' => array(
  27. 'ID' => '(int) The ID of the media item',
  28. 'date' => '(ISO 8601 datetime) The date the media was uploaded',
  29. 'post_ID' => '(int) ID of the post this media is attached to',
  30. 'author_ID' => '(int) ID of the user who uploaded the media',
  31. 'URL' => '(string) URL to the file',
  32. 'guid' => '(string) Unique identifier',
  33. 'file' => '(string) File name',
  34. 'extension' => '(string) File extension',
  35. 'mime_type' => '(string) File mime type',
  36. 'title' => '(string) File name',
  37. 'caption' => '(string) User provided caption of the file',
  38. 'description' => '(string) Description of the file',
  39. 'alt' => '(string) Alternative text for image files.',
  40. 'thumbnails' => '(object) Media item thumbnail URL options',
  41. 'height' => '(int) (Image & video only) Height of the media item',
  42. 'width' => '(int) (Image & video only) Width of the media item',
  43. 'length' => '(int) (Video & audio only) Duration of the media item, in seconds',
  44. 'exif' => '(array) (Image & audio only) Exif (meta) information about the media item',
  45. 'rating' => '(string) (Video only) VideoPress rating of the video',
  46. 'display_embed' => '(string) Video only. Whether to share or not the video.',
  47. 'videopress_guid' => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
  48. '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.',
  49. ),
  50. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/446',
  51. 'example_request_data' => array(
  52. 'headers' => array(
  53. 'authorization' => 'Bearer YOUR_API_TOKEN',
  54. ),
  55. 'body' => array(
  56. 'title' => 'Updated Title',
  57. ),
  58. ),
  59. )
  60. );
  61. class WPCOM_JSON_API_Update_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  62. function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
  63. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  64. if ( is_wp_error( $blog_id ) ) {
  65. return $blog_id;
  66. }
  67. if ( ! current_user_can( 'upload_files', $media_id ) ) {
  68. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  69. }
  70. $item = $this->get_media_item_v1_1( $media_id );
  71. if ( is_wp_error( $item ) ) {
  72. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  73. }
  74. $input = $this->input( true );
  75. $insert = array();
  76. if ( isset( $input['title'] ) ) {
  77. $insert['post_title'] = $input['title'];
  78. }
  79. if ( isset( $input['caption'] ) ) {
  80. $insert['post_excerpt'] = $input['caption'];
  81. }
  82. if ( isset( $input['description'] ) ) {
  83. $insert['post_content'] = $input['description'];
  84. }
  85. if ( isset( $input['parent_id'] ) ) {
  86. $insert['post_parent'] = $input['parent_id'];
  87. }
  88. if ( isset( $input['alt'] ) ) {
  89. $alt = wp_strip_all_tags( $input['alt'], true );
  90. update_post_meta( $media_id, '_wp_attachment_image_alt', $alt );
  91. }
  92. // audio only artist/album info.
  93. if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
  94. $changed = false;
  95. $id3data = wp_get_attachment_metadata( $media_id );
  96. if ( ! is_array( $id3data ) ) {
  97. $changed = true;
  98. $id3data = array();
  99. }
  100. $id3_keys = array(
  101. 'artist' => __( 'Artist', 'jetpack' ),
  102. 'album' => __( 'Album', 'jetpack' ),
  103. );
  104. foreach ( $id3_keys as $key => $label ) {
  105. if ( isset( $input[ $key ] ) ) {
  106. $changed = true;
  107. $id3data[ $key ] = wp_strip_all_tags( $input[ $key ], true );
  108. }
  109. }
  110. if ( $changed ) {
  111. wp_update_attachment_metadata( $media_id, $id3data );
  112. }
  113. }
  114. // Pass the item to the handle_video_meta() that checks if it's a VideoPress item and saves it.
  115. $result = $this->handle_video_meta( $media_id, $input, $item );
  116. if ( is_wp_error( $result ) ) {
  117. return $result;
  118. }
  119. $insert['ID'] = $media_id;
  120. wp_update_post( (object) $insert );
  121. $item = $this->get_media_item_v1_1( $media_id );
  122. return $item;
  123. }
  124. /**
  125. * Persist the VideoPress metadata if the given item argument is a VideoPress item.
  126. *
  127. * @param string $media_id The ID of the video.
  128. * @param array $input The request input.
  129. * @param stdClass $item The response item.
  130. *
  131. * @return bool|WP_Error
  132. */
  133. public function handle_video_meta( $media_id, $input, $item ) {
  134. if ( ! class_exists( \Videopress_Attachment_Metadata::class ) ) {
  135. return false;
  136. }
  137. if ( ! \Videopress_Attachment_Metadata::is_videopress_media( $item ) ) {
  138. return false;
  139. }
  140. return \Videopress_Attachment_Metadata::persist_metadata(
  141. $media_id,
  142. $item->videopress_guid,
  143. $input['title'],
  144. $input['caption'],
  145. $input['description'],
  146. $input['rating'],
  147. $input['display_embed']
  148. );
  149. }
  150. }