Skip to content

Commit 61f2f23

Browse files
register scheduled event for pruner on plugin activation, clear it on deactivation
1 parent edbfaba commit 61f2f23

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/class-pruner.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ class Pruner
1313
public function __construct()
1414
{
1515
add_action('koko_analytics_prune_data', [$this, 'run'], 10, 0);
16-
add_action('admin_init', [$this, 'maybe_schedule'], 10, 0);
16+
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'add_scheduled_event']);
17+
register_deactivation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'clear_scheduled_event']);
1718
}
1819

19-
public function maybe_schedule()
20+
public function add_scheduled_event(): void
2021
{
21-
// only run on POST requests
22-
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
23-
return;
24-
}
22+
wp_schedule_event(time() + DAY_IN_SECONDS, 'daily', 'koko_analytics_prune_data');
23+
}
2524

26-
if (! wp_next_scheduled('koko_analytics_prune_data')) {
27-
wp_schedule_event(time() + DAY_IN_SECONDS, 'daily', 'koko_analytics_prune_data');
28-
}
25+
public function clear_scheduled_event(): void
26+
{
27+
wp_clear_scheduled_hook('koko_analytics_prune_data');
2928
}
3029

3130
public function run()

0 commit comments

Comments
 (0)