Skip to content

Commit d2edbef

Browse files
Copilotswissspidy
andauthored
Use wp_update_image_subsizes() for --only-missing on WP 5.3+ (#236)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent f922e4a commit d2edbef

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

features/media-regenerate.feature

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,50 @@ Feature: Regenerate WordPress attachments
869869
{SCALED_CHECKSUM}
870870
"""
871871

872+
@require-wp-5.3
873+
Scenario: Regenerating only-missing sizes on WP 5.3+ uses wp_update_image_subsizes and does not recreate the scaled version
874+
Given download:
875+
| path | url |
876+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
877+
And I run `wp option update uploads_use_yearmonth_folders 0`
878+
879+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported attachment" --porcelain`
880+
Then save STDOUT as {ATTACHMENT_ID}
881+
And the wp-content/uploads/large-image-scaled.jpg file should exist
882+
And the wp-content/uploads/large-image-300x225.jpg file should exist
883+
884+
# Save a checksum of the scaled image before any regeneration.
885+
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
886+
Then save STDOUT as {SCALED_CHECKSUM}
887+
888+
# Add a new image size and a filter that produces drastically different output quality
889+
# if the scaled image were regenerated from scratch.
890+
Given a wp-content/mu-plugins/media-settings.php file:
891+
"""
892+
<?php
893+
add_action( 'after_setup_theme', function(){
894+
add_image_size( 'test1', 400, 300, true );
895+
});
896+
// If the scaled image is regenerated, its checksum will differ because of this low quality.
897+
add_filter( 'jpeg_quality', function() { return 1; } );
898+
"""
899+
900+
When I run `wp media regenerate {ATTACHMENT_ID} --only-missing --yes`
901+
Then STDOUT should contain:
902+
"""
903+
1/1 Regenerated thumbnails for "My imported attachment"
904+
"""
905+
And the wp-content/uploads/large-image-400x300.jpg file should exist
906+
907+
# Verify the scaled image was NOT regenerated by wp_update_image_subsizes()
908+
# (checksum should be unchanged, because wp_update_image_subsizes() only generates
909+
# the missing sub-sizes and does not recreate the scaled/attached file).
910+
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
911+
Then STDOUT should be:
912+
"""
913+
{SCALED_CHECKSUM}
914+
"""
915+
872916
@require-wp-4.7.3 @require-extension-imagick
873917
Scenario: Regenerate a specific image size for a PDF attachment
874918
Given download:

src/Media_Command.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,31 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
775775
}
776776
$site_icon_filter = $this->add_site_icon_filter( $id );
777777

778+
// On WP 5.3+, for the --only-missing case (no specific image sizes, not a PDF, and not a
779+
// site-icon attachment), prefer wp_update_image_subsizes() which only generates sub-sizes
780+
// that are absent from the attachment metadata and saves metadata incrementally after each
781+
// sub-size, so partial progress is preserved if the server runs out of resources.
782+
$can_use_wp53_subsizes = $only_missing && ! $image_sizes && ! $is_pdf && ! $site_icon_filter
783+
&& function_exists( 'wp_get_missing_image_subsizes' ) && function_exists( 'wp_update_image_subsizes' );
784+
if ( $can_use_wp53_subsizes ) {
785+
$missing_sizes = wp_get_missing_image_subsizes( $id );
786+
if ( ! empty( $missing_sizes ) ) {
787+
$result = wp_update_image_subsizes( $id );
788+
if ( is_wp_error( $result ) ) {
789+
WP_CLI::warning( sprintf( '%s (ID %d)', $result->get_error_message(), $id ) );
790+
WP_CLI::log( "$progress Couldn't regenerate thumbnails for $att_desc." );
791+
++$errors;
792+
return;
793+
}
794+
WP_CLI::log( "$progress Regenerated thumbnails for $att_desc." );
795+
++$successes;
796+
return;
797+
}
798+
// $missing_sizes is empty but needs_regeneration() returned true, which means some
799+
// thumbnail files are physically missing from disk even though the metadata is intact.
800+
// Fall through to the wp_generate_attachment_metadata() path below.
801+
}
802+
778803
// When regenerating specific image size(s), use the file that WordPress normally
779804
// serves (the scaled version for big images), not the original pre-scaled file.
780805
// This prevents wp_generate_attachment_metadata() from re-creating the scaled

0 commit comments

Comments
 (0)