diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index 8d3b02373a..500782178f 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -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; } @@ -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'] ); } @@ -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; } @@ -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; } @@ -420,3 +420,23 @@ 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 is documented in wp-admin/includes/image-edit.php */ + return (bool) apply_filters( 'image_edit_thumbnails_separately', false ); +} diff --git a/modules/images/webp-uploads/readme.txt b/modules/images/webp-uploads/readme.txt index 76d86574bd..1abf237937 100644 --- a/modules/images/webp-uploads/readme.txt +++ b/modules/images/webp-uploads/readme.txt @@ -4,7 +4,7 @@ Contributors: wordpressdotorg Requires at least: 6.1 Tested up to: 6.3 Requires PHP: 5.6 -Stable tag: 1.0.1 +Stable tag: 1.0.2 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Tags: performance, images, webp @@ -58,6 +58,10 @@ By default, the WebP Uploads plugin will only generate WebP versions of the imag == Changelog == += 1.0.2 = + +* Fix WebP handling when editing images based on WordPress 6.3 change. ([796](https://github.com/WordPress/performance/pull/796)) + = 1.0.1 = * Bump tested up to version to 6.3. ([772](https://github.com/WordPress/performance/pull/772)) diff --git a/plugins.json b/plugins.json index acbc574837..9753f694a5 100644 --- a/plugins.json +++ b/plugins.json @@ -5,6 +5,6 @@ }, "images/webp-uploads": { "slug": "webp-uploads", - "version": "1.0.1" + "version": "1.0.2" } } diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index f60357b981..62e4732300 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -37,6 +37,14 @@ public static function tear_down_after_class() { parent::tear_down_after_class(); } + 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' ); diff --git a/tests/modules/images/webp-uploads/image-edit-tests.php b/tests/modules/images/webp-uploads/image-edit-tests.php index 1c2a100c9d..80b1ec5cdb 100644 --- a/tests/modules/images/webp-uploads/image-edit-tests.php +++ b/tests/modules/images/webp-uploads/image-edit-tests.php @@ -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(); @@ -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 ); @@ -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',