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

Accurate sizes: Calculate image sizes during block rendering #1625

Conversation

mukeshpanchal27
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 commented Oct 23, 2024

Summary

These PR updates the existing code to use the new block context approach that we discussed in #1511 (comment)

@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature no milestone PRs that do not have a defined milestone for release [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) labels Oct 23, 2024
@mukeshpanchal27 mukeshpanchal27 self-assigned this Oct 23, 2024
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review October 24, 2024 11:02
Copy link

github-actions bot commented Oct 24, 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: mukeshpanchal27 <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: joemcgill <[email protected]>

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

@mukeshpanchal27
Copy link
Member Author

We are tracking the unit test issue here #1634

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

LGTM, although @joemcgill would know better what to look for in what this is seeking to achieve.

@joemcgill joemcgill self-assigned this Nov 11, 2024
@mukeshpanchal27
Copy link
Member Author

Once #1663 get merged the unit tests issue will be resolved.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

After chatting with @mukeshpanchal27 about this PR, I now better understand that the purpose of this change is to replace the current implementation for more accurate sizes from the initial approach, which filters core's automatic sizes calculation in wp_calculate_image_sizes, to a new process that adds sizes attributes to blocks during the block rendering process, so that we can make use of block information that is available during the block rendering process that is not currently available to the wp_calculate_image_sizes filter as it is currently implemented in Core.

A small nitpick for a future changelog — the title of this PR would more accurately be: Accurate sizes: calculate image sizes during block rendering

I've left some additional feedback inline. Once this is updated, I think we can merge this and use this as a starting point for incorporating other block information (e.g., block context) into the calculation logic.

plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
if ( isset( $parsed_block['attrs']['align'] ) ) {
$processor->set_attribute( 'data-align', $parsed_block['attrs']['align'] );
$images = array();
while ( $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a good use of the WP_HTML_Tag_Processor, and is fine for a first pass at this functionality, but it would be good to do some profiling of this method against the preg_match_all approach that is currently in use in Core.

If we eventually want to land this code in WP, we'll want to move this priming logic from wp_filter_content_tags rather than duplicate it in two places, at which time we should be mindful of any performance differences between the current logic and this slightly updated version that makes use of the WP_HTML_Tag_Processor.

Copy link
Member

Choose a reason for hiding this comment

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

This seems like a good use of the WP_HTML_Tag_Processor, and is fine for a first pass at this functionality, but it would be good to do some profiling of this method against the preg_match_all approach that is currently in use in Core.

Haven't we already debated this at length, with the conclusion that using WP_HTML_Tag_Processor is preferred for correctness and that the practical performance impact is inconsequential?

Copy link
Member Author

Choose a reason for hiding this comment

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

In previous discussion in #1471 we preferred to use WP_HTML_Tag_Processor.

Copy link
Member

Choose a reason for hiding this comment

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

Haven't we already debated this at length, with the conclusion that using WP_HTML_Tag_Processor is preferred for correctness and that the practical performance impact is inconsequential?

I keep seeing this same question asked in almost every PR where WP_HTML_Tag_Processor is used in place of other approaches, so if a consensus conclusion was reached, I was not aware of it. I'm more than happy for this to be the decision though.

plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
@mukeshpanchal27 mukeshpanchal27 changed the title Accurate sizes: Use block context approach Accurate sizes: Calculate image sizes during block rendering Nov 22, 2024
Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Thanks for the updates. Looks good to me.

@joemcgill joemcgill merged commit 6e9f892 into feature/1511-incorporate-layout-constraints-from-ancestors Nov 22, 2024
18 checks passed
@joemcgill joemcgill deleted the update/use-filter-approach branch November 22, 2024 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no milestone PRs that do not have a defined milestone for release [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Done 😃
Development

Successfully merging this pull request may close these issues.

3 participants