diff --git a/modules/ppcp-wc-gateway/connection-tab-settings.php b/modules/ppcp-wc-gateway/connection-tab-settings.php index 600bc70f4..c65814dad 100644 --- a/modules/ppcp-wc-gateway/connection-tab-settings.php +++ b/modules/ppcp-wc-gateway/connection-tab-settings.php @@ -20,6 +20,12 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; return function ( ContainerInterface $container, array $fields ): array { + $current_page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' ); + + if ( $current_page_id !== Settings::CONNECTION_TAB_ID ) { + return $fields; + } + $state = $container->get( 'onboarding.state' ); assert( $state instanceof State ); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 7c5564826..6ecfb8a20 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -274,6 +274,7 @@ return array( $page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' ); $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' ); return new SettingsListener( $settings, $fields, @@ -283,7 +284,8 @@ return array( $bearer, $page_id, $signup_link_cache, - $signup_link_ids + $signup_link_ids, + $pui_status_cache ); }, 'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor { @@ -349,8 +351,28 @@ return array( return new FeesRenderer(); }, + 'wcgateway.settings.should-render-settings' => static function ( ContainerInterface $container ): bool { + + $sections = array( + Settings::CONNECTION_TAB_ID => __( 'Connection', 'woocommerce-paypal-payments' ), + PayPalGateway::ID => __( 'PayPal Checkout', 'woocommerce-paypal-payments' ), + CreditCardGateway::ID => __( 'PayPal Card Processing', 'woocommerce-paypal-payments' ), + CardButtonGateway::ID => __( 'PayPal Card Button', 'woocommerce-paypal-payments' ), + ); + + $current_page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' ); + + return array_key_exists( $current_page_id, $sections ); + }, + 'wcgateway.settings.fields' => static function ( ContainerInterface $container ): array { + $should_render_settings = $container->get( 'wcgateway.settings.should-render-settings' ); + + if ( ! $should_render_settings ) { + return array(); + } + $state = $container->get( 'onboarding.state' ); assert( $state instanceof State ); @@ -1956,7 +1978,8 @@ return array( 'wcgateway.pay-upon-invoice-product-status' => static function( ContainerInterface $container ): PayUponInvoiceProductStatus { return new PayUponInvoiceProductStatus( $container->get( 'wcgateway.settings' ), - $container->get( 'api.endpoint.partners' ) + $container->get( 'api.endpoint.partners' ), + $container->get( 'pui.status-cache' ) ); }, 'wcgateway.pay-upon-invoice' => static function ( ContainerInterface $container ): PayUponInvoice { @@ -2209,4 +2232,7 @@ return array( esc_html( $pui_button_text ) ); }, + 'pui.status-cache' => static function( ContainerInterface $container ): Cache { + return new Cache( 'ppcp-paypal-pui-status-cache' ); + }, ); diff --git a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php index 6512e4ded..b8953a4dc 100644 --- a/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/PayUponInvoiceProductStatus.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 PayUponInvoiceProductStatus { + const PUI_STATUS_CACHE_KEY = 'pui_status_cache'; + + /** + * The Cache. + * + * @var Cache + */ + protected $cache; + /** * Caches the status for the current load. * @@ -44,13 +54,16 @@ class PayUponInvoiceProductStatus { * * @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 PayUponInvoiceProductStatus { * @return bool */ public function pui_is_active() : bool { + if ( $this->cache->has( self::PUI_STATUS_CACHE_KEY ) ) { + return (bool) $this->cache->get( self::PUI_STATUS_CACHE_KEY ); + } + if ( is_bool( $this->current_status_cache ) ) { return $this->current_status_cache; } @@ -95,9 +112,11 @@ class PayUponInvoiceProductStatus { $this->settings->set( 'products_pui_enabled', true ); $this->settings->persist(); $this->current_status_cache = true; + $this->cache->set( self::PUI_STATUS_CACHE_KEY, true, 3 * MONTH_IN_SECONDS ); return true; } } + $this->cache->set( self::PUI_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 d9c184e7f..6859c612c 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\PayUponInvoiceProductStatus; use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; @@ -95,6 +96,13 @@ class SettingsListener { */ protected $signup_link_ids; + /** + * The PUI status cache. + * + * @var Cache + */ + protected $pui_status_cache; + /** * SettingsListener constructor. * @@ -107,6 +115,7 @@ class SettingsListener { * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. * @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. */ public function __construct( Settings $settings, @@ -117,7 +126,8 @@ class SettingsListener { Bearer $bearer, string $page_id, Cache $signup_link_cache, - array $signup_link_ids + array $signup_link_ids, + Cache $pui_status_cache ) { $this->settings = $settings; @@ -129,6 +139,7 @@ class SettingsListener { $this->page_id = $page_id; $this->signup_link_cache = $signup_link_cache; $this->signup_link_ids = $signup_link_ids; + $this->pui_status_cache = $pui_status_cache; } /** @@ -307,6 +318,10 @@ class SettingsListener { $this->cache->delete( PayPalBearer::CACHE_KEY ); } + if ( $this->pui_status_cache->has( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ) ) { + $this->pui_status_cache->delete( PayUponInvoiceProductStatus::PUI_STATUS_CACHE_KEY ); + } + if ( isset( $_GET['ppcp-onboarding-error'] ) ) { $url = remove_query_arg( 'ppcp-onboarding-error' ); wp_safe_redirect( $url, 302 );