Skip to content

Commit

Permalink
Jetpack_Media: Delete revision history items before updating attachme…
Browse files Browse the repository at this point in the history
…nt 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.
  • Loading branch information
kbrown9 committed Jan 15, 2020
1 parent e83d91e commit 51b9f94
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions _inc/lib/class.media.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,50 +397,55 @@ 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 ) ) {
self::remove_tmp_file( $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;
Expand All @@ -456,4 +461,3 @@ function clean_revision_history( $media_id ) {
};

add_action( 'delete_attachment', 'clean_revision_history' );

0 comments on commit 51b9f94

Please sign in to comment.