|
1
|
|
|
<?php |
|
2
|
|
|
namespace YahnisElsts\PluginUpdateChecker\v5p0; |
|
3
|
|
|
|
|
4
|
|
|
if ( !class_exists(Scheduler::class, false) ): |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* The scheduler decides when and how often to check for updates. |
|
8
|
|
|
* It calls @see UpdateChecker::checkForUpdates() to perform the actual checks. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Scheduler { |
|
11
|
|
|
public $checkPeriod = 12; //How often to check for updates (in hours). |
|
12
|
|
|
public $throttleRedundantChecks = false; //Check less often if we already know that an update is available. |
|
13
|
|
|
public $throttledCheckPeriod = 72; |
|
14
|
|
|
|
|
15
|
|
|
protected $hourlyCheckHooks = array('load-update.php'); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var UpdateChecker |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $updateChecker; |
|
21
|
|
|
|
|
22
|
|
|
private $cronHook = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Scheduler constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param UpdateChecker $updateChecker |
|
28
|
|
|
* @param int $checkPeriod How often to check for updates (in hours). |
|
29
|
|
|
* @param array $hourlyHooks |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($updateChecker, $checkPeriod, $hourlyHooks = array('load-plugins.php')) { |
|
32
|
|
|
$this->updateChecker = $updateChecker; |
|
33
|
|
|
$this->checkPeriod = $checkPeriod; |
|
34
|
|
|
|
|
35
|
|
|
//Set up the periodic update checks |
|
36
|
|
|
$this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates'); |
|
37
|
|
|
if ( $this->checkPeriod > 0 ){ |
|
38
|
|
|
|
|
39
|
|
|
//Trigger the check via Cron. |
|
40
|
|
|
//Try to use one of the default schedules if possible as it's less likely to conflict |
|
41
|
|
|
//with other plugins and their custom schedules. |
|
42
|
|
|
$defaultSchedules = array( |
|
43
|
|
|
1 => 'hourly', |
|
44
|
|
|
12 => 'twicedaily', |
|
45
|
|
|
24 => 'daily', |
|
46
|
|
|
); |
|
47
|
|
|
if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) { |
|
48
|
|
|
$scheduleName = $defaultSchedules[$this->checkPeriod]; |
|
49
|
|
|
} else { |
|
50
|
|
|
//Use a custom cron schedule. |
|
51
|
|
|
$scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
52
|
|
|
add_filter('cron_schedules', array($this, '_addCustomSchedule')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) { |
|
56
|
|
|
//Randomly offset the schedule to help prevent update server traffic spikes. Without this |
|
57
|
|
|
//most checks may happen during times of day when people are most likely to install new plugins. |
|
58
|
|
|
$upperLimit = max($this->checkPeriod * 3600 - 15 * 60, 1); |
|
59
|
|
|
if ( function_exists('wp_rand') ) { |
|
60
|
|
|
$randomOffset = wp_rand(0, $upperLimit); |
|
61
|
|
|
} else { |
|
62
|
|
|
//This constructor may be called before wp_rand() is available. |
|
63
|
|
|
//phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand |
|
64
|
|
|
$randomOffset = rand(0, $upperLimit); |
|
65
|
|
|
} |
|
66
|
|
|
$firstCheckTime = time() - $randomOffset; |
|
67
|
|
|
$firstCheckTime = apply_filters( |
|
68
|
|
|
$this->updateChecker->getUniqueName('first_check_time'), |
|
69
|
|
|
$firstCheckTime |
|
70
|
|
|
); |
|
71
|
|
|
wp_schedule_event($firstCheckTime, $scheduleName, $this->cronHook); |
|
72
|
|
|
} |
|
73
|
|
|
add_action($this->cronHook, array($this, 'maybeCheckForUpdates')); |
|
74
|
|
|
|
|
75
|
|
|
//In case Cron is disabled or unreliable, we also manually trigger |
|
76
|
|
|
//the periodic checks while the user is browsing the Dashboard. |
|
77
|
|
|
add_action( 'admin_init', array($this, 'maybeCheckForUpdates') ); |
|
78
|
|
|
|
|
79
|
|
|
//Like WordPress itself, we check more often on certain pages. |
|
80
|
|
|
/** @see wp_update_plugins */ |
|
81
|
|
|
add_action('load-update-core.php', array($this, 'maybeCheckForUpdates')); |
|
82
|
|
|
//"load-update.php" and "load-plugins.php" or "load-themes.php". |
|
83
|
|
|
$this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks); |
|
84
|
|
|
foreach($this->hourlyCheckHooks as $hook) { |
|
85
|
|
|
add_action($hook, array($this, 'maybeCheckForUpdates')); |
|
86
|
|
|
} |
|
87
|
|
|
//This hook fires after a bulk update is complete. |
|
88
|
|
|
add_action('upgrader_process_complete', array($this, 'upgraderProcessComplete'), 11, 2); |
|
89
|
|
|
|
|
90
|
|
|
} else { |
|
91
|
|
|
//Periodic checks are disabled. |
|
92
|
|
|
wp_clear_scheduled_hook($this->cronHook); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Runs upon the WP action upgrader_process_complete. |
|
98
|
|
|
* |
|
99
|
|
|
* We look at the parameters to decide whether to call maybeCheckForUpdates() or not. |
|
100
|
|
|
* We also check if the update checker has been removed by the update. |
|
101
|
|
|
* |
|
102
|
|
|
* @param \WP_Upgrader $upgrader WP_Upgrader instance |
|
103
|
|
|
* @param array $upgradeInfo extra information about the upgrade |
|
104
|
|
|
*/ |
|
105
|
|
|
public function upgraderProcessComplete( |
|
106
|
|
|
/** @noinspection PhpUnusedParameterInspection */ |
|
107
|
|
|
$upgrader, $upgradeInfo |
|
108
|
|
|
) { |
|
109
|
|
|
//Cancel all further actions if the current version of PUC has been deleted or overwritten |
|
110
|
|
|
//by a different version during the upgrade. If we try to do anything more in that situation, |
|
111
|
|
|
//we could trigger a fatal error by trying to autoload a deleted class. |
|
112
|
|
|
clearstatcache(); |
|
113
|
|
|
if ( !file_exists(__FILE__) ) { |
|
114
|
|
|
$this->removeHooks(); |
|
115
|
|
|
$this->updateChecker->removeHooks(); |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
//Sanity check and limitation to relevant types. |
|
120
|
|
|
if ( |
|
121
|
|
|
!is_array($upgradeInfo) || !isset($upgradeInfo['type'], $upgradeInfo['action']) |
|
122
|
|
|
|| 'update' !== $upgradeInfo['action'] || !in_array($upgradeInfo['type'], array('plugin', 'theme')) |
|
123
|
|
|
) { |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
//Filter out notifications of upgrades that should have no bearing upon whether or not our |
|
128
|
|
|
//current info is up-to-date. |
|
129
|
|
|
if ( is_a($this->updateChecker, Theme\UpdateChecker::class) ) { |
|
130
|
|
|
if ( 'theme' !== $upgradeInfo['type'] || !isset($upgradeInfo['themes']) ) { |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
//Letting too many things going through for checks is not a real problem, so we compare widely. |
|
135
|
|
|
if ( !in_array( |
|
136
|
|
|
strtolower($this->updateChecker->directoryName), |
|
137
|
|
|
array_map('strtolower', $upgradeInfo['themes']) |
|
138
|
|
|
) ) { |
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( is_a($this->updateChecker, Plugin\UpdateChecker::class) ) { |
|
144
|
|
|
if ( 'plugin' !== $upgradeInfo['type'] || !isset($upgradeInfo['plugins']) ) { |
|
145
|
|
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
//Themes pass in directory names in the information array, but plugins use the relative plugin path. |
|
149
|
|
|
if ( !in_array( |
|
150
|
|
|
strtolower($this->updateChecker->directoryName), |
|
151
|
|
|
array_map('dirname', array_map('strtolower', $upgradeInfo['plugins'])) |
|
152
|
|
|
) ) { |
|
153
|
|
|
return; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->maybeCheckForUpdates(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Check for updates if the configured check interval has already elapsed. |
|
162
|
|
|
* Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron. |
|
163
|
|
|
* |
|
164
|
|
|
* You can override the default behaviour by using the "puc_check_now-$slug" filter. |
|
165
|
|
|
* The filter callback will be passed three parameters: |
|
166
|
|
|
* - Current decision. TRUE = check updates now, FALSE = don't check now. |
|
167
|
|
|
* - Last check time as a Unix timestamp. |
|
168
|
|
|
* - Configured check period in hours. |
|
169
|
|
|
* Return TRUE to check for updates immediately, or FALSE to cancel. |
|
170
|
|
|
* |
|
171
|
|
|
* This method is declared public because it's a hook callback. Calling it directly is not recommended. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function maybeCheckForUpdates() { |
|
174
|
|
|
if ( empty($this->checkPeriod) ){ |
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$state = $this->updateChecker->getUpdateState(); |
|
179
|
|
|
$shouldCheck = ($state->timeSinceLastCheck() >= $this->getEffectiveCheckPeriod()); |
|
180
|
|
|
|
|
181
|
|
|
//Let plugin authors substitute their own algorithm. |
|
182
|
|
|
$shouldCheck = apply_filters( |
|
183
|
|
|
$this->updateChecker->getUniqueName('check_now'), |
|
184
|
|
|
$shouldCheck, |
|
185
|
|
|
$state->getLastCheck(), |
|
186
|
|
|
$this->checkPeriod |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
if ( $shouldCheck ) { |
|
190
|
|
|
$this->updateChecker->checkForUpdates(); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Calculate the actual check period based on the current status and environment. |
|
196
|
|
|
* |
|
197
|
|
|
* @return int Check period in seconds. |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function getEffectiveCheckPeriod() { |
|
200
|
|
|
$currentFilter = current_filter(); |
|
201
|
|
|
if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) { |
|
202
|
|
|
//Check more often when the user visits "Dashboard -> Updates" or does a bulk update. |
|
203
|
|
|
$period = 60; |
|
204
|
|
|
} else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) { |
|
205
|
|
|
//Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page. |
|
206
|
|
|
$period = 3600; |
|
207
|
|
|
} else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) { |
|
208
|
|
|
//Check less frequently if it's already known that an update is available. |
|
209
|
|
|
$period = $this->throttledCheckPeriod * 3600; |
|
210
|
|
|
} else if ( defined('DOING_CRON') && constant('DOING_CRON') ) { |
|
211
|
|
|
//WordPress cron schedules are not exact, so lets do an update check even |
|
212
|
|
|
//if slightly less than $checkPeriod hours have elapsed since the last check. |
|
213
|
|
|
$cronFuzziness = 20 * 60; |
|
214
|
|
|
$period = $this->checkPeriod * 3600 - $cronFuzziness; |
|
215
|
|
|
} else { |
|
216
|
|
|
$period = $this->checkPeriod * 3600; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $period; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Add our custom schedule to the array of Cron schedules used by WP. |
|
224
|
|
|
* |
|
225
|
|
|
* @param array $schedules |
|
226
|
|
|
* @return array |
|
227
|
|
|
*/ |
|
228
|
|
|
public function _addCustomSchedule($schedules) { |
|
229
|
|
|
if ( $this->checkPeriod && ($this->checkPeriod > 0) ){ |
|
230
|
|
|
$scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
231
|
|
|
$schedules[$scheduleName] = array( |
|
232
|
|
|
'interval' => $this->checkPeriod * 3600, |
|
233
|
|
|
'display' => sprintf('Every %d hours', $this->checkPeriod), |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
return $schedules; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Remove the scheduled cron event that the library uses to check for updates. |
|
241
|
|
|
* |
|
242
|
|
|
* @return void |
|
243
|
|
|
*/ |
|
244
|
|
|
public function removeUpdaterCron() { |
|
245
|
|
|
wp_clear_scheduled_hook($this->cronHook); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get the name of the update checker's WP-cron hook. Mostly useful for debugging. |
|
250
|
|
|
* |
|
251
|
|
|
* @return string |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getCronHookName() { |
|
254
|
|
|
return $this->cronHook; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Remove most hooks added by the scheduler. |
|
259
|
|
|
*/ |
|
260
|
|
|
public function removeHooks() { |
|
261
|
|
|
remove_filter('cron_schedules', array($this, '_addCustomSchedule')); |
|
262
|
|
|
remove_action('admin_init', array($this, 'maybeCheckForUpdates')); |
|
263
|
|
|
remove_action('load-update-core.php', array($this, 'maybeCheckForUpdates')); |
|
264
|
|
|
|
|
265
|
|
|
if ( $this->cronHook !== null ) { |
|
266
|
|
|
remove_action($this->cronHook, array($this, 'maybeCheckForUpdates')); |
|
267
|
|
|
} |
|
268
|
|
|
if ( !empty($this->hourlyCheckHooks) ) { |
|
269
|
|
|
foreach ($this->hourlyCheckHooks as $hook) { |
|
270
|
|
|
remove_action($hook, array($this, 'maybeCheckForUpdates')); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
endif; |
|
277
|
|
|
|