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

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