-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCronUtils.php
83 lines (77 loc) · 3.37 KB
/
CronUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace WebSharks\ZenCache\Pro;
/*
* Extends WP-Cron schedules.
*
* @since 150422 Rewrite.
*
* @attaches-to `cron_schedules` filter.
*
* @param array $schedules An array of the current schedules.
*
* @return array Revised array of WP-Cron schedules.
*/
$self->extendCronSchedules = function ($schedules) use ($self) {
$schedules['every15m'] = array(
'interval' => 900,
'display' => __('Every 15 Minutes', SLUG_TD),
);
return $schedules;
};
/*
* Checks Cron setup, validates schedules, and reschedules events if necessary.
*
* @attaches-to `init` hook.
*
* @since 15xxxx Improving WP Cron setup and validation of schedules
*/
$self->checkCronSetup = function () use ($self) {
if ($self->options['crons_setup'] < 1439005906
|| $self->options['crons_setup_on_namespace'] !== __NAMESPACE__
|| $self->options['crons_setup_with_cache_cleanup_schedule'] !== $self->options['cache_cleanup_schedule']
|| $self->options['crons_setup_on_wp_with_schedules'] !== sha1(serialize(wp_get_schedules()))
|| !wp_next_scheduled('_cron_'.GLOBAL_NS.'_cleanup')
|| !wp_next_scheduled('_cron_'.GLOBAL_NS.'_auto_cache')
) {
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_cleanup');
wp_schedule_event(time() + 60, $self->options['cache_cleanup_schedule'], '_cron_'.GLOBAL_NS.'_cleanup');
/*[pro strip-from="lite"]*/ // Auto-cache engine.
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_auto_cache');
wp_schedule_event(time() + 60, 'every15m', '_cron_'.GLOBAL_NS.'_auto_cache');
/*[/pro]*/
$self->updateOptions(
array(
'crons_setup' => time(),
'crons_setup_on_namespace' => __NAMESPACE__,
'crons_setup_with_cache_cleanup_schedule' => $self->options['cache_cleanup_schedule'],
'crons_setup_on_wp_with_schedules' => sha1(serialize(wp_get_schedules()))
)
);
}
};
/*
* Resets `crons_setup` and clears WP-Cron schedules.
*
* @since 15xxxx Fixing bug with Auto-Cache Engine cron disappearing in some scenarios
*
* @note This MUST happen upon uninstall and deactivation due to buggy WP_Cron behavior. Events with a custom schedule will disappear when plugin is not active (see http://bit.ly/1lGdr78).
*/
$self->resetCronSetup = function ( ) use ($self) {
if (is_multisite()) { // Main site CRON jobs.
switch_to_blog(get_current_site()->blog_id);
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_auto_cache');
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_cleanup');
restore_current_blog(); // Restore current blog.
} else { // Standard WP installation.
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_auto_cache');
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_cleanup');
}
$self->updateOptions(
array( // Reset so that crons are rescheduled upon next activation
'crons_setup' => $self->default_options['crons_setup'],
'crons_setup_on_namespace' => $self->default_options['crons_setup_on_namespace'],
'crons_setup_with_cache_cleanup_schedule' => $self->default_options['crons_setup_with_cache_cleanup_schedule'],
'crons_setup_on_wp_with_schedules' => $self->default_options['crons_setup_on_wp_with_schedules']
)
);
};