diff --git a/changelog.txt b/changelog.txt index 4bfe86f61e..2ffb331039 100644 --- a/changelog.txt +++ b/changelog.txt @@ -5,6 +5,7 @@ * Update - Removes unnecessary legacy checkout gateway instantiations and UPE disablement code * Dev - Renames previous Order Helper class methods to use the `_id` suffix * Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas +* Add - Fetch account data before account cache expires = 10.0.1 - 2025-10-15 = * Fix - Remove persistent reconnection notices diff --git a/includes/class-wc-stripe-account.php b/includes/class-wc-stripe-account.php index 75d86e23b9..9673854071 100644 --- a/includes/class-wc-stripe-account.php +++ b/includes/class-wc-stripe-account.php @@ -86,18 +86,21 @@ public function __construct( WC_Stripe_Connect $connect, $stripe_api ) { /** * Gets and caches the data for the account connected to this site. * - * @param string|null $mode Optional. The mode to get the account data for. 'live' or 'test'. Default will use the current mode. + * @param string|null $mode Optional. The mode to get the account data for. 'live' or 'test'. Default will use the current mode. + * @param bool $bypass_cache Optional. Whether to bypass the cache and retrieve the account data from Stripe. Default is false. * @return array Account data or empty if failed to retrieve account data. */ - public function get_cached_account_data( $mode = null ) { + public function get_cached_account_data( $mode = null, bool $bypass_cache = false ) { if ( ! $this->connect->is_connected( $mode ) ) { return []; } - $account = $this->read_account_from_cache(); + if ( ! $bypass_cache ) { + $account = $this->read_account_from_cache(); - if ( ! empty( $account ) ) { - return $account; + if ( ! empty( $account ) ) { + return $account; + } } return $this->cache_account( $mode ); diff --git a/includes/class-wc-stripe-database-cache-prefetch.php b/includes/class-wc-stripe-database-cache-prefetch.php index 46c62a81d2..b7ab006a96 100644 --- a/includes/class-wc-stripe-database-cache-prefetch.php +++ b/includes/class-wc-stripe-database-cache-prefetch.php @@ -21,6 +21,7 @@ class WC_Stripe_Database_Cache_Prefetch { * @var int[] */ protected const PREFETCH_CONFIG = [ + WC_Stripe_Account::ACCOUNT_CACHE_KEY => 10, WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY => 10, ]; @@ -194,6 +195,10 @@ protected function prefetch_cache_key( string $key ): ?bool { $prefetched = null; switch ( $key ) { + case WC_Stripe_Account::ACCOUNT_CACHE_KEY: + $account_data = WC_Stripe::get_instance()->account->get_cached_account_data( null, true ); + $prefetched = ! empty( $account_data ); + break; case WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY: if ( WC_Stripe_Payment_Method_Configurations::is_enabled() ) { WC_Stripe_Payment_Method_Configurations::get_upe_enabled_payment_method_ids( true ); diff --git a/readme.txt b/readme.txt index d472ec1077..8f63c78b4d 100644 --- a/readme.txt +++ b/readme.txt @@ -115,5 +115,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o * Update - Removes unnecessary legacy checkout gateway instantiations and UPE disablement code * Dev - Renames previous Order Helper class methods to use the `_id` suffix * Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas +* Add - Fetch account data before account cache expires [See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt). diff --git a/tests/phpunit/WC_Stripe_Database_Cache_Prefetch_Test.php b/tests/phpunit/WC_Stripe_Database_Cache_Prefetch_Test.php index 9f0770bb6b..1d981c7efb 100644 --- a/tests/phpunit/WC_Stripe_Database_Cache_Prefetch_Test.php +++ b/tests/phpunit/WC_Stripe_Database_Cache_Prefetch_Test.php @@ -20,6 +20,7 @@ public function provide_handle_prefetch_action_test_cases(): array { return [ 'pmc_key_exists_and_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, true ], 'invalid_key_should_not_prefetch' => [ 'invalid_test_key', false ], + 'account_key_should_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, true ], ]; } @@ -54,13 +55,19 @@ public function test_handle_prefetch_action( string $key, bool $should_prefetch */ public function provide_maybe_queue_prefetch_test_cases(): array { return [ - 'invalid_key_should_not_prefetch' => [ 'invalid_test_key', 5, false ], - 'pmc_key_expires_in_60_seconds_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 60, false ], - 'pmc_key_expires_in_5_seconds_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true ], - 'pmc_key_expires_in_5_seconds_with_option_set_2s_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, false, 2 ], - 'pmc_key_expires_in_5_seconds_with_option_set_-2s_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, false, -2 ], - 'pmc_key_expires_in_5_seconds_with_option_set_-11s_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true, -11 ], - 'pmc_key_expires_in_5_seconds_with_invalid_option_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true, 'invalid' ], + 'invalid_key_should_not_prefetch' => [ 'invalid_test_key', 5, false ], + 'pmc_key_expires_in_60_seconds_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 60, false ], + 'pmc_key_expires_in_5_seconds_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true ], + 'pmc_key_expires_in_5_seconds_with_option_set_2s_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, false, 2 ], + 'pmc_key_expires_in_5_seconds_with_option_set_-2s_should_not_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, false, -2 ], + 'pmc_key_expires_in_5_seconds_with_option_set_-11s_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true, -11 ], + 'pmc_key_expires_in_5_seconds_with_invalid_option_should_prefetch' => [ \WC_Stripe_Payment_Method_Configurations::CONFIGURATION_CACHE_KEY, 5, true, 'invalid' ], + 'account_key_expires_in_60_seconds_should_not_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 60, false ], + 'account_key_expires_in_5_seconds_should_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 5, true ], + 'account_key_expires_in_5_seconds_with_option_set_2s_should_not_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 5, false, 2 ], + 'account_key_expires_in_5_seconds_with_option_set_-2s_should_not_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 5, false, -2 ], + 'account_key_expires_in_5_seconds_with_option_set_-11s_should_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 5, true, -11 ], + 'account_key_expires_in_5_seconds_with_invalid_option_should_prefetch' => [ \WC_Stripe_Account::ACCOUNT_CACHE_KEY, 5, true, 'invalid' ], ]; }