Skip to content

Commit

Permalink
Merge pull request #537 from WordPress/enhancement/525-checkbox-multi…
Browse files Browse the repository at this point in the history
…-mime-setting

Add checkbox to Settings > Media to control whether to generate JPEG in addition to WebP
  • Loading branch information
felixarntz authored Oct 7, 2022
2 parents 950daa7 + 90cac1f commit 55213ba
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function webp_uploads_get_upload_image_mime_transforms() {
'image/webp' => array( 'image/webp' ),
);

// Check setting for whether to generate both JPEG and WebP.
if ( true === (bool) get_option( 'perflab_generate_webp_and_jpeg' ) ) {
$default_transforms = array(
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
'image/webp' => array( 'image/webp', 'image/jpeg' ),
);
}

/**
* Filter to allow the definition of a custom mime types, in which a defined mime type
* can be transformed and provide a wide range of mime types.
Expand Down
1 change: 1 addition & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_once __DIR__ . '/helper.php';
require_once __DIR__ . '/rest-api.php';
require_once __DIR__ . '/image-edit.php';
require_once __DIR__ . '/settings.php';

/**
* Hook called by `wp_generate_attachment_metadata` to create the `sources` property for every image
Expand Down
77 changes: 77 additions & 0 deletions modules/images/webp-uploads/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Settings for the WebP Uploads module.
*
* @package performance-lab
* @since n.e.x.t
*/

/**
* Registers setting for generating both JPEG and WebP versions for image uploads.
*
* @since n.e.x.t
*/
function webp_uploads_register_media_settings_field() {
register_setting(
'media',
'perflab_generate_webp_and_jpeg',
array(
'type' => 'boolean',
'default' => false,
'show_in_rest' => false,
)
);
}
add_action( 'init', 'webp_uploads_register_media_settings_field' );

/**
* Adds media settings field for the 'perflab_generate_webp_and_jpeg' setting.
*
* @since n.e.x.t
*/
function webp_uploads_add_media_settings_field() {
// Add settings field.
add_settings_field(
'perflab_generate_webp_and_jpeg',
__( 'Generate WebP and JPEG', 'performance-lab' ),
'webp_uploads_generate_webp_jpeg_setting_callback',
'media',
'uploads',
array( 'class' => 'perflab-generate-webp-and-jpeg' )
);
}
add_action( 'admin_init', 'webp_uploads_add_media_settings_field' );

/**
* Renders the settings field for the 'perflab_generate_webp_and_jpeg' setting.
*
* @since n.e.x.t
*/
function webp_uploads_generate_webp_jpeg_setting_callback() {
?>
</td>
<td class="td-full">
<label for="perflab_generate_webp_and_jpeg">
<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' ) ); ?> />
<?php esc_html_e( 'Generate JPEG files in addition to WebP', 'performance-lab' ); ?>
</label>
<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>
<?php
}

/**
* Adds custom style for media settings.
*
* @since n.e.x.t
*/
function webp_uploads_media_setting_style() {
?>
<style>
.form-table .perflab-generate-webp-and-jpeg th,
.form-table .perflab-generate-webp-and-jpeg td:not(.td-full) {
display: none;
}
</style>
<?php
}
add_action( 'admin_head-options-media.php', 'webp_uploads_media_setting_style' );
22 changes: 22 additions & 0 deletions tests/modules/images/webp-uploads/helper-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ function () {
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) ), $transforms );
}

/**
* Returns JPG and WebP transforms array when perflab_generate_webp_and_jpeg option is true.
*
* @test
*/
public function it_should_return_jpeg_and_webp_transforms_when_option_generate_webp_and_jpeg_set() {
remove_all_filters( 'webp_uploads_get_upload_image_mime_transforms' );

update_option( 'perflab_generate_webp_and_jpeg', true );

$transforms = webp_uploads_get_upload_image_mime_transforms();

$this->assertIsArray( $transforms );
$this->assertSame(
array(
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
'image/webp' => array( 'image/webp', 'image/jpeg' ),
),
$transforms
);
}

/**
* @dataProvider data_provider_image_filesize
*
Expand Down
31 changes: 31 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ public function it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in() {
}
}

/**
* Create JPEG and WebP for JPEG images, if perflab_generate_webp_and_jpeg option set.
*
* @test
*/
public function it_should_create_jpeg_and_webp_for_jpeg_images_if_generate_webp_and_jpeg_set() {
update_option( 'perflab_generate_webp_and_jpeg', true );

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

// There should be JPEG and WebP sources for the full image.
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
$this->assertImageHasSource( $attachment_id, 'image/webp' );

$metadata = wp_get_attachment_metadata( $attachment_id );

// The full image should be a JPEG.
$this->assertArrayHasKey( 'file', $metadata );
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $metadata['file'] );
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], get_attached_file( $attachment_id ) );

// The post MIME type should be JPEG.
$this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) );

// There should be JPEG and WebP sources for all sizes.
foreach ( array_keys( $metadata['sizes'] ) as $size_name ) {
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
}
}

/**
* Don't create the sources property if no transform is provided.
*
Expand Down
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
*/
function perflab_delete_plugin_option() {
delete_option( 'perflab_modules_settings' );
delete_option( 'perflab_generate_webp_and_jpeg' );
}

0 comments on commit 55213ba

Please sign in to comment.