-
Notifications
You must be signed in to change notification settings - Fork 106
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
Enable end user opt-in to generate all sizes in fallback format #1689
Conversation
…lity to use added new settings
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@b1ink0 A bit of feedback here, but overall this looks great!
plugins/webp-uploads/helper.php
Outdated
* | ||
* @return bool True if the option is enabled, false otherwise. | ||
*/ | ||
function webp_uploads_is_fallback_all_sizes_enabled(): bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better name would be something like webp_uploads_should_generate_all_fallback_sizes()
?
plugins/webp-uploads/hooks.php
Outdated
$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 ] = (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(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than doing this here, I think a more elegant solution would be to decouple the UI control and option from the main logic, by using the webp_uploads_image_sizes_with_additional_mime_type_support
filter below. So I think this would be better separately implemented.
It would also allow the code to be a bit more efficient: We should only need to call webp_uploads_is_fallback_all_sizes_enabled()
once, not for every size available. If false
we can immediately return without changing anything. If true
, we go through the array of sizes and set them to true
.
I think it's okay not to check the provide_additional_mime_types
value directly to see if it's false
. It's primary purpose it to enable (not disable) this behavior anyway, and since the setting explicitly says "all", it's fine to do it for all sizes, regardless of what other configuration may be present.
plugins/webp-uploads/hooks.php
Outdated
if ( webp_uploads_is_fallback_all_sizes_enabled() ) { | ||
echo '<meta name="generator" content="webp-uploads-fallback-all-sizes ' . esc_attr( WEBP_UPLOADS_VERSION ) . '">' . "\n"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that being added? We shouldn't have an extra generator tag for this feature specifically. If we need feature-specific indicators in the frontend, let's discuss that in a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the #1513 there is mention of the adding the generator tag thats why I added it.
Ideally when users enable this feature we should notate that in the Generator tag so we can later discover how many sites opt into generating all image sizes.
Should I remove the generator tag code from this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think so, let's not do it in this PR, as it needs further discussion.
Maybe worth opening an issue to discuss whether/how we want to make more specific features discoverable in the frontend? Such an issue could link to that comment as one example reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the generator tag code from this PR.
plugins/webp-uploads/settings.php
Outdated
// Add a setting to generate fallback images in all sizes including custom sizes. | ||
register_setting( | ||
'media', | ||
'perflab_generate_fallback_all_sizes', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename this to perflab_generate_all_fallback_sizes
, that reads a bit better.
plugins/webp-uploads/settings.php
Outdated
* | ||
* @since n.e.x.t | ||
*/ | ||
function webp_uploads_generate_fallback_all_sizes_callback(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above in terms of naming.
plugins/webp-uploads/settings.php
Outdated
// 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' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__( 'Output all fallback images', 'webp-uploads' ), | |
__( 'Generate all fallback image sizes', 'webp-uploads' ), |
…ub.com/b1ink0/performance into add/option-to-generate-all-image-sizes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @b1ink0, LGTM!
Paging @adamsilverstein for additional review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good to me. Left some feedbacks.
plugins/webp-uploads/helper.php
Outdated
* @return bool True if the option is enabled, false otherwise. | ||
*/ | ||
function webp_uploads_should_generate_all_fallback_sizes(): bool { | ||
return (bool) get_option( 'perflab_generate_all_fallback_sizes' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add default value here as well.
return (bool) get_option( 'perflab_generate_all_fallback_sizes' ); | |
return (bool) get_option( 'perflab_generate_all_fallback_sizes', 0 ); |
plugins/webp-uploads/hooks.php
Outdated
* @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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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. | |
* @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. |
plugins/webp-uploads/settings.php
Outdated
* @since n.e.x.t | ||
*/ | ||
function webp_uploads_generate_all_fallback_sizes_callback(): void { | ||
$all_fallback_sizes_enabled = 1 === (int) get_option( 'perflab_generate_all_fallback_sizes', 0 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$all_fallback_sizes_enabled = 1 === (int) get_option( 'perflab_generate_all_fallback_sizes', 0 ); | |
$all_fallback_sizes_enabled = 1 === webp_uploads_should_generate_all_fallback_sizes(); |
Can we re-use webp_uploads_should_generate_all_fallback_sizes()
here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can done in 3a64713
$all_fallback_sizes_hidden_id = 'perflab_generate_all_fallback_sizes_hidden'; | ||
|
||
?> | ||
<style> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
plugins/webp-uploads/helper.php
Outdated
* | ||
* @since n.e.x.t | ||
* | ||
* @return bool True if the option is enabled, false otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @return bool True if the option is enabled, false otherwise. | |
* @return bool Whether the option is enabled. Default is false. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks great. One small doc change suggestion. Unit tests to validate the feature works as expected would also be nice to have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
Summary
Fixes #1513
Relevant technical choices
This PR adds a checkbox users can check right below the "generate fallback images" checkbox (and it would only be enabled when "generate fallback images" checkbox is checked).
Settings Page Screenshots
Sample outputs for the below custom sizes
When the new option is off
When the new option is on