diff --git a/modules/ppcp-uninstall/services.php b/modules/ppcp-uninstall/services.php index 6f7cda722..ca67dc71b 100644 --- a/modules/ppcp-uninstall/services.php +++ b/modules/ppcp-uninstall/services.php @@ -45,6 +45,12 @@ return array( ); }, + 'uninstall.ppcp-all-action-names' => function( ContainerInterface $container ) : array { + return array( + 'woocommerce_paypal_payments_uninstall', + ); + }, + 'uninstall.clear-db-endpoint' => function( ContainerInterface $container ) : string { return 'ppcp-clear-db'; }, diff --git a/modules/ppcp-uninstall/src/ClearDatabase.php b/modules/ppcp-uninstall/src/ClearDatabase.php index fc09519a5..ed5d73bb6 100644 --- a/modules/ppcp-uninstall/src/ClearDatabase.php +++ b/modules/ppcp-uninstall/src/ClearDatabase.php @@ -31,4 +31,13 @@ class ClearDatabase implements ClearDatabaseInterface { as_unschedule_action( $action_name ); } } + + /** + * {@inheritDoc} + */ + public function clear_actions( array $action_names ): void { + foreach ( $action_names as $action_name ) { + do_action( $action_name ); + } + } } diff --git a/modules/ppcp-uninstall/src/ClearDatabaseInterface.php b/modules/ppcp-uninstall/src/ClearDatabaseInterface.php index 6d6aedb77..34d9b1469 100644 --- a/modules/ppcp-uninstall/src/ClearDatabaseInterface.php +++ b/modules/ppcp-uninstall/src/ClearDatabaseInterface.php @@ -29,4 +29,12 @@ interface ClearDatabaseInterface { */ public function clear_scheduled_actions( array $action_names ): void; + /** + * Clears the given actions. + * + * @param string[] $action_names The list of action names. + * @throws RuntimeException If problem clearing. + */ + public function clear_actions( array $action_names ): void; + } diff --git a/modules/ppcp-uninstall/src/UninstallModule.php b/modules/ppcp-uninstall/src/UninstallModule.php index 649be5c49..25d65ae0f 100644 --- a/modules/ppcp-uninstall/src/UninstallModule.php +++ b/modules/ppcp-uninstall/src/UninstallModule.php @@ -47,8 +47,9 @@ class UninstallModule implements ModuleInterface { $clear_db_endpoint = $container->get( 'uninstall.clear-db-endpoint' ); $option_names = $container->get( 'uninstall.ppcp-all-option-names' ); $scheduled_action_names = $container->get( 'uninstall.ppcp-all-scheduled-action-names' ); + $action_names = $container->get( 'uninstall.ppcp-all-action-names' ); - $this->handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names ); + $this->handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names, $action_names ); } /** @@ -69,17 +70,19 @@ class UninstallModule implements ModuleInterface { * @param string $nonce The nonce. * @param string[] $option_names The list of option names. * @param string[] $scheduled_action_names The list of scheduled action names. + * @param string[] $action_names The list of action names. */ protected function handleClearDbAjaxRequest( RequestData $request_data, ClearDatabaseInterface $clear_db, string $nonce, array $option_names, - array $scheduled_action_names + array $scheduled_action_names, + array $action_names ): void { add_action( "wc_ajax_{$nonce}", - static function () use ( $request_data, $clear_db, $nonce, $option_names, $scheduled_action_names ) { + static function () use ( $request_data, $clear_db, $nonce, $option_names, $scheduled_action_names, $action_names ) { try { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( 'Not admin.', 403 ); @@ -91,6 +94,7 @@ class UninstallModule implements ModuleInterface { $clear_db->delete_options( $option_names ); $clear_db->clear_scheduled_actions( $scheduled_action_names ); + $clear_db->clear_actions( $action_names ); wp_send_json_success(); return true; diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php index e20b7cee8..bd36ca5d4 100644 --- a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -401,9 +401,7 @@ class SettingsListener { $this->webhook_registrar->unregister(); foreach ( $this->signup_link_ids as $key ) { - if ( $this->signup_link_cache->has( $key ) ) { - $this->signup_link_cache->delete( $key ); - } + ( new OnboardingUrl( $this->signup_link_cache, $key, get_current_user_id() ) )->delete(); } } } @@ -613,4 +611,40 @@ class SettingsListener { } return true; } + + /** + * Prevent enabling tracking if it is not enabled for merchant account. + * + * @throws RuntimeException When API request fails. + */ + public function listen_for_tracking_enabled(): void { + if ( State::STATE_ONBOARDED !== $this->state->current_state() ) { + return; + } + + try { + $token = $this->bearer->bearer(); + if ( ! $token->is_tracking_available() ) { + $this->settings->set( 'tracking_enabled', false ); + $this->settings->persist(); + return; + } + } catch ( RuntimeException $exception ) { + $this->settings->set( 'tracking_enabled', false ); + $this->settings->persist(); + + throw $exception; + } + } + + /** + * Handles onboarding URLs deletion + */ + public function listen_for_uninstall(): void { + // Clear onboarding links from cache. + foreach ( $this->signup_link_ids as $key ) { + ( new OnboardingUrl( $this->signup_link_cache, $key, get_current_user_id() ) )->delete(); + } + } + } diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 49fefccaa..705ee307b 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -392,6 +392,16 @@ class WCGatewayModule implements ModuleInterface { 3 ); + add_action( + 'woocommerce_paypal_payments_uninstall', + static function () use ( $c ) { + $listener = $c->get( 'wcgateway.settings.listener' ); + assert( $listener instanceof SettingsListener ); + + $listener->listen_for_uninstall(); + } + ); + if ( defined( 'WP_CLI' ) && WP_CLI ) { \WP_CLI::add_command( 'pcp settings', diff --git a/tests/PHPUnit/Onboarding/Helper/OnboardingUrlTest.php b/tests/PHPUnit/Onboarding/Helper/OnboardingUrlTest.php index db50265d5..ea8b0ab0e 100644 --- a/tests/PHPUnit/Onboarding/Helper/OnboardingUrlTest.php +++ b/tests/PHPUnit/Onboarding/Helper/OnboardingUrlTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Onboarding\Helper; -use PHPUnit\Framework\TestCase; +use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use RuntimeException; use function Brain\Monkey\Functions\when; @@ -15,7 +15,7 @@ class OnboardingUrlTest extends TestCase private $user_id = 123; private $onboardingUrl; - protected function setUp(): void + public function setUp(): void { parent::setUp();