From 709871820ff0aed9d0918a709959c880aea93fb4 Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 29 May 2024 15:59:56 +0200 Subject: [PATCH 1/6] Fix Smart Buttons on Elementor checkout --- modules.php | 2 +- .../ppcp-button/src/Helper/ContextTrait.php | 78 ++++++++++--------- modules/ppcp-compat/src/CompatModule.php | 30 +++++++ .../src/Helper/CartCheckoutDetector.php | 11 ++- 4 files changed, 81 insertions(+), 40 deletions(-) diff --git a/modules.php b/modules.php index 06a9f482d..3672dc515 100644 --- a/modules.php +++ b/modules.php @@ -18,8 +18,8 @@ return function ( string $root_dir ): iterable { ( require "$modules_dir/woocommerce-logging/module.php" )(), ( require "$modules_dir/ppcp-admin-notices/module.php" )(), ( require "$modules_dir/ppcp-api-client/module.php" )(), - ( require "$modules_dir/ppcp-button/module.php" )(), ( require "$modules_dir/ppcp-compat/module.php" )(), + ( require "$modules_dir/ppcp-button/module.php" )(), ( require "$modules_dir/ppcp-onboarding/module.php" )(), ( require "$modules_dir/ppcp-session/module.php" )(), ( require "$modules_dir/ppcp-status-report/module.php" )(), diff --git a/modules/ppcp-button/src/Helper/ContextTrait.php b/modules/ppcp-button/src/Helper/ContextTrait.php index 9b5874bfa..a034eda8b 100644 --- a/modules/ppcp-button/src/Helper/ContextTrait.php +++ b/modules/ppcp-button/src/Helper/ContextTrait.php @@ -95,47 +95,51 @@ trait ContextTrait { * @return string */ protected function context(): string { - if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) { + // Default context. + $context = 'mini-cart'; - // Do this check here instead of reordering outside conditions. - // In order to have more control over the context. - if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) { - return 'checkout'; - } + switch ( true ) { + case is_product() || wc_post_content_has_shortcode( 'product_page' ): + // Do this check here instead of reordering outside conditions. + // In order to have more control over the context. + if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) { + $context = 'checkout'; + } else { + $context = 'product'; + } + break; - return 'product'; + // has_block may not work if called too early, such as during the block registration. + case has_block( 'woocommerce/cart' ): + $context = 'cart-block'; + break; + + case $this->is_cart(): + $context = 'cart'; + break; + + case is_checkout_pay_page(): + $context = 'pay-now'; + break; + + case has_block( 'woocommerce/checkout' ): + $context = 'checkout-block'; + break; + + case $this->is_checkout() && ! $this->is_paypal_continuation(): + $context = 'checkout'; + break; + + case $this->is_add_payment_method_page(): + $context = 'add-payment-method'; + break; + + case $this->is_block_editor(): + $context = 'block-editor'; + break; } - // has_block may not work if called too early, such as during the block registration. - if ( has_block( 'woocommerce/cart' ) ) { - return 'cart-block'; - } - - if ( $this->is_cart() ) { - return 'cart'; - } - - if ( is_checkout_pay_page() ) { - return 'pay-now'; - } - - if ( has_block( 'woocommerce/checkout' ) ) { - return 'checkout-block'; - } - - if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) { - return 'checkout'; - } - - if ( $this->is_add_payment_method_page() ) { - return 'add-payment-method'; - } - - if ( $this->is_block_editor() ) { - return 'block-editor'; - } - - return 'mini-cart'; + return apply_filters( 'woocommerce_paypal_payments_context', $context ); } /** diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index 44cfd1988..a5772dccd 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; +use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; /** @@ -54,6 +55,7 @@ class CompatModule implements ModuleInterface { $this->migrate_smart_button_settings( $c ); $this->fix_page_builders(); + $this->set_elementor_checkout_context(); } /** @@ -329,4 +331,32 @@ class CompatModule implements ModuleInterface { $parent = $theme->parent(); return ( $parent && $parent->get( 'Name' ) === 'Divi' ); } + + /** + * Sets the context for the Elementor checkout page. + * + * @return void + */ + protected function set_elementor_checkout_context(): void { + add_action( + 'wp', + function() { + $page_id = get_the_ID(); + if ( $page_id ) { + if ( CartCheckoutDetector::has_elementor_checkout( $page_id ) ) { + add_filter( + 'woocommerce_paypal_payments_context', + function ( $context ): string { + // Default context. + if ( 'mini-cart' === $context ) { + return 'checkout'; + } + return $context; + } + ); + } + } + } + ); + } } diff --git a/modules/ppcp-wc-gateway/src/Helper/CartCheckoutDetector.php b/modules/ppcp-wc-gateway/src/Helper/CartCheckoutDetector.php index faac7f257..b66a8e8ff 100644 --- a/modules/ppcp-wc-gateway/src/Helper/CartCheckoutDetector.php +++ b/modules/ppcp-wc-gateway/src/Helper/CartCheckoutDetector.php @@ -57,12 +57,19 @@ class CartCheckoutDetector { /** * Check if the Checkout page is using Elementor. * + * @param int $page_id The ID of the page. + * * @return bool */ - public static function has_elementor_checkout(): bool { + public static function has_elementor_checkout( int $page_id = 0 ): bool { // Check if Elementor is installed and activated. if ( did_action( 'elementor/loaded' ) ) { - $elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'checkout' ) ); + if ( $page_id ) { + $elementor_widgets = self::get_elementor_widgets( $page_id ); + } else { + // Check the WooCommerce checkout page. + $elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'checkout' ) ); + } if ( $elementor_widgets ) { return in_array( 'woocommerce-checkout-page', $elementor_widgets, true ); From 14b20a696216eef15c8cd89040be4c28454c401e Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 29 May 2024 22:17:34 +0200 Subject: [PATCH 2/6] Reduce nested conditions --- modules/ppcp-compat/src/CompatModule.php | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index a5772dccd..b2f31f052 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -342,20 +342,17 @@ class CompatModule implements ModuleInterface { 'wp', function() { $page_id = get_the_ID(); - if ( $page_id ) { - if ( CartCheckoutDetector::has_elementor_checkout( $page_id ) ) { - add_filter( - 'woocommerce_paypal_payments_context', - function ( $context ): string { - // Default context. - if ( 'mini-cart' === $context ) { - return 'checkout'; - } - return $context; - } - ); - } + if ( ! $page_id || ! CartCheckoutDetector::has_elementor_checkout( $page_id ) ) { + return; } + + add_filter( + 'woocommerce_paypal_payments_context', + function ( $context ): string { + // Default context. + return ( 'mini-cart' === $context ) ? 'checkout' : $context; + } + ); } ); } From e91e5e718ddcdc775994608ce9802d5309ea3fdd Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 29 May 2024 22:35:48 +0200 Subject: [PATCH 3/6] Fix Psalm error --- modules/ppcp-compat/src/CompatModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index b2f31f052..4c23bd8c1 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -348,7 +348,7 @@ class CompatModule implements ModuleInterface { add_filter( 'woocommerce_paypal_payments_context', - function ( $context ): string { + function ( string $context ): string { // Default context. return ( 'mini-cart' === $context ) ? 'checkout' : $context; } From 144c8da3f6fc7d3a73dc4ac0044eb133d287dd82 Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 5 Jun 2024 20:50:39 +0200 Subject: [PATCH 4/6] Add Litespeed Cache and W3 Total Cache compatibility --- modules/ppcp-compat/services.php | 12 +++++ modules/ppcp-compat/src/CompatModule.php | 60 ++++++++++++++++-------- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 364686a84..70ae6dac2 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -54,6 +54,18 @@ return array( ); }, + 'compat.plugin-script-file-names' => static function( ContainerInterface $container ) : array { + return array( + 'button.js', + 'gateway-settings.js', + 'status-page.js', + 'order-edit-page.js', + 'fraudnet.js', + 'tracking-compat.js', + 'ppcp-clear-db.js', + ); + }, + 'compat.gzd.is_supported_plugin_version_active' => function (): bool { return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+ }, diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index 44cfd1988..14ee3affc 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -41,7 +41,6 @@ class CompatModule implements ModuleInterface { public function run( ContainerInterface $c ): void { $this->initialize_ppec_compat_layer( $c ); - $this->fix_site_ground_optimizer_compatibility( $c ); $this->initialize_tracking_compat_layer( $c ); $asset_loader = $c->get( 'compat.assets' ); @@ -54,6 +53,7 @@ class CompatModule implements ModuleInterface { $this->migrate_smart_button_settings( $c ); $this->fix_page_builders(); + $this->exclude_cache_plugins_js_minification( $c ); } /** @@ -88,24 +88,6 @@ class CompatModule implements ModuleInterface { } } ); - - } - - /** - * Fixes the compatibility issue for SiteGround Optimizer plugin. - * - * @link https://wordpress.org/plugins/sg-cachepress/ - * - * @param ContainerInterface $c The Container. - */ - protected function fix_site_ground_optimizer_compatibility( ContainerInterface $c ): void { - $ppcp_script_names = $c->get( 'compat.plugin-script-names' ); - add_filter( - 'sgo_js_minify_exclude', - function ( array $scripts ) use ( $ppcp_script_names ) { - return array_merge( $scripts, $ppcp_script_names ); - } - ); } /** @@ -329,4 +311,44 @@ class CompatModule implements ModuleInterface { $parent = $theme->parent(); return ( $parent && $parent->get( 'Name' ) === 'Divi' ); } + + /** + * Excludes PayPal scripts from being minified by cache plugins. + * + * @param ContainerInterface $c The Container. + * @return void + */ + protected function exclude_cache_plugins_js_minification( ContainerInterface $c ): void { + $ppcp_script_names = $c->get( 'compat.plugin-script-names' ); + $ppcp_script_file_names = $c->get( 'compat.plugin-script-file-names' ); + + // Siteground SG Optimize. + add_filter( + 'sgo_js_minify_exclude', + function( $scripts ) use ( $ppcp_script_names ) { + return array_merge( $scripts, $ppcp_script_names ); + } + ); + + // LiteSpeed Cache. + add_filter( + 'litespeed_optimize_js_excludes', + function( $excluded_js ) use ( $ppcp_script_file_names ) { + return array_merge( $excluded_js, $ppcp_script_file_names ); + } + ); + + // W3 Total Cache. + add_filter( + 'w3tc_minify_js_do_tag_minification', + function( $do_tag_minification, $script_tag, $file ) { + if ( $file && strpos( $file, 'ppcp' ) !== false ) { + return false; + } + return $do_tag_minification; + }, + 10, + 3 + ); + } } From 1ff8ac6ade76d6c2d9b8f040b945db4db65f5374 Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Wed, 5 Jun 2024 22:15:04 +0200 Subject: [PATCH 5/6] Fix Psalm errors --- modules/ppcp-compat/src/CompatModule.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-compat/src/CompatModule.php b/modules/ppcp-compat/src/CompatModule.php index 14ee3affc..6e1553587 100644 --- a/modules/ppcp-compat/src/CompatModule.php +++ b/modules/ppcp-compat/src/CompatModule.php @@ -325,7 +325,7 @@ class CompatModule implements ModuleInterface { // Siteground SG Optimize. add_filter( 'sgo_js_minify_exclude', - function( $scripts ) use ( $ppcp_script_names ) { + function( array $scripts ) use ( $ppcp_script_names ) { return array_merge( $scripts, $ppcp_script_names ); } ); @@ -333,7 +333,7 @@ class CompatModule implements ModuleInterface { // LiteSpeed Cache. add_filter( 'litespeed_optimize_js_excludes', - function( $excluded_js ) use ( $ppcp_script_file_names ) { + function( array $excluded_js ) use ( $ppcp_script_file_names ) { return array_merge( $excluded_js, $ppcp_script_file_names ); } ); @@ -341,7 +341,7 @@ class CompatModule implements ModuleInterface { // W3 Total Cache. add_filter( 'w3tc_minify_js_do_tag_minification', - function( $do_tag_minification, $script_tag, $file ) { + function( bool $do_tag_minification, string $script_tag, string $file ) { if ( $file && strpos( $file, 'ppcp' ) !== false ) { return false; } From ae9616205a73155f1bcc4349d9c812d2b00b18fb Mon Sep 17 00:00:00 2001 From: James Allan Date: Tue, 11 Jun 2024 12:10:31 +1000 Subject: [PATCH 6/6] Product version bump update --- changelog.txt | 2 +- readme.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index e8de50404..c424884cd 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,6 @@ *** Changelog *** -= 2.8.0 - xxxx-xx-xx = += 2.8.0 - 2024-06-11 = * Fix - Calculate totals after adding shipping to include taxes #2296 * Fix - Package tracking integration throws error in 2.7.1 #2289 * Fix - Make PayPal Subscription products unique in cart #2265 diff --git a/readme.txt b/readme.txt index 1542090d5..28f7e964c 100644 --- a/readme.txt +++ b/readme.txt @@ -179,7 +179,7 @@ If you encounter issues with the PayPal buttons not appearing after an update, p == Changelog == -= 2.8.0 - xxxx-xx-xx = += 2.8.0 - 2024-06-11 = * Fix - Calculate totals after adding shipping to include taxes #2296 * Fix - Package tracking integration throws error in 2.7.1 #2289 * Fix - Make PayPal Subscription products unique in cart #2265