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

Conversation

b1ink0
Copy link
Contributor

@b1ink0 b1ink0 commented Nov 21, 2024

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

Screenshot 2024-11-21 at 6 14 24 PM
Screenshot 2024-11-21 at 6 14 53 PM

Sample outputs for the below custom sizes

add_image_size( 'custom_size_1', 400, 400, true );
add_image_size( 'custom_size_2', 800, 800, true );
add_image_size( 'custom_size_3', 1200, 1200, true );
  • When the new option is off
    Screenshot 2024-11-21 at 6 19 03 PM

  • When the new option is on

Screenshot 2024-11-21 at 6 20 51 PM

@b1ink0 b1ink0 marked this pull request as ready for review November 21, 2024 13:01
Copy link

github-actions bot commented Nov 21, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a 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!

*
* @return bool True if the option is enabled, false otherwise.
*/
function webp_uploads_is_fallback_all_sizes_enabled(): bool {
Copy link
Member

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()?

Comment on lines 722 to 728
$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();
}
Copy link
Member

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.

Comment on lines 774 to 776
if ( webp_uploads_is_fallback_all_sizes_enabled() ) {
echo '<meta name="generator" content="webp-uploads-fallback-all-sizes ' . esc_attr( WEBP_UPLOADS_VERSION ) . '">' . "\n";
}
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

@felixarntz felixarntz Nov 23, 2024

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.

Copy link
Contributor Author

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.

// Add a setting to generate fallback images in all sizes including custom sizes.
register_setting(
'media',
'perflab_generate_fallback_all_sizes',
Copy link
Member

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.

*
* @since n.e.x.t
*/
function webp_uploads_generate_fallback_all_sizes_callback(): void {
Copy link
Member

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.

// 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' ),
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
__( 'Output all fallback images', 'webp-uploads' ),
__( 'Generate all fallback image sizes', 'webp-uploads' ),

@felixarntz felixarntz added this to the webp-uploads n.e.x.t milestone Nov 21, 2024
@felixarntz felixarntz added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Nov 21, 2024
@b1ink0 b1ink0 requested a review from felixarntz November 22, 2024 12:22
Copy link
Member

@felixarntz felixarntz left a 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.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a 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/settings.php Show resolved Hide resolved
* @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' );
Copy link
Member

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.

Suggested change
return (bool) get_option( 'perflab_generate_all_fallback_sizes' );
return (bool) get_option( 'perflab_generate_all_fallback_sizes', 0 );

Comment on lines 815 to 817
* @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.
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
* @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.

* @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 );
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
$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?

Copy link
Contributor Author

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>
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.

*
* @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.

Copy link
Member

@adamsilverstein adamsilverstein left a 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.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

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

Nice work!

@mukeshpanchal27 mukeshpanchal27 merged commit 20a45a3 into WordPress:trunk Dec 6, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable end user opt-in to generate all sizes in fallback format
4 participants