From a4a13052d73b84a86044d129276af5e8f8c3d631 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 09:42:31 -0600 Subject: [PATCH 01/21] Relocate `$extension` variable closer to where is used Make sure the variable is closer to the place where is going to be used. --- modules/images/webp-uploads/load.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index dc60380e98..3fc4fc7011 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -160,9 +160,6 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); } - $extension = explode( '|', $allowed_mimes[ $mime ] ); - $extension = reset( $extension ); - $width = null; $height = null; $crop = false; @@ -183,10 +180,12 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { $crop = (bool) $sizes[ $size ]['crop']; } + $extension = explode( '|', $allowed_mimes[ $mime ] ); + $extension = reset( $extension ); $editor->resize( $width, $height, $crop ); - $filename = $editor->generate_filename( null, null, $extension ); - $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); - $image = $editor->save( $filename, $mime ); + $filename = $editor->generate_filename( null, null, $extension ); + $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); + $image = $editor->save( $filename, $mime ); if ( is_wp_error( $image ) ) { return $image; From 20a4b90c2388902008cab3cc8ab46bc09f5324b3 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 12:08:20 -0600 Subject: [PATCH 02/21] Add the generation of sources property for full size image. Add a sources property for the full size image and stored in a sources property. This property would create an image with the full size dimensions of the image uploaded into the media library. --- modules/images/webp-uploads/load.php | 95 +++++++++++++++++++ .../images/webp-uploads/webp-uploads-test.php | 49 +++++++++- 2 files changed, 142 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3fc4fc7011..d1c8d946ac 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -111,6 +111,101 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 ); +/** + * Creates a `sources` property at the root of the metadata to store the information about + * the created full sized images. It creates an image with the size of the full image size. + * + * @since n.e.x.t + * + * @param array $metadata The current metadta of the modified image. + * @param int $attachment_id The ID of the attachment being modified. + * @return array The metadata modified of the image. + */ +function webp_uploads_create_sources_property_for_full_image( $metadata, $attachment_id ) { + // This should take place only on the JPEG image. + $valid_mime_transforms = webp_uploads_get_supported_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 ] ) ) { + return $metadata; + } + + $file = get_attached_file( $attachment_id, true ); + + // File does not exist. + if ( ! file_exists( $file ) ) { + return $metadata; + } + + if ( ! isset( $metadata['width'], $metadata['height'] ) ) { + return $metadata; + } + + if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) { + $metadata['sources'] = array(); + } + + if ( empty( $metadata['sources'][ $mime_type ] ) ) { + $metadata['sources'][ $mime_type ] = array( + 'file' => wp_basename( $file ), + 'filesize' => filesize( $file ), + ); + wp_update_attachment_metadata( $attachment_id, $metadata ); + } + + $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $original_extension = pathinfo( $file, PATHINFO_EXTENSION ); + foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { + // If this property exists no need to create the image again. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { + continue; + } + + if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { + continue; + } + + if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { + continue; + } + + $editor = wp_get_image_editor( $file ); + + if ( is_wp_error( $editor ) ) { + continue; + } + + $editor->resize( $metadata['width'], $metadata['height'], false ); + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $extension = reset( $extension ); + + if ( empty( $extension ) || empty( $original_extension ) ) { + continue; + } + + $filename = wp_basename( $file, ".{$original_extension}" ); + $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; + $image = $editor->save( $target_file_name, $targeted_mime ); + + if ( is_wp_error( $image ) ) { + continue; + } + + $metadata['sources'][ $targeted_mime ] = array( + 'file' => $image['file'], + 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, + ); + + wp_update_attachment_metadata( $attachment_id, $metadata ); + } + + return $metadata; +} + +add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property_for_full_image', 10, 2 ); + /** * 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 diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 6f631e21dd..05d3ee4858 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -8,18 +8,32 @@ class WebP_Uploads_Tests extends WP_UnitTestCase { /** - * Create the original image mime type when the image is uploaded + * create the original mime type as well with all the available sources for the specified mime * * @dataProvider provider_image_with_default_behaviors_during_upload * * @test */ - public function it_should_create_the_original_image_mime_type_when_the_image_is_uploaded( $file_location, $expected_mime, $targeted_mime ) { + public function it_should_create_the_original_mime_type_as_well_with_all_the_available_sources_for_the_specified_mime( $file_location, $expected_mime, $targeted_mime ) { $attachment_id = $this->factory->attachment->create_upload_object( $file_location ); $metadata = wp_get_attachment_metadata( $attachment_id ); $this->assertIsArray( $metadata ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertIsArray( $metadata['sources'] ); + $this->assertArrayHasKey( $targeted_mime, $metadata['sources'] ); + $this->assertIsArray( $metadata['sources'][ $targeted_mime ] ); + $this->assertArrayHasKey( $expected_mime, $metadata['sources'] ); + $this->assertIsArray( $metadata['sources'][ $expected_mime ] ); + $this->assertArrayHasKey( 'file', $metadata['sources'][ $targeted_mime ] ); + $this->assertArrayHasKey( 'filesize', $metadata['sources'][ $targeted_mime ] ); + $this->assertArrayHasKey( 'file', $metadata['sources'][ $expected_mime ] ); + $this->assertArrayHasKey( 'filesize', $metadata['sources'][ $expected_mime ] ); + + $this->assertArrayHasKey( 'file', $metadata ); + $this->assertStringEndsWith( $metadata['sources'][ $expected_mime ]['file'], $metadata['file'] ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertArrayHasKey( 'sources', $properties ); $this->assertIsArray( $properties['sources'] ); @@ -61,6 +75,7 @@ public function it_should_not_create_the_sources_property_if_no_transform_is_pro $metadata = wp_get_attachment_metadata( $attachment_id ); $this->assertIsArray( $metadata ); + $this->assertArrayNotHasKey( 'sources', $metadata ); foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertArrayNotHasKey( 'sources', $properties ); } @@ -86,6 +101,12 @@ function () { $metadata = wp_get_attachment_metadata( $attachment_id ); $this->assertIsArray( $metadata ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertIsArray( $metadata['sources'] ); + $this->assertArrayHasKey( 'image/jpeg', $metadata['sources'] ); + $this->assertIsArray( $metadata['sources']['image/jpeg'] ); + $this->assertArrayNotHasKey( 'image/webp', $metadata['sources'] ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertArrayHasKey( 'sources', $properties ); $this->assertIsArray( $properties['sources'] ); @@ -116,6 +137,7 @@ function () { $metadata = wp_get_attachment_metadata( $attachment_id ); $this->assertIsArray( $metadata ); + $this->assertArrayNotHasKey( 'sources', $metadata ); foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertArrayNotHasKey( 'sources', $properties ); } @@ -252,6 +274,29 @@ public function it_should_create_a_webp_version_with_all_the_required_properties ); $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertIsArray( $metadata['sources'] ); + + $file = get_attached_file( $attachment_id, true ); + $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + + $this->assertArrayHasKey( 'image/jpeg', $metadata['sources'] ); + $this->assertIsArray( $metadata['sources']['image/jpeg'] ); + $this->assertArrayHasKey( 'file', $metadata['sources']['image/jpeg'] ); + $this->assertArrayHasKey( 'filesize', $metadata['sources']['image/jpeg'] ); + $this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $file ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ); + $this->assertSame( $metadata['sources']['image/jpeg']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ) ); + + $this->assertArrayHasKey( 'image/webp', $metadata['sources'] ); + $this->assertIsArray( $metadata['sources']['image/webp'] ); + $this->assertArrayHasKey( 'file', $metadata['sources']['image/webp'] ); + $this->assertArrayHasKey( 'filesize', $metadata['sources']['image/webp'] ); + $this->assertStringEndsWith( '.webp', $metadata['sources']['image/webp']['file'] ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertSame( $metadata['sources']['image/webp']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ) ); + $this->assertArrayHasKey( 'sources', $metadata['sizes']['thumbnail'] ); $this->assertArrayHasKey( 'image/jpeg', $metadata['sizes']['thumbnail']['sources'] ); $this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] ); From 082bc89840b4eccf53b850fcea7c62836a28f1ac Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 12:10:08 -0600 Subject: [PATCH 03/21] Fix linting errors on PHP file. Make sure the file follows the `phpcs` rules. --- modules/images/webp-uploads/load.php | 15 ++++++++------- .../images/webp-uploads/webp-uploads-test.php | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d1c8d946ac..2640c51f51 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -118,7 +118,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) * @since n.e.x.t * * @param array $metadata The current metadta of the modified image. - * @param int $attachment_id The ID of the attachment being modified. + * @param int $attachment_id The ID of the attachment being modified. * @return array The metadata modified of the image. */ function webp_uploads_create_sources_property_for_full_image( $metadata, $attachment_id ) { @@ -154,9 +154,10 @@ function webp_uploads_create_sources_property_for_full_image( $metadata, $attach wp_update_attachment_metadata( $attachment_id, $metadata ); } - $allowed_mimes = array_flip( wp_get_mime_types() ); + $allowed_mimes = array_flip( wp_get_mime_types() ); $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); $original_extension = pathinfo( $file, PATHINFO_EXTENSION ); + foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { // If this property exists no need to create the image again. if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { @@ -185,9 +186,9 @@ function webp_uploads_create_sources_property_for_full_image( $metadata, $attach continue; } - $filename = wp_basename( $file, ".{$original_extension}" ); + $filename = wp_basename( $file, ".{$original_extension}" ); $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; - $image = $editor->save( $target_file_name, $targeted_mime ); + $image = $editor->save( $target_file_name, $targeted_mime ); if ( is_wp_error( $image ) ) { continue; @@ -278,9 +279,9 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { $extension = explode( '|', $allowed_mimes[ $mime ] ); $extension = reset( $extension ); $editor->resize( $width, $height, $crop ); - $filename = $editor->generate_filename( null, null, $extension ); - $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); - $image = $editor->save( $filename, $mime ); + $filename = $editor->generate_filename( null, null, $extension ); + $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); + $image = $editor->save( $filename, $mime ); if ( is_wp_error( $image ) ) { return $image; diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 05d3ee4858..50c9081ce1 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -285,17 +285,17 @@ public function it_should_create_a_webp_version_with_all_the_required_properties $this->assertIsArray( $metadata['sources']['image/jpeg'] ); $this->assertArrayHasKey( 'file', $metadata['sources']['image/jpeg'] ); $this->assertArrayHasKey( 'filesize', $metadata['sources']['image/jpeg'] ); - $this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $file ); + $this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $file ); $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ); - $this->assertSame( $metadata['sources']['image/jpeg']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ) ); + $this->assertSame( $metadata['sources']['image/jpeg']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ) ); $this->assertArrayHasKey( 'image/webp', $metadata['sources'] ); $this->assertIsArray( $metadata['sources']['image/webp'] ); $this->assertArrayHasKey( 'file', $metadata['sources']['image/webp'] ); $this->assertArrayHasKey( 'filesize', $metadata['sources']['image/webp'] ); - $this->assertStringEndsWith( '.webp', $metadata['sources']['image/webp']['file'] ); + $this->assertStringEndsWith( '.webp', $metadata['sources']['image/webp']['file'] ); $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); - $this->assertSame( $metadata['sources']['image/webp']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ) ); + $this->assertSame( $metadata['sources']['image/webp']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ) ); $this->assertArrayHasKey( 'sources', $metadata['sizes']['thumbnail'] ); $this->assertArrayHasKey( 'image/jpeg', $metadata['sizes']['thumbnail']['sources'] ); From 1b058f3a745f703cfb87d97834df0168ee37ef09 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 12:12:09 -0600 Subject: [PATCH 04/21] Handle the deletion of `sources` files Add the removal of the `sources` property for mimes different from the original image. The function would ensure to remove any remaining image is removed. --- modules/images/webp-uploads/load.php | 42 +++++++++++++++++-- .../images/webp-uploads/webp-uploads-test.php | 34 +++++++-------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 2640c51f51..4e5d199a0b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -379,11 +379,47 @@ function webp_uploads_remove_sources_files( $attachment_id ) { } $intermediate_file = str_replace( $basename, $properties['file'], $file ); - if ( ! empty( $intermediate_file ) ) { - $intermediate_file = path_join( $upload_path['basedir'], $intermediate_file ); - wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ); + if ( empty( $intermediate_file ) ) { + continue; + } + + $intermediate_file = path_join( $upload_path['basedir'], $intermediate_file ); + if ( ! file_exists( $intermediate_file ) ) { + continue; } + + wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ); + } + } + + if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) { + return; + } + + $original_mime_from_post = get_post_mime_type( $attachment_id ); + $original_mime_from_file = wp_check_filetype( $file )['type']; + + // Delete of full sizes mime types. + foreach ( $metadata['sources'] as $mime => $properties ) { + // Don't remove the image with the same mime type as the original image as this would be removed by WordPress. + if ( $mime === $original_mime_from_post || $mime === $original_mime_from_file ) { + continue; + } + + if ( ! is_array( $properties ) || empty( $properties['file'] ) ) { + continue; + } + + $full_size = str_replace( $basename, $properties['file'], $file ); + if ( empty( $full_size ) ) { + continue; + } + + $full_size_file = path_join( $upload_path['basedir'], $full_size ); + if ( ! file_exists( $full_size_file ) ) { + continue; } + wp_delete_file_from_directory( $full_size_file, $intermediate_dir ); } } diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 50c9081ce1..4933f36697 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -8,7 +8,7 @@ class WebP_Uploads_Tests extends WP_UnitTestCase { /** - * create the original mime type as well with all the available sources for the specified mime + * Create the original mime type as well with all the available sources for the specified mime * * @dataProvider provider_image_with_default_behaviors_during_upload * @@ -352,21 +352,21 @@ public function it_should_remove_the_generated_webp_images_when_the_attachment_i $metadata = wp_get_attachment_metadata( $attachment_id ); $sizes = array( 'thumbnail', 'medium' ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + foreach ( $sizes as $size_name ) { $this->assertArrayHasKey( 'image/webp', $metadata['sizes'][ $size_name ]['sources'] ); $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources']['image/webp'] ); - $this->assertFileExists( - path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) - ); + $this->assertFileExists( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); } wp_delete_attachment( $attachment_id ); foreach ( $sizes as $size_name ) { - $this->assertFileDoesNotExist( - path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) - ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) ); } + + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); } /** @@ -388,15 +388,13 @@ public function it_should_remove_the_attached_webp_version_if_the_attachment_is_ $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertFileExists( - path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) - ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertFileExists( path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) ); wp_delete_attachment( $attachment_id, true ); - $this->assertFileDoesNotExist( - path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) - ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); } /** @@ -418,16 +416,14 @@ public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_f $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertFileExists( - path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) - ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertFileExists( path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) ); define( 'EMPTY_TRASH_DAYS', 0 ); wp_delete_attachment( $attachment_id, true ); - $this->assertFileDoesNotExist( - path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) - ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); } } From a12381a633bd78a041c5695e6c1eafc0156b3d69 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 12:32:36 -0600 Subject: [PATCH 05/21] Remove full sizes images when the attachment is removed. Add an edge case when the image sizes are not defined we need to make sure all original images are removed as part of the files removed instead of returning early when no sizes are present. --- modules/images/webp-uploads/load.php | 15 +++--- .../images/webp-uploads/webp-uploads-test.php | 54 +++++++++++++++++-- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 4e5d199a0b..61a957bad6 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -336,17 +336,16 @@ function webp_uploads_get_supported_image_mime_transforms() { * @param int $attachment_id The ID of the attachment the sources are going to be deleted. */ function webp_uploads_remove_sources_files( $attachment_id ) { - $metadata = wp_get_attachment_metadata( $attachment_id ); - $file = get_attached_file( $attachment_id ); + $file = get_attached_file( $attachment_id ); - if ( - ! isset( $metadata['sizes'] ) - || empty( $file ) - || ! is_array( $metadata['sizes'] ) - ) { + if ( empty( $file ) ) { return; } + $metadata = wp_get_attachment_metadata( $attachment_id ); + // Make sure $sizes is always defined to allow the removal of original images after the first foreach loop. + $sizes = ! isset( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ? array() : $metadata['sizes']; + $upload_path = wp_get_upload_dir(); if ( empty( $upload_path['basedir'] ) ) { return; @@ -355,7 +354,7 @@ function webp_uploads_remove_sources_files( $attachment_id ) { $intermediate_dir = path_join( $upload_path['basedir'], dirname( $file ) ); $basename = wp_basename( $file ); - foreach ( $metadata['sizes'] as $size ) { + foreach ( $sizes as $size ) { if ( ! isset( $size['sources'] ) || ! is_array( $size['sources'] ) ) { continue; } diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 4933f36697..5a13e8cf03 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -215,11 +215,11 @@ public function it_should_prevent_to_create_an_image_size_when_attached_file_doe * @test */ public function it_should_prevent_to_create_a_subsize_if_the_image_editor_does_not_exists() { - // Make sure no editor is available. $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + // Make sure no editor is available. add_filter( 'wp_image_editors', '__return_empty_array' ); $result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' ); $this->assertTrue( is_wp_error( $result ) ); @@ -232,7 +232,6 @@ public function it_should_prevent_to_create_a_subsize_if_the_image_editor_does_n * @test */ public function it_should_prevent_to_upload_a_mime_that_is_not_supported_by_wordpress() { - // Make sure no editor is available. $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); @@ -308,6 +307,27 @@ public function it_should_create_a_webp_version_with_all_the_required_properties $this->assertStringEndsWith( '.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ); } + /** + * Create the full size images when no size is available + * + * @test + */ + public function it_should_create_the_full_size_images_when_no_size_is_available() { + add_filter( 'intermediate_image_sizes', '__return_empty_array' ); + add_filter( 'fallback_intermediate_image_sizes', '__return_empty_array' ); + + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertEmpty( $metadata['sizes'] ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertArrayHasKey( 'image/jpeg', $metadata['sources'] ); + + $this->assertIsArray( $metadata['sources']['image/jpeg'] ); + $this->assertIsArray( $metadata['sources']['image/webp'] ); + } + /** * Remove `scaled` suffix from the generated filename * @@ -338,7 +358,6 @@ function () { * @test */ public function it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted() { - // Make sure no editor is available. $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); @@ -375,7 +394,6 @@ public function it_should_remove_the_generated_webp_images_when_the_attachment_i * @test */ public function it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted_but_empty_trash_day_is_not_defined() { - // Make sure no editor is available. $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); @@ -403,7 +421,6 @@ public function it_should_remove_the_attached_webp_version_if_the_attachment_is_ * @test */ public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_force_deleted_and_empty_trash_days_is_set_to_zero() { - // Make sure no editor is available. $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); @@ -426,4 +443,31 @@ public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_f $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) ); $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); } + + /** + * Remove full size images when no size image exists + * + * @test + */ + public function it_should_remove_full_size_images_when_no_size_image_exists() { + add_filter( 'intermediate_image_sizes', '__return_empty_array' ); + add_filter( 'fallback_intermediate_image_sizes', '__return_empty_array' ); + + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + + $file = get_attached_file( $attachment_id, true ); + $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertEmpty( $metadata['sizes'] ); + $this->assertFileExists( $file ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertFileExists( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ); + + wp_delete_attachment( $attachment_id ); + + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ); + $this->assertFileDoesNotExist( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ); + } } From 50ceba0f4d1c6ca08baa0e65899efc0ce6d61ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Mon, 28 Feb 2022 15:17:38 -0600 Subject: [PATCH 06/21] Update wording on documentation. Co-authored-by: Adam Silverstein --- modules/images/webp-uploads/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 4a3a49f8be..7c9c4bd99a 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -398,7 +398,7 @@ function webp_uploads_remove_sources_files( $attachment_id ) { $original_mime_from_post = get_post_mime_type( $attachment_id ); $original_mime_from_file = wp_check_filetype( $file )['type']; - // Delete of full sizes mime types. + // Delete full sizes mime types. foreach ( $metadata['sources'] as $mime => $properties ) { // Don't remove the image with the same mime type as the original image as this would be removed by WordPress. if ( $mime === $original_mime_from_post || $mime === $original_mime_from_file ) { From add98a7bc1eb1ebbe9cc0c8f0b20573753873902 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 15:37:23 -0600 Subject: [PATCH 07/21] Consolidate 2 hooks into a single one Merge the 2 hooks in charge of creating the full size images with thes sources property of all the image sizes in a single function. --- modules/images/webp-uploads/load.php | 176 +++++++++++---------------- 1 file changed, 71 insertions(+), 105 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 7c9c4bd99a..54f9cc2426 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -26,27 +26,89 @@ * @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 ) { - // Make sure we have some sizes to work with, otherwise avoid any work. - if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) { - return $metadata; - } // This should take place only on the JPEG image. $valid_mime_transforms = webp_uploads_get_supported_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 ] ) ) { + if ( ! isset( $valid_mime_transforms[ $mime_type ] ) || ! is_array( $valid_mime_transforms[ $mime_type ] ) ) { return $metadata; } $file = get_attached_file( $attachment_id, true ); - // File does not exist. if ( ! file_exists( $file ) ) { return $metadata; } + // Make sure the top level `sources` key is a valid array. + if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) { + $metadata['sources'] = array(); + } + + if ( empty( $metadata['sources'][ $mime_type ] ) ) { + $metadata['sources'][ $mime_type ] = array( + 'file' => wp_basename( $file ), + 'filesize' => filesize( $file ), + ); + wp_update_attachment_metadata( $attachment_id, $metadata ); + } + + $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $original_extension = pathinfo( $file, PATHINFO_EXTENSION ); + // 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. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { + continue; + } + + if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { + continue; + } + + if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { + continue; + } + + $editor = wp_get_image_editor( $file ); + + if ( is_wp_error( $editor ) ) { + continue; + } + + if ( ! isset( $metadata['width'], $metadata['height'] ) ) { + continue; + } + + $editor->resize( $metadata['width'], $metadata['height'], false ); + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $extension = reset( $extension ); + + if ( empty( $extension ) || empty( $original_extension ) ) { + continue; + } + + $filename = wp_basename( $file, ".{$original_extension}" ); + $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; + $image = $editor->save( $target_file_name, $targeted_mime ); + + if ( is_wp_error( $image ) ) { + continue; + } + + $metadata['sources'][ $targeted_mime ] = array( + 'file' => $image['file'], + 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, + ); + + wp_update_attachment_metadata( $attachment_id, $metadata ); + } + + // Make sure we have some sizes to work with, otherwise avoid any work. + if ( empty( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) { + return $metadata; + } - $dirname = pathinfo( $file, PATHINFO_DIRNAME ); foreach ( $metadata['sizes'] as $size_name => $properties ) { // This image size is not defined or not an array. if ( ! is_array( $properties ) ) { @@ -77,7 +139,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) 'filesize' => 0, ); // Set the filesize from the current mime image. - $file_location = path_join( $dirname, $properties['file'] ); + $file_location = path_join( $original_directory, $properties['file'] ); if ( file_exists( $file_location ) ) { $properties['sources'][ $current_mime ]['filesize'] = filesize( $file_location ); } @@ -111,102 +173,6 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 ); -/** - * Creates a `sources` property at the root of the metadata to store the information about - * the created full sized images. It creates an image with the size of the full image size. - * - * @since n.e.x.t - * - * @param array $metadata The current metadta of the modified image. - * @param int $attachment_id The ID of the attachment being modified. - * @return array The metadata modified of the image. - */ -function webp_uploads_create_sources_property_for_full_image( $metadata, $attachment_id ) { - // This should take place only on the JPEG image. - $valid_mime_transforms = webp_uploads_get_supported_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 ] ) ) { - return $metadata; - } - - $file = get_attached_file( $attachment_id, true ); - - // File does not exist. - if ( ! file_exists( $file ) ) { - return $metadata; - } - - if ( ! isset( $metadata['width'], $metadata['height'] ) ) { - return $metadata; - } - - if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) { - $metadata['sources'] = array(); - } - - if ( empty( $metadata['sources'][ $mime_type ] ) ) { - $metadata['sources'][ $mime_type ] = array( - 'file' => wp_basename( $file ), - 'filesize' => filesize( $file ), - ); - wp_update_attachment_metadata( $attachment_id, $metadata ); - } - - $allowed_mimes = array_flip( wp_get_mime_types() ); - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $original_extension = pathinfo( $file, PATHINFO_EXTENSION ); - - foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { - // If this property exists no need to create the image again. - if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { - continue; - } - - if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - continue; - } - - if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - continue; - } - - $editor = wp_get_image_editor( $file ); - - if ( is_wp_error( $editor ) ) { - continue; - } - - $editor->resize( $metadata['width'], $metadata['height'], false ); - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $extension = reset( $extension ); - - if ( empty( $extension ) || empty( $original_extension ) ) { - continue; - } - - $filename = wp_basename( $file, ".{$original_extension}" ); - $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; - $image = $editor->save( $target_file_name, $targeted_mime ); - - if ( is_wp_error( $image ) ) { - continue; - } - - $metadata['sources'][ $targeted_mime ] = array( - 'file' => $image['file'], - 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, - ); - - wp_update_attachment_metadata( $attachment_id, $metadata ); - } - - return $metadata; -} - -add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property_for_full_image', 10, 2 ); - /** * 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 From e57d728872600ff5e3dfdd361e4c908aaf6b055e Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 28 Feb 2022 15:41:06 -0600 Subject: [PATCH 08/21] Update docblock and references to the images --- modules/images/webp-uploads/load.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 54f9cc2426..480efe63f6 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -11,15 +11,17 @@ /** * 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_valid_image_mime_types`. If the original image is one of the mimes from - * `webp_uploads_valid_image_mime_types` the image is just added to the `sources` property and not - * created again. If the uploaded attachment is not a valid image this function does not alter the - * metadata of the attachment, on the other hand a `sources` property is added. + * `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 + * 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 + * attachment. * * @since n.e.x.t * * @see wp_generate_attachment_metadata() - * @see webp_uploads_valid_image_mime_types() + * @see webp_uploads_get_supported_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. From a53fa5597c7fb73acd6ae109b7d7433819d9e581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Wed, 2 Mar 2022 18:52:50 -0600 Subject: [PATCH 09/21] Removing non required empty space Co-authored-by: Adam Silverstein --- modules/images/webp-uploads/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 480efe63f6..159697c569 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -12,7 +12,7 @@ * 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_supported_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 From e13ead263306a4cf33aa57c064c491783dd2f7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Mon, 7 Mar 2022 19:07:18 -0600 Subject: [PATCH 10/21] Replace `reset` with access direct to zero index. Co-authored-by: Felix Arntz --- modules/images/webp-uploads/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 159697c569..e038eda77e 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -84,7 +84,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $editor->resize( $metadata['width'], $metadata['height'], false ); $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $extension = reset( $extension ); + $extension = $extension[0]; if ( empty( $extension ) || empty( $original_extension ) ) { continue; From 7072af6c30c65e722f030b6c29f3b1b908793631 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 7 Mar 2022 19:29:36 -0600 Subject: [PATCH 11/21] Remove non requierd defensive conditional check --- modules/images/webp-uploads/load.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 69596f848c..dda68ff348 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -85,11 +85,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $editor->resize( $metadata['width'], $metadata['height'], false ); $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $extension = $extension[0]; - - if ( empty( $extension ) || empty( $original_extension ) ) { - continue; - } - + $filename = wp_basename( $file, ".{$original_extension}" ); $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; $image = $editor->save( $target_file_name, $targeted_mime ); From ab421c8ca5c7cca068e0c466eae94b29fb505559 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 8 Mar 2022 18:03:03 -0600 Subject: [PATCH 12/21] Split logic to share with the `full` size image Split and share the logic to generate the `full` size image with the subsizes for all images. --- modules/images/webp-uploads/load.php | 174 +++++++++++++++------------ 1 file changed, 94 insertions(+), 80 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index dda68ff348..5b2c7c8f3e 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -41,6 +41,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) if ( ! file_exists( $file ) ) { return $metadata; } + // Make sure the top level `sources` key is a valid array. if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) { $metadata['sources'] = array(); @@ -54,9 +55,19 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) wp_update_attachment_metadata( $attachment_id, $metadata ); } - $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_size_data = array( + 'width' => isset( $metadata['width'] ) ? (int) $metadata['width'] : 0, + 'height' => isset( $metadata['height'] ) ? (int) $metadata['height'] : 0, + 'crop' => false, + ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $original_extension = pathinfo( $file, PATHINFO_EXTENSION ); + if ( isset( $metadata['file'] ) ) { + $filename = pathinfo( $metadata['file'], PATHINFO_FILENAME ); + } else { + $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. @@ -64,41 +75,20 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) continue; } - if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - continue; - } - - if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - continue; - } - - $editor = wp_get_image_editor( $file ); - - if ( is_wp_error( $editor ) ) { - continue; - } - - if ( ! isset( $metadata['width'], $metadata['height'] ) ) { + // The targeted mime is not allowed in the current installation. + if ( empty( $allowed_mimes[ $targeted_mime ] ) ) { continue; } - $editor->resize( $metadata['width'], $metadata['height'], false ); - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $extension = $extension[0]; - - $filename = wp_basename( $file, ".{$original_extension}" ); - $target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; - $image = $editor->save( $target_file_name, $targeted_mime ); + $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 ); if ( is_wp_error( $image ) ) { continue; } - $metadata['sources'][ $targeted_mime ] = array( - 'file' => $image['file'], - 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, - ); - + $metadata['sources'][ $targeted_mime ] = $image; wp_update_attachment_metadata( $attachment_id, $metadata ); } @@ -178,6 +168,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) * * @since 1.0.0 * + * @see wp_create_image_subsizes() + * * @param int $attachment_id The ID of the attachment we are going to use as a reference to create the image. * @param string $size The size name that would be used to create this image, out of the registered subsizes. * @param string $mime A mime type we are looking to use to create this image. @@ -196,69 +188,29 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { return new WP_Error( 'image_mime_type_invalid_metadata', __( 'The image does not have a valid metadata.', 'performance-lab' ) ); } - // All subsizes are created out of the attached file. - $file = get_attached_file( $attachment_id ); - - // File does not exist. - if ( ! file_exists( $file ) ) { - return new WP_Error( 'image_file_size_not_found', __( 'The provided size does not have a valid image file.', 'performance-lab' ) ); - } - - // Create the subsizes out of the attached file. - $editor = wp_get_image_editor( $file ); - - if ( is_wp_error( $editor ) ) { - return $editor; - } - - $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' ) ); - } - - if ( ! wp_image_editor_supports( array( 'mime_type' => $mime ) ) ) { - return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); - } - - $width = null; - $height = null; - $crop = false; + $size_data = array( + 'width' => 0, + 'height' => 0, + 'crop' => false, + ); if ( isset( $metadata['sizes'][ $size ]['width'] ) ) { - $width = $metadata['sizes'][ $size ]['width']; + $size_data['width'] = $metadata['sizes'][ $size ]['width']; } elseif ( isset( $sizes[ $size ]['width'] ) ) { - $width = $sizes[ $size ]['width']; + $size_data['width'] = $sizes[ $size ]['width']; } if ( isset( $metadata['sizes'][ $size ]['height'] ) ) { - $height = $metadata['sizes'][ $size ]['height']; + $size_data['height'] = $metadata['sizes'][ $size ]['height']; } elseif ( isset( $sizes[ $size ]['height'] ) ) { - $height = $sizes[ $size ]['height']; + $size_data['height'] = $sizes[ $size ]['height']; } if ( isset( $sizes[ $size ]['crop'] ) ) { - $crop = (bool) $sizes[ $size ]['crop']; - } - - $extension = explode( '|', $allowed_mimes[ $mime ] ); - $extension = reset( $extension ); - $editor->resize( $width, $height, $crop ); - $filename = $editor->generate_filename( null, null, $extension ); - $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); - $image = $editor->save( $filename, $mime ); - - if ( is_wp_error( $image ) ) { - return $image; - } - - if ( empty( $image['file'] ) ) { - return new WP_Error( 'image_file_not_present', __( 'The file key is not present on the image data', 'performance-lab' ) ); + $size_data['crop'] = (bool) $sizes[ $size ]['crop']; } - return array( - 'file' => $image['file'], - 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, - ); + return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime ); } /** @@ -286,6 +238,68 @@ function webp_uploads_get_supported_image_mime_transforms() { return (array) apply_filters( 'webp_uploads_supported_image_mime_transforms', $image_mime_transforms ); } +/** + * Creates a resized image with the provided dimensions out of an original attachment, the created image + * would be saved in the specified mime and stored in the destination file. If the image can't be saved correctly + * a WP_Error would be returned otherwise an array with the file and filesize properties. + * + * @since n.e.xt + * @param int $attachment_id The ID of the attachment from where this image would be created. + * @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|null $destination_file_name The path where the file would be stored, it should include the extension, when null would use `generate_filename` to create a destination. + * @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 = array(), $mime = '', $destination_file_name = null ) { + $image_path = wp_get_original_image_path( $attachment_id ); + + // File does not exist. + if ( ! file_exists( $image_path ) ) { + return new WP_Error( 'image_file_size_not_found', __( 'The provided size does not have a valid image file.', 'performance-lab' ) ); + } + + $editor = wp_get_image_editor( $image_path ); + + if ( is_wp_error( $editor ) ) { + return $editor; + } + + $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' ) ); + } + + if ( ! wp_image_editor_supports( array( 'mime_type' => $mime ) ) ) { + return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); + } + + $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']; + + $editor->resize( $width, $height, $crop ); + + if ( null === $destination_file_name ) { + $extension = explode( '|', $allowed_mimes[ $mime ] ); + $destination_file_name = $editor->generate_filename( null, null, $extension[0] ); + } + + $image = $editor->save( $destination_file_name, $mime ); + + if ( is_wp_error( $image ) ) { + return $image; + } + + if ( empty( $image['file'] ) ) { + return new WP_Error( 'image_file_not_present', __( 'The file key is not present on the image data', 'performance-lab' ) ); + } + + return array( + 'file' => $image['file'], + 'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0, + ); +} + /** * Hook fired when an attachment is deleted, this hook is in charge of removing any * additional mime types created by this plugin besides the original image. Any source From 270c696d8a67de940ddf68336f44dbad481dcfa8 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 8 Mar 2022 18:03:38 -0600 Subject: [PATCH 13/21] Mark method as private --- modules/images/webp-uploads/load.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 5b2c7c8f3e..45ad14c8d7 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -244,6 +244,7 @@ function webp_uploads_get_supported_image_mime_transforms() { * a WP_Error would be returned otherwise an array with the file and filesize properties. * * @since n.e.xt + * @private * @param int $attachment_id The ID of the attachment from where this image would be created. * @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. From 557d50dc654114993c5c5a9b3772f15ed5290969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Tue, 8 Mar 2022 22:53:04 -0600 Subject: [PATCH 14/21] Format doc block using WP Standards. Co-authored-by: Felix Arntz --- modules/images/webp-uploads/load.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 45ad14c8d7..5d06e5aa43 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -245,10 +245,11 @@ function webp_uploads_get_supported_image_mime_transforms() { * * @since n.e.xt * @private - * @param int $attachment_id The ID of the attachment from where this image would be created. - * @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|null $destination_file_name The path where the file would be stored, it should include the extension, when null would use `generate_filename` to create a destination. + * + * @param int $attachment_id The ID of the attachment from where this image would be created. + * @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 = array(), $mime = '', $destination_file_name = null ) { From 169e7e6933b163ba1b8dc98881addf1d7f993f5a Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 12:38:16 -0600 Subject: [PATCH 15/21] Rollback changes to use previous implementation Due the original image change is part of a different PR, to keep each PR separate. --- modules/images/webp-uploads/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 45ad14c8d7..fd360f07fb 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -252,7 +252,7 @@ function webp_uploads_get_supported_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, array $size_data = array(), $mime = '', $destination_file_name = null ) { - $image_path = wp_get_original_image_path( $attachment_id ); + $image_path = get_attached_file( $attachment_id ); // File does not exist. if ( ! file_exists( $image_path ) ) { From c035e02640f208a6ccb73cf43a322ddc0d3e7826 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 12:39:05 -0600 Subject: [PATCH 16/21] Remove default values for parameters In order to ensure all the required parameters are passed down when calling this function. --- modules/images/webp-uploads/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index fd360f07fb..47a1e5009b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -251,7 +251,7 @@ function webp_uploads_get_supported_image_mime_transforms() { * @param string|null $destination_file_name The path where the file would be stored, it should include the extension, when null would use `generate_filename` to create a destination. * @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 = array(), $mime = '', $destination_file_name = null ) { +function webp_uploads_generate_additional_image_source( $attachment_id, array $size_data, $mime, $destination_file_name = null ) { $image_path = get_attached_file( $attachment_id ); // File does not exist. From 958d273ebea75eb9b684bd4a8e532ba42e08bdb9 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 16:24:11 -0600 Subject: [PATCH 17/21] Remove non required conditional The conditional and the usage of a fallback value is not required --- modules/images/webp-uploads/load.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 6bec8c20d2..c3bc4b59cb 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -62,12 +62,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) ); $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - if ( isset( $metadata['file'] ) ) { - $filename = pathinfo( $metadata['file'], PATHINFO_FILENAME ); - } else { - $filename = pathinfo( $file, PATHINFO_FILENAME ); - } - $allowed_mimes = array_flip( wp_get_mime_types() ); + $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. From df02fde53fbe8dc5f369f4d626ef093e2a0f1153 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 16:29:53 -0600 Subject: [PATCH 18/21] Prevent to operate with invalid dimensions An image should contain a positive number to be considered a valid dimensions. --- modules/images/webp-uploads/load.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index c3bc4b59cb..cc4c7d892d 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -275,6 +275,10 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s $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' ) ); + } + $editor->resize( $width, $height, $crop ); if ( null === $destination_file_name ) { From ef0576bfdb9e1743e40930e841acc1acdbe0de4c Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 17:07:09 -0600 Subject: [PATCH 19/21] Add tests for `webp_uploads_generate_additional_image_source` Add tests to cover additional scenarios handled in the function `webp_uploads_generate_additional_image_source` --- .../images/webp-uploads/webp-uploads-test.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 756a50c5b2..24d5c017b2 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -616,4 +616,109 @@ public function data_provider_not_supported_webp_images() { yield 'PNG image' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/dice.png' ); yield 'GIFT image' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/earth.gif' ); } + + /** + * Return an error when creating an additional image source with invalid parameters + * + * @dataProvider data_provider_invalid_arguments_for_webp_uploads_generate_additional_image_source + * + * @test + */ + public function it_should_return_an_error_when_creating_an_additional_image_source_with_invalid_parameters( $attachment_id, $size_data, $mime, $destination_file = null ) { + $this->assertInstanceOf( WP_Error::class, webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime, $destination_file ) ); + } + + 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, + 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' ), + 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' ), + 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' ), + 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' ), + array( + 'width' => -10, + 'height' => -20, + ), + 'image/webp' + ); + + yield 'when both dimensions are zero' => array( + $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ), + array( + 'width' => 0, + 'height' => 0, + ), + 'image/webp' + ); + } + + /** + * Create an image with the default suffix in the same location when no destination is specified + * + * @test + */ + public function it_should_create_an_image_with_the_default_suffix_in_the_same_location_when_no_destination_is_specified() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); + $size_data = array( + 'width' => 300, + 'height' => 300, + 'crop' => true, + ); + + $result = webp_uploads_generate_additional_image_source( $attachment_id, $size_data, 'image/webp' ); + $file = get_attached_file( $attachment_id ); + $directory = trailingslashit( pathinfo( $file, PATHINFO_DIRNAME ) ); + $name = pathinfo( $file, PATHINFO_FILENAME ); + + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 'filesize', $result ); + $this->assertArrayHasKey( 'file', $result ); + $this->assertStringEndsWith( '300x300.webp', $result['file'] ); + $this->assertFileExists( "{$directory}{$name}-300x300.webp" ); + } + + /** + * Create a file in the specified location with the specified name + * + * @test + */ + public function it_should_create_a_file_in_the_specified_location_with_the_specified_name() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); + $size_data = array( + 'width' => 300, + 'height' => 300, + 'crop' => true, + ); + + $result = webp_uploads_generate_additional_image_source( $attachment_id, $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' ); + } } From 1c77008cd76c9a1bb5868d1f8c677bb29f1e2802 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 17:25:50 -0600 Subject: [PATCH 20/21] Remove any invalid transform if present --- modules/images/webp-uploads/load.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index cc4c7d892d..81d0698a42 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -32,7 +32,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $valid_mime_transforms = webp_uploads_get_supported_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 ] ) || ! is_array( $valid_mime_transforms[ $mime_type ] ) ) { + if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) { return $metadata; } @@ -223,6 +223,7 @@ function webp_uploads_get_supported_image_mime_transforms() { 'image/webp' => array( '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. @@ -231,7 +232,15 @@ function webp_uploads_get_supported_image_mime_transforms() { * * @param array $image_mime_transforms A map with the valid mime transforms. */ - return (array) apply_filters( 'webp_uploads_supported_image_mime_transforms', $image_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; + } + $valid_transforms[ $mime ] = $list_transforms; + } + return $valid_transforms; } /** From 0b028c28a35137f86cd57f895b5b67b553b94fa4 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 9 Mar 2022 17:27:37 -0600 Subject: [PATCH 21/21] Fix linting errors on test file --- .../images/webp-uploads/webp-uploads-test.php | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 24d5c017b2..938497716b 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -632,46 +632,45 @@ public function data_provider_invalid_arguments_for_webp_uploads_generate_additi yield 'when trying to use an attachment ID that does not exists' => array( PHP_INT_MAX, array(), - 'image/webp' + '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' ), array(), - 'image/avif' + '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' ), array(), - 'image/avif' + 'image/avif', ); yield 'when no dimension is provided' => array( $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ), array(), - 'image/webp' + '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' ), array( - 'width' => -10, + 'width' => -10, 'height' => -20, ), - 'image/webp' + 'image/webp', ); yield 'when both dimensions are zero' => array( $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ), array( - 'width' => 0, + 'width' => 0, 'height' => 0, ), - 'image/webp' + 'image/webp', ); } @@ -682,16 +681,16 @@ public function data_provider_invalid_arguments_for_webp_uploads_generate_additi */ public function it_should_create_an_image_with_the_default_suffix_in_the_same_location_when_no_destination_is_specified() { $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); - $size_data = array( - 'width' => 300, + $size_data = array( + 'width' => 300, 'height' => 300, - 'crop' => true, + 'crop' => true, ); $result = webp_uploads_generate_additional_image_source( $attachment_id, $size_data, 'image/webp' ); $file = get_attached_file( $attachment_id ); $directory = trailingslashit( pathinfo( $file, PATHINFO_DIRNAME ) ); - $name = pathinfo( $file, PATHINFO_FILENAME ); + $name = pathinfo( $file, PATHINFO_FILENAME ); $this->assertIsArray( $result ); $this->assertArrayHasKey( 'filesize', $result ); @@ -707,13 +706,13 @@ public function it_should_create_an_image_with_the_default_suffix_in_the_same_lo */ public function it_should_create_a_file_in_the_specified_location_with_the_specified_name() { $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); - $size_data = array( - 'width' => 300, + $size_data = array( + 'width' => 300, 'height' => 300, - 'crop' => true, + '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, $size_data, 'image/webp', '/tmp/image.jpg' ); $this->assertIsArray( $result ); $this->assertArrayHasKey( 'filesize', $result );