From 3099f672cb6645ed6202b62c0f8f3837265e95d4 Mon Sep 17 00:00:00 2001 From: James Allan Date: Mon, 24 May 2021 09:36:07 +1000 Subject: [PATCH 1/6] No longer load PayPal Standard on new installs --- includes/class-wc-payment-gateways.php | 16 +++++++- .../paypal/class-wc-gateway-paypal.php | 38 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-payment-gateways.php b/includes/class-wc-payment-gateways.php index c6114ef68c7..04520322ee0 100644 --- a/includes/class-wc-payment-gateways.php +++ b/includes/class-wc-payment-gateways.php @@ -78,9 +78,12 @@ class WC_Payment_Gateways { 'WC_Gateway_BACS', 'WC_Gateway_Cheque', 'WC_Gateway_COD', - 'WC_Gateway_Paypal', ); + if ( $this->should_load_paypal_standard() ) { + $load_gateways[] = 'WC_Gateway_Paypal'; + } + // Filter. $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); @@ -219,4 +222,15 @@ class WC_Payment_Gateways { update_option( 'woocommerce_gateway_order', $order ); } + + /** + * Determines if PayPal Standard should be loaded. + * + * @since 5.5.0 + * @return bool Whether PayPal Standard should be loaded or not. + */ + protected function should_load_paypal_standard() { + $paypal = new WC_Gateway_Paypal(); + return $paypal->should_load(); + } } diff --git a/includes/gateways/paypal/class-wc-gateway-paypal.php b/includes/gateways/paypal/class-wc-gateway-paypal.php index 68a8095ad22..e6390151214 100644 --- a/includes/gateways/paypal/class-wc-gateway-paypal.php +++ b/includes/gateways/paypal/class-wc-gateway-paypal.php @@ -473,4 +473,42 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway { return $text; } + + /** + * Determines whether PayPal Standard should be loaded or not. + * + * By default PayPal Standard isn't loaded on new installs or on existing sites which haven't set up the gateway. + * + * @since 5.5.0 + * + * @return bool Whether PayPal Standard should be loaded. + */ + public function should_load() { + $option_key = '_should_load'; + $should_load = $this->get_option( $option_key ); + + if ( '' === $should_load ) { + + // New installs without PayPal Standard enabled don't load it. + if ( 'no' === $this->enabled && WC_Install::is_new_install() ) { + $should_load = false; + } else { + $should_load = true; + } + + $this->update_option( $option_key, wc_bool_to_string( $should_load ) ); + } else { + $should_load = wc_string_to_bool( $should_load ); + } + + /** + * Allow third-parties to filter whether PayPal Standard should be loaded or not. + * + * @since 5.5.0 + * + * @param bool $should_load Whether PayPal Standard should be loaded. + * @param WC_Gateway_Paypal $this The WC_Gateway_Paypal instance. + */ + return apply_filters( 'woocommerce_should_load_paypal_standard', $should_load, $this ); + } } From 7f4f165cc0ae5aabb6e6be2b8c6816cc5f019370 Mon Sep 17 00:00:00 2001 From: James Allan Date: Mon, 24 May 2021 09:38:38 +1000 Subject: [PATCH 2/6] Set the PayPal load flag on install --- includes/class-wc-install.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index c4eddfa86c2..4ad4e557b2f 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -310,6 +310,7 @@ class WC_Install { self::create_files(); self::maybe_create_pages(); self::maybe_set_activation_transients(); + self::set_paypal_standard_load_eligibility(); self::update_wc_version(); self::maybe_update_db_version(); @@ -1622,6 +1623,16 @@ CREATE TABLE {$wpdb->prefix}wc_reserved_stock ( ob_end_clean(); } } + + /** + * Sets whether PayPal Standard will be loaded on install. + * + * @since 5.5.0 + */ + private static function set_paypal_standard_load_eligibility() { + // Initiating the payment gateways sets the flag. + WC()->payment_gateways(); + } } WC_Install::init(); From ed3c76bd1debcc1c463417d7fa90ca1f7bb4668b Mon Sep 17 00:00:00 2001 From: James Allan Date: Mon, 24 May 2021 11:26:49 +1000 Subject: [PATCH 3/6] Load PayPal Standard in unit tests --- tests/legacy/bootstrap.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/legacy/bootstrap.php b/tests/legacy/bootstrap.php index bfbcd11edeb..dbab8509f5c 100644 --- a/tests/legacy/bootstrap.php +++ b/tests/legacy/bootstrap.php @@ -58,6 +58,9 @@ class WC_Unit_Tests_Bootstrap { // load test function so tests_add_filter() is available. require_once $this->wp_tests_dir . '/includes/functions.php'; + // Always load PayPal Standard for unit tests. + tests_add_filter( 'woocommerce_should_load_paypal_standard', '__return_true' ); + // load WC. tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc' ) ); From 381626069ec8480c4ed56572b5f0054ebc53ea7d Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 17 Jun 2021 14:32:21 +0530 Subject: [PATCH 4/6] Load PayPal gateway directly to prevent potential gateway errors. `install()` method may be called before wc_version is set, this may cause unknown errors in 3PD gateways. So its safer to only load PayPal gateway directly. --- includes/class-wc-install.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 4ad4e557b2f..d489fcf333b 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -1631,7 +1631,9 @@ CREATE TABLE {$wpdb->prefix}wc_reserved_stock ( */ private static function set_paypal_standard_load_eligibility() { // Initiating the payment gateways sets the flag. - WC()->payment_gateways(); + if ( class_exists( 'WC_Gateway_Paypal' ) ) { + ( new WC_Gateway_Paypal() )->should_load(); + } } } From 532998b6e8fc159e222a93c8820f0971543c0527 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 17 Jun 2021 14:34:22 +0530 Subject: [PATCH 5/6] No need to check for Paypal standard since its removed. See #29971 but basically we want to remove PayPal standard in favor of PayPal checkout which has better experience. So this test is not valid anymore. --- .../core-tests/specs/shopper/front-end-checkout.test.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js index 17e84c339c7..e5b1bf1e0ad 100644 --- a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js +++ b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js @@ -66,14 +66,6 @@ const runCheckoutPageTest = () => { // Verify that settings have been saved await verifyCheckboxIsSet('#woocommerce_cod_enabled'); - // Enable PayPal payment method - await merchant.openSettings('checkout', 'paypal'); - await setCheckbox('#woocommerce_paypal_enabled'); - await settingsPageSaveChanges(); - - // Verify that settings have been saved - await verifyCheckboxIsSet('#woocommerce_paypal_enabled'); - await merchant.logout(); }); From 8fbb86e0963f6dd44f80596229ea79ef8cdccfc1 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 17 Jun 2021 15:11:42 +0530 Subject: [PATCH 6/6] Remove PayPal test since its removed by default now. --- tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js index e5b1bf1e0ad..8d8b9588d29 100644 --- a/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js +++ b/tests/e2e/core-tests/specs/shopper/front-end-checkout.test.js @@ -82,7 +82,6 @@ const runCheckoutPageTest = () => { await shopper.goToCheckout(); await shopper.productIsInCheckout(simpleProductName, `2`, twoProductPrice, twoProductPrice); - await expect(page).toClick('.wc_payment_method label', {text: 'PayPal'}); await expect(page).toClick('.wc_payment_method label', {text: 'Direct bank transfer'}); await expect(page).toClick('.wc_payment_method label', {text: 'Cash on delivery'}); });