Merge pull request #1668 from woocommerce/PCP-1974-by-disconnecting-or-disabling-the-plugin-the-connection-should-clear-the-onboarding-links-from-cache

By disconnecting or disabling the plugin the connection should clear the Onboarding links from cache (1974)
This commit is contained in:
Emili Castells 2023-09-07 12:40:20 +02:00 committed by GitHub
commit abd26dc016
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 8 deletions

View file

@ -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';
},

View file

@ -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 );
}
}
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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();
}
}
}

View file

@ -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',

View file

@ -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();