Skip to content

Commit

Permalink
Fix ability for BackgroundTaskDeactivator to unschedule hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 30, 2020
1 parent 6b4a537 commit f424735
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 15 deletions.
1 change: 1 addition & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'validated_url_stylesheet_gc' => \AmpProject\AmpWP\BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'url_validation_cron' => \AmpProject\AmpWP\Validation\URLValidationCron::class,
'save_post_validation_event' => \AmpProject\AmpWP\Validation\SavePostValidationEvent::class,
'background_task_deactivator' => \AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator::class,
] )
);

Expand Down
2 changes: 2 additions & 0 deletions src/AmpWpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use AmpProject\AmpWP\Instrumentation;
use AmpProject\AmpWP\Validation\SavePostValidationEvent;
use AmpProject\AmpWP\Validation\URLValidationCron;
use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;

use function is_user_logged_in;

Expand Down Expand Up @@ -84,6 +85,7 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
'validated_url_stylesheet_gc' => BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
'url_validation_cron' => URLValidationCron::class,
'save_post_validation_event' => SavePostValidationEvent::class,
'background_task_deactivator' => BackgroundTaskDeactivator::class,
];

/**
Expand Down
16 changes: 3 additions & 13 deletions src/BackgroundTask/BackgroundTaskDeactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace AmpProject\AmpWP\BackgroundTask;

use AmpProject\AmpWP\Icon;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Deactivateable;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
Expand All @@ -20,7 +19,7 @@
* @since 2.1
* @internal
*/
final class BackgroundTaskDeactivator implements Service, Conditional, Registerable, Deactivateable {
final class BackgroundTaskDeactivator implements Service, Registerable, Deactivateable {

/**
* List of event names to deactivate.
Expand All @@ -38,15 +37,6 @@ final class BackgroundTaskDeactivator implements Service, Conditional, Registera
*/
private $plugin_file;

/**
* Check whether the conditional object is currently needed.
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed() {
return is_admin() || wp_doing_cron();
}

/**
* Class constructor.
*/
Expand Down Expand Up @@ -103,14 +93,14 @@ public function deactivate( $network_wide ) {
switch_to_blog( $blog_id );

foreach ( $this->events_to_deactivate as $event_name ) {
wp_clear_scheduled_hook( $event_name );
wp_unschedule_hook( $event_name );
}

restore_current_blog();
}
} else {
foreach ( $this->events_to_deactivate as $event_name ) {
wp_clear_scheduled_hook( $event_name );
wp_unschedule_hook( $event_name );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace AmpProject\AmpWP\Tests\BackgroundTask;

use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Deactivateable;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
Expand Down Expand Up @@ -37,7 +36,6 @@ public function setUp() {
public function test_register() {
$this->assertInstanceof( BackgroundTaskDeactivator::class, $this->test_instance );
$this->assertInstanceof( Service::class, $this->test_instance );
$this->assertInstanceof( Conditional::class, $this->test_instance );
$this->assertInstanceof( Registerable::class, $this->test_instance );
$this->assertInstanceof( Deactivateable::class, $this->test_instance );

Expand Down

0 comments on commit f424735

Please sign in to comment.