From 95e7878a90742395bbf7bd552860a4b548ad89da Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Thu, 14 Nov 2024 17:03:06 +0530 Subject: [PATCH 01/11] Add settings for generate fallback images in all sizes. Add functionality to use added new settings --- plugins/webp-uploads/helper.php | 11 +++ plugins/webp-uploads/hooks.php | 8 ++- plugins/webp-uploads/settings.php | 110 +++++++++++++++++++++++++++++ plugins/webp-uploads/uninstall.php | 1 + 4 files changed, 129 insertions(+), 1 deletion(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 6c8062664c..71f1cef4f5 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -411,6 +411,17 @@ function webp_uploads_is_fallback_enabled(): bool { return (bool) get_option( 'perflab_generate_webp_and_jpeg' ); } +/** + * Checks if the `perflab_generate_fallback_all_sizes` option is enabled. + * + * @since n.e.x.t + * + * @return bool True if the option is enabled, false otherwise. + */ +function webp_uploads_is_fallback_all_sizes_enabled(): bool { + return (bool) get_option( 'perflab_generate_fallback_all_sizes' ); +} + /** * Retrieves the image URL for a specified MIME type from the attachment metadata. * diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index a7a9b308da..c385bc65d8 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -719,7 +719,13 @@ function webp_uploads_get_image_sizes_additional_mime_type_support(): array { ); foreach ( $additional_sizes as $size => $size_details ) { - $allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] ); + if ( isset( $size_details['provide_additional_mime_types'] ) ) { + // Give priority to the 'provide_additional_mime_types' property. + $allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] ); + } else { + // If 'provide_additional_mime_types' is not set, use the fallback all sizes setting. + $allowed_sizes[ $size ] = webp_uploads_is_fallback_all_sizes_enabled(); + } } /** diff --git a/plugins/webp-uploads/settings.php b/plugins/webp-uploads/settings.php index c44d6c452a..3f701ae819 100644 --- a/plugins/webp-uploads/settings.php +++ b/plugins/webp-uploads/settings.php @@ -40,6 +40,18 @@ function webp_uploads_register_media_settings_field(): void { 'show_in_rest' => false, ) ); + + // Add a setting to generate fallback images in all sizes including custom sizes. + register_setting( + 'media', + 'perflab_generate_fallback_all_sizes', + array( + 'type' => 'boolean', + 'default' => false, + 'show_in_rest' => false, + ) + ); + // Add a setting to use the picture element. register_setting( 'media', @@ -96,6 +108,16 @@ function webp_uploads_add_media_settings_fields(): void { array( 'class' => 'perflab-generate-webp-and-jpeg' ) ); + // Add setting field for generating fallback images in all sizes including custom sizes. + add_settings_field( + 'perflab_generate_fallback_all_sizes', + __( 'Output all fallback images', 'webp-uploads' ), + 'webp_uploads_generate_fallback_all_sizes_callback', + 'media', + 'perflab_modern_image_format_settings', + array( 'class' => 'perflab-generate-fallback-all-sizes' ) + ); + // Add picture element support settings field. add_settings_field( 'webp_uploads_use_picture_element', @@ -178,6 +200,94 @@ function webp_uploads_generate_webp_jpeg_setting_callback(): void { + +
> +

+
+
+ +

+
+ + Date: Fri, 15 Nov 2024 13:57:00 +0530 Subject: [PATCH 02/11] Add generator tag for tracking usage of generate all fallback image sizes option --- plugins/webp-uploads/hooks.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index c385bc65d8..bd66d9520b 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -770,6 +770,10 @@ function webp_uploads_modify_webp_quality( int $quality, string $mime_type ): in function webp_uploads_render_generator(): void { // Use the plugin slug as it is immutable. echo '' . "\n"; + + if ( webp_uploads_is_fallback_all_sizes_enabled() ) { + echo '' . "\n"; + } } add_action( 'wp_head', 'webp_uploads_render_generator' ); From fa34edfbbd86ffc71f8d1cfab73559d79b8f0610 Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Thu, 21 Nov 2024 18:29:30 +0530 Subject: [PATCH 03/11] Remove use of empty() --- plugins/webp-uploads/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index ad1475618a..cb5ae8bae2 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -721,7 +721,7 @@ function webp_uploads_get_image_sizes_additional_mime_type_support(): array { foreach ( $additional_sizes as $size => $size_details ) { if ( isset( $size_details['provide_additional_mime_types'] ) ) { // Give priority to the 'provide_additional_mime_types' property. - $allowed_sizes[ $size ] = ! empty( $size_details['provide_additional_mime_types'] ); + $allowed_sizes[ $size ] = (bool) $size_details['provide_additional_mime_types']; } else { // If 'provide_additional_mime_types' is not set, use the fallback all sizes setting. $allowed_sizes[ $size ] = webp_uploads_is_fallback_all_sizes_enabled(); From e8460fe3254ffb41f96af7ac523d25c981de9946 Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Fri, 22 Nov 2024 14:47:16 +0530 Subject: [PATCH 04/11] Change the function, option, variable names --- plugins/webp-uploads/helper.php | 6 +-- plugins/webp-uploads/hooks.php | 4 +- plugins/webp-uploads/settings.php | 76 +++++++++++++++--------------- plugins/webp-uploads/uninstall.php | 2 +- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 745f954b0c..f57563442d 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -412,14 +412,14 @@ function webp_uploads_is_fallback_enabled(): bool { } /** - * Checks if the `perflab_generate_fallback_all_sizes` option is enabled. + * Checks if the `perflab_generate_all_fallback_sizes` option is enabled. * * @since n.e.x.t * * @return bool True if the option is enabled, false otherwise. */ -function webp_uploads_is_fallback_all_sizes_enabled(): bool { - return (bool) get_option( 'perflab_generate_fallback_all_sizes' ); +function webp_uploads_should_generate_all_fallback_sizes(): bool { + return (bool) get_option( 'perflab_generate_all_fallback_sizes' ); } /** diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index cb5ae8bae2..f3d8a6dc2e 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -724,7 +724,7 @@ function webp_uploads_get_image_sizes_additional_mime_type_support(): array { $allowed_sizes[ $size ] = (bool) $size_details['provide_additional_mime_types']; } else { // If 'provide_additional_mime_types' is not set, use the fallback all sizes setting. - $allowed_sizes[ $size ] = webp_uploads_is_fallback_all_sizes_enabled(); + $allowed_sizes[ $size ] = webp_uploads_should_generate_all_fallback_sizes(); } } @@ -771,7 +771,7 @@ function webp_uploads_render_generator(): void { // Use the plugin slug as it is immutable. echo '' . "\n"; - if ( webp_uploads_is_fallback_all_sizes_enabled() ) { + if ( webp_uploads_should_generate_all_fallback_sizes() ) { echo '' . "\n"; } } diff --git a/plugins/webp-uploads/settings.php b/plugins/webp-uploads/settings.php index 3f701ae819..c2d78da889 100644 --- a/plugins/webp-uploads/settings.php +++ b/plugins/webp-uploads/settings.php @@ -44,7 +44,7 @@ function webp_uploads_register_media_settings_field(): void { // Add a setting to generate fallback images in all sizes including custom sizes. register_setting( 'media', - 'perflab_generate_fallback_all_sizes', + 'perflab_generate_all_fallback_sizes', array( 'type' => 'boolean', 'default' => false, @@ -110,9 +110,9 @@ function webp_uploads_add_media_settings_fields(): void { // Add setting field for generating fallback images in all sizes including custom sizes. add_settings_field( - 'perflab_generate_fallback_all_sizes', - __( 'Output all fallback images', 'webp-uploads' ), - 'webp_uploads_generate_fallback_all_sizes_callback', + 'perflab_generate_all_fallback_sizes', + __( 'Generate all fallback image sizes', 'webp-uploads' ), + 'webp_uploads_generate_all_fallback_sizes_callback', 'media', 'perflab_modern_image_format_settings', array( 'class' => 'perflab-generate-fallback-all-sizes' ) @@ -202,34 +202,34 @@ function webp_uploads_generate_webp_jpeg_setting_callback(): void { /** - * Renders the settings field for generating fallback images in all sizes. + * Renders the settings field for generating all fallback image sizes. * * @since n.e.x.t */ -function webp_uploads_generate_fallback_all_sizes_callback(): void { - $fallback_all_sizes_enabled = 1 === (int) get_option( 'perflab_generate_fallback_all_sizes', 0 ); +function webp_uploads_generate_all_fallback_sizes_callback(): void { + $all_fallback_sizes_enabled = 1 === (int) get_option( 'perflab_generate_all_fallback_sizes', 0 ); $fallback_enabled = webp_uploads_is_fallback_enabled(); - $fallback_all_sizes_hidden_id = 'perflab_generate_fallback_all_sizes_hidden'; + $all_fallback_sizes_hidden_id = 'perflab_generate_all_fallback_sizes_hidden'; ?> -
> +
>

-
-