diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index 84d93a3afa..20d0f20ca4 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -436,12 +436,11 @@ function webp_uploads_remove_sources_files( int $attachment_id ): void { } $original_mime_from_post = get_post_mime_type( $attachment_id ); - $original_mime_from_file = wp_check_filetype( $file )['type']; // Delete full sizes mime types. foreach ( $metadata['sources'] as $mime => $properties ) { // Don't remove the image with the same mime type as the original image as this would be removed by WordPress. - if ( $mime === $original_mime_from_post || $mime === $original_mime_from_file ) { + if ( $mime === $original_mime_from_post ) { continue; } diff --git a/plugins/webp-uploads/tests/test-load.php b/plugins/webp-uploads/tests/test-load.php index d4c954411e..c77a436553 100644 --- a/plugins/webp-uploads/tests/test-load.php +++ b/plugins/webp-uploads/tests/test-load.php @@ -1310,4 +1310,63 @@ public function test_webp_uploads_update_featured_image_picture_element_enabled( $featured_image = get_the_post_thumbnail( $post_id ); $this->assertStringStartsWith( 'markTestSkipped( "Mime type $mime_type is not supported." ); + } + + $this->set_image_output_type( $image_type ); + + $attachment_id = self::factory()->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' + ); + + $file = get_attached_file( $attachment_id, true ); + $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + + $this->assertIsString( $file ); + $this->assertFileExists( $file ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + // Verify the full-size source was created and the file was replaced. + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertArrayHasKey( $mime_type, $metadata['sources'] ); + $full_size_source_file = path_join( $dirname, $metadata['sources'][ $mime_type ]['file'] ); + $this->assertFileExists( $full_size_source_file ); + + // Confirm the attached file's MIME type differs from the post MIME type + // (i.e. the plugin replaced the file). + $this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) ); + $this->assertSame( $mime_type, wp_check_filetype( $file )['type'] ); + + // Call the plugin's deletion function directly to verify it properly + // cleans up its own source files without relying on WordPress's cleanup. + webp_uploads_remove_sources_files( $attachment_id ); + + // Verify the full-size source file was deleted by the plugin. + $this->assertFileDoesNotExist( + $full_size_source_file, + "The full-size $image_type source file should have been deleted by the plugin." + ); + } }