Skip to content

Commit

Permalink
Merge branch 'Automattic:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
clementduncan authored Aug 14, 2023
2 parents 83dfdbb + e1bd26b commit 80eefa7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
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 @@ -185,7 +194,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 @@ -197,8 +206,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 @@ -211,7 +220,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 @@ -265,11 +274,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 @@ -315,7 +324,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;

// This is based on WP_Image_Editor_GD::multi_resize() and others.
foreach ( $sizes as $size => $size_data ) {
Expand Down Expand Up @@ -347,6 +356,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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "regenerate-thumbnails",
"version": "3.1.5",
"version": "3.1.6",
"description": "A WordPress plugin for regenerating the thumbnails for one or more of your image uploads. Useful when changing their sizes or your theme.",
"homepage": "https://alex.blog/wordpress-plugins/regenerate-thumbnails/",
"author": "Alex Mills (Viper007Bond)",
Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Contributors: Viper007Bond
Tags: thumbnail, thumbnails, post thumbnail, post thumbnails
Requires at least: 4.7
Tested up to: 5.7
Tested up to: 6.3
Requires PHP: 5.2.4
Stable tag: 3.1.5
Stable tag: 3.1.6
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -72,9 +72,16 @@ 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.
* Fix: Don't delete all thumbnails when deleting old unregistered thumbnails size.

= Version 3.1.5 =

* Fix: Don't overwrite 'All X Attachment' button label with featured images count.
* Tested successfully with PHP 8.1.
* Tested successfully with PHP 8.2.

= Version 3.1.4 =

Expand Down
4 changes: 2 additions & 2 deletions regenerate-thumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Plugin Name: Regenerate Thumbnails
Description: Regenerate the thumbnails for one or more of your image uploads. Useful when changing their sizes or your theme.
Plugin URI: https://alex.blog/wordpress-plugins/regenerate-thumbnails/
Version: 3.1.5
Version: 3.1.6
Author: Alex Mills (Viper007Bond)
Author URI: https://alex.blog/
Text Domain: regenerate-thumbnails
Expand Down Expand Up @@ -40,7 +40,7 @@ class RegenerateThumbnails {
*
* @var string
*/
public $version = '3.1.5';
public $version = '3.1.6';

/**
* The menu ID of this plugin, as returned by add_management_page().
Expand Down

0 comments on commit 80eefa7

Please sign in to comment.