diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index 8c758bb29..360187b92 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -11,7 +11,7 @@ jobs: matrix: woocommerce: [ 'beta' ] wordpress: [ 'latest' ] - php: [ '7.4', '8.0' ] + php: [ '7.4', '8.0', '8.1' ] name: Beta (PHP=${{ matrix.php }}, WP=${{ matrix.wordpress }}, WC=${{ matrix.woocommerce }}) diff --git a/.gitignore b/.gitignore index fd32909bf..bde1b18cc 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ docker/bin/jt docker/data docker/wordpress docker/logs + +# PHPUnit cache files +.phpunit.cache/ +.phpunit.result.cache diff --git a/changelog.txt b/changelog.txt index a7fa4aa51..2ad3ac846 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 8.7.0 - xxxx-xx-xx = +* Fix - Fix multiple compatibility issues and deprecation warnings when running the extension on PHP 8.1. * Fix - Re-connect promotional surface blinking after disappearing for merchants that have already connected their Stripe account. * Fix - Fix possible fatal errors when Stripe settings format is invalid during account connection. * Fix - Clear webhook state after reconfiguring webhooks to remove outdated error and success statuses. diff --git a/composer.json b/composer.json index b40f0dcc8..7055cf94b 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "require-dev": { "composer/installers": "1.9.0", "phpunit/phpunit": "7.5.20", - "yoast/phpunit-polyfills": "^1.1", + "yoast/phpunit-polyfills": "^2.0", "woocommerce/woocommerce-sniffs": "0.1.0", "wp-cli/wp-cli-bundle": "2.5.0" }, diff --git a/composer.lock b/composer.lock index f7111e189..62dd4da0d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7527df7677da2d49310b02acb2415bb9", + "content-hash": "ce8e7d358061604e3feaeb46c9a8b909", "packages": [], "packages-dev": [ { @@ -6678,7 +6678,7 @@ }, { "name": "wp-cli/wp-cli", - "version": "v2.10.0", + "version": "v2.11.0", "source": { "type": "git", "url": "https://github.com/wp-cli/wp-cli.git", @@ -6917,21 +6917,21 @@ }, { "name": "yoast/phpunit-polyfills", - "version": "1.1.1", + "version": "2.0.1", "source": { "type": "git", "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", - "reference": "a0f7d708794a738f328d7b6c94380fd1d6c40446" + "reference": "4a088f125c970d6d6ea52c927f96fe39b330d0f1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/a0f7d708794a738f328d7b6c94380fd1d6c40446", - "reference": "a0f7d708794a738f328d7b6c94380fd1d6c40446", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/4a088f125c970d6d6ea52c927f96fe39b330d0f1", + "reference": "4a088f125c970d6d6ea52c927f96fe39b330d0f1", "shasum": "" }, "require": { - "php": ">=5.4", - "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" + "php": ">=5.6", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" }, "require-dev": { "php-parallel-lint/php-console-highlighter": "^1.0.0", @@ -6976,7 +6976,7 @@ "security": "https://github.com/Yoast/PHPUnit-Polyfills/security/policy", "source": "https://github.com/Yoast/PHPUnit-Polyfills" }, - "time": "2024-04-05T16:01:51+00:00" + "time": "2024-04-05T16:36:44+00:00" } ], "aliases": [], diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 59b316cea..555b91520 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -120,7 +120,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 4147bf82a..2ae68fccb 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -442,7 +442,7 @@ public function get_stripe_return_url( $order = null, $id = null ) { * @return array() */ public function generate_payment_request( $order, $prepared_payment_method ) { - $settings = get_option( 'woocommerce_stripe_settings', [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $is_short_statement_descriptor_enabled = ! empty( $settings['is_short_statement_descriptor_enabled'] ) && 'yes' === $settings['is_short_statement_descriptor_enabled']; $capture = ! empty( $settings['capture'] ) && 'yes' === $settings['capture'] ? true : false; $post_data = []; diff --git a/includes/admin/class-wc-rest-stripe-account-keys-controller.php b/includes/admin/class-wc-rest-stripe-account-keys-controller.php index bb264e710..22d2c4079 100644 --- a/includes/admin/class-wc-rest-stripe-account-keys-controller.php +++ b/includes/admin/class-wc-rest-stripe-account-keys-controller.php @@ -13,6 +13,11 @@ * @since 5.6.0 */ class WC_REST_Stripe_Account_Keys_Controller extends WC_Stripe_REST_Base_Controller { + /** + * The option name for the Stripe gateway settings. + * + * @deprecated 8.7.0 + */ const STRIPE_GATEWAY_SETTINGS_OPTION_NAME = 'woocommerce_stripe_settings'; /** @@ -154,7 +159,7 @@ public function register_routes() { */ public function get_account_keys() { $allowed_params = [ 'publishable_key', 'secret_key', 'webhook_secret', 'test_publishable_key', 'test_secret_key', 'test_webhook_secret' ]; - $stripe_settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); // Filter only the fields we want to return $account_keys = array_intersect_key( $stripe_settings, array_flip( $allowed_params ) ); @@ -247,7 +252,7 @@ public function validate_test_secret_key( $param, $request, $key ) { * @param WP_REST_Request $request Full data about the request. */ public function set_account_keys( WP_REST_Request $request ) { - $settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $allowed_params = [ 'publishable_key', 'secret_key', 'webhook_secret', 'test_publishable_key', 'test_secret_key', 'test_webhook_secret' ]; $current_account_keys = array_intersect_key( $settings, array_flip( $allowed_params ) ); @@ -278,7 +283,7 @@ public function set_account_keys( WP_REST_Request $request ) { // Before saving the settings, decommission any previously automatically configured webhook endpoint. $settings = $this->decommission_configured_webhook_after_key_update( $settings, $current_account_keys ); - update_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); // Disable all payment methods if all keys are different from the current ones if ( $current_account_keys['publishable_key'] !== $settings['publishable_key'] @@ -324,7 +329,7 @@ public function test_account_keys( WP_REST_Request $request ) { $publishable = wc_clean( wp_unslash( $request->get_param( 'publishable' ) ) ); $secret = wc_clean( wp_unslash( $request->get_param( 'secret' ) ) ); - $settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); if ( $publishable === $this->mask_key_value( $publishable ) ) { $publishable = $settings[ $live_mode ? 'publishable_key' : 'test_publishable_key' ]; @@ -384,7 +389,7 @@ public function configure_webhooks( WP_REST_Request $request ) { WC_Rate_Limiter::set_rate_limit( $rate_limit_key, 60 ); - $settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $secret = wc_clean( wp_unslash( $request->get_param( 'secret' ) ) ); $saved_secret = $settings[ $live_mode ? 'secret_key' : 'test_secret_key' ]; diff --git a/includes/admin/class-wc-rest-stripe-settings-controller.php b/includes/admin/class-wc-rest-stripe-settings-controller.php index c41c81d1b..75ff2882f 100644 --- a/includes/admin/class-wc-rest-stripe-settings-controller.php +++ b/includes/admin/class-wc-rest-stripe-settings-controller.php @@ -454,9 +454,9 @@ private function update_is_upe_enabled( WP_REST_Request $request ) { return; } - $settings = get_option( 'woocommerce_stripe_settings', [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = $is_upe_enabled ? 'yes' : 'disabled'; - update_option( 'woocommerce_stripe_settings', $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); // including the class again because otherwise it's not present. if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) { diff --git a/includes/admin/class-wc-stripe-admin-notices.php b/includes/admin/class-wc-stripe-admin-notices.php index aa3ee28ba..731f46303 100644 --- a/includes/admin/class-wc-stripe-admin-notices.php +++ b/includes/admin/class-wc-stripe-admin-notices.php @@ -122,7 +122,7 @@ public function stripe_check_environment() { $show_curl_notice = get_option( 'wc_stripe_show_curl_notice' ); $show_sca_notice = get_option( 'wc_stripe_show_sca_notice' ); $changed_keys_notice = get_option( 'wc_stripe_show_changed_keys_notice' ); - $options = get_option( 'woocommerce_stripe_settings' ); + $options = WC_Stripe_Helper::get_stripe_settings(); $testmode = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false; $test_pub_key = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : ''; $test_secret_key = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : ''; diff --git a/includes/admin/class-wc-stripe-inbox-notes.php b/includes/admin/class-wc-stripe-inbox-notes.php index f7afa6310..8a7676f7d 100644 --- a/includes/admin/class-wc-stripe-inbox-notes.php +++ b/includes/admin/class-wc-stripe-inbox-notes.php @@ -111,7 +111,7 @@ public static function should_show_marketing_note() { } // Make sure Apple Pay is enabled and setup is successful. - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_enabled = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled']; $button_enabled = isset( $stripe_settings['payment_request'] ) && 'yes' === $stripe_settings['payment_request']; $verification_complete = isset( $stripe_settings['apple_pay_domain_set'] ) && 'yes' === $stripe_settings['apple_pay_domain_set']; diff --git a/includes/admin/class-wc-stripe-payment-gateways-controller.php b/includes/admin/class-wc-stripe-payment-gateways-controller.php index 7fd10c7f2..e94613ff9 100644 --- a/includes/admin/class-wc-stripe-payment-gateways-controller.php +++ b/includes/admin/class-wc-stripe-payment-gateways-controller.php @@ -17,7 +17,7 @@ class WC_Stripe_Payment_Gateways_Controller { */ public function __construct() { // If UPE is enabled and there are enabled payment methods, we need to load the disable Stripe confirmation modal. - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $enabled_upe_payment_methods = isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) ? $stripe_settings['upe_checkout_experience_accepted_payments'] : []; $upe_payment_requests_enabled = 'yes' === $stripe_settings['payment_request']; diff --git a/includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php b/includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php index 77891f665..e00b2effd 100644 --- a/includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php +++ b/includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php @@ -72,10 +72,10 @@ public function set_flag( WP_REST_Request $request ) { return new WP_REST_Response( [ 'result' => 'bad_request' ], 400 ); } - $settings = get_option( 'woocommerce_stripe_settings', [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = $is_upe_enabled ? 'yes' : 'disabled'; - update_option( 'woocommerce_stripe_settings', $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); // including the class again because otherwise it's not present. if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) { diff --git a/includes/admin/class-wc-stripe-settings-controller.php b/includes/admin/class-wc-stripe-settings-controller.php index 7eda8ad9f..cd4ef921b 100644 --- a/includes/admin/class-wc-stripe-settings-controller.php +++ b/includes/admin/class-wc-stripe-settings-controller.php @@ -92,7 +92,7 @@ public function admin_options( WC_Stripe_Payment_Gateway $gateway ) { wc_back_link( __( 'Return to payments', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ); echo ''; - $settings = get_option( WC_Stripe_Connect::SETTINGS_OPTION, [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $account_data_exists = ( ! empty( $settings['publishable_key'] ) && ! empty( $settings['secret_key'] ) ) || ( ! empty( $settings['test_publishable_key'] ) && ! empty( $settings['test_secret_key'] ) ); echo $account_data_exists ? '
' : ''; diff --git a/includes/class-wc-stripe-account.php b/includes/class-wc-stripe-account.php index 989d689c1..d099aa50e 100644 --- a/includes/class-wc-stripe-account.php +++ b/includes/class-wc-stripe-account.php @@ -113,7 +113,7 @@ private function cache_account( $mode = null ) { * @return string Transient key of test mode when testmode is enabled, otherwise returns the key of live mode. */ private function get_transient_key( $mode = null ) { - $settings_options = get_option( 'woocommerce_stripe_settings', [] ); + $settings_options = WC_Stripe_Helper::get_stripe_settings(); // If the mode is not provided or is invalid, we'll check the current mode. if ( is_null( $mode ) || ! in_array( $mode, [ 'test', 'live' ] ) ) { @@ -309,7 +309,7 @@ public function configure_webhooks( $mode = 'live', $secret_key = '' ) { WC_Stripe_API::set_secret_key( $previous_secret ); } - $settings = get_option( WC_Stripe::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $webhook_secret_setting = 'live' === $mode ? 'webhook_secret' : 'test_webhook_secret'; $webhook_data_setting = 'live' === $mode ? 'webhook_data' : 'test_webhook_data'; @@ -322,7 +322,7 @@ public function configure_webhooks( $mode = 'live', $secret_key = '' ) { 'secret' => WC_Stripe_API::get_secret_key(), ]; - update_option( WC_Stripe::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); // After reconfiguring webhooks, clear the webhook state. WC_Stripe_Webhook_State::clear_state(); diff --git a/includes/class-wc-stripe-api.php b/includes/class-wc-stripe-api.php index 46aadf7f8..cf6f702ea 100644 --- a/includes/class-wc-stripe-api.php +++ b/includes/class-wc-stripe-api.php @@ -50,7 +50,7 @@ public static function get_secret_key() { * @param string|null $mode Optional. The mode to set the secret key for. 'live' or 'test'. Default will set the secret for the currently active mode. */ public static function set_secret_key_for_mode( $mode = null ) { - $options = get_option( 'woocommerce_stripe_settings' ); + $options = WC_Stripe_Helper::get_stripe_settings(); $secret_key = $options['secret_key'] ?? ''; $test_secret_key = $options['test_secret_key'] ?? ''; @@ -389,7 +389,7 @@ public static function detach_payment_method_from_customer( string $customer_id, * @return bool True if the payment should be detached, false otherwise. */ public static function should_detach_payment_method_from_customer() { - $options = get_option( 'woocommerce_stripe_settings' ); + $options = WC_Stripe_Helper::get_stripe_settings(); $test_mode = isset( $options['testmode'] ) && 'yes' === $options['testmode']; // If we are in test mode, we can always detach the payment method. diff --git a/includes/class-wc-stripe-apple-pay-registration.php b/includes/class-wc-stripe-apple-pay-registration.php index 266b5c1d0..9bee29615 100644 --- a/includes/class-wc-stripe-apple-pay-registration.php +++ b/includes/class-wc-stripe-apple-pay-registration.php @@ -53,7 +53,7 @@ public function __construct() { add_action( 'add_option_woocommerce_stripe_settings', [ $this, 'verify_domain_on_new_settings' ], 10, 2 ); add_action( 'update_option_woocommerce_stripe_settings', [ $this, 'verify_domain_on_updated_settings' ], 10, 2 ); - $this->stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $this->stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->domain_name = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : str_replace( array( 'https://', 'http://' ), '', get_site_url() ); // @codingStandardsIgnoreLine $this->apple_pay_domain_set = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' ); $this->apple_pay_verify_notice = ''; @@ -286,7 +286,7 @@ public function register_domain_with_apple( $secret_key ) { $this->stripe_settings['apple_pay_domain_set'] = 'yes'; $this->apple_pay_domain_set = true; - update_option( 'woocommerce_stripe_settings', $this->stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $this->stripe_settings ); WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' ); @@ -297,7 +297,7 @@ public function register_domain_with_apple( $secret_key ) { $this->stripe_settings['apple_pay_domain_set'] = 'no'; $this->apple_pay_domain_set = false; - update_option( 'woocommerce_stripe_settings', $this->stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $this->stripe_settings ); WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); diff --git a/includes/class-wc-stripe-blocks-support.php b/includes/class-wc-stripe-blocks-support.php index 8e46d89f1..2f8788418 100644 --- a/includes/class-wc-stripe-blocks-support.php +++ b/includes/class-wc-stripe-blocks-support.php @@ -42,7 +42,7 @@ public function __construct( $payment_request_configuration = null ) { * Initializes the payment method type. */ public function initialize() { - $this->settings = get_option( 'woocommerce_stripe_settings', [] ); + $this->settings = WC_Stripe_Helper::get_stripe_settings(); } /** diff --git a/includes/class-wc-stripe-feature-flags.php b/includes/class-wc-stripe-feature-flags.php index 69377bb25..e066a5c39 100644 --- a/includes/class-wc-stripe-feature-flags.php +++ b/includes/class-wc-stripe-feature-flags.php @@ -22,7 +22,7 @@ public static function is_upe_preview_enabled() { * @return bool */ public static function is_upe_checkout_enabled() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', null ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); return ! empty( $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) && 'yes' === $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ]; } @@ -33,7 +33,7 @@ public static function is_upe_checkout_enabled() { * @return bool */ public static function did_merchant_disable_upe() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', null ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); return ! empty( $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) && 'disabled' === $stripe_settings[ self::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ]; } } diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 5d7cb1676..21049d1ff 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -9,6 +9,7 @@ * @since 4.0.0 */ class WC_Stripe_Helper { + const SETTINGS_OPTION = 'woocommerce_stripe_settings'; const LEGACY_META_NAME_FEE = 'Stripe Fee'; const LEGACY_META_NAME_NET = 'Net Revenue From Stripe'; const META_NAME_FEE = '_stripe_fee'; @@ -23,6 +24,39 @@ class WC_Stripe_Helper { */ public static $stripe_legacy_gateways = []; + /** + * Get the main Stripe settings option. + * + * @param string $method (Optional) The payment method to get the settings from. + * @return array $settings The Stripe settings. + */ + public static function get_stripe_settings( $method = null ) { + $settings = null === $method ? get_option( self::SETTINGS_OPTION, [] ) : get_option( 'woocommerce_stripe_' . $method . '_settings', [] ); + if ( ! is_array( $settings ) ) { + $settings = []; + } + return $settings; + } + + /** + * Update the main Stripe settings option. + * + * @param $options array The Stripe settings. + * @return void + */ + public static function update_main_stripe_settings( $options ) { + update_option( self::SETTINGS_OPTION, $options ); + } + + /** + * Delete the main Stripe settings option. + * + * @return void + */ + public static function delete_main_stripe_settings() { + delete_option( self::SETTINGS_OPTION ); + } + /** * Gets the Stripe currency for order. * @@ -357,13 +391,13 @@ public static function get_minimum_amount() { * @param string $setting The name of the setting to get. */ public static function get_settings( $method = null, $setting = null ) { - $all_settings = null === $method ? get_option( 'woocommerce_stripe_settings', [] ) : get_option( 'woocommerce_stripe_' . $method . '_settings', [] ); + $all_settings = self::get_stripe_settings( $method ); if ( null === $setting ) { return $all_settings; } - return isset( $all_settings[ $setting ] ) ? $all_settings[ $setting ] : ''; + return $all_settings[ $setting ] ?? ''; } /** @@ -449,7 +483,7 @@ public static function get_legacy_payment_method( $id ) { * @return array */ public static function get_legacy_available_payment_method_ids() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = self::get_stripe_settings(); $payment_method_classes = self::get_legacy_payment_method_classes(); $ordered_payment_method_ids = isset( $stripe_settings['stripe_legacy_method_order'] ) ? $stripe_settings['stripe_legacy_method_order'] : []; @@ -478,7 +512,7 @@ function( $payment_method_id ) { // Update the `stripe_legacy_method_order` option with the new order including missing payment methods from the option. $stripe_settings['stripe_legacy_method_order'] = $payment_method_ids; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + self::update_main_stripe_settings( $stripe_settings ); } } else { $payment_method_ids = array_map( @@ -545,7 +579,7 @@ public static function get_legacy_enabled_payment_method_ids() { * @return array */ public static function get_legacy_individual_payment_method_settings() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = self::get_stripe_settings(); $payment_methods = self::get_legacy_payment_methods(); $payment_method_settings = [ @@ -600,7 +634,7 @@ public static function get_upe_individual_payment_method_settings( $gateway ) { // If card settings are not set, get it from the default Stripe settings which might be set before enabling UPE. if ( ! isset( $payment_method_settings['card']['title'] ) && ! isset( $payment_method_settings['card']['description'] ) ) { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = self::get_stripe_settings(); $title = isset( $stripe_settings['title'] ) ? $stripe_settings['title'] : ''; $description = isset( $stripe_settings['description'] ) ? $stripe_settings['description'] : ''; @@ -630,7 +664,7 @@ public static function get_upe_individual_payment_method_settings( $gateway ) { * @return string[] */ public static function get_upe_ordered_payment_method_ids( $gateway ) { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = self::get_stripe_settings(); $testmode = isset( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode']; $ordered_payment_method_ids = isset( $stripe_settings['stripe_upe_payment_method_order'] ) ? $stripe_settings['stripe_upe_payment_method_order'] : []; @@ -671,7 +705,7 @@ function( $payment_method_id ) use ( $available_methods_with_capability ) { $updated_order = array_merge( $ordered_payment_method_ids_with_capability, $additional_methods ); $stripe_settings['stripe_upe_payment_method_order'] = $updated_order; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + self::update_main_stripe_settings( $stripe_settings ); return $updated_order; } @@ -727,7 +761,7 @@ public static function add_stripe_methods_in_woocommerce_gateway_order( $ordered // If the ordered payment method ids are not passed, get them from the relevant settings. if ( empty( $ordered_payment_method_ids ) ) { $is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled(); - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = self::get_stripe_settings(); if ( $is_upe_enabled ) { $ordered_payment_method_ids = $stripe_settings['stripe_upe_payment_method_order'] ?? []; diff --git a/includes/class-wc-stripe-logger.php b/includes/class-wc-stripe-logger.php index 5b545c415..bc7262bd8 100644 --- a/includes/class-wc-stripe-logger.php +++ b/includes/class-wc-stripe-logger.php @@ -98,7 +98,7 @@ private static function can_log(): bool { return false; } - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); if ( empty( $settings ) || isset( $settings['logging'] ) && 'yes' !== $settings['logging'] ) { return false; diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 8b6b50d85..8bb3d006e 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -382,7 +382,7 @@ public function woocommerce_tracks_event_properties( $properties, $prefixed_even // Due diligence done. Collect the metadata. $is_live = true; - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); if ( array_key_exists( 'testmode', $stripe_settings ) ) { $is_live = 'no' === $stripe_settings['testmode']; } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f50a11c36..9d590fe97 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -54,7 +54,7 @@ class WC_Stripe_Webhook_Handler extends WC_Stripe_Payment_Gateway { */ public function __construct() { $this->retry_interval = 2; - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->testmode = ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false; $secret_key = ( $this->testmode ? 'test_' : '' ) . 'webhook_secret'; $this->secret = ! empty( $stripe_settings[ $secret_key ] ) ? $stripe_settings[ $secret_key ] : false; diff --git a/includes/class-wc-stripe-webhook-state.php b/includes/class-wc-stripe-webhook-state.php index 5c953aa78..bb147ccc9 100644 --- a/includes/class-wc-stripe-webhook-state.php +++ b/includes/class-wc-stripe-webhook-state.php @@ -37,7 +37,7 @@ class WC_Stripe_Webhook_State { * @return bool */ public static function get_testmode() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); return ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false; } diff --git a/includes/connect/class-wc-stripe-connect.php b/includes/connect/class-wc-stripe-connect.php index c7226ccdc..3904d752e 100644 --- a/includes/connect/class-wc-stripe-connect.php +++ b/includes/connect/class-wc-stripe-connect.php @@ -10,6 +10,11 @@ */ class WC_Stripe_Connect { + /** + * The option name for the Stripe gateway settings. + * + * @deprecated 8.7.0 + */ const SETTINGS_OPTION = 'woocommerce_stripe_settings'; /** @@ -150,7 +155,7 @@ private function save_stripe_keys( $result, $type = 'connect', $mode = 'live' ) $is_test = 'live' !== $mode; $prefix = $is_test ? 'test_' : ''; $default_options = $this->get_default_stripe_config(); - $current_options = get_option( self::SETTINGS_OPTION, [] ); + $current_options = WC_Stripe_Helper::get_stripe_settings(); $options = array_merge( $default_options, is_array( $current_options ) ? $current_options : [] ); $options['enabled'] = 'yes'; $options['testmode'] = $is_test ? 'yes' : 'no'; @@ -168,7 +173,7 @@ private function save_stripe_keys( $result, $type = 'connect', $mode = 'live' ) unset( $options['account_id'] ); unset( $options['test_account_id'] ); - update_option( self::SETTINGS_OPTION, $options ); + WC_Stripe_Helper::set_stripe_settings( $options ); // Similar to what we do for webhooks, we save some stats to help debug oauth problems. update_option( 'wc_stripe_' . $prefix . 'oauth_updated_at', time() ); @@ -199,7 +204,7 @@ private function save_stripe_keys( $result, $type = 'connect', $mode = 'live' ) * Otherwise for new connections return 'yes' for `upe_checkout_experience_enabled` field. */ private function get_upe_checkout_experience_enabled() { - $existing_stripe_settings = get_option( self::SETTINGS_OPTION, [] ); + $existing_stripe_settings = WC_Stripe_Helper::get_stripe_settings(); if ( isset( $existing_stripe_settings['upe_checkout_experience_enabled'] ) ) { return $existing_stripe_settings['upe_checkout_experience_enabled']; @@ -234,7 +239,7 @@ private function get_default_stripe_config() { * @return bool True if connected, false otherwise. */ public function is_connected( $mode = null ) { - $options = get_option( self::SETTINGS_OPTION, [] ); + $options = WC_Stripe_Helper::get_stripe_settings(); // If the mode is not provided, we'll check the current mode. if ( is_null( $mode ) ) { @@ -259,7 +264,7 @@ public function is_connected_via_oauth( $mode = 'live' ) { return false; } - $options = get_option( self::SETTINGS_OPTION, [] ); + $options = WC_Stripe_Helper::get_stripe_settings(); $key = 'test' === $mode ? 'test_connection_type' : 'connection_type'; return isset( $options[ $key ] ) && in_array( $options[ $key ], [ 'connect', 'app' ], true ); @@ -274,7 +279,7 @@ public function is_connected_via_oauth( $mode = 'live' ) { * @return bool True if connected via Stripe App OAuth, false otherwise. */ public function is_connected_via_app_oauth( $mode = null ) { - $options = get_option( self::SETTINGS_OPTION, [] ); + $options = WC_Stripe_Helper::get_stripe_settings(); // If the mode is not provided, we'll check the current mode. if ( is_null( $mode ) ) { @@ -295,7 +300,7 @@ private function record_account_connect_track_event( bool $had_error ) { return; } - $options = get_option( self::SETTINGS_OPTION, [] ); + $options = WC_Stripe_Helper::get_stripe_settings(); $is_test = isset( $options['testmode'] ) && 'yes' === $options['testmode']; $event_name = ! $had_error ? 'wcstripe_stripe_connected' : 'wcstripe_stripe_connect_error'; @@ -351,7 +356,7 @@ public function refresh_connection() { return; } - $options = get_option( self::SETTINGS_OPTION, [] ); + $options = WC_Stripe_Helper::get_stripe_settings(); $mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'] ? 'test' : 'live'; $prefix = 'test' === $mode ? 'test_' : ''; $refresh_token = $options[ $prefix . 'refresh_token' ]; diff --git a/includes/notes/class-wc-stripe-upe-availability-note.php b/includes/notes/class-wc-stripe-upe-availability-note.php index f9d6902a5..d9b3c93a6 100644 --- a/includes/notes/class-wc-stripe-upe-availability-note.php +++ b/includes/notes/class-wc-stripe-upe-availability-note.php @@ -94,7 +94,7 @@ public static function init() { return; } - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_enabled = isset( $stripe_settings['enabled'] ) && 'yes' === $stripe_settings['enabled']; if ( ! $stripe_enabled ) { return; diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 1442333e3..f8abb1827 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index f58b9eb01..a035efd2d 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index 2e034d306..5a9e216d6 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 433db3454..13c1a01ea 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index d89cf39d7..8e499e0f2 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 090c3f106..099ca060c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 153bb0505..7c830da89 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index ed5960cf3..f55555bff 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -88,7 +88,7 @@ public function __construct() { // Check if pre-orders are enabled and add support for them. $this->maybe_init_pre_orders(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 24c92a444..6ce41adec 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -79,7 +79,7 @@ public function __construct() { // Load the settings. $this->init_settings(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 3fcfe7c8a..373502b51 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -69,7 +69,7 @@ class WC_Stripe_Payment_Request { */ public function __construct() { self::$_this = $this; - $this->stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $this->stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->testmode = ( ! empty( $this->stripe_settings['testmode'] ) && 'yes' === $this->stripe_settings['testmode'] ) ? true : false; $this->publishable_key = ! empty( $this->stripe_settings['publishable_key'] ) ? $this->stripe_settings['publishable_key'] : ''; $this->secret_key = ! empty( $this->stripe_settings['secret_key'] ) ? $this->stripe_settings['secret_key'] : ''; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 10b7ecf84..46e0d4f0e 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -178,7 +178,7 @@ public function __construct() { // Check if pre-orders are enabled and add support for them. $this->maybe_init_pre_orders(); - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $this->title = $this->payment_methods['card']->get_title(); $this->description = $this->payment_methods['card']->get_description(); $this->enabled = $this->get_option( 'enabled' ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method.php b/includes/payment-methods/class-wc-stripe-upe-payment-method.php index 5538ce901..91ec548f9 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method.php @@ -109,7 +109,7 @@ abstract class WC_Stripe_UPE_Payment_Method extends WC_Payment_Gateway { * Create instance of payment method */ public function __construct() { - $main_settings = get_option( 'woocommerce_stripe_settings' ); + $main_settings = WC_Stripe_Helper::get_stripe_settings(); $is_stripe_enabled = ! empty( $main_settings['enabled'] ) && 'yes' === $main_settings['enabled']; $this->enabled = $is_stripe_enabled && in_array( static::STRIPE_ID, $this->get_option( 'upe_checkout_experience_accepted_payments', [ 'card' ] ), true ) ? 'yes' : 'no'; @@ -316,7 +316,7 @@ public function is_reusable() { */ public function is_capability_active() { // Treat all capabilities as active when in test mode. - $plugin_settings = get_option( 'woocommerce_stripe_settings' ); + $plugin_settings = WC_Stripe_Helper::get_stripe_settings(); $test_mode_setting = ! empty( $plugin_settings['testmode'] ) ? $plugin_settings['testmode'] : 'no'; if ( 'yes' === $test_mode_setting ) { diff --git a/readme.txt b/readme.txt index 1cd8c1f71..ce89110a2 100644 --- a/readme.txt +++ b/readme.txt @@ -129,6 +129,7 @@ If you get stuck, you can ask for help in the Plugin Forum. == Changelog == = 8.7.0 - xxxx-xx-xx = +* Fix - Fix multiple compatibility issues and deprecation warnings when running the extension on PHP 8.1. * Fix - Re-connect promotional surface blinking after disappearing for merchants that have already connected their Stripe account. * Fix - Fix possible fatal errors when Stripe settings format is invalid during account connection. * Fix - Clear webhook state after reconfiguring webhooks to remove outdated error and success statuses. diff --git a/tests/phpunit/admin/migrations/test-class-wc-stripe-subscriptions-repairer-legacy-sepa-tokens.php b/tests/phpunit/admin/migrations/test-class-wc-stripe-subscriptions-repairer-legacy-sepa-tokens.php index f3ae641b2..498248b8e 100644 --- a/tests/phpunit/admin/migrations/test-class-wc-stripe-subscriptions-repairer-legacy-sepa-tokens.php +++ b/tests/phpunit/admin/migrations/test-class-wc-stripe-subscriptions-repairer-legacy-sepa-tokens.php @@ -76,7 +76,7 @@ public function set_up() { * We can't mock the check for WC_Subscriptions, so we'll test the rest of the conditions. */ public function test_updater_gets_scheduled_on_right_conditions() { - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); delete_option( 'woocommerce_stripe_subscriptions_legacy_sepa_tokens_updated' ); $this->updater @@ -97,7 +97,7 @@ public function test_updater_doesn_not_get_scheduled_when_legacy_is_enabled() { } public function test_updater_doesn_not_get_scheduled_when_already_done() { - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); update_option( 'woocommerce_stripe_subscriptions_legacy_sepa_tokens_updated', 'yes' ); $this->updater @@ -146,7 +146,7 @@ public function test_get_items_to_repair_return_the_right_items() { } public function test_maybe_update_subscription_legacy_payment_method_logs_on_exception() { - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); // Throw an arbitrary exception to confirm the logger is called with the Exception's message. WC_Subscriptions::set_wcs_get_subscription( @@ -158,11 +158,17 @@ function ( $id ) { $ids_to_migrate = $this->get_subs_ids_to_migrate(); $this->logger_mock - ->expects( $this->at( 1 ) ) + ->expects( $this->exactly( 2 ) ) ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( 'Mistakes were made' ) + ->withConsecutive( + [ + $this->anything(), + $this->anything(), + ], + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( 'Mistakes were made' ), + ], ); $this->updater->repair_item( $ids_to_migrate[0] ); @@ -174,18 +180,24 @@ public function test_maybe_update_subscription_legacy_payment_method_bails_when_ // We didn't set upe_checkout_experience_enabled to 'yes', which means the Legacy experience is enabled. $this->logger_mock - ->expects( $this->at( 1 ) ) + ->expects( $this->exactly( 2 ) ) ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. The Legacy experience is enabled.', $subscription_id ) ) + ->withConsecutive( + [ + $this->anything(), + $this->anything(), + ], + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. The Legacy experience is enabled.', $subscription_id ) ), + ], ); $this->updater->repair_item( $subscription_id ); } public function test_maybe_update_subscription_legacy_payment_method_bails_when_the_subscription_is_not_found() { - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); // Mock the subscription not being found. WC_Subscriptions::set_wcs_get_subscription( @@ -198,18 +210,24 @@ function ( $id ) { $subscription_id = $ids_to_migrate[0]; $this->logger_mock - ->expects( $this->at( 1 ) ) + ->expects( $this->exactly( 2 ) ) ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. Subscription not found.', $subscription_id ) ) + ->withConsecutive( + [ + $this->anything(), + $this->anything(), + ], + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. Subscription not found.', $subscription_id ) ), + ], ); $this->updater->repair_item( $subscription_id ); } public function test_maybe_update_subscription_legacy_payment_method_bails_when_the_payment_method_is_not_sepa() { - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); $ids_to_migrate = $this->get_subs_ids_to_migrate(); $subscription_id = $ids_to_migrate[0]; @@ -227,11 +245,17 @@ function ( $id ) { // The payment method associated with the subscription isn't SEPA, so no migration is needed. $this->logger_mock - ->expects( $this->at( 1 ) ) + ->expects( $this->exactly( 2 ) ) ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. Subscription is not using the legacy SEPA payment method.', $subscription_id ) ) + ->withConsecutive( + [ + $this->anything(), + $this->anything(), + ], + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( sprintf( '---- Skipping migration of subscription #%d. Subscription is not using the legacy SEPA payment method.', $subscription_id ) ), + ], ); $this->updater->repair_item( $subscription_id ); @@ -255,19 +279,17 @@ function ( $id ) { $original_source_id = $subscription->get_meta( self::SOURCE_ID_META_KEY ); $this->logger_mock - ->expects( $this->at( 0 ) ) + ->expects( $this->exactly( 2 ) ) ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( sprintf( 'Migrating subscription #%1$d.', $subscription_id ) ) - ); - - $this->logger_mock - ->expects( $this->at( 1 ) ) - ->method( 'add' ) - ->with( - $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), - $this->equalTo( sprintf( 'Successful migration of subscription #%1$d.', $subscription_id ) ) + ->withConsecutive( + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( sprintf( 'Migrating subscription #%1$d.', $subscription_id ) ), + ], + [ + $this->equalTo( 'woocommerce-gateway-stripe-subscriptions-legacy-sepa-tokens-repairs' ), + $this->equalTo( sprintf( 'Successful migration of subscription #%1$d.', $subscription_id ) ), + ], ); $this->updater->repair_item( $subscription_id ); diff --git a/tests/phpunit/admin/test-class-wc-stripe-settings-controller.php b/tests/phpunit/admin/test-class-wc-stripe-settings-controller.php index 6fc4c14c6..58db383eb 100644 --- a/tests/phpunit/admin/test-class-wc-stripe-settings-controller.php +++ b/tests/phpunit/admin/test-class-wc-stripe-settings-controller.php @@ -37,7 +37,7 @@ public function set_up() { } public function tear_down() { - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); parent::tear_down(); } @@ -46,12 +46,12 @@ public function tear_down() { * Should print a placeholder div with id 'wc-stripe-account-settings-container' */ public function test_admin_options_when_stripe_is_connected() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['test_publishable_key'] = 'pk_test_key'; $stripe_settings['test_secret_key'] = 'sk_test_key'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); ob_start(); $this->controller->admin_options( $this->gateway ); @@ -63,12 +63,12 @@ public function test_admin_options_when_stripe_is_connected() { * Should print a placeholder div with id 'wc-stripe-new-account-container' */ public function test_admin_options_when_stripe_is_not_connected() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['test_publishable_key'] = ''; $stripe_settings['test_secret_key'] = ''; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); ob_start(); $this->controller->admin_options( $this->gateway ); diff --git a/tests/phpunit/admin/test-wc-stripe-admin-notices.php b/tests/phpunit/admin/test-wc-stripe-admin-notices.php index 5fc3d3384..90150ffbe 100644 --- a/tests/phpunit/admin/test-wc-stripe-admin-notices.php +++ b/tests/phpunit/admin/test-wc-stripe-admin-notices.php @@ -23,7 +23,7 @@ public function set_up() { } public function test_no_notices_are_shown_when_user_is_not_admin() { - update_option( 'woocommerce_stripe_settings', [ 'enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes' ] ); $notices = new WC_Stripe_Admin_Notices(); ob_start(); $notices->admin_notices(); @@ -33,7 +33,7 @@ public function test_no_notices_are_shown_when_user_is_not_admin() { public function test_no_notices_are_shown_when_stripe_is_not_enabled() { wp_set_current_user( $this->factory->user->create( [ 'role' => 'administrator' ] ) ); - update_option( 'woocommerce_stripe_settings', [ 'enabled' => 'no' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'no' ] ); $notices = new WC_Stripe_Admin_Notices(); ob_start(); $notices->admin_notices(); @@ -81,8 +81,7 @@ function() { } ); wp_set_current_user( $this->factory->user->create( [ 'role' => 'administrator' ] ) ); - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'testmode' => 'no', @@ -93,7 +92,7 @@ function() { ); $stripe_settings = array_merge( - get_option( 'woocommerce_stripe_settings' ), + WC_Stripe_Helper::get_stripe_settings(), [ 'upe_checkout_experience_accepted_payments' => [ 'giropay', @@ -102,7 +101,7 @@ function() { ], ] ); - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); update_option( 'wc_stripe_show_style_notice', 'no' ); update_option( 'home', 'https://...' ); @@ -134,8 +133,7 @@ public function test_invalid_keys_notice_is_shown_when_account_data_is_not_valid WC_Stripe::get_instance()->account->method( 'get_cached_account_data' )->willReturn( null ); wp_set_current_user( $this->factory->user->create( [ 'role' => 'administrator' ] ) ); - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'testmode' => 'no', diff --git a/tests/phpunit/helpers/class-upe-test-helper.php b/tests/phpunit/helpers/class-upe-test-helper.php index 2fb246621..0d39ee74f 100644 --- a/tests/phpunit/helpers/class-upe-test-helper.php +++ b/tests/phpunit/helpers/class-upe-test-helper.php @@ -13,7 +13,7 @@ function() { return 'yes'; } ); - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); $this->reload_payment_gateways(); } @@ -32,8 +32,8 @@ function () { } public function enable_upe() { - $settings = get_option( 'woocommerce_stripe_settings', [] ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'yes'; - update_option( 'woocommerce_stripe_settings', $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); } } diff --git a/tests/phpunit/test-class-wc-stripe-account.php b/tests/phpunit/test-class-wc-stripe-account.php index 4904b1ce2..5483396ed 100644 --- a/tests/phpunit/test-class-wc-stripe-account.php +++ b/tests/phpunit/test-class-wc-stripe-account.php @@ -19,12 +19,12 @@ class WC_Stripe_Account_Test extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['test_publishable_key'] = 'pk_test_key'; $stripe_settings['test_secret_key'] = 'sk_test_key'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $this->mock_connect = $this->getMockBuilder( 'WC_Stripe_Connect' ) ->disableOriginalConstructor() @@ -42,7 +42,7 @@ public function set_up() { public function tear_down() { delete_transient( 'wcstripe_account_data_test' ); delete_transient( 'wcstripe_account_data_live' ); - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); WC_Helper_Stripe_Api::reset(); @@ -233,7 +233,7 @@ public function test_get_cached_account_data_live_mode() { * Test for get_cached_account_data() with no mode parameter. */ public function test_get_cached_account_data_no_mode() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->mock_connect->method( 'is_connected' )->with( null )->willReturn( true ); $test_account = [ @@ -250,14 +250,14 @@ public function test_get_cached_account_data_no_mode() { // Enable TEST mode. $stripe_settings['testmode'] = 'yes'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Confirm test mode data is returned. $this->assertSame( $this->account->get_cached_account_data(), $test_account ); // Enable LIVE mode. $stripe_settings['testmode'] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Confirm live mode data is returned. $this->assertSame( $this->account->get_cached_account_data(), $live_account ); diff --git a/tests/phpunit/test-class-wc-stripe-api.php b/tests/phpunit/test-class-wc-stripe-api.php index 2878a1e84..bcd027f37 100644 --- a/tests/phpunit/test-class-wc-stripe-api.php +++ b/tests/phpunit/test-class-wc-stripe-api.php @@ -26,19 +26,19 @@ class WC_Stripe_API_Test extends WP_UnitTestCase { public function set_up() { parent::set_up(); - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['secret_key'] = self::LIVE_SECRET_KEY; $stripe_settings['test_secret_key'] = self::TEST_SECRET_KEY; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); } /** * Tear down environment after tests. */ public function tear_down() { - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); WC_Stripe_API::set_secret_key( null ); parent::tear_down(); } @@ -63,9 +63,9 @@ public function test_set_secret_key_for_mode_no_parameter() { $this->assertEquals( self::TEST_SECRET_KEY, WC_Stripe_API::get_secret_key() ); // Enable live mode. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['testmode'] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); WC_Stripe_API::set_secret_key_for_mode(); @@ -87,9 +87,9 @@ public function test_set_secret_key_for_mode_with_parameter() { $this->assertEquals( self::TEST_SECRET_KEY, WC_Stripe_API::get_secret_key() ); // Set the mode to live and test the invalid parameter. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['testmode'] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); WC_Stripe_API::set_secret_key_for_mode( 'invalid' ); $this->assertEquals( self::LIVE_SECRET_KEY, WC_Stripe_API::get_secret_key() ); diff --git a/tests/phpunit/test-class-wc-stripe-notes.php b/tests/phpunit/test-class-wc-stripe-notes.php index a4b3cbce6..ed87efda6 100644 --- a/tests/phpunit/test-class-wc-stripe-notes.php +++ b/tests/phpunit/test-class-wc-stripe-notes.php @@ -30,8 +30,7 @@ public function set_up() { } update_option( '_wcstripe_feature_upe', 'yes' ); - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'no', @@ -42,7 +41,7 @@ public function set_up() { public function tear_down() { woocommerce_gateway_stripe()->connect = $this->stripe_connect_original; delete_option( '_wcstripe_feature_upe' ); - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); parent::tear_down(); } @@ -55,8 +54,7 @@ public function test_create_upe_availability_note() { } public function test_create_upe_stripelink_note() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'yes', @@ -86,8 +84,7 @@ public function test_create_upe_notes_does_not_create_note_when_upe_preview_is_d } public function test_create_upe_notes_does_not_create_availability_note_when_upe_is_enbled() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'yes', @@ -110,8 +107,7 @@ public function test_create_upe_notes_does_not_create_availability_note_when_upe } public function test_create_upe_notes_does_not_create_note_when_stripe_is_disabled() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'no', 'upe_checkout_experience_enabled' => 'no', @@ -126,8 +122,7 @@ public function test_create_upe_notes_does_not_create_note_when_stripe_is_disabl } public function test_create_upe_notes_does_not_create_note_when_upe_has_been_manually_disabled() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'disabled', @@ -142,8 +137,7 @@ public function test_create_upe_notes_does_not_create_note_when_upe_has_been_man } public function test_create_stripelink_note_unavailable_if_cc_not_enabled() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'yes', @@ -158,8 +152,7 @@ public function test_create_stripelink_note_unavailable_if_cc_not_enabled() { } public function test_create_stripelink_note_unavailable_link_enabled() { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'enabled' => 'yes', 'upe_checkout_experience_enabled' => 'yes', @@ -169,10 +162,9 @@ public function test_create_stripelink_note_unavailable_link_enabled() { $this->set_enabled_payment_methods( [ 'card', 'link' ] ); - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( array_merge( - get_option( 'woocommerce_stripe_settings' ), + WC_Stripe_Helper::get_stripe_settings(), [ 'upe_checkout_experience_accepted_payments' => [ 'card', 'link' ], ] @@ -185,10 +177,9 @@ public function test_create_stripelink_note_unavailable_link_enabled() { } private function set_enabled_payment_methods( $payment_methods ) { - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( array_merge( - get_option( 'woocommerce_stripe_settings' ), + WC_Stripe_Helper::get_stripe_settings(), [ 'upe_checkout_experience_accepted_payments' => $payment_methods, ] diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-method.php b/tests/phpunit/test-class-wc-stripe-upe-payment-method.php index d74a5561f..0b8fd5d34 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-method.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-method.php @@ -122,12 +122,12 @@ class WC_Stripe_UPE_Payment_Method_Test extends WP_UnitTestCase { */ public function set_up() { parent::set_up(); - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); $this->reset_payment_method_mocks(); } public function tear_down() { - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); parent::tear_down(); } @@ -376,9 +376,9 @@ public function test_card_payment_method_capability_is_always_enabled() { $this->set_mock_payment_method_return_value( 'get_capabilities_response', self::MOCK_INACTIVE_CAPABILITIES_RESPONSE ); // Disable testmode. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['testmode'] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $card_method = $this->mock_payment_methods['card']; $klarna_method = $this->mock_payment_methods['klarna']; @@ -416,10 +416,10 @@ public function test_card_payment_method_capability_is_always_enabled() { */ public function test_payment_methods_are_only_enabled_when_capability_is_active() { // Disable testmode. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['testmode'] = 'no'; $stripe_settings['capture'] = 'yes'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); WC_Stripe::get_instance()->get_main_stripe_gateway()->init_settings(); $payment_method_ids = array_map( [ $this, 'get_id' ], $this->mock_payment_methods ); @@ -460,9 +460,9 @@ public function test_payment_methods_are_only_enabled_when_capability_is_active( * Payment method is only enabled when its supported currency is present or method supports all currencies. */ public function test_payment_methods_are_only_enabled_when_currency_is_supported() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['capture'] = 'yes'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); WC_Stripe::get_instance()->get_main_stripe_gateway()->init_settings(); $this->set_mock_payment_method_return_value( 'get_current_order_amount', 150, true ); @@ -503,7 +503,7 @@ public function test_payment_methods_are_only_enabled_when_currency_is_supported * When has_domestic_transactions_restrictions is true, the payment method is disabled when the store currency and account currency don't match. */ public function test_payment_methods_with_domestic_restrictions_are_disabled_on_currency_mismatch() { - update_option( 'woocommerce_stripe_settings', [ 'test_mode' => 'true' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'testmode' => 'yes' ] ); // $this->set_mock_payment_method_return_value( 'is_inside_currency_limits', true ); $this->set_mock_payment_method_return_value( 'get_woocommerce_currency', 'MXN', true ); @@ -524,7 +524,7 @@ public function test_payment_methods_with_domestic_restrictions_are_disabled_on_ * When has_domestic_transactions_restrictions is true, the payment method is enabled when the store currency and account currency match. */ public function test_payment_methods_with_domestic_restrictions_are_enabled_on_currency_match() { - update_option( 'woocommerce_stripe_settings', [ 'test_mode' => 'true' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'testmode' => 'yes' ] ); $this->set_mock_payment_method_return_value( 'get_woocommerce_currency', 'USD', true ); @@ -674,10 +674,10 @@ public function test_create_payment_token_for_user() { */ public function test_upe_method_enabled() { // Enable Stripe and reset the accepted payment methods. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['upe_checkout_experience_accepted_payments'] = []; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // For each method we'll test the following combinations: $stripe_enabled_settings = [ 'yes', 'no', '' ]; @@ -697,7 +697,7 @@ public function test_upe_method_enabled() { unset( $stripe_settings['upe_checkout_experience_accepted_payments'][ $payment_method_index ] ); } - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Verify that the payment method is enabled/disabled. $payment_method_instance = new $payment_method(); diff --git a/tests/phpunit/test-wc-rest-stripe-account-keys-controller.php b/tests/phpunit/test-wc-rest-stripe-account-keys-controller.php index 10aae59c5..3eccf5f55 100644 --- a/tests/phpunit/test-wc-rest-stripe-account-keys-controller.php +++ b/tests/phpunit/test-wc-rest-stripe-account-keys-controller.php @@ -31,10 +31,10 @@ public function set_up() { wp_set_current_user( 1 ); // Setup existing keys - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $settings['publishable_key'] = 'original-live-key-9999'; $settings['test_publishable_key'] = 'original-test-key-9999'; - update_option( 'woocommerce_stripe_settings', $settings ); + WC_Stripe_Helper::update_main_stripe_settings( $settings ); $mock_account = $this->getMockBuilder( WC_Stripe_Account::class ) ->disableOriginalConstructor() @@ -73,7 +73,7 @@ public function test_adding_keys_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'pk_live-key-12345', $settings['publishable_key'] ); $this->assertEquals( 'sk_live_secret-key-12345', $settings['secret_key'] ); @@ -96,7 +96,7 @@ public function test_adding_test_keys_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'pk_test-live-key-12345', $settings['test_publishable_key'] ); $this->assertEquals( 'sk_test-secret-key-12345', $settings['test_secret_key'] ); @@ -117,7 +117,7 @@ public function test_update_live_key_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'pk_live-key-12345', $settings['publishable_key'] ); // Other settings do not change and do not get erased. @@ -135,7 +135,7 @@ public function test_setting_blank_live_key_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( '', $settings['publishable_key'] ); // Other settings do not change and do not get erased. @@ -148,8 +148,7 @@ public function test_setting_blank_live_key_returns_status_code_200() { */ public function test_changing_keys_resets_payment_methods() { // Default options - update_option( - WC_REST_Stripe_Account_Keys_Controller::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, + WC_Stripe_Helper::update_main_stripe_settings( [ 'publishable_key' => 'pk_live-key', 'secret_key' => 'sk_live-key', @@ -185,9 +184,9 @@ public function test_changing_keys_resets_legacy_payment_methods() { $request->set_param( 'publishable_key', '' ); // Disable UPE - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Set initial payment methods $payment_gateways = WC_Stripe_Helper::get_legacy_payment_methods(); diff --git a/tests/phpunit/test-wc-rest-upe-flag-toggle-controller.php b/tests/phpunit/test-wc-rest-upe-flag-toggle-controller.php index 25c034e1b..7bf7c1e98 100644 --- a/tests/phpunit/test-wc-rest-upe-flag-toggle-controller.php +++ b/tests/phpunit/test-wc-rest-upe-flag-toggle-controller.php @@ -33,9 +33,9 @@ public function set_up() { wp_set_current_user( 1 ); // Disable UPE. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $this->controller = new WC_Stripe_REST_UPE_Flag_Toggle_Controller(); } @@ -61,7 +61,7 @@ public function test_set_flag_enabled_request_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( $expected, $response->get_data() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'yes', $settings['upe_checkout_experience_enabled'] ); } @@ -78,7 +78,7 @@ public function test_set_flag_disabled_request_returns_status_code_200() { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( $expected, $response->get_data() ); - $settings = get_option( 'woocommerce_stripe_settings' ); + $settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'disabled', $settings['upe_checkout_experience_enabled'] ); } diff --git a/tests/phpunit/test-wc-stripe-helper.php b/tests/phpunit/test-wc-stripe-helper.php index 3a8515c73..0761f96db 100644 --- a/tests/phpunit/test-wc-stripe-helper.php +++ b/tests/phpunit/test-wc-stripe-helper.php @@ -395,4 +395,18 @@ public function provide_is_wallet_payment_method(): array { ], ]; } + + /** + * Test for `update_main_stripe_settings`, `get_stripe_settings` and `delete_main_stripe_settings`. + * + * @return void + */ + public function test_handle_main_stripe_settings() { + WC_Stripe_Helper::update_main_stripe_settings( [ 'test' => 'test' ] ); + $current_settings = WC_Stripe_Helper::get_stripe_settings(); + $this->assertSame( [ 'test' => 'test' ], $current_settings ); + WC_Stripe_Helper::delete_main_stripe_settings(); + $current_settings = WC_Stripe_Helper::get_stripe_settings(); + $this->assertSame( [], $current_settings ); + } } diff --git a/tests/phpunit/test-wc-stripe-payment-gateway.php b/tests/phpunit/test-wc-stripe-payment-gateway.php index ea8950993..0b587311f 100644 --- a/tests/phpunit/test-wc-stripe-payment-gateway.php +++ b/tests/phpunit/test-wc-stripe-payment-gateway.php @@ -41,12 +41,12 @@ private function updateOrderMeta( $order, $key, $value ) { * Should print a placeholder div with id 'wc-stripe-payment-gateway-container' */ public function test_admin_options_when_stripe_is_connected() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['test_publishable_key'] = 'pk_test_key'; $stripe_settings['test_secret_key'] = 'sk_test_key'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); ob_start(); $this->giropay_gateway->admin_options(); @@ -58,12 +58,12 @@ public function test_admin_options_when_stripe_is_connected() { * Should print a placeholder div with id 'wc-stripe-new-account-container' */ public function test_admin_options_when_stripe_is_not_connected() { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = 'yes'; $stripe_settings['test_publishable_key'] = ''; $stripe_settings['test_secret_key'] = ''; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); ob_start(); $this->giropay_gateway->admin_options(); @@ -458,14 +458,14 @@ public function test_add_payment_method_fails_when_payment_method_is_not_reusabl * @dataProvider provide_test_needs_setup */ public function test_needs_setup( $is_test_mode, $test_publishable_key, $test_secret_key, $publishable_key, $secret_key, $expected ) { - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['enabled'] = 'yes'; $stripe_settings['testmode'] = $is_test_mode ? 'yes' : 'no'; $stripe_settings['test_publishable_key'] = $test_publishable_key; $stripe_settings['test_secret_key'] = $test_secret_key; $stripe_settings['publishable_key'] = $publishable_key; $stripe_settings['secret_key'] = $secret_key; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $gateway = new WC_Gateway_Stripe(); $this->assertSame( $expected, $gateway->needs_setup() ); diff --git a/tests/phpunit/test-wc-stripe-payment-request.php b/tests/phpunit/test-wc-stripe-payment-request.php index 76494f4a1..68a5ab492 100644 --- a/tests/phpunit/test-wc-stripe-payment-request.php +++ b/tests/phpunit/test-wc-stripe-payment-request.php @@ -106,6 +106,9 @@ private static function set_shipping_method_cost( $instance_id, $cost ) { $method = WC_Shipping_Zones::get_shipping_method( $instance_id ); $option_key = $method->get_instance_option_key(); $options = get_option( $option_key ); + if ( ! is_array( $options ) ) { + $options = []; + } $options['cost'] = $cost; update_option( $option_key, $options ); } @@ -191,10 +194,9 @@ public function test_is_at_least_one_payment_request_button_enabled_link_enabled $this->upe_helper->enable_upe(); - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( array_merge( - get_option( 'woocommerce_stripe_settings', [] ), + WC_Stripe_Helper::get_stripe_settings(), [ 'upe_checkout_experience_accepted_payments' => [ 'link' ], ] @@ -215,10 +217,9 @@ public function test_is_at_least_one_payment_request_button_enabled_none_enabled $this->pr->stripe_settings = [ 'payment_request' => false ]; // Disable Link by Stripe - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( array_merge( - get_option( 'woocommerce_stripe_settings', [] ), + WC_Stripe_Helper::get_stripe_settings(), [ 'upe_checkout_experience_accepted_payments' => [ 'card' ], ] diff --git a/tests/phpunit/test-wc-stripe-sub-initial.php b/tests/phpunit/test-wc-stripe-sub-initial.php index 0f73c154f..eedc557ee 100644 --- a/tests/phpunit/test-wc-stripe-sub-initial.php +++ b/tests/phpunit/test-wc-stripe-sub-initial.php @@ -41,8 +41,7 @@ public function set_up() { // Mocked in order to get metadata[payment_type] = recurring in the HTTP request. $this->statement_descriptor = 'This is a statement descriptor.'; - update_option( - 'woocommerce_stripe_settings', + WC_Stripe_Helper::update_main_stripe_settings( [ 'statement_descriptor' => $this->statement_descriptor, ] @@ -53,7 +52,7 @@ public function set_up() { * Tears down the stuff we set up. */ public function tear_down() { - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); parent::tear_down(); } diff --git a/tests/phpunit/test-wc-stripe-sub-renewal.php b/tests/phpunit/test-wc-stripe-sub-renewal.php index f2e2bea08..b79a08ce8 100644 --- a/tests/phpunit/test-wc-stripe-sub-renewal.php +++ b/tests/phpunit/test-wc-stripe-sub-renewal.php @@ -49,19 +49,19 @@ public function set_up() { $this->statement_descriptor = 'This is a statement descriptor.'; - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); // Disable UPE. $stripe_settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'no'; // Set statement descriptor. $stripe_settings['statement_descriptor'] = $this->statement_descriptor; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); } /** * Tears down the stuff we set up. */ public function tear_down() { - delete_option( 'woocommerce_stripe_settings' ); + WC_Stripe_Helper::delete_main_stripe_settings(); parent::tear_down(); } diff --git a/tests/phpunit/test-wc-stripe-webhook-state.php b/tests/phpunit/test-wc-stripe-webhook-state.php index 34787970e..ea56ee3b5 100644 --- a/tests/phpunit/test-wc-stripe-webhook-state.php +++ b/tests/phpunit/test-wc-stripe-webhook-state.php @@ -46,11 +46,11 @@ public function set_up() { $this->webhook_secret = 'whsec_123'; // Resets settings. - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['webhook_secret'] = $this->webhook_secret; $stripe_settings['test_webhook_secret'] = $this->webhook_secret; unset( $stripe_settings['testmode'] ); - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $this->wc_stripe_webhook_handler = new WC_Stripe_Webhook_Handler(); } @@ -74,10 +74,10 @@ public function tear_down() { } private function cleanup_webhook_secret() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); unset( $stripe_settings['webhook_secret'] ); unset( $stripe_settings['test_webhook_secret'] ); - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); $this->wc_stripe_webhook_handler = new WC_Stripe_Webhook_Handler(); } @@ -104,9 +104,9 @@ private function set_valid_request_data( $overwrite_timestamp = null ) { } private function set_testmode( $testmode = 'yes' ) { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $stripe_settings['testmode'] = $testmode; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); } /** diff --git a/tests/phpunit/test-wc-stripe.php b/tests/phpunit/test-wc-stripe.php index 66bd176d7..e2ace2c6a 100644 --- a/tests/phpunit/test-wc-stripe.php +++ b/tests/phpunit/test-wc-stripe.php @@ -112,7 +112,7 @@ public function test_legacy_payment_methods_supported_by_upe_are_not_loaded_when $this->upe_helper->enable_upe_feature_flag(); $this->assertTrue( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ); - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); $this->upe_helper->reload_payment_gateways(); $this->assertTrue( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ); @@ -137,16 +137,16 @@ function( $gateway ) { public function test_turning_on_upe_with_no_stripe_legacy_payment_methods_enabled_will_not_turn_on_the_upe_gateway_and_default_to_card_and_link() { $this->upe_helper->enable_upe_feature_flag(); // Store default stripe options - update_option( 'woocommerce_stripe_settings', [] ); + WC_Stripe_Helper::update_main_stripe_settings( [] ); - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'no', $stripe_settings['enabled'] ); $this->assertEquals( 'no', $stripe_settings['upe_checkout_experience_enabled'] ); $stripe_settings['upe_checkout_experience_enabled'] = 'yes'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); // Because no Stripe LPM's were enabled when UPE was enabled, the Stripe gateway is not enabled yet. $this->assertEquals( 'no', $stripe_settings['enabled'] ); $this->assertEquals( 'yes', $stripe_settings['upe_checkout_experience_enabled'] ); @@ -164,9 +164,9 @@ public function test_turning_on_upe_enables_the_correct_upe_methods_based_on_whi $this->upe_helper->reload_payment_gateways(); // Initialize default stripe settings, turn on UPE. - update_option( 'woocommerce_stripe_settings', [ 'upe_checkout_experience_enabled' => 'yes' ] ); + WC_Stripe_Helper::update_main_stripe_settings( [ 'upe_checkout_experience_enabled' => 'yes' ] ); - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'yes', $stripe_settings['enabled'] ); $this->assertEquals( 'yes', $stripe_settings['upe_checkout_experience_enabled'] ); $this->assertNotContains( 'card', $stripe_settings['upe_checkout_experience_accepted_payments'] ); @@ -181,14 +181,14 @@ public function test_turning_on_upe_enables_the_correct_upe_methods_based_on_whi // Enable the EPS UPE method. Now when UPE is disabled, the EPS LPM should be enabled. $stripe_settings['upe_checkout_experience_accepted_payments'][] = 'eps'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Turn UPE off. $stripe_settings['upe_checkout_experience_enabled'] = 'no'; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); // Check that the main 'stripe' gateway was disabled because the 'card' UPE method was not enabled. - $stripe_settings = get_option( 'woocommerce_stripe_settings' ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $this->assertEquals( 'no', $stripe_settings['enabled'] ); // Check that the correct LPMs were re-enabled. $alipay_settings = get_option( 'woocommerce_stripe_alipay_settings' ); diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index e519c02e7..2ce2cee14 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -86,6 +86,8 @@ class WC_Stripe { /** * The option name for the Stripe gateway settings. + * + * @deprecated 8.7.0 */ const STRIPE_GATEWAY_SETTINGS_OPTION_NAME = 'woocommerce_stripe_settings'; @@ -357,7 +359,7 @@ public function install() { * @version 5.5.0 */ public function update_prb_location_settings() { - $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); + $stripe_settings = WC_Stripe_Helper::get_stripe_settings(); $prb_locations = isset( $stripe_settings['payment_request_button_locations'] ) ? $stripe_settings['payment_request_button_locations'] : []; @@ -383,7 +385,7 @@ public function update_prb_location_settings() { } $stripe_settings['payment_request_button_locations'] = $new_prb_locations; - update_option( 'woocommerce_stripe_settings', $stripe_settings ); + WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings ); } }