diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index e5860a10c7..81d0698a42 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -11,28 +11,25 @@ /** * 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 1.0.0 * * @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. * @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 ] ) ) { @@ -40,13 +37,62 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) } $file = get_attached_file( $attachment_id, true ); - // File does not exist. if ( ! file_exists( $file ) ) { return $metadata; } - $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + // 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 ); + } + + $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 ); + $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. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { + continue; + } + + // The targeted mime is not allowed in the current installation. + if ( empty( $allowed_mimes[ $targeted_mime ] ) ) { + continue; + } + + $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 ] = $image; + 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; + } + foreach ( $metadata['sizes'] as $size_name => $properties ) { // This image size is not defined or not an array. if ( ! is_array( $properties ) ) { @@ -77,7 +123,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 ); } @@ -118,6 +164,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. @@ -136,16 +184,88 @@ 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 ); + $size_data = array( + 'width' => 0, + 'height' => 0, + 'crop' => false, + ); + + if ( isset( $metadata['sizes'][ $size ]['width'] ) ) { + $size_data['width'] = $metadata['sizes'][ $size ]['width']; + } elseif ( isset( $sizes[ $size ]['width'] ) ) { + $size_data['width'] = $sizes[ $size ]['width']; + } + + if ( isset( $metadata['sizes'][ $size ]['height'] ) ) { + $size_data['height'] = $metadata['sizes'][ $size ]['height']; + } elseif ( isset( $sizes[ $size ]['height'] ) ) { + $size_data['height'] = $sizes[ $size ]['height']; + } + + if ( isset( $sizes[ $size ]['crop'] ) ) { + $size_data['crop'] = (bool) $sizes[ $size ]['crop']; + } + + return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime ); +} + +/** + * Returns an array with the list of valid mime types that a specific mime type can be converted into it, + * for example an image/jpeg can be converted into an image/webp. + * + * @since 1.0.0 + * + * @return array> 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' ), + ); + + $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. + * + * @since 1.0.0 + * + * @param array $image_mime_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; + } + $valid_transforms[ $mime ] = $list_transforms; + } + return $valid_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 + * @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 $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 ) { + $image_path = get_attached_file( $attachment_id ); // File does not exist. - if ( ! file_exists( $file ) ) { + 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' ) ); } - // Create the subsizes out of the attached file. - $editor = wp_get_image_editor( $file ); + $editor = wp_get_image_editor( $image_path ); if ( is_wp_error( $editor ) ) { return $editor; @@ -160,33 +280,22 @@ 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; + $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 ( isset( $metadata['sizes'][ $size ]['width'] ) ) { - $width = $metadata['sizes'][ $size ]['width']; - } elseif ( isset( $sizes[ $size ]['width'] ) ) { - $width = $sizes[ $size ]['width']; + 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' ) ); } - if ( isset( $metadata['sizes'][ $size ]['height'] ) ) { - $height = $metadata['sizes'][ $size ]['height']; - } elseif ( isset( $sizes[ $size ]['height'] ) ) { - $height = $sizes[ $size ]['height']; - } + $editor->resize( $width, $height, $crop ); - if ( isset( $sizes[ $size ]['crop'] ) ) { - $crop = (bool) $sizes[ $size ]['crop']; + if ( null === $destination_file_name ) { + $extension = explode( '|', $allowed_mimes[ $mime ] ); + $destination_file_name = $editor->generate_filename( null, null, $extension[0] ); } - $editor->resize( $width, $height, $crop ); - $filename = $editor->generate_filename( null, null, $extension ); - $filename = preg_replace( '/-(scaled|rotated|imagifyresized)/', '', $filename ); - $image = $editor->save( $filename, $mime ); + $image = $editor->save( $destination_file_name, $mime ); if ( is_wp_error( $image ) ) { return $image; @@ -202,31 +311,6 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { ); } -/** - * Returns an array with the list of valid mime types that a specific mime type can be converted into it, - * for example an image/jpeg can be converted into an image/webp. - * - * @since 1.0.0 - * - * @return array> 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' ), - ); - - /** - * 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. - * - * @since 1.0.0 - * - * @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 ); -} - /** * 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 @@ -241,17 +325,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; @@ -260,7 +343,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; } @@ -284,11 +367,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 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 c823527276..938497716b 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 ); } @@ -193,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 ) ); @@ -210,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' ); @@ -252,6 +273,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'] ); @@ -263,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 * @@ -293,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' ); @@ -307,21 +371,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'] ) ); } /** @@ -330,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' ); @@ -343,15 +406,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'] ) ); } /** @@ -360,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' ); @@ -373,17 +433,42 @@ 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'] ) ); + } + + /** + * 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'] ) ); } /** @@ -531,4 +616,108 @@ 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' ); + } }