Skip to content

Commit 55213ba

Browse files
authored
Merge pull request #537 from WordPress/enhancement/525-checkbox-multi-mime-setting
Add checkbox to Settings > Media to control whether to generate JPEG in addition to WebP
2 parents 950daa7 + 90cac1f commit 55213ba

6 files changed

Lines changed: 140 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ function webp_uploads_get_upload_image_mime_transforms() {
2020
'image/webp' => array( 'image/webp' ),
2121
);
2222

23+
// Check setting for whether to generate both JPEG and WebP.
24+
if ( true === (bool) get_option( 'perflab_generate_webp_and_jpeg' ) ) {
25+
$default_transforms = array(
26+
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
27+
'image/webp' => array( 'image/webp', 'image/jpeg' ),
28+
);
29+
}
30+
2331
/**
2432
* Filter to allow the definition of a custom mime types, in which a defined mime type
2533
* can be transformed and provide a wide range of mime types.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_once __DIR__ . '/helper.php';
1515
require_once __DIR__ . '/rest-api.php';
1616
require_once __DIR__ . '/image-edit.php';
17+
require_once __DIR__ . '/settings.php';
1718

1819
/**
1920
* Hook called by `wp_generate_attachment_metadata` to create the `sources` property for every image
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Settings for the WebP Uploads module.
4+
*
5+
* @package performance-lab
6+
* @since n.e.x.t
7+
*/
8+
9+
/**
10+
* Registers setting for generating both JPEG and WebP versions for image uploads.
11+
*
12+
* @since n.e.x.t
13+
*/
14+
function webp_uploads_register_media_settings_field() {
15+
register_setting(
16+
'media',
17+
'perflab_generate_webp_and_jpeg',
18+
array(
19+
'type' => 'boolean',
20+
'default' => false,
21+
'show_in_rest' => false,
22+
)
23+
);
24+
}
25+
add_action( 'init', 'webp_uploads_register_media_settings_field' );
26+
27+
/**
28+
* Adds media settings field for the 'perflab_generate_webp_and_jpeg' setting.
29+
*
30+
* @since n.e.x.t
31+
*/
32+
function webp_uploads_add_media_settings_field() {
33+
// Add settings field.
34+
add_settings_field(
35+
'perflab_generate_webp_and_jpeg',
36+
__( 'Generate WebP and JPEG', 'performance-lab' ),
37+
'webp_uploads_generate_webp_jpeg_setting_callback',
38+
'media',
39+
'uploads',
40+
array( 'class' => 'perflab-generate-webp-and-jpeg' )
41+
);
42+
}
43+
add_action( 'admin_init', 'webp_uploads_add_media_settings_field' );
44+
45+
/**
46+
* Renders the settings field for the 'perflab_generate_webp_and_jpeg' setting.
47+
*
48+
* @since n.e.x.t
49+
*/
50+
function webp_uploads_generate_webp_jpeg_setting_callback() {
51+
?>
52+
</td>
53+
<td class="td-full">
54+
<label for="perflab_generate_webp_and_jpeg">
55+
<input name="perflab_generate_webp_and_jpeg" type="checkbox" id="perflab_generate_webp_and_jpeg" aria-describedby="perflab_generate_webp_and_jpeg_description" value="1"<?php checked( '1', get_option( 'perflab_generate_webp_and_jpeg' ) ); ?> />
56+
<?php esc_html_e( 'Generate JPEG files in addition to WebP', 'performance-lab' ); ?>
57+
</label>
58+
<p class="description" id="perflab_generate_webp_and_jpeg_description"><?php esc_html_e( 'Enabling JPEG in addition to WebP can improve compatibility, but will effectively double the filesystem storage use of your images.', 'performance-lab' ); ?></p>
59+
<?php
60+
}
61+
62+
/**
63+
* Adds custom style for media settings.
64+
*
65+
* @since n.e.x.t
66+
*/
67+
function webp_uploads_media_setting_style() {
68+
?>
69+
<style>
70+
.form-table .perflab-generate-webp-and-jpeg th,
71+
.form-table .perflab-generate-webp-and-jpeg td:not(.td-full) {
72+
display: none;
73+
}
74+
</style>
75+
<?php
76+
}
77+
add_action( 'admin_head-options-media.php', 'webp_uploads_media_setting_style' );

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,28 @@ function () {
420420
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) ), $transforms );
421421
}
422422

423+
/**
424+
* Returns JPG and WebP transforms array when perflab_generate_webp_and_jpeg option is true.
425+
*
426+
* @test
427+
*/
428+
public function it_should_return_jpeg_and_webp_transforms_when_option_generate_webp_and_jpeg_set() {
429+
remove_all_filters( 'webp_uploads_get_upload_image_mime_transforms' );
430+
431+
update_option( 'perflab_generate_webp_and_jpeg', true );
432+
433+
$transforms = webp_uploads_get_upload_image_mime_transforms();
434+
435+
$this->assertIsArray( $transforms );
436+
$this->assertSame(
437+
array(
438+
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
439+
'image/webp' => array( 'image/webp', 'image/jpeg' ),
440+
),
441+
$transforms
442+
);
443+
}
444+
423445
/**
424446
* @dataProvider data_provider_image_filesize
425447
*

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ public function it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in() {
108108
}
109109
}
110110

111+
/**
112+
* Create JPEG and WebP for JPEG images, if perflab_generate_webp_and_jpeg option set.
113+
*
114+
* @test
115+
*/
116+
public function it_should_create_jpeg_and_webp_for_jpeg_images_if_generate_webp_and_jpeg_set() {
117+
update_option( 'perflab_generate_webp_and_jpeg', true );
118+
119+
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
120+
121+
// There should be JPEG and WebP sources for the full image.
122+
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
123+
$this->assertImageHasSource( $attachment_id, 'image/webp' );
124+
125+
$metadata = wp_get_attachment_metadata( $attachment_id );
126+
127+
// The full image should be a JPEG.
128+
$this->assertArrayHasKey( 'file', $metadata );
129+
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $metadata['file'] );
130+
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], get_attached_file( $attachment_id ) );
131+
132+
// The post MIME type should be JPEG.
133+
$this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) );
134+
135+
// There should be JPEG and WebP sources for all sizes.
136+
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
137+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
138+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
139+
}
140+
}
141+
111142
/**
112143
* Don't create the sources property if no transform is provided.
113144
*

‎uninstall.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@
3838
*/
3939
function perflab_delete_plugin_option() {
4040
delete_option( 'perflab_modules_settings' );
41+
delete_option( 'perflab_generate_webp_and_jpeg' );
4142
}

0 commit comments

Comments
 (0)