PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | json-endpoints/class.wpcom-json-api-edit-media-v1-2-endpoint.php +46 -32 12.4.2 → 16.3-a.7 View file →
@@ -1,6 +1,10 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit( 0 );
5 +}
6 +
3 7 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php';
4 8
5 9 define( 'REVISION_HISTORY_MAXIMUM_AMOUNT', 5 );
6 10 define( 'WP_ATTACHMENT_IMAGE_ALT', '_wp_attachment_image_alt' );
@@ -33,9 +37,9 @@
33 37 'jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key. ' .
34 38 'Audio and Video may also be available. See <code>allowed_file_types</code> ' .
35 39 'in the options response of the site endpoint. ' .
36 40 '<br /><br /><strong>Example</strong>:<br />' .
37 - "<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>",
41 + "<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/media/new'</code>",
38 42 'attrs' => '(object) An Object of attributes (`title`, `description` and `caption`) ' .
39 43 'are supported to assign to the media uploaded via the `media` or `media_url`',
40 44 'media_url' => '(string) An URL of the image to attach to a post.',
41 45 ),
@@ -80,16 +84,18 @@
80 84 );
81 85
82 86 /**
83 87 * Edit media v1_2 endpoint class.
88 + *
89 + * @phan-constructor-used-for-side-effects
84 90 */
85 91 class WPCOM_JSON_API_Edit_Media_v1_2_Endpoint extends WPCOM_JSON_API_Update_Media_v1_1_Endpoint { //phpcs:ignore
86 92 /**
87 93 * Return an array of mime_type items allowed when the media file is uploaded.
88 94 *
89 - * @param Array $default_mime_types - array of default mime types.
95 + * @param array $default_mime_types - array of default mime types.
90 96 *
91 - * @return {Array} mime_type array
97 + * @return array mime_type array
92 98 */
93 99 public static function get_allowed_mime_types( $default_mime_types ) {
94 100 return array_unique(
95 101 array_merge(
@@ -112,10 +118,10 @@
112 118 /**
113 119 * Update the media post grabbing the post values from
114 120 * the `attrs` parameter
115 121 *
116 - * @param {Number} $media_id - post media ID.
117 - * @param {Object} $attrs - `attrs` parameter sent from the client in the request body.
122 + * @param int $media_id - post media ID.
123 + * @param array $attrs - `attrs` parameter sent from the client in the request body.
118 124 */
119 125 private function update_by_attrs_parameter( $media_id, $attrs ) {
120 126 $post_update_action = null;
121 127 $insert = array();
@@ -162,9 +168,9 @@
162 168
163 169 if ( ! empty( $id3_meta ) ) {
164 170 // Before updating metadata, ensure that the item is audio.
165 171 $item = $this->get_media_item_v1_1( $media_id );
166 - if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
172 + if ( str_starts_with( $item->mime_type, 'audio/' ) ) {
167 173 $update_action = wp_update_attachment_metadata( $media_id, $id3_meta );
168 174 if ( is_wp_error( $update_action ) ) {
169 175 return $update_action;
170 176 }
@@ -176,10 +182,10 @@
176 182
177 183 /**
178 184 * Return an object to be used to store into the revision_history
179 185 *
180 - * @param {Object} $media_item - media post object.
181 - * @return {Object} the snapshot object
186 + * @param object $media_item - media post object.
187 + * @return object the snapshot object
182 188 */
183 189 private function get_snapshot( $media_item ) {
184 190 $current_file = get_attached_file( $media_item->ID );
185 191 $file_paths = pathinfo( $current_file );
@@ -198,9 +204,9 @@
198 204
199 205 /**
200 206 * Try to remove the temporal file from the given file array.
201 207 *
202 - * @param {Array} $file_array - Array with data about the temporal file.
208 + * @param array $file_array - Array with data about the temporal file.
203 209 */
204 210 private function remove_tmp_file( $file_array ) {
205 211 if ( file_exists( $file_array['tmp_name'] ) ) {
206 212 wp_delete_file( $file_array['tmp_name'] );
@@ -209,16 +215,18 @@
209 215
210 216 /**
211 217 * Save the given temporal file in a local folder.
212 218 *
213 - * @param {Array} $file_array - array containing file data.
214 - * @param {Number} $media_id - the media id.
215 - * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
219 + * @param array $file_array - array containing file data.
220 + * @param int $media_id - the media id.
221 + * @param bool $is_upload - True if `$file_array` derives from an upload in `$_FILES`, false if this is a sideload.
222 + * @return array|WP_Error An array with information about the new file saved or a WP_Error is something went wrong.
216 223 */
217 - private function save_temporary_file( $file_array, $media_id ) {
224 + private function save_temporary_file( $file_array, $media_id, $is_upload ) {
218 225 $tmp_filename = $file_array['tmp_name'];
219 226
220 - if ( ! file_exists( $tmp_filename ) ) {
227 + $is_ok = $is_upload ? is_uploaded_file( $tmp_filename ) : file_exists( $tmp_filename );
228 + if ( ! $is_ok ) {
221 229 return new WP_Error( 'invalid_input', 'No media provided in input.' );
222 230 }
223 231
224 232 // add additional mime_types through of the `jetpack_supported_media_sideload_types` filter.
@@ -231,9 +239,11 @@
231 239 if (
232 240 ! $this->is_file_supported_for_sideloading( $tmp_filename ) &&
233 241 ! file_is_displayable_image( $tmp_filename )
234 242 ) {
235 - wp_delete_file( $tmp_filename );
243 + if ( ! $is_upload ) {
244 + wp_delete_file( $tmp_filename );
245 + }
236 246 return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
237 247 }
238 248 remove_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter );
239 249
@@ -245,12 +255,15 @@
245 255
246 256 $time = $this->get_time_string_from_guid( $media_id );
247 257
248 258 $file_array['name'] = $tmp_new_filename;
249 - $file = wp_handle_sideload( $file_array, $overrides, $time );
259 + if ( $is_upload ) {
260 + $file = wp_handle_upload( $file_array, $overrides, $time );
261 + } else {
262 + $file = wp_handle_sideload( $file_array, $overrides, $time );
263 + $this->remove_tmp_file( $file_array );
264 + }
250 265
251 - $this->remove_tmp_file( $file_array );
252 -
253 266 if ( isset( $file['error'] ) ) {
254 267 return new WP_Error( 'upload_error', $file['error'] );
255 268 }
256 269
@@ -288,11 +301,11 @@
288 301
289 302 /**
290 303 * Get the image from a remote url and then save it locally.
291 304 *
292 - * @param {Number} $media_id - media post ID.
293 - * @param {String} $url - image URL to save locally.
294 - * @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
305 + * @param int $media_id - media post ID.
306 + * @param string $url - image URL to save locally.
307 + * @return array|WP_Error An array with information about the new file saved or a WP_Error is something went wrong.
295 308 */
296 309 private function build_file_array_from_url( $media_id, $url ) {
297 310 if ( ! $url ) {
298 311 return null;
@@ -322,12 +335,12 @@
322 335
323 336 /**
324 337 * Add a new item into revision_history array.
325 338 *
326 - * @param {Object} $media_item - media post.
327 - * @param {file} $file - file recentrly added.
328 - * @param {Boolean} $has_original_media - condition is the original media has been already added.
329 - * @return {Boolean} `true` if the item has been added. Otherwise `false`.
339 + * @param object $media_item - media post.
340 + * @param array|WP_Error $file - File data, or WP_Error on error.
341 + * @param bool $has_original_media - condition is the original media has been already added.
342 + * @return bool `true` if the item has been added. Otherwise `false`.
330 343 */
331 344 private function register_revision( $media_item, $file, $has_original_media ) {
332 345 if (
333 346 is_wp_error( $file ) ||
@@ -341,11 +354,11 @@
341 354
342 355 /**
343 356 * Restore the original media file.
344 357 *
345 - * @param {Number} $media_id - media post ID.
346 - * @param {Object} $original_media - orginal media data.
347 - * @return {Array} - restore media info.
358 + * @param int $media_id - media post ID.
359 + * @param object $original_media - orginal media data.
360 + * @return array - restore media info.
348 361 */
349 362 private function restore_original( $media_id, $original_media ) {
350 363 $revisions = (array) Jetpack_Media::get_revision_history( $media_id );
351 364 $revisions = array_filter(
@@ -393,10 +406,10 @@
393 406 if ( is_wp_error( $media_item ) ) {
394 407 return $media_item;
395 408 }
396 409
397 - if ( ! current_user_can( 'upload_files', $media_id ) ) {
398 - return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
410 + if ( ! $this->current_user_can_edit_media_item( $media_id ) ) {
411 + return new WP_Error( 'unauthorized', 'User cannot edit media', 403 );
399 412 }
400 413
401 414 $input = $this->input( true );
402 415
@@ -401,9 +414,9 @@
401 414 $input = $this->input( true );
402 415
403 416 // Images.
404 417 $media_file = isset( $input['media'] ) ? (array) $input['media'] : null;
405 - $media_url = isset( $input['media_url'] ) ? $input['media_url'] : null;
418 + $media_url = $input['media_url'] ?? null;
406 419 $media_attrs = isset( $input['attrs'] ) ? (array) $input['attrs'] : null;
407 420
408 421 if ( isset( $media_url ) || $media_file ) {
409 422 $user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
@@ -421,8 +434,9 @@
421 434 add_post_meta( $media_id, Jetpack_Media::WP_ORIGINAL_MEDIA, $snapshot, true );
422 435 }
423 436
424 437 // save the temporal file locally.
438 + $is_upload = (bool) $media_file;
425 439 $temporal_file = $media_file ? $media_file : $this->build_file_array_from_url( $media_id, $media_url );
426 440
427 441 if ( is_wp_error( $temporal_file ) ) {
428 442 return $temporal_file;
@@ -432,9 +446,9 @@
432 446 $should_restore = isset( $media_url ) && ! isset( $media_file ) && $has_original_media;
433 447
434 448 $uploaded_file = $should_restore
435 449 ? $this->restore_original( $media_id, $has_original_media )
436 - : $this->save_temporary_file( $temporal_file, $media_id );
450 + : $this->save_temporary_file( $temporal_file, $media_id, $is_upload );
437 451
438 452 if ( is_wp_error( $uploaded_file ) ) {
439 453 return $uploaded_file;
440 454 }