Skip to content

Commit 9581c53

Browse files
committed
Ensure even the full image is a WebP when the module is used in WebP-only mode, and add coverage for both use-cases.
1 parent 8825df7 commit 9581c53

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

modules/images/webp-uploads/load.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,44 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )
103103
wp_update_attachment_metadata( $attachment_id, $metadata );
104104
}
105105

106+
// If the original MIME type should not be generated/used, override the main image
107+
// with the first MIME type image that actually should be generated. In that case,
108+
// the original should be backed up.
109+
if (
110+
! in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) &&
111+
isset( $valid_mime_transforms[ $mime_type ][0] ) &&
112+
isset( $allowed_mimes[ $mime_type ] )
113+
) {
114+
$valid_mime_type = $valid_mime_transforms[ $mime_type ][0];
115+
116+
// Only do the replacement if the attachment file is still set to the original MIME type one,
117+
// and if there is a possible replacement source.
118+
$file_data = wp_check_filetype( $metadata['file'], array( $allowed_mimes[ $mime_type ] => $mime_type ) );
119+
if ( $file_data['type'] === $mime_type && isset( $metadata['sources'][ $valid_mime_type ] ) ) {
120+
$saved_data = array(
121+
'path' => trailingslashit( $original_directory ) . $metadata['sources'][ $valid_mime_type ]['file'],
122+
'width' => $metadata['width'],
123+
'height' => $metadata['height'],
124+
);
125+
126+
$original_image = wp_get_original_image_path( $attachment_id );
127+
128+
// If WordPress already modified the original itself, keep the original and discard WordPress's generated version.
129+
if ( ! empty( $metadata['original_image'] ) ) {
130+
$uploadpath = wp_get_upload_dir();
131+
wp_delete_file_from_directory( get_attached_file( $attachment_id ), $uploadpath['basedir'] );
132+
}
133+
134+
// Replace the attached file with the custom MIME type version.
135+
$metadata = _wp_image_meta_replace_original( $saved_data, $original_image, $metadata, $attachment_id );
136+
137+
// Unset sources entry for the original MIME type, then save (to avoid inconsistent data
138+
// in case of an error after this logic).
139+
unset( $metadata['sources'][ $mime_type ] );
140+
wp_update_attachment_metadata( $attachment_id, $metadata );
141+
}
142+
}
143+
106144
// Make sure we have some sizes to work with, otherwise avoid any work.
107145
if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) {
108146
return $metadata;

tests/modules/images/webp-uploads/helper-tests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,14 @@ public function it_should_prevent_to_create_an_image_size_when_attached_file_doe
179179
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'
180180
);
181181
$file = get_attached_file( $attachment_id );
182+
$original_file = wp_get_original_image_path( $attachment_id );
182183

183184
$this->assertFileExists( $file );
185+
$this->assertFileExists( $original_file );
184186
wp_delete_file( $file );
187+
wp_delete_file( $original_file );
185188
$this->assertFileDoesNotExist( $file );
189+
$this->assertFileDoesNotExist( $original_file );
186190

187191
$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' );
188192
$this->assertWPError( $result );

tests/modules/images/webp-uploads/load-tests.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,99 @@ public function set_up() {
1717
}
1818

1919
/**
20-
* Not create the original mime type for JPEG images.
20+
* Don't create the original mime type for JPEG images.
2121
*
2222
* @test
2323
*/
2424
public function it_should_not_create_the_original_mime_type_for_jpeg_images() {
2525
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
2626

27+
// There should be a WebP source, but no JPEG source for the full image.
2728
$this->assertImageHasSource( $attachment_id, 'image/webp' );
2829
$this->assertImageNotHasSource( $attachment_id, 'image/jpeg' );
2930

3031
$metadata = wp_get_attachment_metadata( $attachment_id );
32+
33+
// The full image should be a WebP.
3134
$this->assertArrayHasKey( 'file', $metadata );
35+
$this->assertStringEndsWith( $metadata['sources']['image/webp']['file'], $metadata['file'] );
36+
$this->assertStringEndsWith( $metadata['sources']['image/webp']['file'], get_attached_file( $attachment_id ) );
37+
38+
// The original JPEG should be backed up.
39+
$this->assertStringEndsWith( '.jpg', wp_get_original_image_path( $attachment_id ) );
40+
41+
// For compatibility reasons, the post MIME type should remain JPEG.
42+
$this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) );
3243

44+
// There should be a WebP source, but no JPEG source for all sizes.
3345
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
3446
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
3547
$this->assertImageNotHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
3648
}
3749
}
3850

3951
/**
40-
* Create the original mime type for WEBP images.
52+
* Create the original mime type for WebP images.
4153
*
4254
* @test
4355
*/
4456
public function it_should_create_the_original_mime_type_as_well_with_all_the_available_sources_for_the_specified_mime() {
4557
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/balloons.webp' );
4658

59+
// There should be a WebP source, but no JPEG source for the full image.
4760
$this->assertImageNotHasSource( $attachment_id, 'image/jpeg' );
4861
$this->assertImageHasSource( $attachment_id, 'image/webp' );
4962

5063
$metadata = wp_get_attachment_metadata( $attachment_id );
64+
65+
// The full image should be a WebP.
5166
$this->assertArrayHasKey( 'file', $metadata );
5267
$this->assertStringEndsWith( $metadata['sources']['image/webp']['file'], $metadata['file'] );
68+
$this->assertStringEndsWith( $metadata['sources']['image/webp']['file'], get_attached_file( $attachment_id ) );
69+
70+
// The post MIME type should be WebP.
71+
$this->assertSame( 'image/webp', get_post_mime_type( $attachment_id ) );
5372

73+
// There should be a WebP source, but no JPEG source for all sizes.
5474
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
5575
$this->assertImageNotHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
5676
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
5777
}
5878
}
5979

6080
/**
61-
* Not create the sources property if no transform is provided
81+
* Create JPEG and WebP for JPEG images, if opted in.
82+
*
83+
* @test
84+
*/
85+
public function it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in() {
86+
$this->opt_in_to_jpeg_and_webp();
87+
88+
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
89+
90+
// There should be JPEG and WebP sources for the full image.
91+
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
92+
$this->assertImageHasSource( $attachment_id, 'image/webp' );
93+
94+
$metadata = wp_get_attachment_metadata( $attachment_id );
95+
96+
// The full image should be a JPEG.
97+
$this->assertArrayHasKey( 'file', $metadata );
98+
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $metadata['file'] );
99+
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], get_attached_file( $attachment_id ) );
100+
101+
// The post MIME type should be JPEG.
102+
$this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) );
103+
104+
// There should be JPEG and WebP sources for all sizes.
105+
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
106+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
107+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
108+
}
109+
}
110+
111+
/**
112+
* Don't create the sources property if no transform is provided.
62113
*
63114
* @test
64115
*/

0 commit comments

Comments
 (0)