Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable end user opt-in to generate all sizes in fallback format #1689

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
95e7878
Add settings for generate fallback images in all sizes. Add functiona…
b1ink0 Nov 14, 2024
8473bb8
Add generator tag for tracking usage of generate all fallback image s…
b1ink0 Nov 15, 2024
536c799
Merge branch 'WordPress:trunk' into add/option-to-generate-all-image-…
b1ink0 Nov 21, 2024
fa34edf
Remove use of empty()
b1ink0 Nov 21, 2024
e8460fe
Change the function, option, variable names
b1ink0 Nov 22, 2024
39e6004
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Nov 22, 2024
e297363
Merge branch 'add/option-to-generate-all-image-sizes' of https://gith…
b1ink0 Nov 22, 2024
6ddd884
Move logic to separate function
b1ink0 Nov 22, 2024
b80a9b0
Remove generator tag
b1ink0 Nov 25, 2024
d477128
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Nov 26, 2024
9eaaeec
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Nov 28, 2024
4fdbcc0
Fix fallback option retrieval and clean up docblock in webp uploads p…
b1ink0 Dec 2, 2024
3a64713
Use existing function for getting option value
b1ink0 Dec 2, 2024
546e2d7
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Dec 2, 2024
2747d1b
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Dec 3, 2024
36fbfd7
Update docblock for webp_uploads_should_generate_all_fallback_sizes f…
b1ink0 Dec 3, 2024
e29a33e
Add test case for generating fallback images for all sizes option ena…
b1ink0 Dec 4, 2024
e4601ae
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Dec 4, 2024
3ed8269
Merge branch 'trunk' into add/option-to-generate-all-image-sizes
b1ink0 Dec 5, 2024
10e0c39
Cleanup test attachment after assertions
b1ink0 Dec 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_all_fallback_sizes` option is enabled.
*
* @since n.e.x.t
*
* @return bool True if the option is enabled, false otherwise.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return bool True if the option is enabled, false otherwise.
* @return bool Whether the option is enabled. Default is false.

*/
function webp_uploads_should_generate_all_fallback_sizes(): bool {
return (bool) get_option( 'perflab_generate_all_fallback_sizes', 0 );
}

/**
* Retrieves the image URL for a specified MIME type from the attachment metadata.
*
Expand Down
21 changes: 21 additions & 0 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,24 @@ function webp_uploads_opt_in_extra_image_sizes(): void {
}
}
add_action( 'plugins_loaded', 'webp_uploads_opt_in_extra_image_sizes' );

/**
* Enables additional MIME type support for all image sizes based on the generate all fallback sizes settings.
*
* @since n.e.x.t
*
* @param array<string, bool> $allowed_sizes A map of image size names and whether they are allowed to have additional MIME types.
* @return array<string, bool> Modified map of image sizes with additional MIME type support.
*/
function webp_uploads_enable_additional_mime_type_support_for_all_sizes( array $allowed_sizes ): array {
if ( ! webp_uploads_should_generate_all_fallback_sizes() ) {
return $allowed_sizes;
}

foreach ( array_keys( $allowed_sizes ) as $size ) {
$allowed_sizes[ $size ] = true;
}

return $allowed_sizes;
}
add_filter( 'webp_uploads_image_sizes_with_additional_mime_type_support', 'webp_uploads_enable_additional_mime_type_support_for_all_sizes' );
110 changes: 110 additions & 0 deletions plugins/webp-uploads/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_all_fallback_sizes',
array(
'type' => 'boolean',
'default' => false,
'show_in_rest' => false,
)
);

// Add a setting to use the picture element.
register_setting(
'media',
Expand Down Expand Up @@ -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_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' )
);

// Add picture element support settings field.
add_settings_field(
'webp_uploads_use_picture_element',
Expand Down Expand Up @@ -178,6 +200,94 @@ function webp_uploads_generate_webp_jpeg_setting_callback(): void {
<?php
}


/**
* Renders the settings field for generating all fallback image sizes.
*
* @since n.e.x.t
*/
function webp_uploads_generate_all_fallback_sizes_callback(): void {
$all_fallback_sizes_enabled = webp_uploads_should_generate_all_fallback_sizes();
$fallback_enabled = webp_uploads_is_fallback_enabled();
$all_fallback_sizes_hidden_id = 'perflab_generate_all_fallback_sizes_hidden';

?>
<style>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future request for <style> and <script> tags for individual functions:

Currently, for the <picture> element, we have added additional CSS and script tags. Can we centralize these in a common location ( Single tag for <style> and <script> )? This would ensure that in the future, we don’t need to re-add them in specific callback function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would moving <style> and <script> to files and enqueuing them better option for centralization then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something we can do in follow-up PR once these one is committed.

#perflab_generate_all_fallback_sizes_fieldset.disabled label,
#perflab_generate_all_fallback_sizes_fieldset.disabled p {
opacity: 0.7;
}
</style>
<div id="perflab_generate_all_fallback_sizes_notice" class="notice notice-info inline" <?php echo $fallback_enabled ? 'hidden' : ''; ?>>
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
<p><?php esc_html_e( 'This setting requires fallback image output to be enabled.', 'webp-uploads' ); ?></p>
</div>
<div id="perflab_generate_all_fallback_sizes_fieldset" class="<?php echo ! $fallback_enabled ? 'disabled' : ''; ?>">
<label for="perflab_generate_all_fallback_sizes" id="perflab_generate_all_fallback_sizes_label">
<input
type="checkbox"
id="perflab_generate_all_fallback_sizes"
name="perflab_generate_all_fallback_sizes"
aria-describedby="perflab_generate_all_fallback_sizes_description"
value="1"
<?php checked( $all_fallback_sizes_enabled ); ?>
<?php disabled( ! $fallback_enabled ); ?>
>
<?php
/*
* If the checkbox is disabled, but the option is enabled, include a hidden input to continue sending the
* same value upon form submission.
*/
if ( ! $fallback_enabled && $all_fallback_sizes_enabled ) {
?>
<input
type="hidden"
id="<?php echo esc_attr( $all_fallback_sizes_hidden_id ); ?>"
name="perflab_generate_all_fallback_sizes"
value="1"
>
<?php
}
esc_html_e( 'Generate all fallback image sizes including custom sizes', 'webp-uploads' );
?>
</label>
<p class="description" id="perflab_generate_all_fallback_sizes_description"><?php esc_html_e( 'Enabling this option will generate all fallback image sizes including custom sizes. Note: uses even more storage space.', 'webp-uploads' ); ?></p>
</div>
<script>
( function ( allFallbackSizesHiddenId ) {
const fallbackCheckbox = document.getElementById( 'perflab_generate_webp_and_jpeg' );
const allFallbackSizesCheckbox = document.getElementById( 'perflab_generate_all_fallback_sizes' );
const allFallbackSizesFieldset = document.getElementById( 'perflab_generate_all_fallback_sizes_fieldset' );
const allFallbackSizesNotice = document.getElementById( 'perflab_generate_all_fallback_sizes_notice' );

function toggleAllFallbackSizes() {
const fallbackEnabled = fallbackCheckbox.checked;
allFallbackSizesFieldset.classList.toggle( 'disabled', ! fallbackEnabled );
allFallbackSizesCheckbox.disabled = ! fallbackEnabled;
allFallbackSizesNotice.hidden = fallbackEnabled;

// Remove or inject hidden input to preserve original setting value as needed.
if ( fallbackEnabled ) {
const hiddenInput = document.getElementById( allFallbackSizesHiddenId );
if ( hiddenInput ) {
hiddenInput.parentElement.removeChild( hiddenInput );
}
} else if ( allFallbackSizesCheckbox.checked && ! document.getElementById( allFallbackSizesHiddenId ) ) {
// The hidden input is only needed if the value was originally set (i.e., the checkbox enabled).
const hiddenInput = document.createElement( 'input' );
hiddenInput.type = 'hidden';
hiddenInput.id = allFallbackSizesHiddenId;
hiddenInput.name = allFallbackSizesCheckbox.name;
hiddenInput.value = allFallbackSizesCheckbox.value;
allFallbackSizesCheckbox.parentElement.insertBefore( hiddenInput, allFallbackSizesCheckbox.nextSibling );
}
}

fallbackCheckbox.addEventListener( 'change', toggleAllFallbackSizes );
} )( <?php echo wp_json_encode( $all_fallback_sizes_hidden_id ); ?> );
</script>
<?php
}

/**
* Renders the settings field for the 'webp_uploads_use_picture_element' setting.
*
Expand Down
1 change: 1 addition & 0 deletions plugins/webp-uploads/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
*/
function webp_uploads_delete_plugin_option(): void {
delete_option( 'perflab_generate_webp_and_jpeg' );
delete_option( 'perflab_generate_all_fallback_sizes' );
}
Loading