-
Notifications
You must be signed in to change notification settings - Fork 110
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
Fix Modern Image Format not cropping image if crop is an array #1887
base: trunk
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## trunk #1887 +/- ##
==========================================
+ Coverage 67.90% 69.59% +1.69%
==========================================
Files 86 86
Lines 6997 6983 -14
==========================================
+ Hits 4751 4860 +109
+ Misses 2246 2123 -123
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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 Unlinked AccountsThe 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.
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 This looks great! Just one recommendation to simplify the implementation and make it more future-proof.
Another point of feedback: It would be great if you could add a unit test to verify the fix. You could write a test that calls webp_uploads_generate_additional_image_source()
with $size_data['crop']
set to an array and verify the output is correct.
plugins/webp-uploads/helper.php
Outdated
if ( | ||
isset( $sizes[ $size ]['crop'] ) && | ||
( | ||
( | ||
is_array( $sizes[ $size ]['crop'] ) && | ||
count( $sizes[ $size ]['crop'] ) === 2 && | ||
isset( $sizes[ $size ]['crop'][0] ) && | ||
isset( $sizes[ $size ]['crop'][1] ) && | ||
is_string( $sizes[ $size ]['crop'][0] ) && | ||
is_string( $sizes[ $size ]['crop'][1] ) | ||
) || | ||
is_bool( $sizes[ $size ]['crop'] ) | ||
) | ||
) { | ||
$size_data['crop'] = $sizes[ $size ]['crop']; |
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.
I don't think we need to do all this granular validation on the crop
key. WordPress Core doesn't do it and lets everything through. It only ensures at the very last point when it's actually used (in the image_resize_dimensions()
function) that it has the correct format.
I think allowing any value here to pass through makes more sense as it makes our code aligned with Core and more future-proof, in case Core would later add support for other types for the crop
key. It's also in line with width
and height
above, where we don't verify the type.
if ( | |
isset( $sizes[ $size ]['crop'] ) && | |
( | |
( | |
is_array( $sizes[ $size ]['crop'] ) && | |
count( $sizes[ $size ]['crop'] ) === 2 && | |
isset( $sizes[ $size ]['crop'][0] ) && | |
isset( $sizes[ $size ]['crop'][1] ) && | |
is_string( $sizes[ $size ]['crop'][0] ) && | |
is_string( $sizes[ $size ]['crop'][1] ) | |
) || | |
is_bool( $sizes[ $size ]['crop'] ) | |
) | |
) { | |
$size_data['crop'] = $sizes[ $size ]['crop']; | |
if ( isset( $sizes[ $size ]['crop'] ) ) { | |
$size_data['crop'] = $sizes[ $size ]['crop']; |
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.
I added granular checks to ensure that the Modern Image Formats code does not produce errors on invalid input. However, to future-proof code, as you suggested, simplifying the condition makes more sense. Fixed in af35deb.
Summary
Fixes #1881
Relevant technical choices
This PR fixes the issue of Modern Image Format not cropping image if crop is an array.
Below is the code snippet to reproduce the bug: