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

Update webp_uploads_pre_generate_additional_image_source filter to allow returning file size #334

Merged
merged 6 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
20 changes: 9 additions & 11 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function webp_uploads_get_upload_image_mime_transforms() {
* @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, $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.
Expand All @@ -84,19 +83,23 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
* @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'] )
is_array( $image ) &&
! empty( $image['file'] ) &&
(
! empty( $image['path'] ) ||
array_key_exists( 'filesize', $image )
)
) {
return array(
'file' => $image['file'],
'filesize' => filesize( $image['path'] ),
'filesize' => array_key_exists( 'filesize', $image )
? $image['filesize']
: filesize( $image['path'] ),
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -110,22 +113,18 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
}

$image_path = wp_get_original_image_path( $attachment_id );

// File does not exist.
if ( ! file_exists( $image_path ) ) {
return new WP_Error( 'original_image_file_not_found', __( 'The original image file does not exists, subsizes are created out of the original image.', 'performance-lab' ) );
}

$editor = wp_get_image_editor( $image_path, array( 'mime_type' => $mime ) );

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

$height = isset( $size_data['height'] ) ? (int) $size_data['height'] : 0;
$width = isset( $size_data['width'] ) ? (int) $size_data['width'] : 0;
$crop = isset( $size_data['crop'] ) && $size_data['crop'];

if ( $width <= 0 && $height <= 0 ) {
return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) );
}
Expand All @@ -144,7 +143,6 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
}

$image = $editor->save( $destination_file_name, $mime );

if ( is_wp_error( $image ) ) {
return $image;
}
Expand Down
39 changes: 29 additions & 10 deletions tests/modules/images/webp-uploads/helper-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,33 @@ function () {
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
}

/**
* Tests the webp_uploads_pre_generate_additional_image_source filter returning filesize property.
*
* @test
*/
public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() {
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );

add_filter(
'webp_uploads_pre_generate_additional_image_source',
function () {
return array(
'file' => 'image.webp',
'filesize' => 777,
);
}
);

$result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

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

/**
Expand All @@ -281,23 +307,16 @@ function () {
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' );
$result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' );
$this->assertWPError( $result );
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
}

}