From 51b9f94d9a183f03a4155b94cd08f316d7f21f0f Mon Sep 17 00:00:00 2001 From: Kim Brown <50059399+kbrown9@users.noreply.github.com> Date: Tue, 14 Jan 2020 22:34:12 -0500 Subject: [PATCH] Jetpack_Media: Delete revision history items before updating attachment metadata Fixes Issue # 14315 When a media file is remotely edited, the Jetpack_Media::edit_media_file() method is called. Within this method, the wp_generate_attachment_metadata() function is called. Then the Jetpack_Media::limit_revision_history() method is called, which determines if the revision history limit has been reached. If the revision history limit has been reached, the Jetpack_Media::delete_media_history_file() method is called. This method calls wp_generated_attachment_metadata(), which overwrites the attachment metadata that was just updated with the attachment metadata of the revision that is about to be deleted. This results in broken image URLs in the wp-admin media library. To fix this, call Jetpack_Media::limit_revision_history() before calling wp_generate_attachment_metadata() in the Jetpack_Media::edit_media_file() method. This will prevent the updated attachment metadata from being overwritten. Finally, fix some PHPCS warnings for comments and whitespace. --- _inc/lib/class.media.php | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/_inc/lib/class.media.php b/_inc/lib/class.media.php index 9e4195800c9c3..1d2c574198daa 100644 --- a/_inc/lib/class.media.php +++ b/_inc/lib/class.media.php @@ -397,22 +397,24 @@ public static function clean_revision_history( $media_id ) { * - preserve original media file * - trace revision history * - * @param number $media_id - media post ID - * @param array $file_array - temporal file + * @param number $media_id - media post ID. + * @param array $file_array - temporal file. * @return {Post|WP_Error} Updated media item or a WP_Error is something went wrong. */ public static function edit_media_file( $media_id, $file_array ) { - $media_item = get_post( $media_id ); + $media_item = get_post( $media_id ); $has_original_media = self::get_original_media( $media_id ); if ( ! $has_original_media ) { + // The first time that the media is updated - // the original media is stored into the revision_history + // the original media is stored into the revision_history. $snapshot = self::get_snapshot( $media_item ); + //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase add_post_meta( $media_id, self::$WP_ORIGINAL_MEDIA, $snapshot, true ); } - // save temporary file in the correct location + // Save temporary file in the correct location. $uploaded_file = self::save_temporary_file( $file_array, $media_id ); if ( is_wp_error( $uploaded_file ) ) { @@ -420,27 +422,30 @@ public static function edit_media_file( $media_id, $file_array ) { return $uploaded_file; } - // revision_history control + // Revision_history control. self::register_revision( $media_item, $uploaded_file, $has_original_media ); - $uploaded_path = $uploaded_file['file']; + $uploaded_path = $uploaded_file['file']; $udpated_mime_type = $uploaded_file['type']; - $was_updated = update_attached_file( $media_id, $uploaded_path ); + $was_updated = update_attached_file( $media_id, $uploaded_path ); if ( ! $was_updated ) { return WP_Error( 'update_error', 'Media update error' ); } + // Check maximum amount of revision_history before updating the attachment metadata. + self::limit_revision_history( $media_id ); + $new_metadata = wp_generate_attachment_metadata( $media_id, $uploaded_path ); wp_update_attachment_metadata( $media_id, $new_metadata ); - // check maximum amount of revision_history - self::limit_revision_history( $media_id ); - - $edited_action = wp_update_post( (object) array( - 'ID' => $media_id, - 'post_mime_type' => $udpated_mime_type - ), true ); + $edited_action = wp_update_post( + (object) array( + 'ID' => $media_id, + 'post_mime_type' => $udpated_mime_type, + ), + true + ); if ( is_wp_error( $edited_action ) ) { return $edited_action; @@ -456,4 +461,3 @@ function clean_revision_history( $media_id ) { }; add_action( 'delete_attachment', 'clean_revision_history' ); -