Skip to content
Merged
Show file tree
Hide file tree
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 Mar 11, 2022
c1550b2
Update the webp_uploads_create_sources_property function to return ea…
eugene-manuilov Mar 11, 2022
04e67a6
Add a new hook for the image_editor_output_format filter to select th…
eugene-manuilov Mar 11, 2022
daf8620
Fix the phpcs issue.
eugene-manuilov Mar 11, 2022
579f7a1
Update phpdoc for the webp_uploads_upload_image_mime_transforms filter.
eugene-manuilov Mar 11, 2022
87cc7f2
Fix formatting issue.
eugene-manuilov Mar 11, 2022
0b3b687
Apply suggestions from code review
eugene-manuilov Mar 12, 2022
dd30d0f
Update the webp_uploads_get_supported_image_mime_transforms function …
eugene-manuilov Mar 12, 2022
a8911bc
Remove "-scaled" condition.
eugene-manuilov Mar 12, 2022
3a04771
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov Mar 14, 2022
4024e3d
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov Mar 15, 2022
f118537
Address code review feedback.
eugene-manuilov Mar 15, 2022
401ecd6
Update formatting.
eugene-manuilov Mar 16, 2022
66fea39
Refactor webp-uploads tests by moving the same checks into separate c…
eugene-manuilov Mar 16, 2022
1ef8544
Refactor wepb-uploads tests.
eugene-manuilov Mar 16, 2022
fe32ee3
Add tests for the new functionality and fix found issues.
eugene-manuilov Mar 16, 2022
cd40fa0
Merge remote-tracking branch 'origin/trunk' into feature/187-image-fo…
eugene-manuilov Mar 16, 2022
77737da
Fix formatting issues.
eugene-manuilov Mar 16, 2022
b341963
Update tests/modules/images/webp-uploads/webp-uploads-test.php
eugene-manuilov Mar 16, 2022
630929e
Update custom constraints to have the attachment ID parameter at the …
eugene-manuilov Mar 17, 2022
1babcc9
Add the file extension check to image source constraints.
eugene-manuilov Mar 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
},
"autoload-dev": {
"psr-4": {
"PerformanceLab\\Tests\\": "tests/utils"
}
}
Comment on lines +38 to 42
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Awesome! Thanks 😄

}
96 changes: 69 additions & 27 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ] ) ) {
Expand All @@ -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 ),
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

// 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 ) ) ) {
$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
Expand Down Expand Up @@ -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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
}

/**
Expand Down Expand Up @@ -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 );

/**
Expand Down Expand Up @@ -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 );

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
16 changes: 16 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@
<rule ref="Squiz.Commenting.FunctionCommentThrowTag.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Do not apply filename rules for unit tests -->
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Do not apply compatibility rules to allow using the modern PHPUnit functionality -->
<rule ref="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.stringFound">
<exclude-pattern>tests/utils/*</exclude-pattern>
</rule>
<rule ref="PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.boolFound">
<exclude-pattern>tests/utils/*</exclude-pattern>
</rule>
</ruleset>
Loading