From 165fa7700e02a20ac428c10e954433087eea7e3f Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Fri, 30 Aug 2024 14:25:51 +0530 Subject: [PATCH 1/6] Fix optimised image issue with the WC cart page --- inc/tag_replacer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/tag_replacer.php b/inc/tag_replacer.php index 90d21c11..72feb9c4 100644 --- a/inc/tag_replacer.php +++ b/inc/tag_replacer.php @@ -49,13 +49,13 @@ public function init() { parent::init(); add_filter( 'optml_content_images_tags', [ $this, 'process_image_tags' ], 1, 2 ); + add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 ); if ( $this->settings->use_lazyload() ) { return; } add_filter( 'optml_tag_replace', [ $this, 'regular_tag_replace' ], 1, 6 ); - add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 ); add_filter( 'wp_calculate_image_srcset', [ $this, 'filter_srcset_attr' ], PHP_INT_MAX, 5 ); add_filter( 'wp_calculate_image_sizes', [ $this, 'filter_sizes_attr' ], 1, 2 ); } From f04d3876ae1390e909cde0e4126e2fd755d89c6b Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 2 Sep 2024 15:24:02 +0530 Subject: [PATCH 2/6] Move filter to compatibilities/woocommerce.php --- inc/compatibilities/woocommerce.php | 3 +++ inc/tag_replacer.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/inc/compatibilities/woocommerce.php b/inc/compatibilities/woocommerce.php index ff9e70bf..9da86748 100644 --- a/inc/compatibilities/woocommerce.php +++ b/inc/compatibilities/woocommerce.php @@ -24,6 +24,9 @@ public function should_load() { public function register() { if ( Optml_Main::instance()->admin->settings->use_lazyload() ) { add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 ); + if ( \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { + add_filter( 'image_downsize', [ Optml_Tag_Replacer::instance(), 'filter_image_downsize' ], PHP_INT_MAX, 3 ); + } } } /** diff --git a/inc/tag_replacer.php b/inc/tag_replacer.php index 72feb9c4..90d21c11 100644 --- a/inc/tag_replacer.php +++ b/inc/tag_replacer.php @@ -49,13 +49,13 @@ public function init() { parent::init(); add_filter( 'optml_content_images_tags', [ $this, 'process_image_tags' ], 1, 2 ); - add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 ); if ( $this->settings->use_lazyload() ) { return; } add_filter( 'optml_tag_replace', [ $this, 'regular_tag_replace' ], 1, 6 ); + add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 ); add_filter( 'wp_calculate_image_srcset', [ $this, 'filter_srcset_attr' ], PHP_INT_MAX, 5 ); add_filter( 'wp_calculate_image_sizes', [ $this, 'filter_sizes_attr' ], 1, 2 ); } From 7ab21b43a4e8977e77aed127e8c3eda27cd8c175 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 2 Sep 2024 15:27:56 +0530 Subject: [PATCH 3/6] Fix phpstan error --- inc/compatibilities/woocommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/compatibilities/woocommerce.php b/inc/compatibilities/woocommerce.php index 9da86748..6908f853 100644 --- a/inc/compatibilities/woocommerce.php +++ b/inc/compatibilities/woocommerce.php @@ -24,7 +24,7 @@ public function should_load() { public function register() { if ( Optml_Main::instance()->admin->settings->use_lazyload() ) { add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 ); - if ( \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { + if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils', false ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { add_filter( 'image_downsize', [ Optml_Tag_Replacer::instance(), 'filter_image_downsize' ], PHP_INT_MAX, 3 ); } } From d8a38b1ce47d2f264dd1a8e317916076d92ecbac Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 2 Sep 2024 18:21:41 +0530 Subject: [PATCH 4/6] Remove class_exists autoload args --- inc/compatibilities/woocommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/compatibilities/woocommerce.php b/inc/compatibilities/woocommerce.php index 6908f853..cd07916c 100644 --- a/inc/compatibilities/woocommerce.php +++ b/inc/compatibilities/woocommerce.php @@ -24,7 +24,7 @@ public function should_load() { public function register() { if ( Optml_Main::instance()->admin->settings->use_lazyload() ) { add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 ); - if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils', false ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { + if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { add_filter( 'image_downsize', [ Optml_Tag_Replacer::instance(), 'filter_image_downsize' ], PHP_INT_MAX, 3 ); } } From 31a85ab002890210e57e328b3cd244713fe64c19 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Wed, 4 Sep 2024 10:43:30 +0530 Subject: [PATCH 5/6] Ensure filters are loaded only on the is_cart page --- inc/compatibilities/woocommerce.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/inc/compatibilities/woocommerce.php b/inc/compatibilities/woocommerce.php index cd07916c..7e09ce46 100644 --- a/inc/compatibilities/woocommerce.php +++ b/inc/compatibilities/woocommerce.php @@ -24,8 +24,8 @@ public function should_load() { public function register() { if ( Optml_Main::instance()->admin->settings->use_lazyload() ) { add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 ); - if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() ) { - add_filter( 'image_downsize', [ Optml_Tag_Replacer::instance(), 'filter_image_downsize' ], PHP_INT_MAX, 3 ); + if ( class_exists( '\Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' ) && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_cart_block_default() && function_exists( 'wc_cart_totals_subtotal_html' ) ) { + add_filter( 'image_downsize', [ $this, 'filter_cart_item_image' ], PHP_INT_MAX, 3 ); } } } @@ -80,4 +80,20 @@ public function should_load_early() { } return false; } + + /** + * This filter will replace all the images retrieved via "wp_get_image" type of functions. + * + * @param array $image The filtered value. + * @param int $attachment_id The related attachment id. + * @param array|string $size This could be the name of the thumbnail size or an array of custom dimensions. + * + * @return array + */ + public function filter_cart_item_image( $image, $attachment_id, $size ) { + if ( ! is_cart() ) { + return $image; + } + return Optml_Tag_Replacer::instance()->filter_image_downsize( $image, $attachment_id, $size ); + } } From e9dd1d00fbf64aa4cc233099d2aea30e5326ff1d Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Wed, 4 Sep 2024 10:51:36 +0530 Subject: [PATCH 6/6] Fix PHPStan error --- inc/compatibilities/woocommerce.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/compatibilities/woocommerce.php b/inc/compatibilities/woocommerce.php index 7e09ce46..f8856d98 100644 --- a/inc/compatibilities/woocommerce.php +++ b/inc/compatibilities/woocommerce.php @@ -91,6 +91,9 @@ public function should_load_early() { * @return array */ public function filter_cart_item_image( $image, $attachment_id, $size ) { + if ( ! function_exists( 'is_cart' ) ) { + return $image; + } if ( ! is_cart() ) { return $image; }