Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep reference to metadata before regenerating images. #143

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions includes/class-regeneratethumbnails-regenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class RegenerateThumbnails_Regenerator {
*/
public $skipped_thumbnails = array();

/**
* The metadata for the attachment before the regeneration process starts.
*
* @since 3.1.6
*
* @var array
*/
private $old_metadata = array();

/**
* Generates an instance of this class after doing some setup.
*
Expand Down Expand Up @@ -183,7 +192,7 @@ public function regenerate( $args = array() ) {
return $fullsizepath;
}

$old_metadata = wp_get_attachment_metadata( $this->attachment->ID );
$this->old_metadata = wp_get_attachment_metadata( $this->attachment->ID );

if ( $args['only_regenerate_missing_thumbnails'] ) {
add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 2 );
Expand All @@ -195,8 +204,8 @@ public function regenerate( $args = array() ) {
if ( $args['only_regenerate_missing_thumbnails'] ) {
// Thumbnail sizes that existed were removed and need to be added back to the metadata.
foreach ( $this->skipped_thumbnails as $skipped_thumbnail ) {
if ( ! empty( $old_metadata['sizes'][ $skipped_thumbnail ] ) ) {
$new_metadata['sizes'][ $skipped_thumbnail ] = $old_metadata['sizes'][ $skipped_thumbnail ];
if ( ! empty( $this->old_metadata['sizes'][ $skipped_thumbnail ] ) ) {
$new_metadata['sizes'][ $skipped_thumbnail ] = $this->old_metadata['sizes'][ $skipped_thumbnail ];
}
}
$this->skipped_thumbnails = array();
Expand All @@ -209,7 +218,7 @@ public function regenerate( $args = array() ) {
if ( $args['delete_unregistered_thumbnail_files'] ) {
// Delete old sizes that are still in the metadata.
$intermediate_image_sizes = get_intermediate_image_sizes();
foreach ( $old_metadata['sizes'] as $old_size => $old_size_data ) {
foreach ( $this->old_metadata['sizes'] as $old_size => $old_size_data ) {
if ( in_array( $old_size, $intermediate_image_sizes ) ) {
continue;
}
Expand Down Expand Up @@ -263,11 +272,11 @@ public function regenerate( $args = array() ) {

wp_delete_file( $wp_upload_dir . $file );
}
} elseif ( ! empty( $old_metadata ) && ! empty( $old_metadata['sizes'] ) && is_array( $old_metadata['sizes'] ) ) {
} elseif ( ! empty( $this->old_metadata ) && ! empty( $this->old_metadata['sizes'] ) && is_array( $this->old_metadata['sizes'] ) ) {
// If not deleting, rename any size conflicts to avoid them being lost if the file still exists.
foreach ( $old_metadata['sizes'] as $old_size => $old_size_data ) {
foreach ( $this->old_metadata['sizes'] as $old_size => $old_size_data ) {
if ( empty( $new_metadata['sizes'][ $old_size ] ) ) {
$new_metadata['sizes'][ $old_size ] = $old_metadata['sizes'][ $old_size ];
$new_metadata['sizes'][ $old_size ] = $this->old_metadata['sizes'][ $old_size ];
continue;
}

Expand Down Expand Up @@ -313,7 +322,7 @@ public function filter_image_sizes_to_only_missing_thumbnails( $sizes, $fullsize
return $sizes;
}

$metadata = wp_get_attachment_metadata( $this->attachment->ID );
$metadata = $this->old_metadata;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a quite silly thing but, WDYT about resetting the old_metadata property after the remove_filter call for this filter?

The reason I'm asking this is that, while the property itself is private, it keeps the internal state of the attachment being processed. Hence, if a third party for some reason decides to call this method (which it shouldn't, but oh well, people are people.. and technically better safe than sorry), then it will find unexpected results because of the internal state saved in old_metadata.

Based on this, my suggestion would be to just reset old_metadata to an empty array, and then check here if this variable is empty, and if it is, we just return early with the $sizes variable, without doing anything else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Fernando, initially I thought it made a lot of sense. But when implementing the changes I realised that this class i already quite dependent on its internal status (like attachment->ID, fullsizepath and so on).

So for the moment I am leaving it as it is, so we don't add more complexity to the logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, very good point indeed, I didn't see that we already have a bunch of states already in the properties of the class.

Thanks for the heads up! ;)


// This is based on WP_Image_Editor_GD::multi_resize() and others.
foreach ( $sizes as $size => $size_data ) {
Expand Down Expand Up @@ -345,6 +354,7 @@ public function filter_image_sizes_to_only_missing_thumbnails( $sizes, $fullsize
$size_data['crop']
);


// The false check filters out thumbnails that would be larger than the fullsize image.
// The size comparison makes sure that the size is also correct.
if (
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ This plugin does not log nor transmit any user data. Infact it doesn't even do a

== ChangeLog ==

= Version 3.1.6 =

* Fix: Respect "Skip regenerating existing correctly sized thumbnails" setting.

= Version 3.1.5 =

* Fix: Don't overwrite 'All X Attachment' button label with featured images count.
Expand Down