class.wpcom-json-api-upload-media-v1-1-endpoint.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. new WPCOM_JSON_API_Upload_Media_v1_1_Endpoint( array(
  3. 'description' => 'Upload a new piece of media.',
  4. 'allow_cross_origin_request' => true,
  5. 'allow_upload_token_auth' => true,
  6. 'group' => 'media',
  7. 'stat' => 'media:new',
  8. 'min_version' => '1.1',
  9. 'max_version' => '1.1',
  10. 'method' => 'POST',
  11. 'path' => '/sites/%s/media/new',
  12. 'path_labels' => array(
  13. '$site' => '(int|string) Site ID or domain',
  14. ),
  15. 'request_format' => array(
  16. 'media' => "(media) An array of media to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Accepts jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. Audio and Video may also be available. See <code>allowed_file_types</code> in the options response of the site endpoint.<br /><br /><strong>Example</strong>:<br />" .
  17. "<code>curl \<br />--form 'media[]=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/media/new'</code>",
  18. 'media_urls' => "(array) An array of URLs to upload to the post. Errors produced by media uploads, if any, will be in `media_errors` in the response.",
  19. 'attrs' => "(array) An array of attributes (`title`, `description`, `caption` `alt` for images, `artist` for audio, `album` for audio, and `parent_id`) are supported to assign to the media uploaded via the `media` or `media_urls` properties. You must use a numeric index for the keys of `attrs` which follows the same sequence as `media` and `media_urls`. <br /><br /><strong>Example</strong>:<br />" .
  20. "<code>curl \<br />--form 'media[]=@/path/to/file1.jpg' \<br />--form 'media_urls[]=http://example.com/file2.jpg' \<br /> \<br />--form 'attrs[0][caption]=This will be the caption for file1.jpg' \<br />--form 'attrs[1][title]=This will be the title for file2.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/posts/new'</code>",
  21. ),
  22. 'response_format' => array(
  23. 'media' => '(array) Array of uploaded media objects',
  24. 'errors' => '(array) Array of error messages of uploading media failures',
  25. ),
  26. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/new',
  27. 'example_request_data' => array(
  28. 'headers' => array(
  29. 'authorization' => 'Bearer YOUR_API_TOKEN',
  30. ),
  31. 'body' => array(
  32. 'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
  33. ),
  34. )
  35. ) );
  36. class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  37. /**
  38. * @param string $path
  39. * @param int $blog_id
  40. *
  41. * @return array|int|WP_Error|void
  42. */
  43. function callback( $path = '', $blog_id = 0 ) {
  44. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  45. if ( is_wp_error( $blog_id ) ) {
  46. return $blog_id;
  47. }
  48. if ( ! current_user_can( 'upload_files' ) && ! $this->api->is_authorized_with_upload_token() ) {
  49. return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
  50. }
  51. $input = $this->input( true );
  52. $media_files = ! empty( $input['media'] ) ? $input['media'] : array();
  53. $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
  54. $media_attrs = ! empty( $input['attrs'] ) ? $input['attrs'] : array();
  55. if ( empty( $media_files ) && empty( $media_urls ) ) {
  56. return new WP_Error( 'invalid_input', 'No media provided in input.' );
  57. }
  58. $is_jetpack_site = false;
  59. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  60. // For jetpack sites, we send the media via a different method, because the sync is very different.
  61. $jetpack_sync = Jetpack_Media_Sync::summon( $blog_id );
  62. $is_jetpack_site = $jetpack_sync->is_jetpack_site();
  63. }
  64. $jetpack_media_files = array();
  65. $other_media_files = array();
  66. $media_items = array();
  67. $errors = array();
  68. // We're splitting out videos for Jetpack sites
  69. foreach ( $media_files as $media_item ) {
  70. if ( preg_match( '@^video/@', $media_item['type'] ) && $is_jetpack_site ) {
  71. $jetpack_media_files[] = $media_item;
  72. } else {
  73. $other_media_files[] = $media_item;
  74. }
  75. }
  76. // New Jetpack / VideoPress media upload processing
  77. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  78. if ( count( $jetpack_media_files ) > 0 ) {
  79. add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
  80. $media_items = $jetpack_sync->upload_media( $jetpack_media_files, $this->api );
  81. $errors = $jetpack_sync->get_errors();
  82. foreach ( $media_items as & $media_item ) {
  83. // More than likely a post has not been created yet, so we pass in the media item we
  84. // got back from the Jetpack site.
  85. $post = (object) $media_item['post'];
  86. $media_item = $this->get_media_item_v1_1( $post->ID, $post, $media_item['file'] );
  87. }
  88. }
  89. }
  90. // Normal WPCOM upload processing
  91. if ( count( $other_media_files ) > 0 || count( $media_urls ) > 0 ) {
  92. $create_media = $this->handle_media_creation_v1_1( $other_media_files, $media_urls, $media_attrs );
  93. $media_ids = $create_media['media_ids'];
  94. $errors = $create_media['errors'];
  95. $media_items = array();
  96. foreach ( $media_ids as $media_id ) {
  97. $media_items[] = $this->get_media_item_v1_1( $media_id );
  98. }
  99. }
  100. if ( count( $media_items ) <= 0 ) {
  101. return $this->api->output_early( 400, array( 'errors' => $errors ) );
  102. }
  103. $results = array();
  104. foreach ( $media_items as $media_item ) {
  105. if ( is_wp_error( $media_item ) ) {
  106. $errors[] = array( 'file' => $media_item['ID'], 'error' => $media_item->get_error_code(), 'message' => $media_item->get_error_message() );
  107. } else {
  108. $results[] = $media_item;
  109. }
  110. }
  111. $response = array( 'media' => $results );
  112. if ( count( $errors ) > 0 ) {
  113. $response['errors'] = $errors;
  114. }
  115. return $response;
  116. }
  117. /**
  118. * Force to use the WPCOM API instead of proxy back to the Jetpack API if the blog is a paid Jetpack
  119. * blog w/ the VideoPress module enabled AND the uploaded file is a video.
  120. *
  121. * @param int $blog_id
  122. * @return bool
  123. */
  124. function force_wpcom_request( $blog_id ) {
  125. // We don't need to do anything if VideoPress is not enabled for the blog.
  126. if ( ! is_videopress_enabled_on_jetpack_blog( $blog_id ) ) {
  127. return false;
  128. }
  129. // Check to see if the upload is not a video type, if not then return false.
  130. $input = $this->input( true );
  131. $media_files = ! empty( $input['media'] ) ? $input['media'] : array();
  132. if ( empty( $media_files ) ) {
  133. return false;
  134. }
  135. foreach ( $media_files as $media_item ) {
  136. if ( ! preg_match( '@^video/@', $media_item['type'] ) ) {
  137. return false;
  138. }
  139. }
  140. // The API request should be for a blog w/ Jetpack, A valid plan, has VideoPress enabled,
  141. // and is a video file. Let's let it through.
  142. return true;
  143. }
  144. }