summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php')
-rw-r--r--plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php130
1 files changed, 73 insertions, 57 deletions
diff --git a/plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php b/plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php
index 86bbec41..f2ce11e7 100644
--- a/plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php
+++ b/plugins/jetpack/json-endpoints/class.wpcom-json-api-upload-media-v1-1-endpoint.php
@@ -1,52 +1,64 @@
-<?php
-
-new WPCOM_JSON_API_Upload_Media_v1_1_Endpoint( array(
- 'description' => 'Upload a new piece of media.',
- 'allow_cross_origin_request' => true,
- 'allow_upload_token_auth' => true,
- 'group' => 'media',
- 'stat' => 'media:new',
- 'min_version' => '1.1',
- 'max_version' => '1.1',
- 'method' => 'POST',
- 'path' => '/sites/%s/media/new',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- ),
-
- 'request_format' => array(
- '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 />" .
- "<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>",
- '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.",
- '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 />" .
- "<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>",
- ),
-
- 'response_format' => array(
- 'media' => '(array) Array of uploaded media objects',
- 'errors' => '(array) Array of error messages of uploading media failures',
- ),
-
- 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/new',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN',
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
+/**
+ * Upload media item API endpoint v1.1
+ *
+ * Endpoint: /sites/%s/media/new
+ */
+
+new WPCOM_JSON_API_Upload_Media_v1_1_Endpoint(
+ array(
+ 'description' => 'Upload a new piece of media.',
+ 'allow_cross_origin_request' => true,
+ 'allow_upload_token_auth' => true,
+ 'group' => 'media',
+ 'stat' => 'media:new',
+ 'min_version' => '1.1',
+ 'max_version' => '1.1',
+ 'method' => 'POST',
+ 'path' => '/sites/%s/media/new',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
),
- 'body' => array(
- 'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
+
+ 'request_format' => array(
+ '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 />' .
+ "<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>",
+ '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.',
+ '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 />' .
+ "<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>",
+ ),
+
+ 'response_format' => array(
+ 'media' => '(array) Array of uploaded media objects',
+ 'errors' => '(array) Array of error messages of uploading media failures',
+ ),
+
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/new',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ 'body' => array(
+ 'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
+ ),
),
)
-) );
+);
+// phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid
+/**
+ * Upload media item API class v1.1
+ */
class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
-
/**
- * @param string $path
- * @param int $blog_id
+ * Upload media item API endpoint callback v1.1
+ *
+ * @param string $path API path.
+ * @param int $blog_id Blog ID.
*
* @return array|int|WP_Error|void
*/
- function callback( $path = '', $blog_id = 0 ) {
+ public function callback( $path = '', $blog_id = 0 ) {
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
@@ -59,7 +71,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
$input = $this->input( true );
$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
- $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
+ $media_urls = ! empty( $input['media_urls'] ) ? $input['media_urls'] : array();
$media_attrs = ! empty( $input['attrs'] ) ? $input['attrs'] : array();
if ( empty( $media_files ) && empty( $media_urls ) ) {
@@ -69,7 +81,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
$is_jetpack_site = false;
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// For jetpack sites, we send the media via a different method, because the sync is very different.
- $jetpack_sync = Jetpack_Media_Sync::summon( $blog_id );
+ $jetpack_sync = Jetpack_Media_Sync::summon( $blog_id );
$is_jetpack_site = $jetpack_sync->is_jetpack_site();
}
@@ -78,7 +90,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
$media_items = array();
$errors = array();
- // We're splitting out videos for Jetpack sites
+ // We're splitting out videos for Jetpack sites.
foreach ( $media_files as $media_item ) {
if ( preg_match( '@^video/@', $media_item['type'] ) && $is_jetpack_site ) {
$jetpack_media_files[] = $media_item;
@@ -88,7 +100,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
}
}
- // New Jetpack / VideoPress media upload processing
+ // New Jetpack / VideoPress media upload processing.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
if ( count( $jetpack_media_files ) > 0 ) {
add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
@@ -106,7 +118,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
}
}
- // Normal WPCOM upload processing
+ // Normal WPCOM upload processing.
if ( count( $other_media_files ) > 0 || count( $media_urls ) > 0 ) {
if ( is_multisite() ) { // Do not check for available space in non multisites.
add_filter( 'wp_handle_upload_prefilter', array( $this, 'check_upload_size' ), 9 ); // used for direct media uploads.
@@ -114,8 +126,8 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
}
$create_media = $this->handle_media_creation_v1_1( $other_media_files, $media_urls, $media_attrs );
- $media_ids = $create_media['media_ids'];
- $errors = $create_media['errors'];
+ $media_ids = $create_media['media_ids'];
+ $errors = $create_media['errors'];
$media_items = array();
foreach ( $media_ids as $media_id ) {
@@ -130,7 +142,11 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
$results = array();
foreach ( $media_items as $media_item ) {
if ( is_wp_error( $media_item ) ) {
- $errors[] = array( 'file' => $media_item['ID'], 'error' => $media_item->get_error_code(), 'message' => $media_item->get_error_message() );
+ $errors[] = array(
+ 'file' => $media_item['ID'],
+ 'error' => $media_item->get_error_code(),
+ 'message' => $media_item->get_error_message(),
+ );
} else {
$results[] = $media_item;
@@ -152,10 +168,10 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
* @param array $errors Errors for the uploaded file.
* @return array The same array with an improved error message.
*/
- function rewrite_generic_upload_error( $errors ) {
- foreach ( $errors as $k => $error ) {
+ public function rewrite_generic_upload_error( $errors ) {
+ foreach ( $errors as $k => $error ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( 'upload_error' === $error['error'] && false !== strpos( $error['message'], '|' ) ) {
- list( $errors[ $k ]['error'], $errors[ $k ]['message'] ) = explode( '|', $error['message'], 2 );
+ list( $errors[ $k ]['error'], $errors[ $k ]['message'] ) = explode( '|', $error['message'], 2 ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
}
}
return $errors;
@@ -171,7 +187,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
* @param array $file $_FILES array for a given file.
* @return array Maybe extended with an error message.
*/
- function check_upload_size( $file ) {
+ public function check_upload_size( $file ) {
if ( get_site_option( 'upload_space_check_disabled' ) ) {
return $file;
}
@@ -189,7 +205,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
$file_size = filesize( $file['tmp_name'] );
if ( $space_left < $file_size ) {
/* translators: %s: Required disk space in kilobytes. */
- $file['error'] = 'rest_upload_limited_space|' . sprintf( __( 'Not enough space to upload. %s KB needed.', 'default' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) );
+ $file['error'] = 'rest_upload_limited_space|' . sprintf( __( 'Not enough space to upload. %s KB needed.', 'default' ), number_format( ( $file_size - $space_left ) / KB_IN_BYTES ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
}
$max_upload_size = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
@@ -203,7 +219,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
}
if ( upload_is_user_over_quota( false ) ) {
- $file['error'] = 'rest_upload_user_quota_exceeded|' . __( 'You have used your space quota. Please delete files before uploading.', 'default' );
+ $file['error'] = 'rest_upload_user_quota_exceeded|' . __( 'You have used your space quota. Please delete files before uploading.', 'default' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
}
return $file;
@@ -212,10 +228,10 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
* Force to use the WPCOM API instead of proxy back to the Jetpack API if the blog is a paid Jetpack
* blog w/ the VideoPress module enabled AND the uploaded file is a video.
*
- * @param int $blog_id
+ * @param int $blog_id Blog ID.
* @return bool
*/
- function force_wpcom_request( $blog_id ) {
+ public function force_wpcom_request( $blog_id ) {
// We don't need to do anything if VideoPress is not enabled for the blog.
if ( ! is_videopress_enabled_on_jetpack_blog( $blog_id ) ) {
@@ -223,7 +239,7 @@ class WPCOM_JSON_API_Upload_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint
}
// Check to see if the upload is not a video type, if not then return false.
- $input = $this->input( true );
+ $input = $this->input( true );
$media_files = ! empty( $input['media'] ) ? $input['media'] : array();
if ( empty( $media_files ) ) {