Skip to content

Enhance JS replacement mechanism for WebP to JPEG to more reliably replace full file name #443

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

Merged
merged 8 commits into from
Aug 9, 2022

Conversation

mukeshpanchal27
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 commented Jul 20, 2022

Summary

In this PR I use media[i].media_details.sizes to check if the image size has an additional mime type or not. I also use webp_uploads_get_image_sizes_additional_mime_type_support() to get only allowed sizes, so we don't need to check for other sizes.

The console.log for the image data.

image

Fixes #426

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Jul 20, 2022
@mukeshpanchal27 mukeshpanchal27 added this to the 1.4.0 milestone Jul 20, 2022
@mukeshpanchal27 mukeshpanchal27 self-assigned this Jul 20, 2022
@mukeshpanchal27 mukeshpanchal27 requested a review from mitogh as a code owner July 20, 2022 05:41
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.

@mukeshpanchal27 Left some feedback. Most importantly, we need to rely on the actually available MIME types for each size rather than on the filter value for which sizes should have additional MIME types.


// If a full image is present in srcset, it should be updated.
if( srcset && media[i].media_details.sources['image/webp'] ) {
srcset = srcset.replace( media[i].media_details.sources['image/webp'].file, media[i].media_details.sources['image/jpeg'].file );
Copy link
Member

Choose a reason for hiding this comment

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

nitpick - i believe core JS coding standards still specify spaces around variables in brackets, eg media[ i ]

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing out these changes. The PR does not validate JS coding standards. Do we need to fix it? Can I raise the issue of tracking it?

The PR was updated with JS coding standards.

Copy link
Member

Choose a reason for hiding this comment

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

yes, it would be good to add linting for JS if that is missing

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.

@mukeshpanchal27 Thanks for the updates. The approach here now looks good, however I think there's in fact another underlying problem that makes the implementation here overly complicated: The plugin's implementation of the REST API response integration is inconsistent with the core implementation of the endpoint - we need to also add the sources to the full size entry within media_details.sizes. See details in my comments below.

That is crucial for the core implementation too and should be straightforward, so it would be great if you could make this change in the rest-api.php file as part of this pull request. From there, we can simplify the JS code to no longer have any special treatment for the full size, as it should be handled just like any other size.

Comment on lines 22 to 24
if ( srcset && media_sources['image/webp'] ) {
srcset = srcset.replace( media_sources['image/webp'].file, media_sources['image/jpeg'].file );
}
Copy link
Member

Choose a reason for hiding this comment

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

This code makes sense the way our REST API response is currently structured, however I just checked that again and found out our REST API implementation is inconsistent with core's:

  • WordPress core returns the data for the full image within media_details.sizes as opposed to a separate field. When you review the WordPress core implementation of the REST endpoint, you will see that it adds the full image data to media_details.sizes. This is different from how the metadata itself is structured, but we should stick to the convention that core has established here.
  • So we should probably monitor this in our REST API implementation and only provide the full image's sources within that value too, instead of media_details.sources.
  • Let's adjust our implementation in the rest-api.php file so that it also includes the correct sources for the full image size.

Once we have made that change, we will no longer need this extra call here. We can simply loop through sizes_keys like below, because that will then include the full size as expected. It should notably simplify the logic here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the review. This feedback is addressed.

Additional to this I found one case in which the REST API endpoint does not return sizes. If you upload an image with 100X100 size then we don't get sizes in media_details to tackle this case:

Please review it and share your feedback for this implementation.

Copy link
Member

Choose a reason for hiding this comment

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

@mukeshpanchal27 Ah, good point. Sorry, I only read this after my review. I will update my one comment below to reflect this, as I agree with your observation.

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.

@mukeshpanchal27 Thank you for the updates. The REST API logic looks right now, I only have some small comments on improving and simplifying the code there. The JS logic also looks good for the most part, there are just a few quirks left on removing references to the root media_details.sources and the part of assuming those sources apply to src.

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.

left one more small question/point of feedback; this is ready for some more testing!

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.

@mukeshpanchal27 This is almost good to go, I see two remaining problems here.

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.

@mukeshpanchal27 Excellent work!

Now that this has been completed, we can add the remaining changes to WordPress/wordpress-develop#3034 and finalize that core PR. 🎉

@felixarntz felixarntz merged commit ef32fb2 into trunk Aug 9, 2022
@felixarntz felixarntz deleted the fix/426-enhance-js-replacement-mechanism branch August 9, 2022 08:05
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.

Enhance JS replacement mechanism for WebP to JPEG to more reliably replace full file name
4 participants