Skip to content

Commit 914f0c2

Browse files
get rid of maybe_setup_scheduled_event by scheduling it at plugin activation and whenever settings are saved
1 parent f623987 commit 914f0c2

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/class-aggregator.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public function __construct()
1616
{
1717
add_action('koko_analytics_aggregate_stats', [$this, 'aggregate'], 10, 0);
1818
add_filter('cron_schedules', [$this, 'add_interval'], 10, 1);
19-
add_action('init', [$this, 'maybe_setup_scheduled_event'], 10, 0);
19+
add_action('koko_analytics_save_settings', [$this, 'setup_scheduled_event'], 10, 0);
20+
register_activation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'setup_scheduled_event'], 10, 0);
21+
register_deactivation_hook(KOKO_ANALYTICS_PLUGIN_FILE, [$this, 'clear_scheduled_event'], 10, 0);
2022
}
2123

2224
/**
@@ -38,13 +40,9 @@ public function setup_scheduled_event(): void
3840
}
3941
}
4042

41-
public function maybe_setup_scheduled_event(): void
43+
public function clear_scheduled_event(): void
4244
{
43-
if (! isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'POST' || ! is_admin()) {
44-
return;
45-
}
46-
47-
$this->setup_scheduled_event();
45+
wp_clear_scheduled_hook('koko_analytics_aggregate_stats');
4846
}
4947

5048
/**
@@ -54,7 +52,7 @@ public function maybe_setup_scheduled_event(): void
5452
*/
5553
public function aggregate(): void
5654
{
57-
update_option('koko_analytics_last_aggregation_at', time(), true);
55+
update_option('koko_analytics_last_aggregation_at', \time(), true);
5856

5957
// init pageview aggregator
6058
$pageview_aggregator = new Pageview_Aggregator();
@@ -67,7 +65,7 @@ public function aggregate(): void
6765
}
6866

6967
// rename file to temporary location so nothing new is written to it while we process it
70-
$tmp_filename = \dirname($filename) . '/pageviews-' . time() . '.php';
68+
$tmp_filename = \dirname($filename) . '/pageviews-' . \time() . '.php';
7169
$renamed = \rename($filename, $tmp_filename);
7270
if ($renamed !== true) {
7371
if (WP_DEBUG) {

0 commit comments

Comments
 (0)