diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 6ecfb8a20..4581b2952 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -275,6 +275,7 @@ return array( $signup_link_cache = $container->get( 'onboarding.signup-link-cache' ); $signup_link_ids = $container->get( 'onboarding.signup-link-ids' ); $pui_status_cache = $container->get( 'pui.status-cache' ); + $dcc_status_cache = $container->get( 'dcc.status-cache' ); return new SettingsListener( $settings, $fields, @@ -285,7 +286,8 @@ return array( $page_id, $signup_link_cache, $signup_link_ids, - $pui_status_cache + $pui_status_cache, + $dcc_status_cache ); }, 'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor { @@ -1914,7 +1916,7 @@ return array( $settings = $container->get( 'wcgateway.settings' ); $partner_endpoint = $container->get( 'api.endpoint.partners' ); - return new DCCProductStatus( $settings, $partner_endpoint ); + return new DCCProductStatus( $settings, $partner_endpoint, $container->get( 'dcc.status-cache' ) ); }, 'button.helper.messages-disclaimers' => static function ( ContainerInterface $container ): MessagesDisclaimers { @@ -2235,4 +2237,7 @@ return array( 'pui.status-cache' => static function( ContainerInterface $container ): Cache { return new Cache( 'ppcp-paypal-pui-status-cache' ); }, + 'dcc.status-cache' => static function( ContainerInterface $container ): Cache { + return new Cache( 'ppcp-paypal-dcc-status-cache' ); + }, ); diff --git a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php index e13fd38af..bd55cfbff 100644 --- a/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/DCCProductStatus.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Helper; use Throwable; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct; +use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; /** @@ -19,6 +20,15 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; */ class DCCProductStatus { + const DCC_STATUS_CACHE_KEY = 'dcc_status_cache'; + + /** + * The Cache. + * + * @var Cache + */ + protected $cache; + /** * Caches the status for the current load. * @@ -44,13 +54,16 @@ class DCCProductStatus { * * @param Settings $settings The Settings. * @param PartnersEndpoint $partners_endpoint The Partner Endpoint. + * @param Cache $cache The cache. */ public function __construct( Settings $settings, - PartnersEndpoint $partners_endpoint + PartnersEndpoint $partners_endpoint, + Cache $cache ) { $this->settings = $settings; $this->partners_endpoint = $partners_endpoint; + $this->cache = $cache; } /** @@ -59,6 +72,10 @@ class DCCProductStatus { * @return bool */ public function dcc_is_active() : bool { + if ( $this->cache->has( self::DCC_STATUS_CACHE_KEY ) ) { + return (bool) $this->cache->get( self::DCC_STATUS_CACHE_KEY ); + } + if ( is_bool( $this->current_status_cache ) ) { return $this->current_status_cache; } @@ -92,9 +109,11 @@ class DCCProductStatus { $this->settings->set( 'products_dcc_enabled', true ); $this->settings->persist(); $this->current_status_cache = true; + $this->cache->set( self::DCC_STATUS_CACHE_KEY, true, 3 * MONTH_IN_SECONDS ); return true; } } + $this->cache->set( self::DCC_STATUS_CACHE_KEY, false, 3 * MONTH_IN_SECONDS ); $this->current_status_cache = false; return false; diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php index 6859c612c..cf43fa518 100644 --- a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; @@ -103,6 +104,13 @@ class SettingsListener { */ protected $pui_status_cache; + /** + * The DCC status cache. + * + * @var Cache + */ + protected $dcc_status_cache; + /** * SettingsListener constructor. * @@ -116,6 +124,7 @@ class SettingsListener { * @param Cache $signup_link_cache The signup link cache. * @param array $signup_link_ids Signup link ids. * @param Cache $pui_status_cache The PUI status cache. + * @param Cache $dcc_status_cache The DCC status cache. */ public function __construct( Settings $settings, @@ -127,7 +136,8 @@ class SettingsListener { string $page_id, Cache $signup_link_cache, array $signup_link_ids, - Cache $pui_status_cache + Cache $pui_status_cache, + Cache $dcc_status_cache ) { $this->settings = $settings; @@ -140,6 +150,7 @@ class SettingsListener { $this->signup_link_cache = $signup_link_cache; $this->signup_link_ids = $signup_link_ids; $this->pui_status_cache = $pui_status_cache; + $this->dcc_status_cache = $dcc_status_cache; } /** @@ -322,6 +333,10 @@ class SettingsListener { $this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ); } + if ( $this->dcc_status_cache->has( DCCProductStatus::DCC_STATUS_CACHE_KEY ) ) { + $this->dcc_status_cache->delete( DCCProductStatus::DCC_STATUS_CACHE_KEY ); + } + if ( isset( $_GET['ppcp-onboarding-error'] ) ) { $url = remove_query_arg( 'ppcp-onboarding-error' ); wp_safe_redirect( $url, 302 ); diff --git a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php index a84a86020..bad2dda33 100644 --- a/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php +++ b/tests/PHPUnit/WcGateway/Settings/SettingsListenerTest.php @@ -37,6 +37,7 @@ class SettingsListenerTest extends ModularTestCase $signup_link_cache = Mockery::mock(Cache::class); $signup_link_ids = array(); $pui_status_cache = Mockery::mock(Cache::class); + $dcc_status_cache = Mockery::mock(Cache::class); $testee = new SettingsListener( $settings, @@ -48,7 +49,8 @@ class SettingsListenerTest extends ModularTestCase PayPalGateway::ID, $signup_link_cache, $signup_link_ids, - $pui_status_cache + $pui_status_cache, + $dcc_status_cache ); $_GET['section'] = PayPalGateway::ID; @@ -79,6 +81,8 @@ class SettingsListenerTest extends ModularTestCase $signup_link_cache->shouldReceive('has') ->andReturn(false); $pui_status_cache->shouldReceive('has') + ->andReturn(false); + $dcc_status_cache->shouldReceive('has') ->andReturn(false); $testee->listen();