From 104cc8d8c6537a0cbd3d96322b7c00451c33bcc6 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 17 Mar 2022 19:40:46 +0530 Subject: [PATCH 01/54] Convert first file to webp --- modules/images/webp-uploads/load.php | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 98da401090..f2799b7b73 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -641,3 +641,35 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos } add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); + +function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { + if ( $override !== null ) { + return $override; + } + // 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( $post_id ); + foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { + + $allowed_mimes = array_flip( wp_get_mime_types() ); + + if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_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' => $targeted_mime ) ) ) { + return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); + } + + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; + + $image->save( $destination, $targeted_mime ); + } +} + +add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From 5348eeaa7e51221158bcf41b3784263585864ce9 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 18 Mar 2022 18:19:22 +0530 Subject: [PATCH 02/54] Convert image of all size to WebP --- modules/images/webp-uploads/load.php | 53 ++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index f2799b7b73..09a51ca7f2 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -646,13 +646,18 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ if ( $override !== null ) { return $override; } - // 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( $post_id ); + + $valid_mime_transforms = webp_uploads_get_supported_image_mime_transforms(); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $sizes = get_intermediate_image_sizes(); + $_wp_additional_image_sizes = wp_get_additional_image_sizes(); + $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; + $nocrop = false; + foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { - $allowed_mimes = array_flip( wp_get_mime_types() ); + $allowed_mimes = wp_get_mime_types(); if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) ); @@ -662,13 +667,41 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); } - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; $image->save( $destination, $targeted_mime ); + + $new_image = wp_get_image_editor( $destination ); + + if ( 'thumbnail' === $target ) { + $nocrop = true; + } + + if ( isset( $sizes ) ) { + $_sizes = array(); + + foreach ( $sizes as $size ) { + + if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { + $width = (int) $_wp_additional_image_sizes[ $size ]['width']; + $height = (int) $_wp_additional_image_sizes[ $size ]['height']; + $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; + } else { + $height = get_option( "{$size}_size_h" ); + $width = get_option( "{$size}_size_w" ); + $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); + } + + $_sizes[ $size ] = array( + 'width' => $width, + 'height' => $height, + 'crop' => $crop, + ); + } + + $new_image->multi_resize( $_sizes ); + } } } From 436c32d3d1bba3e7d86d43973e34072518d2389d Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 18 Mar 2022 18:30:24 +0530 Subject: [PATCH 03/54] Add doc comment to function --- modules/images/webp-uploads/load.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 94a0e43b77..6c77b21f64 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -684,6 +684,18 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos } add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); +/** + * + * Updates webp image when original image is edited + * + * @since n.e.x.t + * + * @param bool|null $override Value to return instead of saving. Default null. + * @param string $file Name of the file to be saved. + * @param WP_Image_Editor $image The image editor instance. + * @param string $mime_type The mime type of the image. + * @param int $post_id Attachment post ID. + */ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { if ( $override !== null ) { return $override; From e7c3aa133213b827558a679f65315f00d592d4ff Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 18 Mar 2022 19:25:32 +0530 Subject: [PATCH 04/54] Skip changes in current mime type --- modules/images/webp-uploads/load.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 6c77b21f64..04c07903e5 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -701,17 +701,21 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return $override; } - $valid_mime_transforms = webp_uploads_get_supported_image_mime_transforms(); + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); $filename = pathinfo( $file, PATHINFO_FILENAME ); $sizes = get_intermediate_image_sizes(); $_wp_additional_image_sizes = wp_get_additional_image_sizes(); $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; $nocrop = false; + $current_mime_type = get_post_mime_type( $post_id ); foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { + if ( $targeted_mime === $current_mime_type ) { + continue; + } - $allowed_mimes = wp_get_mime_types(); + $allowed_mimes = array_flip( wp_get_mime_types() ); if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) ); From 5089128b5413b61f3abe67192fc09335b9c2986d Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Mon, 21 Mar 2022 16:39:19 +0530 Subject: [PATCH 05/54] Update modules/images/webp-uploads/load.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- modules/images/webp-uploads/load.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 04c07903e5..711ebf0d48 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -746,8 +746,8 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $height = (int) $_wp_additional_image_sizes[ $size ]['height']; $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; } else { - $height = get_option( "{$size}_size_h" ); - $width = get_option( "{$size}_size_w" ); + $height = (int) get_option( "{$size}_size_h" ); + $width = (int) get_option( "{$size}_size_w" ); $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); } From 3ca1da1c5f91260c10adf24439a313911e98ca73 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Mon, 21 Mar 2022 16:39:31 +0530 Subject: [PATCH 06/54] Update modules/images/webp-uploads/load.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- modules/images/webp-uploads/load.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 711ebf0d48..cc24087bbc 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -736,7 +736,9 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $nocrop = true; } - if ( isset( $sizes ) ) { + if ( ! isset( $sizes ) ) { + continue; + } $_sizes = array(); foreach ( $sizes as $size ) { From 1eaa260163a8f53116937e78cb3a430777ec9fe4 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Mon, 21 Mar 2022 18:41:13 +0530 Subject: [PATCH 07/54] Address feedback in comments --- modules/images/webp-uploads/load.php | 49 +++++++++++++++------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index cc24087bbc..876a8a0731 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -695,6 +695,8 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos * @param WP_Image_Editor $image The image editor instance. * @param string $mime_type The mime type of the image. * @param int $post_id Attachment post ID. + * + * @return bool|null */ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { if ( $override !== null ) { @@ -709,14 +711,13 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; $nocrop = false; $current_mime_type = get_post_mime_type( $post_id ); + $allowed_mimes = array_flip( wp_get_mime_types() ); foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { if ( $targeted_mime === $current_mime_type ) { continue; } - $allowed_mimes = array_flip( wp_get_mime_types() ); - if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) ); } @@ -732,6 +733,10 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $new_image = wp_get_image_editor( $destination ); + if ( is_wp_error( $new_image ) ) { + return $new_image; + } + if ( 'thumbnail' === $target ) { $nocrop = true; } @@ -739,29 +744,29 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ if ( ! isset( $sizes ) ) { continue; } - $_sizes = array(); - - foreach ( $sizes as $size ) { - - if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { - $width = (int) $_wp_additional_image_sizes[ $size ]['width']; - $height = (int) $_wp_additional_image_sizes[ $size ]['height']; - $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; - } else { - $height = (int) get_option( "{$size}_size_h" ); - $width = (int) get_option( "{$size}_size_w" ); - $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); - } - - $_sizes[ $size ] = array( - 'width' => $width, - 'height' => $height, - 'crop' => $crop, - ); + + $_sizes = array(); + + foreach ( $sizes as $size ) { + + if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { + $width = (int) $_wp_additional_image_sizes[ $size ]['width']; + $height = (int) $_wp_additional_image_sizes[ $size ]['height']; + $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; + } else { + $height = (int) get_option( "{$size}_size_h" ); + $width = (int) get_option( "{$size}_size_w" ); + $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); } - $new_image->multi_resize( $_sizes ); + $_sizes[ $size ] = array( + 'width' => $width, + 'height' => $height, + 'crop' => $crop, + ); } + + $new_image->multi_resize( $_sizes ); } } From a4185c543c6c761e97949c19eea8e18d16ba1216 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:20:57 +0530 Subject: [PATCH 08/54] Update doc comment 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 876a8a0731..47eb8d6fdb 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -686,7 +686,7 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos /** * - * Updates webp image when original image is edited + * Creates additional image formats when original image is edited. * * @since n.e.x.t * From fefd06f1eb5c0d4619e351e7b379d159473e525b Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:23:46 +0530 Subject: [PATCH 09/54] Put filter calls right below their corresponding function Co-authored-by: Felix Arntz --- modules/images/webp-uploads/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 47eb8d6fdb..c5be0178a5 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -769,5 +769,4 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $new_image->multi_resize( $_sizes ); } } - add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From d5e42952de7f69ff3968e97ad3f65d37f64e26fd Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Thu, 24 Mar 2022 16:39:35 +0530 Subject: [PATCH 10/54] Update modules/images/webp-uploads/load.php 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 c5be0178a5..3ee69502b4 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -714,7 +714,7 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $allowed_mimes = array_flip( wp_get_mime_types() ); foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { - if ( $targeted_mime === $current_mime_type ) { + if ( $targeted_mime === $mime_type ) { continue; } From 2339ac69004e12953a0e528a232340b4755c0660 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 24 Mar 2022 19:05:42 +0530 Subject: [PATCH 11/54] Remove redundant logic as per feedback --- modules/images/webp-uploads/load.php | 43 ++++++---------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3ee69502b4..e3aa6921e1 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -703,17 +703,13 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return $override; } - $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $sizes = get_intermediate_image_sizes(); - $_wp_additional_image_sizes = wp_get_additional_image_sizes(); - $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; - $nocrop = false; - $current_mime_type = get_post_mime_type( $post_id ); - $allowed_mimes = array_flip( wp_get_mime_types() ); + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $sizes = wp_get_registered_image_subsizes(); + $allowed_mimes = array_flip( wp_get_mime_types() ); - foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { + foreach ( $valid_mime_transforms as $targeted_mime ) { if ( $targeted_mime === $mime_type ) { continue; } @@ -737,33 +733,10 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return $new_image; } - if ( 'thumbnail' === $target ) { - $nocrop = true; - } - - if ( ! isset( $sizes ) ) { - continue; - } - $_sizes = array(); - foreach ( $sizes as $size ) { - - if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { - $width = (int) $_wp_additional_image_sizes[ $size ]['width']; - $height = (int) $_wp_additional_image_sizes[ $size ]['height']; - $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; - } else { - $height = (int) get_option( "{$size}_size_h" ); - $width = (int) get_option( "{$size}_size_w" ); - $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); - } - - $_sizes[ $size ] = array( - 'width' => $width, - 'height' => $height, - 'crop' => $crop, - ); + foreach ( $sizes as $size => $size_details ) { + $_sizes[ $size ] = $size_details; } $new_image->multi_resize( $_sizes ); From 928c37e8cee89759e7b28ae37961580f9955df6e Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 24 Mar 2022 19:25:53 +0530 Subject: [PATCH 12/54] Add sources in metadata --- modules/images/webp-uploads/load.php | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index e3aa6921e1..6477043bff 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -684,6 +684,34 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos } add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); +/** + * Adds sources to metadata for an attachment. + * + * @param array $metadata Metadata of the attachment. + * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. + * @param array $allowed_mimes Allowed mime types. + * @param string $file Path to original file. + * @return array Metadata with sources added. + */ +function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowed_mimes, $file ) { + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + + foreach ( $metadata['sizes'] as $size => $size_details ) { + foreach ( $valid_mime_transforms as $targeted_mime ) { + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); + $newfile = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; + + $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( + 'file' => $newfile, + 'filesize' => filesize( $newfile ), + ); + } + } + + return $metadata; +} + /** * * Creates additional image formats when original image is edited. @@ -741,5 +769,17 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $new_image->multi_resize( $_sizes ); } + add_filter( + 'wp_update_attachment_metadata', + function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $allowed_mimes, $file ) { + if ( $post_meta_id !== $post_id ) { + return $metadata; + } + + return webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowed_mimes, $file ); + }, + 10, + 2 + ); } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From d72617e5b44dff542d1aeec3510a25b815f4e7b4 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 24 Mar 2022 19:26:44 +0530 Subject: [PATCH 13/54] Add yoda condition check --- 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 6477043bff..6bd8447801 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -727,7 +727,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe * @return bool|null */ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { - if ( $override !== null ) { + if ( null !== $override ) { return $override; } From 4f5c0e5bea3f62df992f95eba91f9dc9e55cf70e Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 24 Mar 2022 19:49:44 +0530 Subject: [PATCH 14/54] Return override instead of WP_Error --- modules/images/webp-uploads/load.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 6bd8447801..3e99d46889 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -743,11 +743,11 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ } if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - return new WP_Error( 'image_mime_type_invalid', __( 'The provided mime type is not allowed.', 'performance-lab' ) ); + return $override; } if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); + return $override; } $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); @@ -755,10 +755,8 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $image->save( $destination, $targeted_mime ); - $new_image = wp_get_image_editor( $destination ); - - if ( is_wp_error( $new_image ) ) { - return $new_image; + if ( is_wp_error( $image ) ) { + return $override; } $_sizes = array(); @@ -767,7 +765,7 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $_sizes[ $size ] = $size_details; } - $new_image->multi_resize( $_sizes ); + $image->multi_resize( $_sizes ); } add_filter( 'wp_update_attachment_metadata', From bc28a858efe17cd4a71c79b7640d669c465868af Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Thu, 24 Mar 2022 20:18:19 +0530 Subject: [PATCH 15/54] Ensure there's no error while saving image --- modules/images/webp-uploads/load.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3e99d46889..1a16d91b7e 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -753,9 +753,7 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - $image->save( $destination, $targeted_mime ); - - if ( is_wp_error( $image ) ) { + if ( is_wp_error( $image->save( $destination, $targeted_mime ) ) ) { return $override; } From 1e44b9fe4f57613e138090454a57ea0dac1c33b9 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:52:34 +0530 Subject: [PATCH 16/54] Check $mime_type exists in the array before accessing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 1a16d91b7e..39d8d6dc42 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -731,6 +731,10 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return $override; } + if ( empty( webp_uploads_get_upload_image_mime_transforms()[ $mime_type ] ) { + return $overrride; + } + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); $filename = pathinfo( $file, PATHINFO_FILENAME ); From 76b6d59dc4ccb3786547eeb5f78091acff38d923 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:53:02 +0530 Subject: [PATCH 17/54] Store only basename of image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 39d8d6dc42..d5ef59ded1 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -703,7 +703,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe $newfile = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( - 'file' => $newfile, + 'file' => wp_basename( $newfile ), 'filesize' => filesize( $newfile ), ); } From 5956b6eacbd7ad738b758cfa62343a1dbe6a5697 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:54:08 +0530 Subject: [PATCH 18/54] Make sure the file exists before accessing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- modules/images/webp-uploads/load.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d5ef59ded1..01a88cff6b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -701,6 +701,9 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); $newfile = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; + if ( ! file_exists( $newfile ) { + continue; + } $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( 'file' => wp_basename( $newfile ), From 9d937132e4ad5beb95f56072f5ddc6e65f78645b Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:55:46 +0530 Subject: [PATCH 19/54] Change variable name to make it more readable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 01a88cff6b..fc9cdbddae 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -700,7 +700,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $valid_mime_transforms as $targeted_mime ) { $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); - $newfile = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; + $image_file = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; if ( ! file_exists( $newfile ) { continue; } From 160340e4cd38783500e0e29c57347c66973de99c Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:59:01 +0530 Subject: [PATCH 20/54] Add filterable value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 fc9cdbddae..7d7f4f1f6b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -783,6 +783,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $a }, 10, 2 + return $override; ); } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From df722d9ccfd2846ce19497245302105bd324f803 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 25 Mar 2022 14:01:37 +0530 Subject: [PATCH 21/54] Remove commited suggestion --- modules/images/webp-uploads/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 7d7f4f1f6b..fc9cdbddae 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -783,7 +783,6 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $a }, 10, 2 - return $override; ); } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From 47a921a99a413f55aaffc1d6f4087b969142ad10 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 25 Mar 2022 14:04:55 +0530 Subject: [PATCH 22/54] Fix minor errors from suggestion --- modules/images/webp-uploads/load.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index fc9cdbddae..acb1073d6b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -701,13 +701,13 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); $image_file = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; - if ( ! file_exists( $newfile ) { - continue; + if ( ! file_exists( $image_file ) ) { + continue; } $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( - 'file' => wp_basename( $newfile ), - 'filesize' => filesize( $newfile ), + 'file' => wp_basename( $image_file ), + 'filesize' => filesize( $image_file ), ); } } @@ -734,9 +734,9 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return $override; } - if ( empty( webp_uploads_get_upload_image_mime_transforms()[ $mime_type ] ) { - return $overrride; - } + if ( empty( webp_uploads_get_upload_image_mime_transforms()[ $mime_type ] ) ) { + return $override; + } $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); From 9a7a97d260923953df568b1f59bf7b515e6594a8 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Fri, 25 Mar 2022 16:44:58 +0530 Subject: [PATCH 23/54] Populate top level sources array --- modules/images/webp-uploads/load.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index acb1073d6b..6ced5f7fa9 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -696,9 +696,23 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowed_mimes, $file ) { $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - foreach ( $metadata['sizes'] as $size => $size_details ) { - foreach ( $valid_mime_transforms as $targeted_mime ) { - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + foreach ( $valid_mime_transforms as $targeted_mime ) { + // Add sources to original image metadata. + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $filename_without_ext = implode( explode( '.', $file, -1 ) ); + $image_file = $filename_without_ext . ".{$extension[0]}"; + + if ( ! file_exists( $image_file ) ) { + continue; + } + + $metadata['sources'][ $targeted_mime ] = array( + 'file' => wp_basename( $image_file ), + 'filesize' => filesize( $image_file ), + ); + + foreach ( $metadata['sizes'] as $size => $size_details ) { + // Add sources to resized image metadata. $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); $image_file = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; if ( ! file_exists( $image_file ) ) { From 0158b7cc228a2bb1d82ea28ffab01c940bade143 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Mon, 28 Mar 2022 13:11:25 +0530 Subject: [PATCH 24/54] Update modules/images/webp-uploads/load.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 6ced5f7fa9..0ee7f30ef0 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -699,7 +699,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $valid_mime_transforms as $targeted_mime ) { // Add sources to original image metadata. $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $filename_without_ext = implode( explode( '.', $file, -1 ) ); + $filename_without_ext = pathinfo( $file, PATHINFO_FILENAME ); $image_file = $filename_without_ext . ".{$extension[0]}"; if ( ! file_exists( $image_file ) ) { From d5be5d324471f770850f004cbc8b3ec511729d30 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar <8456197+kirtangajjar@users.noreply.github.com> Date: Mon, 28 Mar 2022 13:11:38 +0530 Subject: [PATCH 25/54] Update modules/images/webp-uploads/load.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Crisoforo Gaspar Hernández --- 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 0ee7f30ef0..302c740a90 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -714,7 +714,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $metadata['sizes'] as $size => $size_details ) { // Add sources to resized image metadata. $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); - $image_file = trailingslashit( $original_directory ) . $filename_without_ext . ".{$extension[0]}"; + $image_file = path_join( $original_directory, "$filename_without_ex.{$extension[0]}" ) if ( ! file_exists( $image_file ) ) { continue; } From 94820f9015de3c1cf705acd541884c81610b7a51 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Mon, 28 Mar 2022 14:11:21 +0530 Subject: [PATCH 26/54] Fix top level sources metadata not updating --- modules/images/webp-uploads/load.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 302c740a90..f62120ba02 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -698,9 +698,9 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $valid_mime_transforms as $targeted_mime ) { // Add sources to original image metadata. - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $filename_without_ext = pathinfo( $file, PATHINFO_FILENAME ); - $image_file = $filename_without_ext . ".{$extension[0]}"; + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $filename_without_extension = pathinfo( $file, PATHINFO_FILENAME ); + $image_file = path_join( $original_directory, "$filename_without_extension.{$extension[0]}" ); if ( ! file_exists( $image_file ) ) { continue; @@ -713,8 +713,9 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $metadata['sizes'] as $size => $size_details ) { // Add sources to resized image metadata. - $filename_without_ext = implode( explode( '.', $size_details['file'], -1 ) ); - $image_file = path_join( $original_directory, "$filename_without_ex.{$extension[0]}" ) + $filename_without_extension = pathinfo( $size_details['file'], PATHINFO_FILENAME ); + $image_file = path_join( $original_directory, "$filename_without_extension.{$extension[0]}" ); + if ( ! file_exists( $image_file ) ) { continue; } From c6a2ca8cd6ef66e7dae5eee54cf36d8584d2c311 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 29 Mar 2022 17:54:39 +0530 Subject: [PATCH 27/54] Add WP_Image_Edit testing helper class --- .../webp-uploads/class-wp-image-edit.php | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 tests/testdata/modules/images/webp-uploads/class-wp-image-edit.php diff --git a/tests/testdata/modules/images/webp-uploads/class-wp-image-edit.php b/tests/testdata/modules/images/webp-uploads/class-wp-image-edit.php new file mode 100644 index 0000000000..b3591b0cf0 --- /dev/null +++ b/tests/testdata/modules/images/webp-uploads/class-wp-image-edit.php @@ -0,0 +1,177 @@ +attachment_id = $attachment_id; + } + + /** + * Once the object is removed make sure that `history` and `target` are + * removed from the $_REQUEST. + */ + public function __destruct() { + unset( $_REQUEST['history'], $_REQUEST['target'] ); + } + + /** + * Register a change to rotate an image to the right. + * + * @return $this + */ + public function rotate_right() { + $this->changes[] = array( 'r' => -90 ); + + return $this; + } + + /** + * Register a new change to rotate an image to the left. + * + * @return $this + */ + public function rotate_left() { + $this->changes[] = array( 'r' => 90 ); + + return $this; + } + + /** + * Add a new change, to flip an image vertically. + * + * @return $this + */ + public function flip_vertical() { + $this->changes[] = array( 'f' => 1 ); + + return $this; + } + + /** + * Add a new change to the image to flip it right. + * + * @return $this + */ + public function flip_right() { + $this->changes[] = array( 'f' => 2 ); + + return $this; + } + + /** + * Store a crop change for an image. + * + * @param int $width The width of the crop. + * @param int $height The height of the crop. + * @param int $x The X position on the axis where the image would be cropped. + * @param int $y The Y position on the axis where the image would be cropped. + * + * @return $this + */ + public function crop( $width, $height, $x, $y ) { + $this->changes[] = array( + 'c' => array( + 'x' => (int) $x, + 'y' => (int) $y, + 'w' => (int) $width, + 'h' => (int) $height, + ), + ); + + return $this; + } + + /** + * Set the target of the edits to all the image sizes. + * + * @return $this + */ + public function all() { + $this->target = 'all'; + + return $this; + } + + /** + * Set the target of the edit only to the thumbnail image. + * + * @return $this + */ + public function only_thumbnail() { + $this->target = 'thumbnail'; + + return $this; + } + + /** + * Set the target to all image sizes except the thumbnail. + * + * @return $this + */ + public function all_except_thumbnail() { + $this->target = 'nothumb'; + + return $this; + } + + /** + * Setup the $_REQUEST global so `wp_save_image` can process the image with the same editions + * performend into an image as it was performed from the editor. + * + * @see wp_save_image + * + * @return stdClass The operation resulted from calling `wp_save_image` + */ + public function save() { + $_REQUEST['target'] = $this->target; + $_REQUEST['history'] = wp_slash( wp_json_encode( $this->changes ) ); + + if ( ! function_exists( 'wp_save_image' ) ) { + include_once ABSPATH . 'wp-admin/includes/image-edit.php'; + } + + $this->result = wp_save_image( $this->attachment_id ); + + return $this->result; + } + + /** + * Determine if the last operation executed to edit the image was successfully or not. + * + * @return bool whether the operation to save the image was succesfully or not. + */ + public function success() { + if ( ! is_object( $this->result ) ) { + return false; + } + + $valid_target = true; + // The thumbnail property is only set in `all` and `thumbnail` target. + if ( 'all' === $this->target || 'thumbnail' === $this->target ) { + $valid_target = property_exists( $this->result, 'thumbnail' ); + } + + return property_exists( $this->result, 'msg' ) && $valid_target && 'Image saved' === $this->result->msg; + } +} From e2d7c5f55ab656be5ae097c3f852ce73c58e2260 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 29 Mar 2022 17:59:57 +0530 Subject: [PATCH 28/54] Add test for source attribute post editing --- .../images/webp-uploads/webp-uploads-test.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 93084b0593..ead405bb2d 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -783,4 +783,27 @@ function( $transforms ) { $this->assertImageNotHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); } } + + /** + * Update source attributes when webp is edited. + * + * @test + */ + public function it_should_validate_source_attribute_update_when_webp_edited() { + + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + + $editor = new WP_Image_Edit( $attachment_id ); + $editor->crop( 10, 10, 0, 0 )->save(); + $this->assertTrue( $editor->success() ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertArrayHasKey( 'sizes', $metadata ); + + foreach ( $metadata['sizes'] as $properties ) { + $this->assertArrayHasKey( 'sources', $properties ); + } + } } From 1aee915f586516e7042d1b082d7331bf51de3938 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 29 Mar 2022 18:20:40 +0530 Subject: [PATCH 29/54] Ignore autogenerated file from PHPCS rule --- phpcs.xml.dist | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index f542112bef..cfdc9b1708 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -19,6 +19,9 @@ ./tests + + module-i18n.php + tests/* From 5790b8027e9a4216de2709304b251dbba413bcab Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 29 Mar 2022 18:30:21 +0530 Subject: [PATCH 30/54] Fix PHPCS errors --- .../images/webp-uploads/webp-uploads-test.php | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index f6c3de9dd5..022dfdf860 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -814,22 +814,20 @@ function( $transforms ) { * * @test */ - public function it_should_validate_source_attribute_update_when_webp_edited() - { - - $attachment_id = $this->factory->attachment->create_upload_object(TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'); + public function it_should_validate_source_attribute_update_when_webp_edited() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); - $editor = new WP_Image_Edit($attachment_id); - $editor->crop(10, 10, 0, 0)->save(); - $this->assertTrue($editor->success()); + $editor = new WP_Image_Edit( $attachment_id ); + $editor->crop( 10, 10, 0, 0 )->save(); + $this->assertTrue( $editor->success() ); - $metadata = wp_get_attachment_metadata($attachment_id); + $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertArrayHasKey('sources', $metadata); - $this->assertArrayHasKey('sizes', $metadata); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertArrayHasKey( 'sizes', $metadata ); - foreach ($metadata['sizes'] as $properties) { - $this->assertArrayHasKey('sources', $properties); + foreach ( $metadata['sizes'] as $properties ) { + $this->assertArrayHasKey( 'sources', $properties ); } } From 8db0827acae1246b2c7eadb3085ed704459a8eb4 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Wed, 30 Mar 2022 15:14:43 +0530 Subject: [PATCH 31/54] Add new assertions to tests --- .../images/webp-uploads/webp-uploads-test.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 022dfdf860..6b5317c47d 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -818,16 +818,27 @@ public function it_should_validate_source_attribute_update_when_webp_edited() { $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); $editor = new WP_Image_Edit( $attachment_id ); - $editor->crop( 10, 10, 0, 0 )->save(); + $editor->crop( 1000, 200, 0, 0 )->save(); $this->assertTrue( $editor->success() ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $metadata = wp_get_attachment_metadata( $attachment_id ); + $this->assertRegExp( '/e\d{13}/', $metadata['sources']['image/webp']['file'] ); + $this->assertRegExp( '/e\d{13}/', $metadata['sources']['image/jpeg']['file'] ); + $this->assertArrayHasKey( 'sources', $metadata ); $this->assertArrayHasKey( 'sizes', $metadata ); - foreach ( $metadata['sizes'] as $properties ) { + foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertArrayHasKey( 'sources', $properties ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + + $this->assertRegExp( '/e\d{13}/', $properties['sources']['image/webp']['file'] ); + $this->assertRegExp( '/e\d{13}/', $properties['sources']['image/jpeg']['file'] ); } } From 93cc08c7b8ed56b91f87d0f0f868ac128407b55e Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 31 Mar 2022 18:59:22 -0600 Subject: [PATCH 32/54] Add missing `@since` tag --- modules/images/webp-uploads/load.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d3938096f3..d4cae362be 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -687,6 +687,8 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos /** * Adds sources to metadata for an attachment. * + * @since n.e.x.t + * * @param array $metadata Metadata of the attachment. * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. * @param array $allowed_mimes Allowed mime types. From 8ebefa8a66e89c69df38178869a2ff372f36134b Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 31 Mar 2022 18:59:48 -0600 Subject: [PATCH 33/54] Add missing override --- modules/images/webp-uploads/load.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d4cae362be..256c6dfd4c 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -789,6 +789,7 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ $image->multi_resize( $_sizes ); } + add_filter( 'wp_update_attachment_metadata', function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $allowed_mimes, $file ) { @@ -801,5 +802,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $a 10, 2 ); + + return $override; } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From 15e07d7732773afc819e33722a67d506908d6cd2 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 31 Mar 2022 19:01:54 -0600 Subject: [PATCH 34/54] Replace `$override` with plain `null` value instead --- modules/images/webp-uploads/load.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 256c6dfd4c..abec9efc26 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -752,7 +752,7 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ } if ( empty( webp_uploads_get_upload_image_mime_transforms()[ $mime_type ] ) ) { - return $override; + return null; } $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; @@ -767,18 +767,18 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ } if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - return $override; + return null; } if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - return $override; + return null; } $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; if ( is_wp_error( $image->save( $destination, $targeted_mime ) ) ) { - return $override; + return null; } $_sizes = array(); @@ -803,6 +803,6 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $a 2 ); - return $override; + return null; } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); From b0aee6d9ca8684409a2c8a4da8602f4b2dfeccba Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Fri, 1 Apr 2022 11:28:00 -0600 Subject: [PATCH 35/54] Add helpers for tests --- .../images/webp-uploads/webp-uploads-test.php | 12 +++++----- tests/utils/TestCase/ImagesTestCase.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 65aab11c7e..01c178f20d 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -990,8 +990,8 @@ public function it_should_validate_source_attribute_update_when_webp_edited() { $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertRegExp( '/e\d{13}/', $metadata['sources']['image/webp']['file'] ); - $this->assertRegExp( '/e\d{13}/', $metadata['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsEdited( $metadata['sources']['image/webp']['file'] ); + $this->assertFileNameIsEdited( $metadata['sources']['image/jpeg']['file'] ); $this->assertArrayHasKey( 'sources', $metadata ); $this->assertArrayHasKey( 'sizes', $metadata ); @@ -1001,8 +1001,8 @@ public function it_should_validate_source_attribute_update_when_webp_edited() { $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); - $this->assertRegExp( '/e\d{13}/', $properties['sources']['image/webp']['file'] ); - $this->assertRegExp( '/e\d{13}/', $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] ); } } @@ -1091,7 +1091,7 @@ public function it_should_u_se_the_next_available_hash_for_the_full_size_image_o remove_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ); $editor->rotate_right()->save(); - $this->assertRegExp( '/full-\d{13}/', webp_uploads_get_next_full_size_key_from_backup( $attachment_id ) ); + $this->assertSizeNameIsHashed( 'full', webp_uploads_get_next_full_size_key_from_backup( $attachment_id ) ); } /** @@ -1139,7 +1139,7 @@ public function it_should_store_the_metadata_on_the_next_available_hash() { $backup_sources_keys = array_keys( $backup_sources ); $this->assertSame( 'full-orig', reset( $backup_sources_keys ) ); - $this->assertRegExp( '/full-\d{13}/', end( $backup_sources_keys ) ); + $this->assertSizeNameIsHashed( 'full', end( $backup_sources_keys ) ); $this->assertSame( $sources, end( $backup_sources ) ); } diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index 0deb152bc9..9f83577eff 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -13,6 +13,8 @@ * @method void assertImageHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image has the appropriate source for the subsize. * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. + * @method void assertFileNameIsEdited( string $filename ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -76,4 +78,24 @@ public static function assertImageNotHasSizeSource( $attachment_id, $size, $mime self::assertThat( $attachment_id, $constraint, $message ); } + /** + * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * + * @param string $filename The name of the filename to be asserted. + * @return void + */ + public static function assertFileNameIsEdited( $filename ) { + self::assertRegExp( '/e\d{13}/', $filename ); + } + + /** + * Asserts that the provided size name is an edited name that contains a hash with digits. + * + * @param string $size_name The size name we are looking for. + * @param string $hashed_size_name The current size name we are comparing against. + * @return void + */ + public static function assertSizeNameIsHashed( $size_name, $hashed_size_name ) { + self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name ); + } } From 54ca7ccf281cb7e926ac0d8ec11c0ede5497de0b Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 4 Apr 2022 18:53:19 -0500 Subject: [PATCH 36/54] Remove not required empty space --- modules/images/webp-uploads/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3788657a7d..6f9727950b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -733,7 +733,6 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe } /** - * * Creates additional image formats when original image is edited. * * @since n.e.x.t From 9f41f7587eb13c1013f3f4dd477a167f7f056e49 Mon Sep 17 00:00:00 2001 From: Kirtan Gajjar Date: Tue, 5 Apr 2022 17:50:55 +0530 Subject: [PATCH 37/54] Address all the relavant feedback given by reviewers --- modules/images/webp-uploads/load.php | 89 +++++++++++++++------------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index e46ad7f9c5..20fb46b429 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -691,18 +691,18 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos * * @param array $metadata Metadata of the attachment. * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. - * @param array $allowed_mimes Allowed mime types. * @param string $file Path to original file. + * @param array $main_images Path of all main image files of all mime types. + * @param array $subsized_images Path of all subsized image file of all mime types. + * * @return array Metadata with sources added. */ -function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowed_mimes, $file ) { +function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ) { $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); foreach ( $valid_mime_transforms as $targeted_mime ) { // Add sources to original image metadata. - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $filename_without_extension = pathinfo( $file, PATHINFO_FILENAME ); - $image_file = path_join( $original_directory, "$filename_without_extension.{$extension[0]}" ); + $image_file = $main_images[ $targeted_mime ]['path']; if ( ! file_exists( $image_file ) ) { continue; @@ -715,8 +715,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowe foreach ( $metadata['sizes'] as $size => $size_details ) { // Add sources to resized image metadata. - $filename_without_extension = pathinfo( $size_details['file'], PATHINFO_FILENAME ); - $image_file = path_join( $original_directory, "$filename_without_extension.{$extension[0]}" ); + $image_file = $original_directory . '/' . $subsized_images[ $targeted_mime ][ $size ]['file']; if ( ! file_exists( $image_file ) ) { continue; @@ -755,49 +754,57 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ return null; } - $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $sizes = wp_get_registered_image_subsizes(); - $allowed_mimes = array_flip( wp_get_mime_types() ); - - foreach ( $valid_mime_transforms as $targeted_mime ) { - if ( $targeted_mime === $mime_type ) { - continue; - } + add_filter( + 'wp_update_attachment_metadata', + function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image ) { + if ( $post_meta_id !== $post_id ) { + return $metadata; + } - if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - return null; - } + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $allowed_mimes = array_flip( wp_get_mime_types() ); + $old_metadata = wp_get_attachment_metadata( $post_id ); + $resize_sizes = array(); + $main_images = array(); + $subsized_images = array(); + + foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { + if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && + $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { + $resize_sizes[ $size_name ] = $metadata['sizes'][ $size_name ]; + } + } - if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - return null; - } + foreach ( $valid_mime_transforms as $targeted_mime ) { + if ( $targeted_mime === $mime_type ) { + $main_images[ $targeted_mime ] = array( 'path' => $file ); + $subsized_images[ $targeted_mime ] = $metadata['sizes']; + continue; + } - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; + if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { + return null; + } - if ( is_wp_error( $image->save( $destination, $targeted_mime ) ) ) { - return null; - } + if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { + return null; + } - $_sizes = array(); + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - foreach ( $sizes as $size => $size_details ) { - $_sizes[ $size ] = $size_details; - } + $main_images[ $targeted_mime ] = $image->save( $destination, $targeted_mime ); - $image->multi_resize( $_sizes ); - } + if ( is_wp_error( $main_images[ $targeted_mime ] ) ) { + return null; + } - add_filter( - 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $allowed_mimes, $file ) { - if ( $post_meta_id !== $post_id ) { - return $metadata; + $subsized_images[ $targeted_mime ] = $image->multi_resize( $resize_sizes ); } - return webp_uploads_update_sources( $metadata, $valid_mime_transforms, $allowed_mimes, $file ); + return webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ); }, 10, 2 @@ -805,7 +812,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $valid_mime_transforms, $a return null; } -add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); +add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 7 ); /** * Inspect if the current call to `wp_update_attachment_metadata()` was done from within the context From dd5585a8ac5528f0043c908cea35917446742f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Thu, 7 Apr 2022 16:26:09 -0500 Subject: [PATCH 38/54] Add empty line. Co-authored-by: Felix Arntz --- modules/images/webp-uploads/load.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 385eda36bc..39f61fd900 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -694,8 +694,7 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos * @param string $file Path to original file. * @param array $main_images Path of all main image files of all mime types. * @param array $subsized_images Path of all subsized image file of all mime types. - * - * @return array Metadata with sources added. + * @return array Metadata with sources added. */ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ) { $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); From 242a5758bfbd1129cd8892ad746acd14530e2dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Thu, 7 Apr 2022 16:29:44 -0500 Subject: [PATCH 39/54] Add description to the returned value. Co-authored-by: Felix Arntz --- modules/images/webp-uploads/load.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 39f61fd900..cc0d9e3eab 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -740,8 +740,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, * @param WP_Image_Editor $image The image editor instance. * @param string $mime_type The mime type of the image. * @param int $post_id Attachment post ID. - * - * @return bool|null + * @return bool|null Potentially modified $override value. */ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { if ( null !== $override ) { From ebb4ce94be410ee0caafd872969bcbb61792da81 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 7 Apr 2022 16:39:45 -0500 Subject: [PATCH 40/54] Allow to continue the flow in case of a failure If a single image mime type is not supported allow for a continuation on the flow instead of abrutly stopping in the first mime that is no supported. --- modules/images/webp-uploads/load.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index cc0d9e3eab..9b55597adb 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -782,11 +782,11 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image } if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - return null; + continue; } if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { - return null; + continue; } $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); @@ -795,7 +795,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image $main_images[ $targeted_mime ] = $image->save( $destination, $targeted_mime ); if ( is_wp_error( $main_images[ $targeted_mime ] ) ) { - return null; + continue; } $subsized_images[ $targeted_mime ] = $image->multi_resize( $resize_sizes ); From b1015091a8077490ea264a2c2b1e298b60820fc7 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 7 Apr 2022 17:16:26 -0500 Subject: [PATCH 41/54] Update code following code review feedback --- modules/images/webp-uploads/load.php | 35 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 9b55597adb..0b38ee9b2b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -737,12 +737,12 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, * * @param bool|null $override Value to return instead of saving. Default null. * @param string $file Name of the file to be saved. - * @param WP_Image_Editor $image The image editor instance. + * @param WP_Image_Editor $editor The image editor instance. * @param string $mime_type The mime type of the image. * @param int $post_id Attachment post ID. * @return bool|null Potentially modified $override value. */ -function webp_uploads_update_image_onchange( $override, $file, $image, $mime_type, $post_id ) { +function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_type, $post_id ) { if ( null !== $override ) { return $override; } @@ -753,19 +753,18 @@ function webp_uploads_update_image_onchange( $override, $file, $image, $mime_typ add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor ) { if ( $post_meta_id !== $post_id ) { return $metadata; } - $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $allowed_mimes = array_flip( wp_get_mime_types() ); - $old_metadata = wp_get_attachment_metadata( $post_id ); - $resize_sizes = array(); - $main_images = array(); - $subsized_images = array(); + // No sizes to be created. + if ( empty( $metadata['sizes'] ) ) { + return $metadata; + } + + $old_metadata = wp_get_attachment_metadata( $post_id ); + $resize_sizes = array(); foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && @@ -774,6 +773,12 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image } } + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; + $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $main_images = array(); + $subsized_images = array(); foreach ( $valid_mime_transforms as $targeted_mime ) { if ( $targeted_mime === $mime_type ) { $main_images[ $targeted_mime ] = array( 'path' => $file ); @@ -785,20 +790,20 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $image continue; } - if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) { + if ( ! $editor::supports_mime_type( $targeted_mime ) ) { continue; } $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - $main_images[ $targeted_mime ] = $image->save( $destination, $targeted_mime ); + $result = $editor->save( $destination, $targeted_mime ); - if ( is_wp_error( $main_images[ $targeted_mime ] ) ) { + if ( is_wp_error( $result ) ) { continue; } - $subsized_images[ $targeted_mime ] = $image->multi_resize( $resize_sizes ); + $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } return webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ); From 1f83396211337229c1a7b224099f620e7ca87d00 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Thu, 7 Apr 2022 17:23:23 -0500 Subject: [PATCH 42/54] Use existing transforms as a variable Instead of calling the function multiple times --- modules/images/webp-uploads/load.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 0b38ee9b2b..48c30f9653 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -747,13 +747,15 @@ function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_ty return $override; } - if ( empty( webp_uploads_get_upload_image_mime_transforms()[ $mime_type ] ) ) { + $transforms = webp_uploads_get_upload_image_mime_transforms(); + if ( empty( $transforms[ $mime_type ] ) ) { return null; } + $mime_transforms = $transforms[ $mime_type ]; add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor, $mime_transforms ) { if ( $post_meta_id !== $post_id ) { return $metadata; } @@ -773,13 +775,12 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor } } - $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms()[ $mime_type ]; - $allowed_mimes = array_flip( wp_get_mime_types() ); - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $main_images = array(); - $subsized_images = array(); - foreach ( $valid_mime_transforms as $targeted_mime ) { + $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $main_images = array(); + $subsized_images = array(); + foreach ( $mime_transforms as $targeted_mime ) { if ( $targeted_mime === $mime_type ) { $main_images[ $targeted_mime ] = array( 'path' => $file ); $subsized_images[ $targeted_mime ] = $metadata['sizes']; @@ -806,7 +807,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } - return webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ); + return webp_uploads_update_sources( $metadata, $mime_transforms, $file, $main_images, $subsized_images ); }, 10, 2 From 019531aa1bdfd995055c66acf3e1f9db28d79fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Thu, 7 Apr 2022 17:24:36 -0500 Subject: [PATCH 43/54] Use file property 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 0b38ee9b2b..6e6db9422f 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -721,7 +721,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, } $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( - 'file' => wp_basename( $image_file ), + 'file' => $subsized_images[ $targeted_mime ][ $size ]['file'], 'filesize' => filesize( $image_file ), ); } From 6d30ccb869938c0f3fae9f9a06b3906c5952557a Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Fri, 8 Apr 2022 18:18:08 -0500 Subject: [PATCH 44/54] Update existing mechanism to generate the edited sources Reuse the same function for initial creation but with the context addition. So the logic can be adjust based on the update mechanism in order to prevent infinite loops and reset the values of each existing mime type if was already defined. --- modules/images/webp-uploads/load.php | 198 ++++-------------- .../images/webp-uploads/webp-uploads-test.php | 35 +++- tests/utils/TestCase/ImagesTestCase.php | 14 +- 3 files changed, 84 insertions(+), 163 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d7099df98b..b6a5b175ba 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -23,11 +23,12 @@ * @see wp_generate_attachment_metadata() * @see webp_uploads_get_upload_image_mime_transforms() * - * @param array $metadata An array with the metadata from this attachment. - * @param int $attachment_id The ID of the attachment where the hook was dispatched. + * @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. + * @param string $context The context of the current operation either: create or update. * @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 ) { +function webp_uploads_create_sources_property( array $metadata, $attachment_id, $context = 'create' ) { // This should take place only on the JPEG image. $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); @@ -37,7 +38,21 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) return $metadata; } - $file = get_attached_file( $attachment_id, true ); + $is_update = 'update' === $context; + if ( $is_update ) { + if ( empty( $metadata['file'] ) ) { + return $metadata; + } + $uploads = wp_get_upload_dir(); + if ( isset( $uploads['basedir'] ) ) { + $file = path_join( $uploads['basedir'], $metadata['file'] ); + } else { + return $metadata; + } + } else { + $file = get_attached_file( $attachment_id, true ); + } + // File does not exist. if ( ! file_exists( $file ) ) { return $metadata; @@ -48,15 +63,18 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $metadata['sources'] = array(); } - if ( - empty( $metadata['sources'][ $mime_type ] ) && - in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) - ) { - $metadata['sources'][ $mime_type ] = array( - 'file' => wp_basename( $file ), - 'filesize' => filesize( $file ), - ); - wp_update_attachment_metadata( $attachment_id, $metadata ); + if ( $is_update && has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { + remove_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ); + } + + if ( in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) ) { + if ( empty( $metadata['sources'][ $mime_type ] ) || $is_update ) { + $metadata['sources'][ $mime_type ] = array( + 'file' => wp_basename( $file ), + 'filesize' => filesize( $file ), + ); + wp_update_attachment_metadata( $attachment_id, $metadata ); + } } $original_size_data = array( @@ -71,8 +89,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) // 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 ] ) ) { + // If this property exists no need to create the image again unless is an update. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { continue; } @@ -95,6 +113,11 @@ 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'] ) ) { + + if ( $is_update && ! has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { + add_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata', 10, 3 ); + } + return $metadata; } @@ -122,7 +145,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $properties['sources'] = array(); } - if ( empty( $properties['sources'][ $current_mime ] ) ) { + if ( empty( $properties['sources'][ $current_mime ] ) || $is_update ) { $properties['sources'][ $current_mime ] = array( 'file' => isset( $properties['file'] ) ? $properties['file'] : '', 'filesize' => 0, @@ -138,7 +161,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) { // If this property exists no need to create the image again. - if ( ! empty( $properties['sources'][ $mime ] ) ) { + if ( ! empty( $properties['sources'][ $mime ] ) && ! $is_update ) { continue; } @@ -155,9 +178,13 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id ) $metadata['sizes'][ $size_name ] = $properties; } + if ( $is_update && ! has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { + add_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata', 10, 3 ); + } + return $metadata; } -add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 ); +add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 3 ); /** * Filter the image editor default output format mapping to select the most appropriate @@ -684,139 +711,6 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos } add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); -/** - * Adds sources to metadata for an attachment. - * - * @since n.e.x.t - * - * @param array $metadata Metadata of the attachment. - * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. - * @param string $file Path to original file. - * @param array $main_images Path of all main image files of all mime types. - * @param array $subsized_images Path of all subsized image file of all mime types. - * @return array Metadata with sources added. - */ -function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ) { - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - - foreach ( $valid_mime_transforms as $targeted_mime ) { - // Add sources to original image metadata. - $image_file = $main_images[ $targeted_mime ]['path']; - - if ( ! file_exists( $image_file ) ) { - continue; - } - - $metadata['sources'][ $targeted_mime ] = array( - 'file' => wp_basename( $image_file ), - 'filesize' => filesize( $image_file ), - ); - - foreach ( $metadata['sizes'] as $size => $size_details ) { - // Add sources to resized image metadata. - $image_file = $original_directory . '/' . $subsized_images[ $targeted_mime ][ $size ]['file']; - - if ( ! file_exists( $image_file ) ) { - continue; - } - - $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( - 'file' => $subsized_images[ $targeted_mime ][ $size ]['file'], - 'filesize' => filesize( $image_file ), - ); - } - } - - return $metadata; -} - -/** - * Creates additional image formats when original image is edited. - * - * @since n.e.x.t - * - * @param bool|null $override Value to return instead of saving. Default null. - * @param string $file Name of the file to be saved. - * @param WP_Image_Editor $editor The image editor instance. - * @param string $mime_type The mime type of the image. - * @param int $post_id Attachment post ID. - * @return bool|null Potentially modified $override value. - */ -function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_type, $post_id ) { - if ( null !== $override ) { - return $override; - } - - $transforms = webp_uploads_get_upload_image_mime_transforms(); - if ( empty( $transforms[ $mime_type ] ) ) { - return null; - } - - $mime_transforms = $transforms[ $mime_type ]; - add_filter( - 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor, $mime_transforms ) { - if ( $post_meta_id !== $post_id ) { - return $metadata; - } - - // No sizes to be created. - if ( empty( $metadata['sizes'] ) ) { - return $metadata; - } - - $old_metadata = wp_get_attachment_metadata( $post_id ); - $resize_sizes = array(); - - foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { - if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && - $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { - $resize_sizes[ $size_name ] = $metadata['sizes'][ $size_name ]; - } - } - - $allowed_mimes = array_flip( wp_get_mime_types() ); - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); - $main_images = array(); - $subsized_images = array(); - foreach ( $mime_transforms as $targeted_mime ) { - if ( $targeted_mime === $mime_type ) { - $main_images[ $targeted_mime ] = array( 'path' => $file ); - $subsized_images[ $targeted_mime ] = $metadata['sizes']; - continue; - } - - if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { - continue; - } - - if ( ! $editor::supports_mime_type( $targeted_mime ) ) { - continue; - } - - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - - $result = $editor->save( $destination, $targeted_mime ); - - if ( is_wp_error( $result ) ) { - continue; - } - - $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); - } - - return webp_uploads_update_sources( $metadata, $mime_transforms, $file, $main_images, $subsized_images ); - }, - 10, - 2 - ); - - return null; -} -add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 7 ); - /** * Inspect if the current call to `wp_update_attachment_metadata()` was done from within the context * of an edit to an attachment either restore or other type of edit, in that case we perform operations @@ -841,7 +735,7 @@ function webp_uploads_update_attachment_metadata( $data, $attachment_id ) { switch ( $element['function'] ) { case 'wp_save_image': // Right after an image has been edited. - return webp_uploads_backup_sources( $attachment_id, $data ); + return webp_uploads_create_sources_property( webp_uploads_backup_sources( $attachment_id, $data ), $attachment_id, 'update' ); case 'wp_restore_image': // When an image has been restored. return webp_uploads_restore_image( $attachment_id, $data ); diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 5edbb4c635..74ab3055d2 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -873,15 +873,31 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag wp_restore_image( $attachment_id ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertSame( $backup_sources['full-orig'], $metadata['sources'] ); $this->assertSame( $backup_sources, get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ) ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); - foreach ( $metadata['sizes'] as $size_name => $properties ) { - $this->assertArrayHasKey( 'sources', $backup_sizes[ $size_name . '-orig' ] ); - $this->assertSame( $backup_sizes[ $size_name . '-orig' ]['sources'], $properties['sources'] ); + foreach ( $backup_sizes as $size_name => $properties ) { + // We are only interested in the original filenames to be compared against the backup and restored values. + if ( false === strpos( $size_name, '-orig' ) ) { + $this->assertSizeNameIsHashed( '', $size_name, "{$size_name} is not a valid edited name" ); + continue; + } + + $size_name = str_replace( '-orig', '', $size_name ); + // Full name is verified above. + if ( 'full' === $size_name ) { + continue; + } + + $this->assertArrayHasKey( $size_name, $metadata['sizes'] ); + $this->assertArrayHasKey( 'sources', $metadata['sizes'][ $size_name ] ); + $this->assertSame( $properties['sources'], $metadata['sizes'][ $size_name ]['sources'] ); } } @@ -959,7 +975,16 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ $this->assertArrayHasKey( 'full-orig', $backup_sources ); $this->assertSame( $metadata['sources'], $backup_sources['full-orig'] ); - $this->assertArrayNotHasKey( 'sources', wp_get_attachment_metadata( $attachment_id ), 'The sources attributes was not removed from the metadata.' ); + $updated_metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'sources', $updated_metadata ); + $this->assertNotSame( $metadata['sources'], $updated_metadata['sources'] ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + foreach ( $updated_metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + } $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index 9f83577eff..375c6eab41 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -13,8 +13,8 @@ * @method void assertImageHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image has the appropriate source for the subsize. * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. - * @method void assertFileNameIsEdited( string $filename ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. - * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name ) Asserts that the provided size name is an edited name that contains a hash with digits. + * @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -82,10 +82,11 @@ public static function assertImageNotHasSizeSource( $attachment_id, $size, $mime * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. * * @param string $filename The name of the filename to be asserted. + * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertFileNameIsEdited( $filename ) { - self::assertRegExp( '/e\d{13}/', $filename ); + public static function assertFileNameIsEdited( $filename, $message = '' ) { + self::assertRegExp( '/e\d{13}/', $filename, $message ); } /** @@ -93,9 +94,10 @@ public static function assertFileNameIsEdited( $filename ) { * * @param string $size_name The size name we are looking for. * @param string $hashed_size_name The current size name we are comparing against. + * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertSizeNameIsHashed( $size_name, $hashed_size_name ) { - self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name ); + public static function assertSizeNameIsHashed( $size_name, $hashed_size_name, $message = '' ) { + self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name, $message ); } } From b32ee73dc122b4235fc288eea7fefdb4e408c352 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Fri, 8 Apr 2022 19:58:09 -0500 Subject: [PATCH 45/54] Introduce support for target `thumbnail`o when the only modified image size is the `thumbnail` make sure the changes are applied appropiately to the specifeid image size only with the file from that particular image size. --- modules/images/webp-uploads/load.php | 93 +++++++++++++------ .../images/webp-uploads/webp-uploads-test.php | 25 ++++- tests/utils/TestCase/ImagesTestCase.php | 12 +++ 3 files changed, 100 insertions(+), 30 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index b6a5b175ba..8c54d66a92 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -39,6 +39,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } $is_update = 'update' === $context; + $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; if ( $is_update ) { if ( empty( $metadata['file'] ) ) { return $metadata; @@ -53,8 +54,8 @@ 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 ) ) { + // File does not exist and we are not editing only the thumbnail. + if ( 'thumbnail' !== $target && ! file_exists( $file ) ) { return $metadata; } @@ -68,7 +69,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } if ( in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) ) { - if ( empty( $metadata['sources'][ $mime_type ] ) || $is_update ) { + if ( empty( $metadata['sources'][ $mime_type ] ) || ( $is_update && 'thumbnail' !== $target ) ) { $metadata['sources'][ $mime_type ] = array( 'file' => wp_basename( $file ), 'filesize' => filesize( $file ), @@ -87,28 +88,30 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $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 unless is an update. - if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { - continue; - } + // Create the sources for the full sized image only if the target is not the thumbnail only. + if ( 'thumbnail' !== $target ) { + foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { + // If this property exists no need to create the image again unless is an update. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { + continue; + } - // The targeted mime is not allowed in the current installation. - if ( empty( $allowed_mimes[ $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 ); + $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; - } + if ( is_wp_error( $image ) ) { + continue; + } - $metadata['sources'][ $targeted_mime ] = $image; - wp_update_attachment_metadata( $attachment_id, $metadata ); + $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. @@ -122,6 +125,11 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $metadata['sizes'] as $size_name => $properties ) { + + if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) { + continue; + } + // This image size is not defined or not an array. if ( ! is_array( $properties ) ) { continue; @@ -160,12 +168,32 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) { - // If this property exists no need to create the image again. + // If this property exists no need to create the image again unless is an update with a different mime. if ( ! empty( $properties['sources'][ $mime ] ) && ! $is_update ) { continue; } - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); + if ( $mime === $current_mime ) { + continue; + } + + if ( 'update' === $context && 'thumbnail' === $target ) { + /** + * When only the thumbnail requires additional image, make sure that the base image to create additional + * mime types is the thumbnail with the original mime type due this image is the only one that was modified + * using the attached image or original image would be. The filename should match the original image with + * the only difference of the extension on the filename instead, so the new created image does not have multiple + * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp + */ + $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); + $target_extension = explode( '|', $allowed_mimes[ $mime ] ); + $file_path = path_join( $original_directory, $properties['file'] ); + $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); + } else { + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); + } + if ( is_wp_error( $source ) ) { continue; } @@ -229,13 +257,15 @@ function webp_uploads_filter_image_editor_output_format( $output_format, $filena * * @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. + * @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. + * @param string $file_path The path to the file used to create the image with. + * @param string $destination_file_name The name used to store the created file it should include the full path. * * @return array|WP_Error */ -function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { +function webp_uploads_generate_image_size( $attachment_id, $size, $mime, $file_path = null, $destination_file_name = null ) { $sizes = wp_get_registered_image_subsizes(); $metadata = wp_get_attachment_metadata( $attachment_id ); @@ -251,6 +281,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { 'width' => 0, 'height' => 0, 'crop' => false, + 'file' => $file_path, ); if ( isset( $sizes[ $size ]['width'] ) ) { @@ -269,7 +300,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { $size_data['crop'] = (bool) $sizes[ $size ]['crop']; } - return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime ); + return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime, $destination_file_name ); } /** @@ -340,7 +371,11 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); } - $image_path = wp_get_original_image_path( $attachment_id ); + if ( empty( $size_data['file'] ) ) { + $image_path = wp_get_original_image_path( $attachment_id ); + } else { + $image_path = $size_data['file']; + } // File does not exist. if ( ! file_exists( $image_path ) ) { diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 74ab3055d2..565b148056 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -954,7 +954,30 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum $this->assertArrayHasKey( 'sources', $backup_sizes['thumbnail-orig'] ); $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertArrayHasKey( 'sources', $metadata ); + + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' ); + + $this->assertFileNameIsNotEdited( $metadata['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $metadata['sources']['image/webp']['file'] ); + + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + + if ( 'thumbnail' === $size_name ) { + $this->assertSame( $properties['file'], $properties['sources']['image/jpeg']['file'] ); + $this->assertSame( str_replace( '.jpg', '.webp', $properties['file'] ), $properties['sources']['image/webp']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] ); + } else { + $this->assertFileNameIsNotEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $properties['sources']['image/webp']['file'] ); + } + } } /** diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index 375c6eab41..cc44dec263 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -14,6 +14,7 @@ * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. * @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertFileNameIsNotEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -89,6 +90,17 @@ public static function assertFileNameIsEdited( $filename, $message = '' ) { self::assertRegExp( '/e\d{13}/', $filename, $message ); } + /** + * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * + * @param string $filename The name of the filename to be asserted. + * @param string $message The Error message used to display when the assertion fails. + * @return void + */ + public static function assertFileNameIsNotEdited( $filename, $message = '' ) { + self::assertNotRegExp( '/e\d{13}/', $filename, $message ); + } + /** * Asserts that the provided size name is an edited name that contains a hash with digits. * From 4ac0a91b63eec1de39209ecdf3621ef8fd564187 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Mon, 11 Apr 2022 06:39:47 -0500 Subject: [PATCH 46/54] Use the attached image on subsequent image edits When an edit takes place make sure that the source to create the image is the attached image and not the original image if present. Due every edit uses the attached version of the media library if attached and original image are different. --- modules/images/webp-uploads/load.php | 44 +++++++++------ .../images/webp-uploads/webp-uploads-test.php | 54 +++++++++++++++++++ 2 files changed, 81 insertions(+), 17 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 8c54d66a92..7145da9780 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -38,8 +38,9 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, return $metadata; } - $is_update = 'update' === $context; - $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; + $is_update = 'update' === $context; + $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; + $attached_file = get_attached_file( $attachment_id, true ); if ( $is_update ) { if ( empty( $metadata['file'] ) ) { return $metadata; @@ -51,7 +52,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, return $metadata; } } else { - $file = get_attached_file( $attachment_id, true ); + $file = $attached_file; } // File does not exist and we are not editing only the thumbnail. @@ -125,11 +126,16 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $metadata['sizes'] as $size_name => $properties ) { - + // WHen only the thumbnail is selected to be edited any other size is dismissed. if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) { continue; } + // When all the sizes except the thumbnail are edited the thumbnail is dismissed. + if ( 'nothumb' === $target && 'thumbnail' === $size_name ) { + continue; + } + // This image size is not defined or not an array. if ( ! is_array( $properties ) ) { continue; @@ -177,19 +183,23 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, continue; } - if ( 'update' === $context && 'thumbnail' === $target ) { - /** - * When only the thumbnail requires additional image, make sure that the base image to create additional - * mime types is the thumbnail with the original mime type due this image is the only one that was modified - * using the attached image or original image would be. The filename should match the original image with - * the only difference of the extension on the filename instead, so the new created image does not have multiple - * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp - */ - $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); - $target_extension = explode( '|', $allowed_mimes[ $mime ] ); - $file_path = path_join( $original_directory, $properties['file'] ); - $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); + if ( 'update' === $context ) { + if ( 'thumbnail' === $target ) { + /** + * When only the thumbnail requires additional image, make sure that the base image to create additional + * mime types is the thumbnail with the original mime type due this image is the only one that was modified + * using the attached image or original image would be. The filename should match the original image with + * the only difference of the extension on the filename instead, so the new created image does not have multiple + * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp + */ + $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); + $target_extension = explode( '|', $allowed_mimes[ $mime ] ); + $file_path = path_join( $original_directory, $properties['file'] ); + $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); + } else { + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $attached_file ); + } } else { $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); } diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 565b148056..b9ea7d4018 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -1021,6 +1021,60 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ } } + /** + * Use the attached image when updating subsequent images not the original version + * + * @test + */ + public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() { + // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test. + add_filter( + 'big_image_size_threshold', + function () { + // Due to the largest image size is 1024 and the image is 1080x720, 1050 is a good spot to create a scaled size for all images sizes. + return 1050; + } + ); + + $attachment_id = $this->factory->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' + ); + + $this->assertNotSame( wp_get_original_image_path( $attachment_id ), get_attached_file( $attachment_id ) ); + + $editor = new WP_Image_Edit( $attachment_id ); + $editor->flip_right()->all_except_thumbnail()->save(); + + $this->assertTrue( $editor->success() ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'original_image', $metadata ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + foreach ( $metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + $this->assertStringContainsString( '-scaled-', $properties['file'] ); + } + + $this->assertArrayHasKey( 'sizes', $metadata ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + foreach ( $properties['sources'] as $mime_type => $values ) { + if ( 'thumbnail' === $size_name ) { + $this->assertFileNameIsNotEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringNotContainsString( '-scaled-', $values['file'] ); + } else { + $this->assertFileNameIsEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringContainsString( '-scaled-', $values['file'] ); + } + } + } + } + /** * Update source attributes when webp is edited. * From 96f8ceb10b18fa7c6e8076602e57a2acfeb4a672 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 12 Apr 2022 21:55:20 -0500 Subject: [PATCH 47/54] Revert "Use the attached image on subsequent image edits" This reverts commit 4ac0a91b63eec1de39209ecdf3621ef8fd564187. --- modules/images/webp-uploads/load.php | 44 ++++++--------- .../images/webp-uploads/webp-uploads-test.php | 54 ------------------- 2 files changed, 17 insertions(+), 81 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 7145da9780..8c54d66a92 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -38,9 +38,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, return $metadata; } - $is_update = 'update' === $context; - $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; - $attached_file = get_attached_file( $attachment_id, true ); + $is_update = 'update' === $context; + $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; if ( $is_update ) { if ( empty( $metadata['file'] ) ) { return $metadata; @@ -52,7 +51,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, return $metadata; } } else { - $file = $attached_file; + $file = get_attached_file( $attachment_id, true ); } // File does not exist and we are not editing only the thumbnail. @@ -126,13 +125,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $metadata['sizes'] as $size_name => $properties ) { - // WHen only the thumbnail is selected to be edited any other size is dismissed. - if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) { - continue; - } - // When all the sizes except the thumbnail are edited the thumbnail is dismissed. - if ( 'nothumb' === $target && 'thumbnail' === $size_name ) { + if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) { continue; } @@ -183,23 +177,19 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, continue; } - if ( 'update' === $context ) { - if ( 'thumbnail' === $target ) { - /** - * When only the thumbnail requires additional image, make sure that the base image to create additional - * mime types is the thumbnail with the original mime type due this image is the only one that was modified - * using the attached image or original image would be. The filename should match the original image with - * the only difference of the extension on the filename instead, so the new created image does not have multiple - * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp - */ - $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); - $target_extension = explode( '|', $allowed_mimes[ $mime ] ); - $file_path = path_join( $original_directory, $properties['file'] ); - $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); - } else { - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $attached_file ); - } + if ( 'update' === $context && 'thumbnail' === $target ) { + /** + * When only the thumbnail requires additional image, make sure that the base image to create additional + * mime types is the thumbnail with the original mime type due this image is the only one that was modified + * using the attached image or original image would be. The filename should match the original image with + * the only difference of the extension on the filename instead, so the new created image does not have multiple + * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp + */ + $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); + $target_extension = explode( '|', $allowed_mimes[ $mime ] ); + $file_path = path_join( $original_directory, $properties['file'] ); + $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); } else { $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); } diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index b9ea7d4018..565b148056 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -1021,60 +1021,6 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ } } - /** - * Use the attached image when updating subsequent images not the original version - * - * @test - */ - public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() { - // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test. - add_filter( - 'big_image_size_threshold', - function () { - // Due to the largest image size is 1024 and the image is 1080x720, 1050 is a good spot to create a scaled size for all images sizes. - return 1050; - } - ); - - $attachment_id = $this->factory->attachment->create_upload_object( - TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' - ); - - $this->assertNotSame( wp_get_original_image_path( $attachment_id ), get_attached_file( $attachment_id ) ); - - $editor = new WP_Image_Edit( $attachment_id ); - $editor->flip_right()->all_except_thumbnail()->save(); - - $this->assertTrue( $editor->success() ); - - $metadata = wp_get_attachment_metadata( $attachment_id ); - - $this->assertArrayHasKey( 'original_image', $metadata ); - $this->assertArrayHasKey( 'sources', $metadata ); - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); - $this->assertImageHasSource( $attachment_id, 'image/webp' ); - - foreach ( $metadata['sources'] as $properties ) { - $this->assertFileNameIsEdited( $properties['file'] ); - $this->assertStringContainsString( '-scaled-', $properties['file'] ); - } - - $this->assertArrayHasKey( 'sizes', $metadata ); - foreach ( $metadata['sizes'] as $size_name => $properties ) { - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); - foreach ( $properties['sources'] as $mime_type => $values ) { - if ( 'thumbnail' === $size_name ) { - $this->assertFileNameIsNotEdited( $values['file'], "'{$size_name}' is not valid." ); - $this->assertStringNotContainsString( '-scaled-', $values['file'] ); - } else { - $this->assertFileNameIsEdited( $values['file'], "'{$size_name}' is not valid." ); - $this->assertStringContainsString( '-scaled-', $values['file'] ); - } - } - } - } - /** * Update source attributes when webp is edited. * From bbc43c7087bf8e1666e78ac98aef8be335fe6302 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 12 Apr 2022 21:55:20 -0500 Subject: [PATCH 48/54] Revert "Introduce support for target `thumbnail`o" This reverts commit b32ee73dc122b4235fc288eea7fefdb4e408c352. --- modules/images/webp-uploads/load.php | 93 ++++++------------- .../images/webp-uploads/webp-uploads-test.php | 25 +---- tests/utils/TestCase/ImagesTestCase.php | 12 --- 3 files changed, 30 insertions(+), 100 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 8c54d66a92..b6a5b175ba 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -39,7 +39,6 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } $is_update = 'update' === $context; - $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; if ( $is_update ) { if ( empty( $metadata['file'] ) ) { return $metadata; @@ -54,8 +53,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $file = get_attached_file( $attachment_id, true ); } - // File does not exist and we are not editing only the thumbnail. - if ( 'thumbnail' !== $target && ! file_exists( $file ) ) { + // File does not exist. + if ( ! file_exists( $file ) ) { return $metadata; } @@ -69,7 +68,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } if ( in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) ) { - if ( empty( $metadata['sources'][ $mime_type ] ) || ( $is_update && 'thumbnail' !== $target ) ) { + if ( empty( $metadata['sources'][ $mime_type ] ) || $is_update ) { $metadata['sources'][ $mime_type ] = array( 'file' => wp_basename( $file ), 'filesize' => filesize( $file ), @@ -88,30 +87,28 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $filename = pathinfo( $file, PATHINFO_FILENAME ); $allowed_mimes = array_flip( wp_get_mime_types() ); - // Create the sources for the full sized image only if the target is not the thumbnail only. - if ( 'thumbnail' !== $target ) { - foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) { - // If this property exists no need to create the image again unless is an update. - if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { - continue; - } - - // The targeted mime is not allowed in the current installation. - if ( empty( $allowed_mimes[ $targeted_mime ] ) ) { - continue; - } + // 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 unless is an update. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { + 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 ); + // The targeted mime is not allowed in the current installation. + if ( empty( $allowed_mimes[ $targeted_mime ] ) ) { + continue; + } - if ( is_wp_error( $image ) ) { - 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 ); - $metadata['sources'][ $targeted_mime ] = $image; - wp_update_attachment_metadata( $attachment_id, $metadata ); + 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. @@ -125,11 +122,6 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $metadata['sizes'] as $size_name => $properties ) { - - if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) { - continue; - } - // This image size is not defined or not an array. if ( ! is_array( $properties ) ) { continue; @@ -168,32 +160,12 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, } foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) { - // If this property exists no need to create the image again unless is an update with a different mime. + // If this property exists no need to create the image again. if ( ! empty( $properties['sources'][ $mime ] ) && ! $is_update ) { continue; } - if ( $mime === $current_mime ) { - continue; - } - - if ( 'update' === $context && 'thumbnail' === $target ) { - /** - * When only the thumbnail requires additional image, make sure that the base image to create additional - * mime types is the thumbnail with the original mime type due this image is the only one that was modified - * using the attached image or original image would be. The filename should match the original image with - * the only difference of the extension on the filename instead, so the new created image does not have multiple - * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp - */ - $original_extension = explode( '|', $allowed_mimes[ $current_mime ] ); - $target_extension = explode( '|', $allowed_mimes[ $mime ] ); - $file_path = path_join( $original_directory, $properties['file'] ); - $destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path ); - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination ); - } else { - $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); - } - + $source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime ); if ( is_wp_error( $source ) ) { continue; } @@ -257,15 +229,13 @@ function webp_uploads_filter_image_editor_output_format( $output_format, $filena * * @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. - * @param string $file_path The path to the file used to create the image with. - * @param string $destination_file_name The name used to store the created file it should include the full path. + * @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. * * @return array|WP_Error */ -function webp_uploads_generate_image_size( $attachment_id, $size, $mime, $file_path = null, $destination_file_name = null ) { +function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) { $sizes = wp_get_registered_image_subsizes(); $metadata = wp_get_attachment_metadata( $attachment_id ); @@ -281,7 +251,6 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime, $file_p 'width' => 0, 'height' => 0, 'crop' => false, - 'file' => $file_path, ); if ( isset( $sizes[ $size ]['width'] ) ) { @@ -300,7 +269,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime, $file_p $size_data['crop'] = (bool) $sizes[ $size ]['crop']; } - return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime, $destination_file_name ); + return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime ); } /** @@ -371,11 +340,7 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) ); } - if ( empty( $size_data['file'] ) ) { - $image_path = wp_get_original_image_path( $attachment_id ); - } else { - $image_path = $size_data['file']; - } + $image_path = wp_get_original_image_path( $attachment_id ); // File does not exist. if ( ! file_exists( $image_path ) ) { diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 565b148056..74ab3055d2 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -954,30 +954,7 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum $this->assertArrayHasKey( 'sources', $backup_sizes['thumbnail-orig'] ); $metadata = wp_get_attachment_metadata( $attachment_id ); - - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); - $this->assertImageHasSource( $attachment_id, 'image/webp' ); - - $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' ); - $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' ); - - $this->assertFileNameIsNotEdited( $metadata['sources']['image/jpeg']['file'] ); - $this->assertFileNameIsNotEdited( $metadata['sources']['image/webp']['file'] ); - - foreach ( $metadata['sizes'] as $size_name => $properties ) { - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); - - if ( 'thumbnail' === $size_name ) { - $this->assertSame( $properties['file'], $properties['sources']['image/jpeg']['file'] ); - $this->assertSame( str_replace( '.jpg', '.webp', $properties['file'] ), $properties['sources']['image/webp']['file'] ); - $this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] ); - $this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] ); - } else { - $this->assertFileNameIsNotEdited( $properties['sources']['image/jpeg']['file'] ); - $this->assertFileNameIsNotEdited( $properties['sources']['image/webp']['file'] ); - } - } + $this->assertArrayHasKey( 'sources', $metadata ); } /** diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index cc44dec263..375c6eab41 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -14,7 +14,6 @@ * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. * @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. - * @method void assertFileNameIsNotEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -90,17 +89,6 @@ public static function assertFileNameIsEdited( $filename, $message = '' ) { self::assertRegExp( '/e\d{13}/', $filename, $message ); } - /** - * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. - * - * @param string $filename The name of the filename to be asserted. - * @param string $message The Error message used to display when the assertion fails. - * @return void - */ - public static function assertFileNameIsNotEdited( $filename, $message = '' ) { - self::assertNotRegExp( '/e\d{13}/', $filename, $message ); - } - /** * Asserts that the provided size name is an edited name that contains a hash with digits. * From 75f15477fe4d1d18fd9978877a86e8753bb77196 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 12 Apr 2022 21:55:20 -0500 Subject: [PATCH 49/54] Revert "Update existing mechanism to generate the edited sources" This reverts commit 6d30ccb869938c0f3fae9f9a06b3906c5952557a. --- modules/images/webp-uploads/load.php | 198 ++++++++++++++---- .../images/webp-uploads/webp-uploads-test.php | 35 +--- tests/utils/TestCase/ImagesTestCase.php | 14 +- 3 files changed, 163 insertions(+), 84 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index b6a5b175ba..d7099df98b 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -23,12 +23,11 @@ * @see wp_generate_attachment_metadata() * @see webp_uploads_get_upload_image_mime_transforms() * - * @param array $metadata An array with the metadata from this attachment. - * @param int $attachment_id The ID of the attachment where the hook was dispatched. - * @param string $context The context of the current operation either: create or update. + * @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, $context = 'create' ) { +function webp_uploads_create_sources_property( array $metadata, $attachment_id ) { // This should take place only on the JPEG image. $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); @@ -38,21 +37,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, return $metadata; } - $is_update = 'update' === $context; - if ( $is_update ) { - if ( empty( $metadata['file'] ) ) { - return $metadata; - } - $uploads = wp_get_upload_dir(); - if ( isset( $uploads['basedir'] ) ) { - $file = path_join( $uploads['basedir'], $metadata['file'] ); - } else { - return $metadata; - } - } else { - $file = get_attached_file( $attachment_id, true ); - } - + $file = get_attached_file( $attachment_id, true ); // File does not exist. if ( ! file_exists( $file ) ) { return $metadata; @@ -63,18 +48,15 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $metadata['sources'] = array(); } - if ( $is_update && has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { - remove_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ); - } - - if ( in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) ) { - if ( empty( $metadata['sources'][ $mime_type ] ) || $is_update ) { - $metadata['sources'][ $mime_type ] = array( - 'file' => wp_basename( $file ), - 'filesize' => filesize( $file ), - ); - wp_update_attachment_metadata( $attachment_id, $metadata ); - } + if ( + empty( $metadata['sources'][ $mime_type ] ) && + in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) + ) { + $metadata['sources'][ $mime_type ] = array( + 'file' => wp_basename( $file ), + 'filesize' => filesize( $file ), + ); + wp_update_attachment_metadata( $attachment_id, $metadata ); } $original_size_data = array( @@ -89,8 +71,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, // 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 unless is an update. - if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) { + // If this property exists no need to create the image again. + if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) { continue; } @@ -113,11 +95,6 @@ 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'] ) ) { - - if ( $is_update && ! has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { - add_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata', 10, 3 ); - } - return $metadata; } @@ -145,7 +122,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $properties['sources'] = array(); } - if ( empty( $properties['sources'][ $current_mime ] ) || $is_update ) { + if ( empty( $properties['sources'][ $current_mime ] ) ) { $properties['sources'][ $current_mime ] = array( 'file' => isset( $properties['file'] ) ? $properties['file'] : '', 'filesize' => 0, @@ -161,7 +138,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) { // If this property exists no need to create the image again. - if ( ! empty( $properties['sources'][ $mime ] ) && ! $is_update ) { + if ( ! empty( $properties['sources'][ $mime ] ) ) { continue; } @@ -178,13 +155,9 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id, $metadata['sizes'][ $size_name ] = $properties; } - if ( $is_update && ! has_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata' ) ) { - add_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata', 10, 3 ); - } - return $metadata; } -add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 3 ); +add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 ); /** * Filter the image editor default output format mapping to select the most appropriate @@ -711,6 +684,139 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos } add_filter( 'rest_prepare_attachment', 'webp_uploads_update_rest_attachment', 10, 3 ); +/** + * Adds sources to metadata for an attachment. + * + * @since n.e.x.t + * + * @param array $metadata Metadata of the attachment. + * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. + * @param string $file Path to original file. + * @param array $main_images Path of all main image files of all mime types. + * @param array $subsized_images Path of all subsized image file of all mime types. + * @return array Metadata with sources added. + */ +function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ) { + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + + foreach ( $valid_mime_transforms as $targeted_mime ) { + // Add sources to original image metadata. + $image_file = $main_images[ $targeted_mime ]['path']; + + if ( ! file_exists( $image_file ) ) { + continue; + } + + $metadata['sources'][ $targeted_mime ] = array( + 'file' => wp_basename( $image_file ), + 'filesize' => filesize( $image_file ), + ); + + foreach ( $metadata['sizes'] as $size => $size_details ) { + // Add sources to resized image metadata. + $image_file = $original_directory . '/' . $subsized_images[ $targeted_mime ][ $size ]['file']; + + if ( ! file_exists( $image_file ) ) { + continue; + } + + $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( + 'file' => $subsized_images[ $targeted_mime ][ $size ]['file'], + 'filesize' => filesize( $image_file ), + ); + } + } + + return $metadata; +} + +/** + * Creates additional image formats when original image is edited. + * + * @since n.e.x.t + * + * @param bool|null $override Value to return instead of saving. Default null. + * @param string $file Name of the file to be saved. + * @param WP_Image_Editor $editor The image editor instance. + * @param string $mime_type The mime type of the image. + * @param int $post_id Attachment post ID. + * @return bool|null Potentially modified $override value. + */ +function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_type, $post_id ) { + if ( null !== $override ) { + return $override; + } + + $transforms = webp_uploads_get_upload_image_mime_transforms(); + if ( empty( $transforms[ $mime_type ] ) ) { + return null; + } + + $mime_transforms = $transforms[ $mime_type ]; + add_filter( + 'wp_update_attachment_metadata', + function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor, $mime_transforms ) { + if ( $post_meta_id !== $post_id ) { + return $metadata; + } + + // No sizes to be created. + if ( empty( $metadata['sizes'] ) ) { + return $metadata; + } + + $old_metadata = wp_get_attachment_metadata( $post_id ); + $resize_sizes = array(); + + foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { + if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && + $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { + $resize_sizes[ $size_name ] = $metadata['sizes'][ $size_name ]; + } + } + + $allowed_mimes = array_flip( wp_get_mime_types() ); + $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); + $filename = pathinfo( $file, PATHINFO_FILENAME ); + $main_images = array(); + $subsized_images = array(); + foreach ( $mime_transforms as $targeted_mime ) { + if ( $targeted_mime === $mime_type ) { + $main_images[ $targeted_mime ] = array( 'path' => $file ); + $subsized_images[ $targeted_mime ] = $metadata['sizes']; + continue; + } + + if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { + continue; + } + + if ( ! $editor::supports_mime_type( $targeted_mime ) ) { + continue; + } + + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; + + $result = $editor->save( $destination, $targeted_mime ); + + if ( is_wp_error( $result ) ) { + continue; + } + + $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); + } + + return webp_uploads_update_sources( $metadata, $mime_transforms, $file, $main_images, $subsized_images ); + }, + 10, + 2 + ); + + return null; +} +add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 7 ); + /** * Inspect if the current call to `wp_update_attachment_metadata()` was done from within the context * of an edit to an attachment either restore or other type of edit, in that case we perform operations @@ -735,7 +841,7 @@ function webp_uploads_update_attachment_metadata( $data, $attachment_id ) { switch ( $element['function'] ) { case 'wp_save_image': // Right after an image has been edited. - return webp_uploads_create_sources_property( webp_uploads_backup_sources( $attachment_id, $data ), $attachment_id, 'update' ); + return webp_uploads_backup_sources( $attachment_id, $data ); case 'wp_restore_image': // When an image has been restored. return webp_uploads_restore_image( $attachment_id, $data ); diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 74ab3055d2..5edbb4c635 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -873,31 +873,15 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag wp_restore_image( $attachment_id ); - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); - $this->assertImageHasSource( $attachment_id, 'image/webp' ); - $metadata = wp_get_attachment_metadata( $attachment_id ); - + $this->assertArrayHasKey( 'sources', $metadata ); $this->assertSame( $backup_sources['full-orig'], $metadata['sources'] ); $this->assertSame( $backup_sources, get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ) ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); - foreach ( $backup_sizes as $size_name => $properties ) { - // We are only interested in the original filenames to be compared against the backup and restored values. - if ( false === strpos( $size_name, '-orig' ) ) { - $this->assertSizeNameIsHashed( '', $size_name, "{$size_name} is not a valid edited name" ); - continue; - } - - $size_name = str_replace( '-orig', '', $size_name ); - // Full name is verified above. - if ( 'full' === $size_name ) { - continue; - } - - $this->assertArrayHasKey( $size_name, $metadata['sizes'] ); - $this->assertArrayHasKey( 'sources', $metadata['sizes'][ $size_name ] ); - $this->assertSame( $properties['sources'], $metadata['sizes'][ $size_name ]['sources'] ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertArrayHasKey( 'sources', $backup_sizes[ $size_name . '-orig' ] ); + $this->assertSame( $backup_sizes[ $size_name . '-orig' ]['sources'], $properties['sources'] ); } } @@ -975,16 +959,7 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ $this->assertArrayHasKey( 'full-orig', $backup_sources ); $this->assertSame( $metadata['sources'], $backup_sources['full-orig'] ); - $updated_metadata = wp_get_attachment_metadata( $attachment_id ); - - $this->assertArrayHasKey( 'sources', $updated_metadata ); - $this->assertNotSame( $metadata['sources'], $updated_metadata['sources'] ); - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); - $this->assertImageHasSource( $attachment_id, 'image/webp' ); - - foreach ( $updated_metadata['sources'] as $properties ) { - $this->assertFileNameIsEdited( $properties['file'] ); - } + $this->assertArrayNotHasKey( 'sources', wp_get_attachment_metadata( $attachment_id ), 'The sources attributes was not removed from the metadata.' ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index 375c6eab41..9f83577eff 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -13,8 +13,8 @@ * @method void assertImageHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image has the appropriate source for the subsize. * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. - * @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. - * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits. + * @method void assertFileNameIsEdited( string $filename ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -82,11 +82,10 @@ public static function assertImageNotHasSizeSource( $attachment_id, $size, $mime * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. * * @param string $filename The name of the filename to be asserted. - * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertFileNameIsEdited( $filename, $message = '' ) { - self::assertRegExp( '/e\d{13}/', $filename, $message ); + public static function assertFileNameIsEdited( $filename ) { + self::assertRegExp( '/e\d{13}/', $filename ); } /** @@ -94,10 +93,9 @@ public static function assertFileNameIsEdited( $filename, $message = '' ) { * * @param string $size_name The size name we are looking for. * @param string $hashed_size_name The current size name we are comparing against. - * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertSizeNameIsHashed( $size_name, $hashed_size_name, $message = '' ) { - self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name, $message ); + public static function assertSizeNameIsHashed( $size_name, $hashed_size_name ) { + self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name ); } } From d71f37f822b3aad21b77911e1017ce72bf938ba6 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Tue, 12 Apr 2022 22:29:44 -0500 Subject: [PATCH 50/54] Update code following the provided feedback --- modules/images/webp-uploads/load.php | 37 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index d7099df98b..abee6be4a2 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -700,6 +700,11 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); foreach ( $valid_mime_transforms as $targeted_mime ) { + // Make sure the path and file exists as those values are being accessed. + if ( empty( $main_images[ $targeted_mime ]['path'] ) || empty( $main_images[ $targeted_mime ]['file'] ) ) { + continue; + } + // Add sources to original image metadata. $image_file = $main_images[ $targeted_mime ]['path']; @@ -708,20 +713,24 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, } $metadata['sources'][ $targeted_mime ] = array( - 'file' => wp_basename( $image_file ), + 'file' => $main_images[ $targeted_mime ]['file'], 'filesize' => filesize( $image_file ), ); - foreach ( $metadata['sizes'] as $size => $size_details ) { + foreach ( $metadata['sizes'] as $size_name => $size_details ) { + if ( empty( $subsized_images[ $targeted_mime ][ $size_name ]['file'] ) ) { + continue; + } + // Add sources to resized image metadata. - $image_file = $original_directory . '/' . $subsized_images[ $targeted_mime ][ $size ]['file']; + $image_file = path_join( $original_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); if ( ! file_exists( $image_file ) ) { continue; } - $metadata['sizes'][ $size ]['sources'][ $targeted_mime ] = array( - 'file' => $subsized_images[ $targeted_mime ][ $size ]['file'], + $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] = array( + 'file' => $subsized_images[ $targeted_mime ][ $size_name ]['file'], 'filesize' => filesize( $image_file ), ); } @@ -736,13 +745,13 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, * @since n.e.x.t * * @param bool|null $override Value to return instead of saving. Default null. - * @param string $file Name of the file to be saved. + * @param string $file_path Name of the file to be saved. * @param WP_Image_Editor $editor The image editor instance. * @param string $mime_type The mime type of the image. * @param int $post_id Attachment post ID. * @return bool|null Potentially modified $override value. */ -function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_type, $post_id ) { +function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mime_type, $post_id ) { if ( null !== $override ) { return $override; } @@ -755,7 +764,7 @@ function webp_uploads_update_image_onchange( $override, $file, $editor, $mime_ty $mime_transforms = $transforms[ $mime_type ]; add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor, $mime_transforms ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms ) { if ( $post_meta_id !== $post_id ) { return $metadata; } @@ -776,13 +785,16 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor } $allowed_mimes = array_flip( wp_get_mime_types() ); - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - $filename = pathinfo( $file, PATHINFO_FILENAME ); + $original_directory = pathinfo( $file_path, PATHINFO_DIRNAME ); + $filename = pathinfo( $file_path, PATHINFO_FILENAME ); $main_images = array(); $subsized_images = array(); foreach ( $mime_transforms as $targeted_mime ) { if ( $targeted_mime === $mime_type ) { - $main_images[ $targeted_mime ] = array( 'path' => $file ); + $main_images[ $targeted_mime ] = array( + 'path' => $file_path, + 'file' => pathinfo( $file_path, PATHINFO_BASENAME ), + ); $subsized_images[ $targeted_mime ] = $metadata['sizes']; continue; } @@ -804,10 +816,11 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file, $mime_type, $editor continue; } + $main_images[ $targeted_mime ] = $result; $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } - return webp_uploads_update_sources( $metadata, $mime_transforms, $file, $main_images, $subsized_images ); + return webp_uploads_update_sources( $metadata, $mime_transforms, $file_path, $main_images, $subsized_images ); }, 10, 2 From 063d0abec0b3de05dfca9237346f945d246f7fa4 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 13 Apr 2022 19:14:51 -0500 Subject: [PATCH 51/54] Update list of available assertions for edited images --- tests/utils/TestCase/ImagesTestCase.php | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/utils/TestCase/ImagesTestCase.php b/tests/utils/TestCase/ImagesTestCase.php index 9f83577eff..cc44dec263 100644 --- a/tests/utils/TestCase/ImagesTestCase.php +++ b/tests/utils/TestCase/ImagesTestCase.php @@ -13,8 +13,9 @@ * @method void assertImageHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image has the appropriate source for the subsize. * @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source. * @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize. - * @method void assertFileNameIsEdited( string $filename ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. - * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name ) Asserts that the provided size name is an edited name that contains a hash with digits. + * @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertFileNameIsNotEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits. */ abstract class ImagesTestCase extends WP_UnitTestCase { @@ -82,10 +83,22 @@ public static function assertImageNotHasSizeSource( $attachment_id, $size, $mime * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. * * @param string $filename The name of the filename to be asserted. + * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertFileNameIsEdited( $filename ) { - self::assertRegExp( '/e\d{13}/', $filename ); + public static function assertFileNameIsEdited( $filename, $message = '' ) { + self::assertRegExp( '/e\d{13}/', $filename, $message ); + } + + /** + * Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename. + * + * @param string $filename The name of the filename to be asserted. + * @param string $message The Error message used to display when the assertion fails. + * @return void + */ + public static function assertFileNameIsNotEdited( $filename, $message = '' ) { + self::assertNotRegExp( '/e\d{13}/', $filename, $message ); } /** @@ -93,9 +106,10 @@ public static function assertFileNameIsEdited( $filename ) { * * @param string $size_name The size name we are looking for. * @param string $hashed_size_name The current size name we are comparing against. + * @param string $message The Error message used to display when the assertion fails. * @return void */ - public static function assertSizeNameIsHashed( $size_name, $hashed_size_name ) { - self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name ); + public static function assertSizeNameIsHashed( $size_name, $hashed_size_name, $message = '' ) { + self::assertRegExp( "/{$size_name}-\d{13}/", $hashed_size_name, $message ); } } From e4c213e83927ca88520a726449724ba837573ce1 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 13 Apr 2022 19:15:18 -0500 Subject: [PATCH 52/54] FIx failing tests and prevent to fail when editing an image Ensure that an image can be edited, as soon as this change is completed there's no need to wait for subsequent updates on the clousure, so even if the code is executed it can be avoided, for subsequent updates. --- modules/images/webp-uploads/load.php | 42 +++++++++--------- .../images/webp-uploads/webp-uploads-test.php | 43 ++++++++++++++++--- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index abee6be4a2..e044ccf490 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -701,37 +701,33 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, foreach ( $valid_mime_transforms as $targeted_mime ) { // Make sure the path and file exists as those values are being accessed. - if ( empty( $main_images[ $targeted_mime ]['path'] ) || empty( $main_images[ $targeted_mime ]['file'] ) ) { - continue; - } - - // Add sources to original image metadata. - $image_file = $main_images[ $targeted_mime ]['path']; + if ( isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) ) { + if ( ! file_exists( $main_images[ $targeted_mime ]['path'] ) ) { + continue; + } - if ( ! file_exists( $image_file ) ) { - continue; + // Add sources to original image metadata. + $metadata['sources'][ $targeted_mime ] = array( + 'file' => $main_images[ $targeted_mime ]['file'], + 'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ), + ); } - $metadata['sources'][ $targeted_mime ] = array( - 'file' => $main_images[ $targeted_mime ]['file'], - 'filesize' => filesize( $image_file ), - ); - foreach ( $metadata['sizes'] as $size_name => $size_details ) { if ( empty( $subsized_images[ $targeted_mime ][ $size_name ]['file'] ) ) { continue; } // Add sources to resized image metadata. - $image_file = path_join( $original_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); + $subsize_path = path_join( $original_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); - if ( ! file_exists( $image_file ) ) { + if ( ! file_exists( $subsize_path ) ) { continue; } $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] = array( 'file' => $subsized_images[ $targeted_mime ][ $size_name ]['file'], - 'filesize' => filesize( $image_file ), + 'filesize' => filesize( $subsize_path ), ); } } @@ -762,13 +758,21 @@ function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mi } $mime_transforms = $transforms[ $mime_type ]; + // This variable allows to unhook the logic from within the closure without the need fo a function name. + $callback_executed = false; add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms, &$callback_executed ) { if ( $post_meta_id !== $post_id ) { return $metadata; } + // This callback was already executed for this post, nothing to do at this point. + if ( $callback_executed ) { + return $metadata; + } + + $callback_executed = true; // No sizes to be created. if ( empty( $metadata['sizes'] ) ) { return $metadata; @@ -776,7 +780,6 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e $old_metadata = wp_get_attachment_metadata( $post_id ); $resize_sizes = array(); - foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { @@ -809,8 +812,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - - $result = $editor->save( $destination, $targeted_mime ); + $result = $editor->save( $destination, $targeted_mime ); if ( is_wp_error( $result ) ) { continue; diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 5edbb4c635..8a624ced51 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -873,15 +873,31 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag wp_restore_image( $attachment_id ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertSame( $backup_sources['full-orig'], $metadata['sources'] ); $this->assertSame( $backup_sources, get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ) ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); - foreach ( $metadata['sizes'] as $size_name => $properties ) { - $this->assertArrayHasKey( 'sources', $backup_sizes[ $size_name . '-orig' ] ); - $this->assertSame( $backup_sizes[ $size_name . '-orig' ]['sources'], $properties['sources'] ); + foreach ( $backup_sizes as $size_name => $properties ) { + // We are only interested in the original filenames to be compared against the backup and restored values. + if ( false === strpos( $size_name, '-orig' ) ) { + $this->assertSizeNameIsHashed( '', $size_name, "{$size_name} is not a valid edited name" ); + continue; + } + + $size_name = str_replace( '-orig', '', $size_name ); + // Full name is verified above. + if ( 'full' === $size_name ) { + continue; + } + + $this->assertArrayHasKey( $size_name, $metadata['sizes'] ); + $this->assertArrayHasKey( 'sources', $metadata['sizes'][ $size_name ] ); + $this->assertSame( $properties['sources'], $metadata['sizes'][ $size_name ]['sources'] ); } } @@ -938,7 +954,17 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum $this->assertArrayHasKey( 'sources', $backup_sizes['thumbnail-orig'] ); $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertArrayHasKey( 'sources', $metadata ); + + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' ); + + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + } } /** @@ -959,7 +985,12 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ $this->assertArrayHasKey( 'full-orig', $backup_sources ); $this->assertSame( $metadata['sources'], $backup_sources['full-orig'] ); - $this->assertArrayNotHasKey( 'sources', wp_get_attachment_metadata( $attachment_id ), 'The sources attributes was not removed from the metadata.' ); + $updated_metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'sources', $updated_metadata ); + $this->assertNotSame( $metadata['sources'], $updated_metadata['sources'] ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); From 2d55d2dd2dab6f3441757b7b59faf6c980734bce Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 14 Apr 2022 10:55:50 -0700 Subject: [PATCH 53/54] Return $override instead of null for extra safety. --- modules/images/webp-uploads/load.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index e044ccf490..98e7cf3959 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -754,7 +754,7 @@ function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mi $transforms = webp_uploads_get_upload_image_mime_transforms(); if ( empty( $transforms[ $mime_type ] ) ) { - return null; + return $override; } $mime_transforms = $transforms[ $mime_type ]; @@ -828,7 +828,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e 2 ); - return null; + return $override; } add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 7 ); From 5c829efc4708b7f0e335f061a43fd5a4feb6b7c4 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 14 Apr 2022 11:17:45 -0700 Subject: [PATCH 54/54] Simplify webp_uploads_update_sources() to not require original file path as parameter. --- modules/images/webp-uploads/load.php | 39 +++++++++++++--------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 98e7cf3959..e49da078fd 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -689,37 +689,34 @@ function webp_uploads_update_rest_attachment( WP_REST_Response $response, WP_Pos * * @since n.e.x.t * - * @param array $metadata Metadata of the attachment. - * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. - * @param string $file Path to original file. - * @param array $main_images Path of all main image files of all mime types. - * @param array $subsized_images Path of all subsized image file of all mime types. + * @param array $metadata Metadata of the attachment. + * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. + * @param array $main_images Path of all main image files of all mime types. + * @param array $subsized_images Path of all subsized image file of all mime types. * @return array Metadata with sources added. */ -function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $file, $main_images, $subsized_images ) { - $original_directory = pathinfo( $file, PATHINFO_DIRNAME ); - +function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_images, $subsized_images ) { foreach ( $valid_mime_transforms as $targeted_mime ) { // Make sure the path and file exists as those values are being accessed. - if ( isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) ) { - if ( ! file_exists( $main_images[ $targeted_mime ]['path'] ) ) { - continue; - } - - // Add sources to original image metadata. - $metadata['sources'][ $targeted_mime ] = array( - 'file' => $main_images[ $targeted_mime ]['file'], - 'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ), - ); + if ( ! isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) || ! file_exists( $main_images[ $targeted_mime ]['path'] ) ) { + continue; } + $image_directory = pathinfo( $main_images[ $targeted_mime ]['path'], PATHINFO_DIRNAME ); + + // Add sources to original image metadata. + $metadata['sources'][ $targeted_mime ] = array( + 'file' => $main_images[ $targeted_mime ]['file'], + 'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ), + ); + foreach ( $metadata['sizes'] as $size_name => $size_details ) { if ( empty( $subsized_images[ $targeted_mime ][ $size_name ]['file'] ) ) { continue; } // Add sources to resized image metadata. - $subsize_path = path_join( $original_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); + $subsize_path = path_join( $image_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); if ( ! file_exists( $subsize_path ) ) { continue; @@ -771,8 +768,8 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e if ( $callback_executed ) { return $metadata; } - $callback_executed = true; + // No sizes to be created. if ( empty( $metadata['sizes'] ) ) { return $metadata; @@ -822,7 +819,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } - return webp_uploads_update_sources( $metadata, $mime_transforms, $file_path, $main_images, $subsized_images ); + return webp_uploads_update_sources( $metadata, $mime_transforms, $main_images, $subsized_images ); }, 10, 2