From d0eab813909362cd54939118892d75a7734f89d2 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Wed, 27 Apr 2022 15:57:50 +0530 Subject: [PATCH 01/13] filter webp_uploads_pre_replace_additional_image_source added --- modules/images/webp-uploads/load.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 7c8125cdec..19eb1c6132 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -476,6 +476,20 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id $metadata['sources'][ $target_mime ]['file'], $image ); + + /** + * Filter to replace additional image source file, by locating the original + * mime types of the file and return correct file path in the end. + * + * @since n.e.xt + * + * @param string $image An tag where the urls would be updated. + * @param int $attachment_id The ID of the attachment being modified. + * @param string $size The size name that would be used to create this image, out of the registered subsizes. + * @param string $target_mime The target mime in which the image should be created. + * @param string $context The context where this is function is being used. + */ + $image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); } } @@ -498,6 +512,20 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id $size_data['sources'][ $target_mime ]['file'], $image ); + + /** + * Filter to replace additional image source file, by locating the original + * mime types of the file and return correct file path in the end. + * + * @since n.e.xt + * + * @param string $image An tag where the urls would be updated. + * @param int $attachment_id The ID of the attachment being modified. + * @param string $size The size name that would be used to create this image, out of the registered subsizes. + * @param string $target_mime The target mime in which the image should be created. + * @param string $context The context where this is function is being used. + */ + $image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); } return $image; From e40987a0c22a266c1ad8312e0a05e135354b4a42 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Fri, 29 Apr 2022 18:17:10 +0530 Subject: [PATCH 02/13] filtered image compared with original image if same then replace otherwise not --- modules/images/webp-uploads/load.php | 39 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 19eb1c6132..1ea197fa00 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -471,11 +471,6 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id if ( isset( $metadata['sources'][ $target_mime ]['file'] ) ) { $basename = wp_basename( $metadata['file'] ); if ( $basename !== $metadata['sources'][ $target_mime ]['file'] ) { - $image = str_replace( - $basename, - $metadata['sources'][ $target_mime ]['file'], - $image - ); /** * Filter to replace additional image source file, by locating the original @@ -489,7 +484,18 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id * @param string $target_mime The target mime in which the image should be created. * @param string $context The context where this is function is being used. */ - $image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); + $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); + + // Check if filtered image is same as the image, replace the image otherwise not. + if ( $filtered_image === $image ) { + $image = str_replace( + $basename, + $metadata['sources'][ $target_mime ]['file'], + $image + ); + } else { + $image = $filtered_image; + } } } @@ -507,12 +513,6 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id continue; } - $image = str_replace( - $size_data['file'], - $size_data['sources'][ $target_mime ]['file'], - $image - ); - /** * Filter to replace additional image source file, by locating the original * mime types of the file and return correct file path in the end. @@ -521,11 +521,22 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id * * @param string $image An tag where the urls would be updated. * @param int $attachment_id The ID of the attachment being modified. - * @param string $size The size name that would be used to create this image, out of the registered subsizes. + * @param string $name The size name that would be used to create this image, out of the registered subsizes. * @param string $target_mime The target mime in which the image should be created. * @param string $context The context where this is function is being used. */ - $image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); + $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $name, $target_mime, $context ); + + // Check if filtered image is same as the image, replace the image otherwise not. + if ( $filtered_image === $image ) { + $image = str_replace( + $size_data['file'], + $size_data['sources'][ $target_mime ]['file'], + $image + ); + } else { + $image = $filtered_image; + } } return $image; From 42fed35fce490e809f7d21b1e0c6d367705b9595 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Fri, 29 Apr 2022 18:17:55 +0530 Subject: [PATCH 03/13] test case added for filter webp_uploads_pre_replace_additional_image_source --- .../images/webp-uploads/load-tests.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index 6288a9b5a6..d4613c2bf6 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -662,4 +662,25 @@ function( $data, $attachment_id ) { $this->assertNotContains( 'image/invalid', $mime_types ); $this->assertSame( array( 'image/jpeg', 'image/webp' ), $mime_types ); } + + /** + * Prevent replacing an image if image was uploaded via external source or plugin. + * + * @group webp_uploads_update_image_references + * + * @test + */ + public function it_should_prevent_replacing_an_image_uploaded_via_external_source() { + add_filter( + 'webp_uploads_pre_replace_additional_image_source', + function() { + return TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'; + } + ); + + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); + + $tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) ); + $this->assertNotSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); + } } From 96700c0bcc5e87049fbecb6265caf4fb232edfcd Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 3 May 2022 15:42:47 +0530 Subject: [PATCH 04/13] Update modules/images/webp-uploads/load.php Co-authored-by: Eugene Manuilov --- 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 1ea197fa00..43075cf852 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -476,7 +476,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id * Filter to replace additional image source file, by locating the original * mime types of the file and return correct file path in the end. * - * @since n.e.xt + * @since n.e.x.t * * @param string $image An tag where the urls would be updated. * @param int $attachment_id The ID of the attachment being modified. From fc0794e7f072346500ca61e268d2d14b7afe42e3 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 3 May 2022 15:42:56 +0530 Subject: [PATCH 05/13] Update modules/images/webp-uploads/load.php Co-authored-by: Eugene Manuilov --- 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 43075cf852..67d550b3bc 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -517,7 +517,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id * Filter to replace additional image source file, by locating the original * mime types of the file and return correct file path in the end. * - * @since n.e.xt + * @since n.e.x.t * * @param string $image An tag where the urls would be updated. * @param int $attachment_id The ID of the attachment being modified. From d5dbcf2f615ee6fde0ed701ec6553c42c460d95f Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Sun, 24 Apr 2022 18:13:13 -0500 Subject: [PATCH 06/13] Replace the featured image to a modern format When a modern format is available to render the featured image make sure that the replacement image uses the same logic used to replace the images in the content in order to be consistent with the rest of rendered format. Fixes #288 --- modules/images/webp-uploads/load.php | 17 +++++++++++++++++ .../modules/images/webp-uploads/load-tests.php | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 67d550b3bc..31499c0109 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -541,3 +541,20 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id return $image; } + +/** + * Updates the references of the featured image to the a new image format if available, in the same way it + * occurs in the_content of a post. + * + * @since n.e.x.t + * + * @param string $html The current HTML markup of the featured image. + * @param int $post_id The current post ID where the featured image is requested. + * @param int $attachment_id The ID of the attachment image. + * @return string The updated HTML markup. + */ +function webp_uploads_update_featured_image( $html, $post_id, $attachment_id ) { + return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id ); +} + +add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 ); diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index d4613c2bf6..3bc6ccd405 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -663,6 +663,23 @@ function( $data, $attachment_id ) { $this->assertSame( array( 'image/jpeg', 'image/webp' ), $mime_types ); } + /** + * Replace the featured image to WebP when requesting the featured image + * + * @test + */ + public function it_should_replace_the_featured_image_to_web_p_when_requesting_the_featured_image() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/paint.jpeg' ); + $post_id = $this->factory()->post->create(); + set_post_thumbnail( $post_id, $attachment_id ); + + $featured_image = get_the_post_thumbnail( $post_id ); + + $this->assertTrue( has_post_thumbnail( $post_id ) ); + $this->assertStringContainsString( '.webp', $featured_image ); + $this->assertStringNotContainsString( '.jpeg', $featured_image ); + } + /** * Prevent replacing an image if image was uploaded via external source or plugin. * From eca8f6e96062d4619aec5801a4d70d11698c5f91 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 2 May 2022 16:48:55 -0700 Subject: [PATCH 07/13] Remove blank line above add_filter call. --- 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 31499c0109..3420b886b4 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -556,5 +556,4 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id function webp_uploads_update_featured_image( $html, $post_id, $attachment_id ) { return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id ); } - add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 ); From 9ee668ab20a063b74a2216bdc9b878b9171cecac Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 3 May 2022 15:46:47 +0530 Subject: [PATCH 08/13] php unit test case - remove all filter added for test --- tests/modules/images/webp-uploads/load-tests.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index 3bc6ccd405..a90f0147cb 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -688,6 +688,8 @@ public function it_should_replace_the_featured_image_to_web_p_when_requesting_th * @test */ public function it_should_prevent_replacing_an_image_uploaded_via_external_source() { + remove_all_filters( 'webp_uploads_pre_replace_additional_image_source' ); + add_filter( 'webp_uploads_pre_replace_additional_image_source', function() { From 6ce6293f384dab9db2565f3311dd028a36a534ae Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Wed, 4 May 2022 19:21:05 +0530 Subject: [PATCH 09/13] filter php doc updated. Co-authored-by: Mukesh Panchal --- modules/images/webp-uploads/load.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3420b886b4..b4fc982761 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -500,7 +500,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id } // Replace sub sizes for the image if present. - foreach ( $metadata['sizes'] as $name => $size_data ) { + foreach ( $metadata['sizes'] as $size => $size_data ) { if ( empty( $size_data['file'] ) ) { continue; } @@ -513,19 +513,8 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id continue; } - /** - * Filter to replace additional image source file, by locating the original - * mime types of the file and return correct file path in the end. - * - * @since n.e.x.t - * - * @param string $image An tag where the urls would be updated. - * @param int $attachment_id The ID of the attachment being modified. - * @param string $name The size name that would be used to create this image, out of the registered subsizes. - * @param string $target_mime The target mime in which the image should be created. - * @param string $context The context where this is function is being used. - */ - $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $name, $target_mime, $context ); + /** This filter is documented in modules/images/webp-uploads/load.php */ + $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $size, $target_mime, $context ); // Check if filtered image is same as the image, replace the image otherwise not. if ( $filtered_image === $image ) { From e35fccdc8a3839dd92212832c553af2f7f51bae0 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 10 May 2022 20:02:02 +0530 Subject: [PATCH 10/13] Update modules/images/webp-uploads/load.php Co-authored-by: Felix Arntz --- 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 df7a6e7a57..c7e9c5003e 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -491,6 +491,8 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id * Filter to replace additional image source file, by locating the original * mime types of the file and return correct file path in the end. * + * Altering the $image tag through this filter effectively short-circuits the default replacement logic using the preferred MIME type. + * * @since n.e.x.t * * @param string $image An tag where the urls would be updated. From 305b44a6fc9adaa7c21df792396b42bc0acb0082 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 10 May 2022 20:02:10 +0530 Subject: [PATCH 11/13] 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 c7e9c5003e..6c42caa2c4 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -503,7 +503,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id */ $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, 'full', $target_mime, $context ); - // Check if filtered image is same as the image, replace the image otherwise not. + // If filtered image is same as the image, run our own replacement logic, otherwise rely on the filtered image. if ( $filtered_image === $image ) { $image = str_replace( $basename, From 38900a0718f70585e3a8feeae2749b230b0dd9e9 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 10 May 2022 20:02:19 +0530 Subject: [PATCH 12/13] 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 6c42caa2c4..8ffbcfad8c 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -543,7 +543,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id /** This filter is documented in modules/images/webp-uploads/load.php */ $filtered_image = (string) apply_filters( 'webp_uploads_pre_replace_additional_image_source', $image, $attachment_id, $size, $target_mime, $context ); - // Check if filtered image is same as the image, replace the image otherwise not. + // If filtered image is same as the image, run our own replacement logic, otherwise rely on the filtered image. if ( $filtered_image === $image ) { $image = str_replace( $size_data['file'], From 335596ccf281d648a79c4ad4b216e492642c6b0f Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Tue, 10 May 2022 21:54:13 +0530 Subject: [PATCH 13/13] php testcase - return img tag and assert both tag doesnt match --- tests/modules/images/webp-uploads/load-tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/images/webp-uploads/load-tests.php b/tests/modules/images/webp-uploads/load-tests.php index 65b64af5ed..dfd41c658c 100644 --- a/tests/modules/images/webp-uploads/load-tests.php +++ b/tests/modules/images/webp-uploads/load-tests.php @@ -622,7 +622,7 @@ public function it_should_prevent_replacing_an_image_uploaded_via_external_sourc add_filter( 'webp_uploads_pre_replace_additional_image_source', function() { - return TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'; + return ''; } );