Skip to content

Commit

Permalink
Merge pull request #9302 from google/enhancement/#6992-remove-schedul…
Browse files Browse the repository at this point in the history
…ed-events

Clear all scheduled events on deactivation/reset.
  • Loading branch information
techanvil authored Sep 9, 2024
2 parents 039fec3 + 0e9075a commit c0c07f4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
48 changes: 47 additions & 1 deletion includes/Core/Util/Uninstallation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
use Google\Site_Kit\Core\Storage\Encrypted_Options;
use Google\Site_Kit\Core\Authentication\Credentials;
use Google\Site_Kit\Core\Authentication\Google_Proxy;
use Exception;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Remote_Features\Remote_Features_Cron;
use Google\Site_Kit\Modules\Analytics_4\Conversion_Reporting\Conversion_Reporting_Cron;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_AdSenseLinked;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_AdsLinked;
use Google\Site_Kit\Modules\Analytics_4\Synchronize_Property;

/**
* Utility class for handling uninstallation of the plugin.
Expand All @@ -42,6 +47,21 @@ class Uninstallation {
*/
private $options;

/**
* List of scheduled events.
*
* @since n.e.x.t
* @var array
*/
const SCHEDULED_EVENTS = array(
Conversion_Reporting_Cron::CRON_ACTION,
OAuth_Client::CRON_REFRESH_PROFILE_DATA,
Remote_Features_Cron::CRON_ACTION,
Synchronize_AdSenseLinked::CRON_SYNCHRONIZE_ADSENSE_LINKED,
Synchronize_AdsLinked::CRON_SYNCHRONIZE_ADS_LINKED,
Synchronize_Property::CRON_SYNCHRONIZE_PROPERTY,
);

/**
* Constructor.
*
Expand Down Expand Up @@ -72,6 +92,21 @@ public function register() {
'googlesitekit_uninstallation',
function () {
$this->uninstall();
$this->clear_scheduled_events();
}
);

add_action(
'googlesitekit_deactivation',
function () {
$this->clear_scheduled_events();
}
);

add_action(
'googlesitekit_reset',
function () {
$this->clear_scheduled_events();
}
);
}
Expand All @@ -90,4 +125,15 @@ private function uninstall() {
$google_proxy->unregister_site( $credentials );
}
}

/**
* Clears all scheduled events.
*
* @since n.e.x.t
*/
private function clear_scheduled_events() {
foreach ( self::SCHEDULED_EVENTS as $event ) {
wp_clear_scheduled_hook( $event );
}
}
}
66 changes: 66 additions & 0 deletions tests/phpunit/integration/Core/Util/UninstallationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,76 @@ public function test_uninstall_not_using_proxy() {
$this->assertFalse( $this->issued_delete_site_request );
}

public function test_clear_scheduled_events__uninstall() {
$this->set_scheduled_events();

// Assert scheduled events were set.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertNotEmpty( wp_next_scheduled( $event ) );
}

remove_all_actions( 'googlesitekit_uninstallation' );

$this->uninstallation->register();

do_action( 'googlesitekit_uninstallation' );

// Assert scheduled events were cleared.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertEmpty( wp_next_scheduled( $event ) );
}
}

public function test_clear_scheduled_events__reset() {
$this->set_scheduled_events();

// Assert scheduled events were set.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertNotEmpty( wp_next_scheduled( $event ) );
}

remove_all_actions( 'googlesitekit_reset' );

$this->uninstallation->register();

do_action( 'googlesitekit_reset' );

// Assert scheduled events were cleared.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertEmpty( wp_next_scheduled( $event ) );
}
}

public function test_clear_scheduled_events__deactivation() {
$this->set_scheduled_events();

// Assert scheduled events were set.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertNotEmpty( wp_next_scheduled( $event ) );
}

remove_all_actions( 'googlesitekit_deactivation' );

$this->uninstallation->register();

do_action( 'googlesitekit_deactivation' );

// Assert scheduled events were cleared.
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
$this->assertEmpty( wp_next_scheduled( $event ) );
}
}

public function check_proxy_delete_site_url( $preempt, $args, $url ) {
if ( $this->google_proxy->url( Google_Proxy::OAUTH2_DELETE_SITE_URI ) === $url ) {
$this->issued_delete_site_request = true;
}
return $preempt;
}

private function set_scheduled_events() {
foreach ( Uninstallation::SCHEDULED_EVENTS as $event ) {
wp_schedule_event( time(), 'daily', $event );
}
}
}

0 comments on commit c0c07f4

Please sign in to comment.