Skip to content

Introduce filter webp_uploads_pre_generate_additional_image_source to short-circuit generating additional image sources on upload #318

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Merged
merged 20 commits into from
May 12, 2022

Conversation

mehulkaklotar
Copy link
Member

@mehulkaklotar mehulkaklotar commented Apr 27, 2022

Summary

Fixes #307

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.

… use custom mechanism to create additional mime types for the file
@mehulkaklotarmehulkaklotar added [Type] EnhancementA suggestion for improvement of an existing feature[Focus] Images labels Apr 27, 2022
@mehulkaklotarmehulkaklotar self-assigned this Apr 27, 2022
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.

Some comments, would be great to add some tests around the filter as well.

@mehulkaklotarmehulkaklotar requested a review from mitogh April 29, 2022 10:28
@mehulkaklotarmehulkaklotar marked this pull request as ready for review April 29, 2022 10:28
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.

LGTM

However it looks like we might want to introduce a new filter in the return value as currently, we are assuming the image would be in the same directory as the WordPress installation and calling filesize for files stored in a bucket like S3 this is not the case, I think in those cases we can rely on a new filter being used so plugins can define the logic in how the filesize is calculated.

For example;

return apply_filters( 'filter_name', array... 

Specifically 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.

@mehulkaklotar The implementation is missing support for the possible WP_Error return type of the filter, which is critical and needs to be fixed. Other than that, just a documentation enhancement suggestion.

cc @eugene-manuilov @mitogh

@felixarntzfelixarntz changed the title Introduce webp_uploads_pre_generate_additional_image_source filter to use custom mechanism to create additional mime types for the file Introduce filter webp_uploads_pre_generate_additional_image_source to short-circuit generating additional image sources on upload May 10, 2022
@felixarntzfelixarntz changed the base branch from trunk to release/1.1.0 May 10, 2022 19:52
@felixarntz
Copy link
Member

@mehulkaklotar I've updated the base branch to release/1.1.0, with the goal to get this into the upcoming release. It would be great if you could implement the requested changes by tomorrow so that we can include this filter in the release. 🙌

@mehulkaklotar
Copy link
Member Author

@felixarntz I am still not sure about the $image_size parameter in the filter here. #318 (comment)

so are we going to make bydefault full for all image size in filter? because it doesn't really make sense as I tested it and it turns out all image size data is being worked on in that.

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.

@mehulkaklotar Thank you for making the changes, I have two follow-up comments.

*
* @return array|null|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error
*/
$image = apply_filters( 'webp_uploads_pre_generate_additional_image_source', null, $attachment_id, 'full', $size_data, $mime );
Copy link
Member

Choose a reason for hiding this comment

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

@mehulkaklotar

so are we going to make bydefault full for all image size in filter? because it doesn't really make sense as I tested it and it turns out all image size data is being worked on in that.

Ahh, sorry I had totally missed this. Yes, this absolutely shouldn't show full for any image size. We will need to provide the name of the image size where the $size_data comes from. We don't have access to that currently from this function, so let's add a new function parameter $size_name right before $size_data.

We then need to pass this new parameter accordingly everywhere that webp_uploads_generate_additional_image_source() is called. Can you please make that update 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.

I have added changes for this in the latest commit.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @mehulkaklotar. Please review my new feedback in #318 (review) - part of your new implementation isn't needed, as the one function by definition is only used for the full image size, so we don't need to try to find the matching size, it's always full in that scenario.

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.

@mehulkaklotar One more follow-up around the newly added change, which can be simplified.

@mehulkaklotar
Copy link
Member Author

@felixarntz We should look into this comment from @mitogh about introducing another filter for returning value for function.

Should we introduce that in this change or make new PR for it?

@jjgrainger
Copy link
Contributor

@felixarntz We should look into this comment from @mitogh about introducing another filter for returning value for function.

Should we introduce that in this change or make new PR for it?

@mehulkaklotar Is there a reason we couldn't expect the webp_uploads_pre_generate_additional_image_source filter to return the image array with the file and filesize keys? This would match the comment for the filter and also allow plugins to calculate the filesize appropriately depending on how the image is stored.

I would want @felixarntz and @mitogh to input here just incase I've overlooked something.

Thanks!

Copy link
Contributor

@jjgrainger jjgrainger left a comment

Choose a reason for hiding this comment

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

Looks good to me, I've added a comment based on adding an additional filter to confirm #318 (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.

Thank you @mehulkaklotar!

Regarding @mitogh's comment, that's an important point. I think a good solution would be to allow for an optional path key within the return array, which we would use instead of file in case it is set. Let's open a follow-up issue for that and work on it for the 1.2.0 plugin release.

Sign up for free to join this conversation on . Already have an account? Sign in to comment
Labels
[Plugin] Modern Image FormatsIssues for the Modern Image Formats plugin (formerly WebP Uploads)[Type] EnhancementA suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce filter to use a custom mechanism to create additional mime images
6 participants