Skip to content

Commit f424735

Browse files
committed
Fix ability for BackgroundTaskDeactivator to unschedule hooks
1 parent 6b4a537 commit f424735

File tree

4 files changed

+6
-15
lines changed

4 files changed

+6
-15
lines changed

.phpstorm.meta.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'validated_url_stylesheet_gc' => \AmpProject\AmpWP\BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
3636
'url_validation_cron' => \AmpProject\AmpWP\Validation\URLValidationCron::class,
3737
'save_post_validation_event' => \AmpProject\AmpWP\Validation\SavePostValidationEvent::class,
38+
'background_task_deactivator' => \AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator::class,
3839
] )
3940
);
4041

src/AmpWpPlugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use AmpProject\AmpWP\Instrumentation;
1414
use AmpProject\AmpWP\Validation\SavePostValidationEvent;
1515
use AmpProject\AmpWP\Validation\URLValidationCron;
16+
use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;
1617

1718
use function is_user_logged_in;
1819

@@ -84,6 +85,7 @@ final class AmpWpPlugin extends ServiceBasedPlugin {
8485
'validated_url_stylesheet_gc' => BackgroundTask\ValidatedUrlStylesheetDataGarbageCollection::class,
8586
'url_validation_cron' => URLValidationCron::class,
8687
'save_post_validation_event' => SavePostValidationEvent::class,
88+
'background_task_deactivator' => BackgroundTaskDeactivator::class,
8789
];
8890

8991
/**

src/BackgroundTask/BackgroundTaskDeactivator.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace AmpProject\AmpWP\BackgroundTask;
99

1010
use AmpProject\AmpWP\Icon;
11-
use AmpProject\AmpWP\Infrastructure\Conditional;
1211
use AmpProject\AmpWP\Infrastructure\Deactivateable;
1312
use AmpProject\AmpWP\Infrastructure\Registerable;
1413
use AmpProject\AmpWP\Infrastructure\Service;
@@ -20,7 +19,7 @@
2019
* @since 2.1
2120
* @internal
2221
*/
23-
final class BackgroundTaskDeactivator implements Service, Conditional, Registerable, Deactivateable {
22+
final class BackgroundTaskDeactivator implements Service, Registerable, Deactivateable {
2423

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

41-
/**
42-
* Check whether the conditional object is currently needed.
43-
*
44-
* @return bool Whether the conditional object is needed.
45-
*/
46-
public static function is_needed() {
47-
return is_admin() || wp_doing_cron();
48-
}
49-
5040
/**
5141
* Class constructor.
5242
*/
@@ -103,14 +93,14 @@ public function deactivate( $network_wide ) {
10393
switch_to_blog( $blog_id );
10494

10595
foreach ( $this->events_to_deactivate as $event_name ) {
106-
wp_clear_scheduled_hook( $event_name );
96+
wp_unschedule_hook( $event_name );
10797
}
10898

10999
restore_current_blog();
110100
}
111101
} else {
112102
foreach ( $this->events_to_deactivate as $event_name ) {
113-
wp_clear_scheduled_hook( $event_name );
103+
wp_unschedule_hook( $event_name );
114104
}
115105
}
116106
}

tests/php/src/BackgroundTask/BackgroundTaskDeactivatorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace AmpProject\AmpWP\Tests\BackgroundTask;
77

88
use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;
9-
use AmpProject\AmpWP\Infrastructure\Conditional;
109
use AmpProject\AmpWP\Infrastructure\Deactivateable;
1110
use AmpProject\AmpWP\Infrastructure\Registerable;
1211
use AmpProject\AmpWP\Infrastructure\Service;
@@ -37,7 +36,6 @@ public function setUp() {
3736
public function test_register() {
3837
$this->assertInstanceof( BackgroundTaskDeactivator::class, $this->test_instance );
3938
$this->assertInstanceof( Service::class, $this->test_instance );
40-
$this->assertInstanceof( Conditional::class, $this->test_instance );
4139
$this->assertInstanceof( Registerable::class, $this->test_instance );
4240
$this->assertInstanceof( Deactivateable::class, $this->test_instance );
4341

0 commit comments

Comments
 (0)