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

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

Merged
merged 20 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c58ffee
introduce webp_uploads_pre_generate_additional_image_source filter to…
mehulkaklotar Apr 27, 2022
dbce8ce
filter webp_uploads_pre_generate_additional_image_source description …
mehulkaklotar Apr 27, 2022
b6235ed
Merge branch 'trunk' into enhancement/additional-image-souce-filter
mehulkaklotar Apr 29, 2022
6639804
filter default image as null - feedback changes
mehulkaklotar Apr 29, 2022
2160d42
test case for filter webp_uploads_pre_generate_additional_image_sourc…
mehulkaklotar Apr 29, 2022
2ab4be9
Update modules/images/webp-uploads/helper.php
mehulkaklotar May 3, 2022
389d7cb
Update modules/images/webp-uploads/helper.php
mehulkaklotar May 3, 2022
2839ad3
Update modules/images/webp-uploads/helper.php
mehulkaklotar May 3, 2022
2efa53a
php unit test case - remove all filters before test
mehulkaklotar May 3, 2022
071ebd9
Update modules/images/webp-uploads/helper.php
mehulkaklotar May 10, 2022
2583b34
check for wp_error and validation added for filter returned image data
mehulkaklotar May 11, 2022
2b73597
test cases for wp_error and invalid data added for filter webp_upload…
mehulkaklotar May 11, 2022
11f24b1
code reformat changes
mehulkaklotar May 11, 2022
eaa4a41
Merge branch 'trunk' into enhancement/additional-image-souce-filter
mehulkaklotar May 11, 2022
ea6bfd0
revert spacing issues and since return jumbles
mehulkaklotar May 11, 2022
f23fc57
revert spacing issues and since return jumbles
mehulkaklotar May 11, 2022
2b629fc
removed conditions for filter returned invalid data - pr feedback
mehulkaklotar May 11, 2022
39d44c0
image size parameter in the filter added
mehulkaklotar May 11, 2022
37eabf6
full image size as default for image generation fixed
mehulkaklotar May 12, 2022
2419892
Update modules/images/webp-uploads/helper.php
mehulkaklotar May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,47 @@ function webp_uploads_get_upload_image_mime_transforms() {
* @access private
*
* @param int $attachment_id The ID of the attachment from where this image would be created.
* @param string $image_size The size name that would be used to create the image source, out of the registered subsizes.
* @param array $size_data An array with the dimensions of the image: height, width and crop.
* @param string $mime The target mime in which the image should be created.
* @param string $destination_file_name The path where the file would be stored, including the extension. If empty, `generate_filename` is used to create the destination file name.
*
* @return array|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error
*/
function webp_uploads_generate_additional_image_source( $attachment_id, array $size_data, $mime, $destination_file_name = null ) {
function webp_uploads_generate_additional_image_source( $attachment_id, $image_size, array $size_data, $mime, $destination_file_name = null ) {

/**
* Filter to allow the generation of additional image sources, in which a defined mime type
* can be transformed and create additional mime types for the file.
*
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
* Returning an image data array or WP_Error here effectively short-circuits the default logic to generate the image source.
*
* @since n.e.x.t
*
mehulkaklotar marked this conversation as resolved.
Show resolved Hide resolved
* @param array|null|WP_Error $image Image data {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} or null or WP_Error.
* @param int $attachment_id The ID of the attachment from where this image would be created.
* @param string $image_size The size name that would be used to create this image, out of the registered subsizes.
* @param array $size_data An array with the dimensions of the image: height, width and crop {'height'=>int, 'width'=>int, 'crop'}.
* @param string $mime The target mime in which the image should be created.
* @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, $image_size, $size_data, $mime );

if ( is_wp_error( $image ) ) {
return $image;
}

if (
is_array( $image )
&& ! empty( $image['file'] )
&& ! empty( $image['path'] )
) {
return array(
'file' => $image['file'],
'filesize' => filesize( $image['path'] ),
);
}

$allowed_mimes = array_flip( wp_get_mime_types() );
if ( ! isset( $allowed_mimes[ $mime ] ) || ! is_string( $allowed_mimes[ $mime ] ) ) {
return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) );
Expand Down Expand Up @@ -173,7 +208,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
$size_data['crop'] = (bool) $sizes[ $size ]['crop'];
}

return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime );
return webp_uploads_generate_additional_image_source( $attachment_id, $size, $size_data, $mime );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )

$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
$destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}";
$image = webp_uploads_generate_additional_image_source( $attachment_id, $original_size_data, $targeted_mime, $destination );
$image = webp_uploads_generate_additional_image_source( $attachment_id, 'full', $original_size_data, $targeted_mime, $destination );

if ( is_wp_error( $image ) ) {
continue;
Expand Down
73 changes: 71 additions & 2 deletions tests/modules/images/webp-uploads/helper-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,37 @@ public function it_should_return_an_error_when_creating_an_additional_image_sour
public function data_provider_invalid_arguments_for_webp_uploads_generate_additional_image_source() {
yield 'when trying to use an attachment ID that does not exists' => array(
PHP_INT_MAX,
'medium',
array(),
'image/webp',
);

add_filter( 'wp_image_editors', '__return_empty_array' );
yield 'when no editor is present' => array(
$this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ),
'medium',
array(),
'image/avif',
);

remove_filter( 'wp_image_editors', '__return_empty_array' );
yield 'when using a mime that is not supported' => array(
$this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ),
'medium',
array(),
'image/avif',
);

yield 'when no dimension is provided' => array(
$this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ),
'medium',
array(),
'image/webp',
);

yield 'when both dimensions are negative numbers' => array(
$this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ),
'medium',
array(
'width' => -10,
'height' => -20,
Expand All @@ -57,6 +62,7 @@ public function data_provider_invalid_arguments_for_webp_uploads_generate_additi

yield 'when both dimensions are zero' => array(
$this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ),
'medium',
array(
'width' => 0,
'height' => 0,
Expand All @@ -78,7 +84,7 @@ public function it_should_create_an_image_with_the_default_suffix_in_the_same_lo
'crop' => true,
);

$result = webp_uploads_generate_additional_image_source( $attachment_id, $size_data, 'image/webp' );
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp' );
$file = get_attached_file( $attachment_id );
$directory = trailingslashit( pathinfo( $file, PATHINFO_DIRNAME ) );
$name = pathinfo( $file, PATHINFO_FILENAME );
Expand All @@ -103,7 +109,7 @@ public function it_should_create_a_file_in_the_specified_location_with_the_speci
'crop' => true,
);

$result = webp_uploads_generate_additional_image_source( $attachment_id, $size_data, 'image/webp', '/tmp/image.jpg' );
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
Expand Down Expand Up @@ -231,4 +237,67 @@ function () {
$this->assertWPError( $result );
$this->assertSame( 'image_mime_type_not_supported', $result->get_error_code() );
}

/**
* Create an image with the filter webp_uploads_pre_generate_additional_image_source added.
*
* @test
*/
public function it_should_create_an_image_with_filter_webp_uploads_pre_generate_additional_image_source() {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );

add_filter(
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
'webp_uploads_pre_generate_additional_image_source',
function () {
return array(
'file' => 'image.webp',
'path' => '/tmp/image.webp',
);
}
);

$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);

$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
}

/**
* Return an error when filter webp_uploads_pre_generate_additional_image_source returns WP_Error.
*
* @test
*/
public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_wp_error() {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );

add_filter(
'webp_uploads_pre_generate_additional_image_source',
function () {
return new WP_Error( 'image_additional_generated_error', __( 'Additional image was not generated.', 'performance-lab' ) );
}
);

$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);

$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertWPError( $result );
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
}
}