Skip to content

Conversation

@b1ink0
Copy link
Contributor

@b1ink0 b1ink0 commented Feb 25, 2025

Summary

Fixes #1882

Relevant technical choices

The srcset is generated differently for the SOURCE and IMG tags when there are two image sizes with the same width but different heights. This discrepancy occurs because the wp_image_matches_ratio check is applied during wp_calculate_image_srcset for generating the srcset of the IMG tag. However, for the SOURCE tag, the srcset is generated within the webp_uploads_wrap_image_in_picture function, which then overrides the sources created by the wp_get_attachment_image_srcset function using wp_calculate_image_srcset filter. The issue was caused by the absence of the wp_image_matches_ratio check in webp_uploads_wrap_image_in_picture, leading to incorrect image size selection.

Below is the code snippet to reproduce the bug.

add_image_size( 'square', 768, 768, true );
add_filter( 'pre_option_medium_large_size_w', function() { return '768'; } );
add_filter( 'pre_option_medium_large_size_h', function() { return '512'; } );

@b1ink0 b1ink0 added the [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) label Feb 25, 2025
@b1ink0 b1ink0 added this to the webp-uploads n.e.x.t milestone Feb 25, 2025
@b1ink0 b1ink0 added the [Type] Bug An existing feature is broken label Feb 25, 2025
@codecov
Copy link

codecov bot commented Feb 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.01%. Comparing base (ad15727) to head (2c92462).
Report is 14 commits behind head on trunk.

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1885      +/-   ##
==========================================
+ Coverage   70.99%   71.01%   +0.01%     
==========================================
  Files          85       85              
  Lines        6958     6959       +1     
==========================================
+ Hits         4940     4942       +2     
+ Misses       2018     2017       -1     
Flag Coverage Δ
multisite 71.01% <100.00%> (+0.01%) ⬆️
single 40.93% <93.75%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@b1ink0 b1ink0 marked this pull request as ready for review February 26, 2025 20:02
@github-actions
Copy link

github-actions bot commented Feb 26, 2025

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @1ucay.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: 1ucay.

Co-authored-by: b1ink0 <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: westonruter <[email protected]>

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 The production code changes look good to me, but it would be great if you could add a unit test to verify that it fixes the originally reported bug.

/**
* Test handling of case when wp_get_attachment_image_src returns false.
*/
public function test_wrap_image_in_picture_with_false_image_src(): void {
Copy link
Member

@felixarntz felixarntz Feb 26, 2025

Choose a reason for hiding this comment

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

This test is good to have, but it's not actually testing the more important fix that this PR implements. It would be great if you could add a test that verifies that what is reported in #1882 gets fixed by this PR. You could probably use the code snippet you have in the PR description in that test itself.

A great way to do that is test-driven development:

  1. Work on the test based on the codebase without this fix. (For example temporarily comment out or revert the changes in webp_uploads_wrap_image_in_picture() locally.)
  2. Once you implemented the test, it should fail.
  3. Re-enable the changes in webp_uploads_wrap_image_in_picture().
  4. Now the same test should pass without any changes to it.

This way you verify the bug gets fixed by this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed guidance! I will make sure to integrate the step provided for test-driven development into my workflow from now on.

@b1ink0 b1ink0 requested a review from felixarntz March 4, 2025 09:49
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 Thank you for the updates, the test looks great! Just a few minor points of feedback on following some best practices for PHPUnit tests in WordPress.

Comment on lines 331 to 335
wp_delete_attachment( $attachment_id, true );

if ( $tear_down instanceof Closure ) {
$tear_down();
}
Copy link
Member

Choose a reason for hiding this comment

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

Having this cleanup logic called here is not reliable, since any assertion failing would result in this code not being reached. Can we instead incorporate this in an actual tear_down() method on the class that will always get invoked after each test?

You could store the attachment IDs generated in a class property, so that after each check any new IDs result in their attachment being deleted.

Comment on lines 367 to 371
'tear_down' => static function (): void {
remove_image_size( 'square' );
remove_all_filters( 'pre_option_medium_large_size_w' );
remove_all_filters( 'pre_option_medium_large_size_h' );
},
Copy link
Member

Choose a reason for hiding this comment

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

Since this is the only usage of the tear_down closure, we can probably work without them:

  • Filters added in a test are removed automatically after the test, so I don't think we need to clean them up here.
  • For the image size, maybe you can handle it in a tear_down() method on the class? Alternatively if it makes sense to keep it, we need to move when this tear_down closure is invoked in the test (see above comment).

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 This looks great, thank you for the updates!

I have one more feedback, which would be great to address, but I'm already going to approve it as it overall is good.

@b1ink0 b1ink0 requested a review from felixarntz March 12, 2025 16:59
@felixarntz
Copy link
Member

felixarntz commented Mar 13, 2025

Thanks for the updates @b1ink0, looks good!

@westonruter Did you already create a branch for the next release set on Monday, or can this still be merged into trunk? This should be included in Monday's releases.

@westonruter
Copy link
Member

@felixarntz No, I didn't create branch yet. There are still several PRs pending merge.

@westonruter westonruter merged commit 5600db9 into WordPress:trunk Mar 14, 2025
15 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] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modern Image Format return wrong size in picture element

3 participants