From b11a539c2402c82856fbee7bb6c31c7476cce7df Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Thu, 19 Oct 2023 18:44:44 +0100 Subject: [PATCH] Refactor styles loading on GooglePay and ApplePay Remove unnecessary console.log on GooglePay and ApplePay --- modules/ppcp-applepay/resources/js/boot.js | 1 - modules/ppcp-applepay/src/ApplepayModule.php | 12 +++++++-- .../src/Assets/ApplePayButton.php | 25 +++++++++++++------ .../resources/js/GooglepayButton.js | 1 - modules/ppcp-googlepay/src/Assets/Button.php | 23 ++++++++++++----- .../ppcp-googlepay/src/GooglepayModule.php | 15 ++++++++--- 6 files changed, 56 insertions(+), 21 deletions(-) diff --git a/modules/ppcp-applepay/resources/js/boot.js b/modules/ppcp-applepay/resources/js/boot.js index 3830929ef..42607e6da 100644 --- a/modules/ppcp-applepay/resources/js/boot.js +++ b/modules/ppcp-applepay/resources/js/boot.js @@ -20,7 +20,6 @@ import ApplepayManager from "./ApplepayManager"; (typeof (buttonConfig) === 'undefined') || (typeof (ppcpConfig) === 'undefined') ) { - console.error('PayPal button could not be configured.'); return; } const isMiniCart = ppcpConfig.mini_cart_buttons_enabled; diff --git a/modules/ppcp-applepay/src/ApplepayModule.php b/modules/ppcp-applepay/src/ApplepayModule.php index c941797e2..dee06dad6 100644 --- a/modules/ppcp-applepay/src/ApplepayModule.php +++ b/modules/ppcp-applepay/src/ApplepayModule.php @@ -136,10 +136,18 @@ class ApplepayModule implements ModuleInterface { assert( $button instanceof ApplePayButton ); $smart_button = $c->get( 'button.smart-button' ); assert( $smart_button instanceof SmartButtonInterface ); - $page_has_block = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ); - if ( $smart_button->should_load_ppcp_script() || $page_has_block ) { + if ( $smart_button->should_load_ppcp_script() ) { $button->enqueue(); } + + if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) { + /** + * Should add this to the ButtonInterface. + * + * @psalm-suppress UndefinedInterfaceMethod + */ + $button->enqueue_styles(); + } } ); add_action( diff --git a/modules/ppcp-applepay/src/Assets/ApplePayButton.php b/modules/ppcp-applepay/src/Assets/ApplePayButton.php index a4ff3ca67..f27950fcd 100644 --- a/modules/ppcp-applepay/src/Assets/ApplePayButton.php +++ b/modules/ppcp-applepay/src/Assets/ApplePayButton.php @@ -1017,13 +1017,7 @@ class ApplePayButton implements ButtonInterface { ); wp_enqueue_script( 'wc-ppcp-applepay' ); - wp_register_style( - 'wc-ppcp-applepay', - untrailingslashit( $this->module_url ) . '/assets/css/styles.css', - array(), - $this->version - ); - wp_enqueue_style( 'wc-ppcp-applepay' ); + $this->enqueue_styles(); wp_localize_script( 'wc-ppcp-applepay', @@ -1038,6 +1032,23 @@ class ApplePayButton implements ButtonInterface { ); } + /** + * Enqueues styles. + */ + public function enqueue_styles(): void { + if ( ! $this->is_enabled() ) { + return; + } + + wp_register_style( + 'wc-ppcp-applepay', + untrailingslashit( $this->module_url ) . '/assets/css/styles.css', + array(), + $this->version + ); + wp_enqueue_style( 'wc-ppcp-applepay' ); + } + /** * Returns the script data. * diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index 57d29a75d..9ecb778d5 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -189,7 +189,6 @@ class GooglepayButton { callback(el); } else if (timeElapsed > timeout) { clearInterval(interval); - console.error('Waiting for wrapper timed out.', selector); } }, delay); } diff --git a/modules/ppcp-googlepay/src/Assets/Button.php b/modules/ppcp-googlepay/src/Assets/Button.php index b4ad9c297..6609ef0e1 100644 --- a/modules/ppcp-googlepay/src/Assets/Button.php +++ b/modules/ppcp-googlepay/src/Assets/Button.php @@ -363,6 +363,23 @@ class Button implements ButtonInterface { ); wp_enqueue_script( 'wc-ppcp-googlepay' ); + $this->enqueue_styles(); + + wp_localize_script( + 'wc-ppcp-googlepay', + 'wc_ppcp_googlepay', + $this->script_data() + ); + } + + /** + * Enqueues styles. + */ + public function enqueue_styles(): void { + if ( ! $this->is_enabled() ) { + return; + } + wp_register_style( 'wc-ppcp-googlepay', untrailingslashit( $this->module_url ) . '/assets/css/styles.css', @@ -370,12 +387,6 @@ class Button implements ButtonInterface { $this->version ); wp_enqueue_style( 'wc-ppcp-googlepay' ); - - wp_localize_script( - 'wc-ppcp-googlepay', - 'wc_ppcp_googlepay', - $this->script_data() - ); } /** diff --git a/modules/ppcp-googlepay/src/GooglepayModule.php b/modules/ppcp-googlepay/src/GooglepayModule.php index 614647181..2365ab23f 100644 --- a/modules/ppcp-googlepay/src/GooglepayModule.php +++ b/modules/ppcp-googlepay/src/GooglepayModule.php @@ -89,11 +89,18 @@ class GooglepayModule implements ModuleInterface { static function () use ( $c, $button ) { $smart_button = $c->get( 'button.smart-button' ); assert( $smart_button instanceof SmartButtonInterface ); - $page_has_block = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ); - if ( ! $smart_button->should_load_ppcp_script() && ! $page_has_block ) { - return; + if ( $smart_button->should_load_ppcp_script() ) { + $button->enqueue(); + } + + if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) { + /** + * Should add this to the ButtonInterface. + * + * @psalm-suppress UndefinedInterfaceMethod + */ + $button->enqueue_styles(); } - $button->enqueue(); } );