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

Replace images in frontend content without using an additional regular expression #262

Merged
merged 8 commits into from Mar 25, 2022

Conversation

eugene-manuilov
Copy link
Contributor

Summary

Fixes #238

Relevant technical choices

Checklist

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

@eugene-manuilov eugene-manuilov added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images Issues related to the Images focus area [Module] WebP Support Issues for the WebP Support module labels Mar 24, 2022
@eugene-manuilov eugene-manuilov added this to the 1.0.0-beta.4 milestone Mar 24, 2022
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.

@eugene-manuilov Great work! One minor thing, but nothing blocking.

// Replace the full size image if present.
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) ) {
$basename = wp_basename( $metadata['file'] );
$image = str_replace( $basename, $metadata['sources'][ $target_mime ]['file'], $image );
Copy link
Member

Choose a reason for hiding this comment

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

These values could be the same. Just thinking out loud here, in terms of PHP performance (I know it's very minor but still worth thinking about) not sure whether that's okay or whether it would be better to use an if check around them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a check to avoid replacing the same images.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
@felixarntz felixarntz changed the title Updated the webp_uploads_img_tag_update_mime_type function to replace images without using a regular expression Replace images in frontend content without using an additional regular expression Mar 24, 2022
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.

Great!

@mitogh mitogh changed the base branch from trunk to release/1.0.0-beta.4 March 24, 2022 23:55
Copy link
Member

@mitogh mitogh left a comment

Choose a reason for hiding this comment

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

Just one comment besides that great refactor.

Comment on lines 628 to 632
if (
! empty( $size_data['file'] ) &&
! empty( $size_data['sources'][ $target_mime ]['file'] ) &&
$size_data['file'] !== $size_data['sources'][ $target_mime ]['file']
) {
Copy link
Member

Choose a reason for hiding this comment

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

While technically this is the same group of conditionals as before I would argue that this way it's harder to follow, instead 1 line condition moving on makes for a more natural and simple flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would agree with it if this condition is written on a single line. IMO having every check on its own line within one condition has the same effect as if we would add three separate conditions for every check. However, if more people think that it's hard to follow, I don't mind to update it to have separate conditions.

Copy link
Member

Choose a reason for hiding this comment

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

I think this could be considered a personal preference, but my take here is that I would agree with @mitogh the previous way was easier to follow. I also prefer the if not x, then bail early (i.e. continue) approach better than if x, run the logic, as it avoids excessive nesting. Of course it's just a single if clause here, but I think it's easier to read if the replacement remains directly within the foreach loop and the checks happen before as safe guards.

So to summarize my personal take:

  • I prefer the way it was before.
  • However, my main point would be to reverse the condition and bail early based on that, rather than only running the logic within the if clause.
  • I don't have a strong preference on multiple if clauses or a single one, but I tend to agree when split in multiple it's easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me then. Updated. Thanks, @mitogh and @felixarntz.

@felixarntz felixarntz merged commit a80c53a into release/1.0.0-beta.4 Mar 25, 2022
@felixarntz felixarntz deleted the enhancement/238-image-format-sources branch March 25, 2022 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Images Issues related to the Images focus area [Module] WebP Support Issues for the WebP Support module [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow controlling which image format sources in the frontend can be replaced
3 participants