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

Fix WebP handling when editing images based on WordPress 6.3 change #796

Merged
merged 7 commits into from
Aug 14, 2023
31 changes: 27 additions & 4 deletions modules/images/webp-uploads/image-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_t

foreach ( $old_metadata['sizes'] as $size_name => $size_details ) {
// If the target is 'nothumb', skip generating the 'thumbnail' size.
if ( 'nothumb' === $target && 'thumbnail' === $size_name ) {
if ( webp_uploads_image_edit_thumbnails_separately() && 'nothumb' === $target && 'thumbnail' === $size_name ) {
continue;
}

Expand All @@ -150,7 +150,7 @@ static function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_t
foreach ( $mime_transforms as $targeted_mime ) {
if ( $targeted_mime === $mime_type ) {
// If the target is `thumbnail` make sure it is the only selected size.
if ( 'thumbnail' === $target ) {
if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) {
if ( isset( $metadata['sizes']['thumbnail'] ) ) {
$subsized_images[ $targeted_mime ] = array( 'thumbnail' => $metadata['sizes']['thumbnail'] );
}
Expand Down Expand Up @@ -178,7 +178,7 @@ static function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_t
$extension = $extension[0];

// If the target is `thumbnail` make sure only that size is generated.
if ( 'thumbnail' === $target ) {
if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) {
if ( ! isset( $subsized_images[ $mime_type ]['thumbnail']['file'] ) ) {
continue;
}
Expand Down Expand Up @@ -287,7 +287,7 @@ function webp_uploads_backup_sources( $attachment_id, $data ) {
$target = isset( $_REQUEST['target'] ) ? sanitize_key( $_REQUEST['target'] ) : 'all';

// When an edit to an image is only applied to a thumbnail there's nothing we need to back up.
if ( 'thumbnail' === $target ) {
if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) {
return $data;
}

Expand Down Expand Up @@ -420,3 +420,26 @@ function webp_uploads_restore_image( $attachment_id, $data ) {

return $data;
}

/**
* Compatibility function to check whether editing image thumbnails separately is enabled.
*
* The filter {@see 'image_edit_thumbnails_separately'} was introduced in WordPress 6.3 with default value of `false`,
* for a behavior that previously was always enabled.
*
* @since n.e.x.t
* @see https://core.trac.wordpress.org/ticket/57685
*
* @return bool True if editing image thumbnails is enabled, false otherwise.
*/
function webp_uploads_image_edit_thumbnails_separately() {
if ( version_compare( get_bloginfo( 'version' ), '6.3', '<' ) ) {
return true;
}

/*
* This filter was introduced in WordPress 6.3 with default value `false`,
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* thus changing the behavior of several image editing functions.
*/
return apply_filters( 'image_edit_thumbnails_separately', false );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 7 additions & 7 deletions tests/modules/images/fetchpriority/fetchpriority-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public static function tear_down_after_class() {
parent::tear_down_after_class();
}

public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() {
if ( version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ) {
$this->markTestSkipped( 'Fetchpriority module no longer relevant with WordPress 6.3 shipping with the feature' );
public function set_up() {
parent::set_up();

if ( ! perflab_can_load_module( 'images/fetchpriority' ) ) {
$this->markTestSkipped( 'Fetchpriority module tests irrelevant since available in WordPress core' );
}
}

public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() {
$img = get_image_tag( self::$attachment_id, '', '', '', 'large' );

$this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) );
Expand All @@ -52,10 +56,6 @@ public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading
}

public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() {
if ( version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ) {
$this->markTestSkipped( 'Fetchpriority module no longer relevant with WordPress 6.3 shipping with the feature' );
}

global $wp_query;
global $wp_the_query;
$img = get_image_tag( self::$attachment_id, '', '', '', 'large' );
Expand Down
12 changes: 12 additions & 0 deletions tests/modules/images/webp-uploads/image-edit-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public function it_should_prevent_to_back_up_the_sources_when_the_sources_attrib
* @test
*/
public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thumbnail_is_edited() {
if ( ! webp_uploads_image_edit_thumbnails_separately() ) {
$this->markTestSkipped( 'Editing image thumbnails separately is disabled' );
}

// Create JPEG and WebP.
$this->opt_in_to_jpeg_and_webp();

Expand Down Expand Up @@ -195,6 +199,10 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum
* @test
*/
public function it_should_backup_the_image_when_all_images_except_the_thumbnail_are_updated() {
if ( ! webp_uploads_image_edit_thumbnails_separately() ) {
$this->markTestSkipped( 'Editing image thumbnails separately is disabled' );
}

$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
$metadata = wp_get_attachment_metadata( $attachment_id );

Expand Down Expand Up @@ -236,6 +244,10 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_
* @test
*/
public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() {
if ( ! webp_uploads_image_edit_thumbnails_separately() ) {
$this->markTestSkipped( 'Editing image thumbnails separately is disabled' );
}

// The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test.
add_filter(
'big_image_size_threshold',
Expand Down