PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.9.2 14.0.1 All 504 releases
← All changes | json-endpoints/class.wpcom-json-api-edit-media-v1-2-endpoint.php +48 -33 12.0.3 → 16.3-a.1 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,13 +118,14 @@
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 - $insert = array();
126 + $post_update_action = null;
127 + $insert = array();
121 128
122 129 // Attributes: Title, Caption, Description.
123 130 if ( isset( $attrs['title'] ) ) {
124 131 $insert['post_title'] = $attrs['title'];
@@ -161,9 +168,9 @@
161 168
162 169 if ( ! empty( $id3_meta ) ) {
163 170 // Before updating metadata, ensure that the item is audio.
164 171 $item = $this->get_media_item_v1_1( $media_id );
165 - if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
172 + if ( str_starts_with( $item->mime_type, 'audio/' ) ) {
166 173 $update_action = wp_update_attachment_metadata( $media_id, $id3_meta );
167 174 if ( is_wp_error( $update_action ) ) {
168 175 return $update_action;
169 176 }
@@ -175,10 +182,10 @@
175 182
176 183 /**
177 184 * Return an object to be used to store into the revision_history
178 185 *
179 - * @param {Object} $media_item - media post object.
180 - * @return {Object} the snapshot object
186 + * @param object $media_item - media post object.
187 + * @return object the snapshot object
181 188 */
182 189 private function get_snapshot( $media_item ) {
183 190 $current_file = get_attached_file( $media_item->ID );
184 191 $file_paths = pathinfo( $current_file );
@@ -197,9 +204,9 @@
197 204
198 205 /**
199 206 * Try to remove the temporal file from the given file array.
200 207 *
201 - * @param {Array} $file_array - Array with data about the temporal file.
208 + * @param array $file_array - Array with data about the temporal file.
202 209 */
203 210 private function remove_tmp_file( $file_array ) {
204 211 if ( file_exists( $file_array['tmp_name'] ) ) {
205 212 wp_delete_file( $file_array['tmp_name'] );
@@ -208,16 +215,18 @@
208 215
209 216 /**
210 217 * Save the given temporal file in a local folder.
211 218 *
212 - * @param {Array} $file_array - array containing file data.
213 - * @param {Number} $media_id - the media id.
214 - * @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.
215 223 */
216 - private function save_temporary_file( $file_array, $media_id ) {
224 + private function save_temporary_file( $file_array, $media_id, $is_upload ) {
217 225 $tmp_filename = $file_array['tmp_name'];
218 226
219 - if ( ! file_exists( $tmp_filename ) ) {
227 + $is_ok = $is_upload ? is_uploaded_file( $tmp_filename ) : file_exists( $tmp_filename );
228 + if ( ! $is_ok ) {
220 229 return new WP_Error( 'invalid_input', 'No media provided in input.' );
221 230 }
222 231
223 232 // add additional mime_types through of the `jetpack_supported_media_sideload_types` filter.
@@ -230,9 +239,11 @@
230 239 if (
231 240 ! $this->is_file_supported_for_sideloading( $tmp_filename ) &&
232 241 ! file_is_displayable_image( $tmp_filename )
233 242 ) {
234 - wp_delete_file( $tmp_filename );
243 + if ( ! $is_upload ) {
244 + wp_delete_file( $tmp_filename );
245 + }
235 246 return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
236 247 }
237 248 remove_filter( 'jetpack_supported_media_sideload_types', $mime_type_static_filter );
238 249
@@ -244,12 +255,15 @@
244 255
245 256 $time = $this->get_time_string_from_guid( $media_id );
246 257
247 258 $file_array['name'] = $tmp_new_filename;
248 - $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 + }
249 265
250 - $this->remove_tmp_file( $file_array );
251 -
252 266 if ( isset( $file['error'] ) ) {
253 267 return new WP_Error( 'upload_error', $file['error'] );
254 268 }
255 269
@@ -287,11 +301,11 @@
287 301
288 302 /**
289 303 * Get the image from a remote url and then save it locally.
290 304 *
291 - * @param {Number} $media_id - media post ID.
292 - * @param {String} $url - image URL to save locally.
293 - * @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.
294 308 */
295 309 private function build_file_array_from_url( $media_id, $url ) {
296 310 if ( ! $url ) {
297 311 return null;
@@ -321,12 +335,12 @@
321 335
322 336 /**
323 337 * Add a new item into revision_history array.
324 338 *
325 - * @param {Object} $media_item - media post.
326 - * @param {file} $file - file recentrly added.
327 - * @param {Boolean} $has_original_media - condition is the original media has been already added.
328 - * @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`.
329 343 */
330 344 private function register_revision( $media_item, $file, $has_original_media ) {
331 345 if (
332 346 is_wp_error( $file ) ||
@@ -340,11 +354,11 @@
340 354
341 355 /**
342 356 * Restore the original media file.
343 357 *
344 - * @param {Number} $media_id - media post ID.
345 - * @param {Object} $original_media - orginal media data.
346 - * @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.
347 361 */
348 362 private function restore_original( $media_id, $original_media ) {
349 363 $revisions = (array) Jetpack_Media::get_revision_history( $media_id );
350 364 $revisions = array_filter(
@@ -392,10 +406,10 @@
392 406 if ( is_wp_error( $media_item ) ) {
393 407 return $media_item;
394 408 }
395 409
396 - if ( ! current_user_can( 'upload_files', $media_id ) ) {
397 - 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 );
398 412 }
399 413
400 414 $input = $this->input( true );
401 415
@@ -400,9 +414,9 @@
400 414 $input = $this->input( true );
401 415
402 416 // Images.
403 417 $media_file = isset( $input['media'] ) ? (array) $input['media'] : null;
404 - $media_url = isset( $input['media_url'] ) ? $input['media_url'] : null;
418 + $media_url = $input['media_url'] ?? null;
405 419 $media_attrs = isset( $input['attrs'] ) ? (array) $input['attrs'] : null;
406 420
407 421 if ( isset( $media_url ) || $media_file ) {
408 422 $user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
@@ -420,8 +434,9 @@
420 434 add_post_meta( $media_id, Jetpack_Media::WP_ORIGINAL_MEDIA, $snapshot, true );
421 435 }
422 436
423 437 // save the temporal file locally.
438 + $is_upload = (bool) $media_file;
424 439 $temporal_file = $media_file ? $media_file : $this->build_file_array_from_url( $media_id, $media_url );
425 440
426 441 if ( is_wp_error( $temporal_file ) ) {
427 442 return $temporal_file;
@@ -431,9 +446,9 @@
431 446 $should_restore = isset( $media_url ) && ! isset( $media_file ) && $has_original_media;
432 447
433 448 $uploaded_file = $should_restore
434 449 ? $this->restore_original( $media_id, $has_original_media )
435 - : $this->save_temporary_file( $temporal_file, $media_id );
450 + : $this->save_temporary_file( $temporal_file, $media_id, $is_upload );
436 451
437 452 if ( is_wp_error( $uploaded_file ) ) {
438 453 return $uploaded_file;
439 454 }