-
Notifications
You must be signed in to change notification settings - Fork 143
Allow developers to tweak which image formats to generate on upload #227
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
57cfc27
Rename the webp_uploads_supported_image_mime_transforms filter to be …
eugene-manuilov c1550b2
Update the webp_uploads_create_sources_property function to return ea…
eugene-manuilov 04e67a6
Add a new hook for the image_editor_output_format filter to select th…
eugene-manuilov daf8620
Fix the phpcs issue.
eugene-manuilov 579f7a1
Update phpdoc for the webp_uploads_upload_image_mime_transforms filter.
eugene-manuilov 87cc7f2
Fix formatting issue.
eugene-manuilov 0b3b687
Apply suggestions from code review
eugene-manuilov dd30d0f
Update the webp_uploads_get_supported_image_mime_transforms function …
eugene-manuilov a8911bc
Remove "-scaled" condition.
eugene-manuilov 3a04771
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov 4024e3d
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov f118537
Address code review feedback.
eugene-manuilov 401ecd6
Update formatting.
eugene-manuilov 66fea39
Refactor webp-uploads tests by moving the same checks into separate c…
eugene-manuilov 1ef8544
Refactor wepb-uploads tests.
eugene-manuilov fe32ee3
Add tests for the new functionality and fix found issues.
eugene-manuilov cd40fa0
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov 77737da
Fix formatting issues.
eugene-manuilov b341963
Update tests/modules/images/webp-uploads/webp-uploads-test.php
eugene-manuilov 630929e
Update custom constraints to have the attachment ID parameter at the …
eugene-manuilov 1babcc9
Add the file extension check to image source constraints.
eugene-manuilov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,8 @@ | |
| /** | ||
| * Hook called by `wp_generate_attachment_metadata` to create the `sources` property for every image | ||
| * size, the sources' property would create a new image size with all the mime types specified in | ||
| * `webp_uploads_get_supported_image_mime_transforms`. If the original image is one of the mimes from | ||
| * `webp_uploads_get_supported_image_mime_transforms` the image is just added to the `sources` property and not | ||
| * `webp_uploads_get_upload_image_mime_transforms`. If the original image is one of the mimes from | ||
| * `webp_uploads_get_upload_image_mime_transforms` the image is just added to the `sources` property and not | ||
| * created again. If the uploaded attachment is not a supported mime by this function, the hook does not alter the | ||
| * metadata of the attachment. In addition to every single size the `sources` property is added at the | ||
| * top level of the image metadata to store the references for all the mime types for the `full` size image of the | ||
|
|
@@ -21,15 +21,16 @@ | |
| * @since 1.0.0 | ||
| * | ||
| * @see wp_generate_attachment_metadata() | ||
| * @see webp_uploads_get_supported_image_mime_transforms() | ||
| * @see webp_uploads_get_upload_image_mime_transforms() | ||
| * | ||
| * @param array $metadata An array with the metadata from this attachment. | ||
| * @param int $attachment_id The ID of the attachment where the hook was dispatched. | ||
| * @return array An array with the updated structure for the metadata before is stored in the database. | ||
| */ | ||
| function webp_uploads_create_sources_property( array $metadata, $attachment_id ) { | ||
| // This should take place only on the JPEG image. | ||
| $valid_mime_transforms = webp_uploads_get_supported_image_mime_transforms(); | ||
| $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); | ||
|
|
||
| // Not a supported mime type to create the sources property. | ||
| $mime_type = get_post_mime_type( $attachment_id ); | ||
| if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) { | ||
|
|
@@ -47,7 +48,10 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) | |
| $metadata['sources'] = array(); | ||
| } | ||
|
|
||
| if ( empty( $metadata['sources'][ $mime_type ] ) ) { | ||
| if ( | ||
| empty( $metadata['sources'][ $mime_type ] ) && | ||
| in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) | ||
| ) { | ||
| $metadata['sources'][ $mime_type ] = array( | ||
| 'file' => wp_basename( $file ), | ||
| 'filesize' => filesize( $file ), | ||
|
|
@@ -64,6 +68,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) | |
| $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); | ||
| $filename = pathinfo( $file, PATHINFO_FILENAME ); | ||
| $allowed_mimes = array_flip( wp_get_mime_types() ); | ||
|
|
||
| // Create the sources for the full sized image. | ||
| foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { | ||
| // If this property exists no need to create the image again. | ||
|
|
@@ -131,9 +136,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) | |
| wp_update_attachment_metadata( $attachment_id, $metadata ); | ||
| } | ||
|
|
||
| $formats = isset( $valid_mime_transforms[ $current_mime ] ) ? $valid_mime_transforms[ $current_mime ] : array(); | ||
|
|
||
| foreach ( $formats as $mime ) { | ||
| foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) { | ||
| // If this property exists no need to create the image again. | ||
| if ( ! empty( $properties['sources'][ $mime ] ) ) { | ||
| continue; | ||
|
|
@@ -154,9 +157,42 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) | |
|
|
||
| return $metadata; | ||
| } | ||
|
|
||
| add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 ); | ||
|
|
||
| /** | ||
| * Filter the image editor default output format mapping to select the most appropriate | ||
| * output format depending on desired output formats and supported mime types by the image | ||
| * editor. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $output_format The image editor default output format mapping. | ||
| * @param string $filename Path to the image. | ||
| * @param string $mime_type The source image mime type. | ||
| * @return string The new output format mapping. | ||
| */ | ||
| function webp_uploads_filter_image_editor_output_format( $output_format, $filename, $mime_type ) { | ||
| // Use the original mime type if this type is allowed. | ||
| $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); | ||
| if ( | ||
| ! isset( $valid_mime_transforms[ $mime_type ] ) || | ||
| in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) | ||
| ) { | ||
| return $output_format; | ||
| } | ||
mitogh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Find the first supported mime type by the image editor to use it as the default one. | ||
| foreach ( $valid_mime_transforms[ $mime_type ] as $target_mime ) { | ||
| if ( wp_image_editor_supports( array( 'mime_type' => $target_mime ) ) ) { | ||
eugene-manuilov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $output_format[ $mime_type ] = $target_mime; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return $output_format; | ||
| } | ||
| add_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 ); | ||
|
|
||
| /** | ||
| * Creates a new image based of the specified attachment with a defined mime type | ||
| * this image would be stored in the same place as the provided size name inside the | ||
|
|
@@ -217,30 +253,40 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { | |
| * | ||
| * @return array<string, array<string>> An array of valid mime types, where the key is the mime type and the value is the extension type. | ||
| */ | ||
| function webp_uploads_get_supported_image_mime_transforms() { | ||
| $image_mime_transforms = array( | ||
| 'image/jpeg' => array( 'image/webp' ), | ||
| 'image/webp' => array( 'image/jpeg' ), | ||
| function webp_uploads_get_upload_image_mime_transforms() { | ||
| $default_transforms = array( | ||
| 'image/jpeg' => array( 'image/jpeg', 'image/webp' ), | ||
| 'image/webp' => array( 'image/webp', 'image/jpeg' ), | ||
| ); | ||
|
|
||
| $valid_transforms = array(); | ||
| /** | ||
| * Filter to allow the definition of a custom mime types, in which a defined mime type | ||
| * can be transformed and provide a wide range of mime types. | ||
| * | ||
| * The order of supported mime types matters. If the original mime type of the uploaded image | ||
| * is not needed, then the first mime type in the list supported by the image editor will be | ||
| * selected for the default subsizes. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param array $image_mime_transforms A map with the valid mime transforms. | ||
| * @param array $default_transforms A map with the valid mime transforms. | ||
| */ | ||
| $transforms = (array) apply_filters( 'webp_uploads_supported_image_mime_transforms', $image_mime_transforms ); | ||
| // Remove any invalid transform, by making sure all the transform values are arrays. | ||
| foreach ( $transforms as $mime => $list_transforms ) { | ||
| if ( ! is_array( $list_transforms ) ) { | ||
| continue; | ||
| $transforms = (array) apply_filters( 'webp_uploads_upload_image_mime_transforms', $default_transforms ); | ||
|
|
||
| // Return the default mime transforms if a non-array result is returned from the filter. | ||
| if ( ! is_array( $transforms ) ) { | ||
| return $default_transforms; | ||
| } | ||
|
|
||
| // Ensure that all mime types have correct transforms. If a mime type has invalid transforms array, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great |
||
| // then fallback to the original mime type to make sure that the correct subsizes are created. | ||
| foreach ( $transforms as $mime_type => $transform_types ) { | ||
| if ( ! is_array( $transform_types ) || empty( $transform_types ) ) { | ||
| $transforms[ $mime_type ] = array( $mime_type ); | ||
| } | ||
| $valid_transforms[ $mime ] = $list_transforms; | ||
| } | ||
| return $valid_transforms; | ||
|
|
||
| return $transforms; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -416,7 +462,6 @@ function webp_uploads_remove_sources_files( $attachment_id ) { | |
| wp_delete_file_from_directory( $full_size_file, $intermediate_dir ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'delete_attachment', 'webp_uploads_remove_sources_files', 10, 1 ); | ||
|
|
||
| /** | ||
|
|
@@ -463,7 +508,6 @@ function webp_uploads_wp_get_missing_image_subsizes( $missing_sizes, $image_meta | |
|
|
||
| return array(); | ||
| } | ||
|
|
||
| add_filter( 'wp_get_missing_image_subsizes', 'webp_uploads_wp_get_missing_image_subsizes', 10, 3 ); | ||
|
|
||
| /** | ||
|
|
@@ -524,6 +568,7 @@ function webp_uploads_update_image_references( $content ) { | |
|
|
||
| return $content; | ||
| } | ||
| add_filter( 'the_content', 'webp_uploads_update_image_references', 10 ); | ||
|
|
||
| /** | ||
| * Finds all the urls with *.jpg and *.jpeg extension and updates with *.webp version for the provided image | ||
|
|
@@ -602,8 +647,6 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id | |
| return $image; | ||
| } | ||
|
|
||
| add_filter( 'the_content', 'webp_uploads_update_image_references', 10 ); | ||
|
|
||
| /** | ||
| * Updates the response for an attachment to include sources for additional mime types available the image. | ||
| * | ||
|
|
@@ -639,5 +682,4 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos | |
|
|
||
| return rest_ensure_response( $data ); | ||
| } | ||
|
|
||
| add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure Composer usage like this would play well together with WordPress core, but since it's only tests IMO it should be okay at least in terms of this plugin. The classes would need to be renamed for core anyway.
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.
Do you mean I need to rename util classes to follow
class-...pattern? If so, can we make an exception for phpunit classes only?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.
WordPress core still doesn't use any autoloading, there's always some sort of "includes" file that
require_onces all files. Obviously not great, but that's how I would do it, for maximum "compatibility" with the core paradigms.However, since this is purely about PHPUnit classes and we're in this plugin, I'm not opposed to this (obviously better) approach :)
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.
Awesome! Thanks 😄