diff --git a/modules/images/webp-uploads/helper.php b/modules/images/webp-uploads/helper.php index 00e7eebbea..bf1f6f4b5d 100644 --- a/modules/images/webp-uploads/helper.php +++ b/modules/images/webp-uploads/helper.php @@ -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. diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 89dec0cf21..40e1fdab78 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -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 diff --git a/modules/images/webp-uploads/settings.php b/modules/images/webp-uploads/settings.php new file mode 100644 index 0000000000..6e66b0f84e --- /dev/null +++ b/modules/images/webp-uploads/settings.php @@ -0,0 +1,77 @@ + '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() { + ?> + + + +

+ + + 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 * diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index df245d017d..995eae7cc4 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -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. * diff --git a/uninstall.php b/uninstall.php index 2c30066dde..bb349f9a87 100644 --- a/uninstall.php +++ b/uninstall.php @@ -38,4 +38,5 @@ */ function perflab_delete_plugin_option() { delete_option( 'perflab_modules_settings' ); + delete_option( 'perflab_generate_webp_and_jpeg' ); }