author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* WordPress Cron API |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
* Schedules an event to run only once. |
0 | 10 |
* |
9 | 11 |
* Schedules a hook which will be triggered by WordPress at the specified time. |
12 |
* The action will trigger when someone visits your WordPress site if the scheduled |
|
13 |
* time has passed. |
|
0 | 14 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* Note that scheduling an event to occur within 10 minutes of an existing event |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* with the same action hook will be ignored unless you pass unique `$args` values |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* for each scheduled event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* |
9 | 19 |
* Use wp_next_scheduled() to prevent duplicate events. |
20 |
* |
|
21 |
* Use wp_schedule_event() to schedule a recurring event. |
|
22 |
* |
|
0 | 23 |
* @since 2.1.0 |
9 | 24 |
* @since 5.1.0 Return value modified to boolean indicating success or failure, |
25 |
* {@see 'pre_schedule_event'} filter added to short-circuit the function. |
|
26 |
* |
|
5 | 27 |
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event |
0 | 28 |
* |
9 | 29 |
* @param int $timestamp Unix timestamp (UTC) for when to next run the event. |
30 |
* @param string $hook Action hook to execute when the event is run. |
|
31 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
32 |
* @return bool True if event successfully scheduled. False for failure. |
|
0 | 33 |
*/ |
9 | 34 |
function wp_schedule_single_event( $timestamp, $hook, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
|
9 | 40 |
$event = (object) array( |
41 |
'hook' => $hook, |
|
42 |
'timestamp' => $timestamp, |
|
43 |
'schedule' => false, |
|
44 |
'args' => $args, |
|
45 |
); |
|
46 |
||
47 |
/** |
|
48 |
* Filter to preflight or hijack scheduling an event. |
|
49 |
* |
|
50 |
* Returning a non-null value will short-circuit adding the event to the |
|
51 |
* cron array, causing the function to return the filtered value instead. |
|
52 |
* |
|
53 |
* Both single events and recurring events are passed through this filter; |
|
54 |
* single events have `$event->schedule` as false, whereas recurring events |
|
55 |
* have this set to a recurrence from wp_get_schedules(). Recurring |
|
56 |
* events also have the integer recurrence interval set as `$event->interval`. |
|
57 |
* |
|
58 |
* For plugins replacing wp-cron, it is recommended you check for an |
|
59 |
* identical event within ten minutes and apply the {@see 'schedule_event'} |
|
60 |
* filter to check if another plugin has disallowed the event before scheduling. |
|
61 |
* |
|
62 |
* Return true if the event was scheduled, false if not. |
|
63 |
* |
|
64 |
* @since 5.1.0 |
|
65 |
* |
|
66 |
* @param null|bool $pre Value to return instead. Default null to continue adding the event. |
|
67 |
* @param stdClass $event { |
|
68 |
* An object containing an event's data. |
|
69 |
* |
|
70 |
* @type string $hook Action hook to execute when the event is run. |
|
71 |
* @type int $timestamp Unix timestamp (UTC) for when to next run the event. |
|
72 |
* @type string|false $schedule How often the event should subsequently recur. |
|
73 |
* @type array $args Array containing each separate argument to pass to the hook's callback function. |
|
74 |
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events. |
|
75 |
* } |
|
76 |
*/ |
|
77 |
$pre = apply_filters( 'pre_schedule_event', null, $event ); |
|
78 |
if ( null !== $pre ) { |
|
79 |
return $pre; |
|
80 |
} |
|
81 |
||
82 |
/* |
|
83 |
* Check for a duplicated event. |
|
84 |
* |
|
85 |
* Don't schedule an event if there's already an identical event |
|
86 |
* within 10 minutes. |
|
87 |
* |
|
88 |
* When scheduling events within ten minutes of the current time, |
|
89 |
* all past identical events are considered duplicates. |
|
90 |
* |
|
91 |
* When scheduling an event with a past timestamp (ie, before the |
|
92 |
* current time) all events scheduled within the next ten minutes |
|
93 |
* are considered duplicates. |
|
94 |
*/ |
|
95 |
$crons = (array) _get_cron_array(); |
|
96 |
$key = md5( serialize( $event->args ) ); |
|
97 |
$duplicate = false; |
|
98 |
||
99 |
if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) { |
|
100 |
$min_timestamp = 0; |
|
101 |
} else { |
|
102 |
$min_timestamp = $event->timestamp - 10 * MINUTE_IN_SECONDS; |
|
103 |
} |
|
104 |
||
105 |
if ( $event->timestamp < time() ) { |
|
106 |
$max_timestamp = time() + 10 * MINUTE_IN_SECONDS; |
|
107 |
} else { |
|
108 |
$max_timestamp = $event->timestamp + 10 * MINUTE_IN_SECONDS; |
|
109 |
} |
|
110 |
||
111 |
foreach ( $crons as $event_timestamp => $cron ) { |
|
112 |
if ( $event_timestamp < $min_timestamp ) { |
|
113 |
continue; |
|
114 |
} |
|
115 |
if ( $event_timestamp > $max_timestamp ) { |
|
116 |
break; |
|
117 |
} |
|
118 |
if ( isset( $cron[ $event->hook ][ $key ] ) ) { |
|
119 |
$duplicate = true; |
|
120 |
break; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
if ( $duplicate ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
return false; |
5 | 126 |
} |
0 | 127 |
|
5 | 128 |
/** |
9 | 129 |
* Modify an event before it is scheduled. |
5 | 130 |
* |
131 |
* @since 3.1.0 |
|
132 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* @param stdClass $event { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* An object containing an event's data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* |
9 | 136 |
* @type string $hook Action hook to execute when the event is run. |
137 |
* @type int $timestamp Unix timestamp (UTC) for when to next run the event. |
|
138 |
* @type string|false $schedule How often the event should subsequently recur. |
|
139 |
* @type array $args Array containing each separate argument to pass to the hook's callback function. |
|
140 |
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* } |
5 | 142 |
*/ |
143 |
$event = apply_filters( 'schedule_event', $event ); |
|
0 | 144 |
|
145 |
// A plugin disallowed this event |
|
9 | 146 |
if ( ! $event ) { |
0 | 147 |
return false; |
9 | 148 |
} |
0 | 149 |
|
9 | 150 |
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array( |
151 |
'schedule' => $event->schedule, |
|
152 |
'args' => $event->args, |
|
153 |
); |
|
154 |
uksort( $crons, 'strnatcasecmp' ); |
|
155 |
return _set_cron_array( $crons ); |
|
0 | 156 |
} |
157 |
||
158 |
/** |
|
9 | 159 |
* Schedules a recurring event. |
0 | 160 |
* |
9 | 161 |
* Schedules a hook which will be triggered by WordPress at the specified interval. |
162 |
* The action will trigger when someone visits your WordPress site if the scheduled |
|
163 |
* time has passed. |
|
0 | 164 |
* |
9 | 165 |
* Valid values for the recurrence are 'hourly', 'daily', and 'twicedaily'. These can |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* be extended using the {@see 'cron_schedules'} filter in wp_get_schedules(). |
0 | 167 |
* |
9 | 168 |
* Note that scheduling an event to occur within 10 minutes of an existing event |
169 |
* with the same action hook will be ignored unless you pass unique `$args` values |
|
170 |
* for each scheduled event. |
|
171 |
* |
|
172 |
* Use wp_next_scheduled() to prevent duplicate events. |
|
173 |
* |
|
174 |
* Use wp_schedule_single_event() to schedule a non-recurring event. |
|
0 | 175 |
* |
176 |
* @since 2.1.0 |
|
9 | 177 |
* @since 5.1.0 Return value modified to boolean indicating success or failure, |
178 |
* {@see 'pre_schedule_event'} filter added to short-circuit the function. |
|
0 | 179 |
* |
9 | 180 |
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_event |
181 |
* |
|
182 |
* @param int $timestamp Unix timestamp (UTC) for when to next run the event. |
|
183 |
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values. |
|
184 |
* @param string $hook Action hook to execute when the event is run. |
|
185 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
186 |
* @return bool True if event successfully scheduled. False for failure. |
|
0 | 187 |
*/ |
9 | 188 |
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
|
0 | 194 |
$schedules = wp_get_schedules(); |
195 |
||
9 | 196 |
if ( ! isset( $schedules[ $recurrence ] ) ) { |
0 | 197 |
return false; |
9 | 198 |
} |
0 | 199 |
|
9 | 200 |
$event = (object) array( |
201 |
'hook' => $hook, |
|
202 |
'timestamp' => $timestamp, |
|
203 |
'schedule' => $recurrence, |
|
204 |
'args' => $args, |
|
205 |
'interval' => $schedules[ $recurrence ]['interval'], |
|
206 |
); |
|
207 |
||
208 |
/** This filter is documented in wp-includes/cron.php */ |
|
209 |
$pre = apply_filters( 'pre_schedule_event', null, $event ); |
|
210 |
if ( null !== $pre ) { |
|
211 |
return $pre; |
|
212 |
} |
|
213 |
||
5 | 214 |
/** This filter is documented in wp-includes/cron.php */ |
215 |
$event = apply_filters( 'schedule_event', $event ); |
|
0 | 216 |
|
217 |
// A plugin disallowed this event |
|
9 | 218 |
if ( ! $event ) { |
0 | 219 |
return false; |
9 | 220 |
} |
221 |
||
222 |
$key = md5( serialize( $event->args ) ); |
|
0 | 223 |
|
9 | 224 |
$crons = _get_cron_array(); |
225 |
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array( |
|
226 |
'schedule' => $event->schedule, |
|
227 |
'args' => $event->args, |
|
228 |
'interval' => $event->interval, |
|
229 |
); |
|
230 |
uksort( $crons, 'strnatcasecmp' ); |
|
231 |
return _set_cron_array( $crons ); |
|
0 | 232 |
} |
233 |
||
234 |
/** |
|
9 | 235 |
* Reschedules a recurring event. |
236 |
* |
|
237 |
* Mainly for internal use, this takes the time stamp of a previously run |
|
238 |
* recurring event and reschedules it for its next run. |
|
239 |
* |
|
240 |
* To change upcoming scheduled events, use wp_schedule_event() to |
|
241 |
* change the recurrence frequency. |
|
0 | 242 |
* |
243 |
* @since 2.1.0 |
|
9 | 244 |
* @since 5.1.0 Return value modified to boolean indicating success or failure, |
245 |
* {@see 'pre_reschedule_event'} filter added to short-circuit the function. |
|
0 | 246 |
* |
9 | 247 |
* @param int $timestamp Unix timestamp (UTC) for when the event was scheduled. |
248 |
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values. |
|
249 |
* @param string $hook Action hook to execute when the event is run. |
|
250 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
251 |
* @return bool True if event successfully rescheduled. False for failure. |
|
0 | 252 |
*/ |
5 | 253 |
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
|
0 | 259 |
$schedules = wp_get_schedules(); |
9 | 260 |
$interval = 0; |
0 | 261 |
|
9 | 262 |
// First we try to get the interval from the schedule. |
5 | 263 |
if ( isset( $schedules[ $recurrence ] ) ) { |
264 |
$interval = $schedules[ $recurrence ]['interval']; |
|
265 |
} |
|
9 | 266 |
|
267 |
// Now we try to get it from the saved interval in case the schedule disappears. |
|
268 |
if ( 0 === $interval ) { |
|
269 |
$scheduled_event = wp_get_scheduled_event( $hook, $args, $timestamp ); |
|
270 |
if ( $scheduled_event && isset( $scheduled_event->interval ) ) { |
|
271 |
$interval = $scheduled_event->interval; |
|
272 |
} |
|
5 | 273 |
} |
9 | 274 |
|
275 |
$event = (object) array( |
|
276 |
'hook' => $hook, |
|
277 |
'timestamp' => $timestamp, |
|
278 |
'schedule' => $recurrence, |
|
279 |
'args' => $args, |
|
280 |
'interval' => $interval, |
|
281 |
); |
|
282 |
||
283 |
/** |
|
284 |
* Filter to preflight or hijack rescheduling of events. |
|
285 |
* |
|
286 |
* Returning a non-null value will short-circuit the normal rescheduling |
|
287 |
* process, causing the function to return the filtered value instead. |
|
288 |
* |
|
289 |
* For plugins replacing wp-cron, return true if the event was successfully |
|
290 |
* rescheduled, false if not. |
|
291 |
* |
|
292 |
* @since 5.1.0 |
|
293 |
* |
|
294 |
* @param null|bool $pre Value to return instead. Default null to continue adding the event. |
|
295 |
* @param stdClass $event { |
|
296 |
* An object containing an event's data. |
|
297 |
* |
|
298 |
* @type string $hook Action hook to execute when the event is run. |
|
299 |
* @type int $timestamp Unix timestamp (UTC) for when to next run the event. |
|
300 |
* @type string|false $schedule How often the event should subsequently recur. |
|
301 |
* @type array $args Array containing each separate argument to pass to the hook's callback function. |
|
302 |
* @type int $interval The interval time in seconds for the schedule. Only present for recurring events. |
|
303 |
* } |
|
304 |
*/ |
|
305 |
$pre = apply_filters( 'pre_reschedule_event', null, $event ); |
|
306 |
if ( null !== $pre ) { |
|
307 |
return $pre; |
|
308 |
} |
|
309 |
||
0 | 310 |
// Now we assume something is wrong and fail to schedule |
5 | 311 |
if ( 0 == $interval ) { |
0 | 312 |
return false; |
5 | 313 |
} |
0 | 314 |
|
315 |
$now = time(); |
|
316 |
||
5 | 317 |
if ( $timestamp >= $now ) { |
0 | 318 |
$timestamp = $now + $interval; |
5 | 319 |
} else { |
320 |
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); |
|
321 |
} |
|
0 | 322 |
|
9 | 323 |
return wp_schedule_event( $timestamp, $recurrence, $hook, $args ); |
0 | 324 |
} |
325 |
||
326 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* Unschedule a previously scheduled event. |
0 | 328 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
* The $timestamp and $hook parameters are required so that the event can be |
0 | 330 |
* identified. |
331 |
* |
|
332 |
* @since 2.1.0 |
|
9 | 333 |
* @since 5.1.0 Return value modified to boolean indicating success or failure, |
334 |
* {@see 'pre_unschedule_event'} filter added to short-circuit the function. |
|
0 | 335 |
* |
9 | 336 |
* @param int $timestamp Unix timestamp (UTC) of the event. |
337 |
* @param string $hook Action hook of the event. |
|
338 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
339 |
* Although not passed to a callback, these arguments are used to uniquely identify the |
|
340 |
* event, so they should be the same as those used when originally scheduling the event. |
|
341 |
* @return bool True if event successfully unscheduled. False for failure. |
|
0 | 342 |
*/ |
343 |
function wp_unschedule_event( $timestamp, $hook, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
|
9 | 349 |
/** |
350 |
* Filter to preflight or hijack unscheduling of events. |
|
351 |
* |
|
352 |
* Returning a non-null value will short-circuit the normal unscheduling |
|
353 |
* process, causing the function to return the filtered value instead. |
|
354 |
* |
|
355 |
* For plugins replacing wp-cron, return true if the event was successfully |
|
356 |
* unscheduled, false if not. |
|
357 |
* |
|
358 |
* @since 5.1.0 |
|
359 |
* |
|
360 |
* @param null|bool $pre Value to return instead. Default null to continue unscheduling the event. |
|
361 |
* @param int $timestamp Timestamp for when to run the event. |
|
362 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
|
363 |
* @param array $args Arguments to pass to the hook's callback function. |
|
364 |
*/ |
|
365 |
$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args ); |
|
366 |
if ( null !== $pre ) { |
|
367 |
return $pre; |
|
368 |
} |
|
369 |
||
0 | 370 |
$crons = _get_cron_array(); |
9 | 371 |
$key = md5( serialize( $args ) ); |
372 |
unset( $crons[ $timestamp ][ $hook ][ $key ] ); |
|
373 |
if ( empty( $crons[ $timestamp ][ $hook ] ) ) { |
|
374 |
unset( $crons[ $timestamp ][ $hook ] ); |
|
375 |
} |
|
376 |
if ( empty( $crons[ $timestamp ] ) ) { |
|
377 |
unset( $crons[ $timestamp ] ); |
|
378 |
} |
|
379 |
return _set_cron_array( $crons ); |
|
0 | 380 |
} |
381 |
||
382 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
* Unschedules all events attached to the hook with the specified arguments. |
0 | 384 |
* |
9 | 385 |
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean |
386 |
* value which evaluates to FALSE. For information about casting to booleans see the |
|
387 |
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use |
|
388 |
* the `===` operator for testing the return value of this function. |
|
389 |
* |
|
0 | 390 |
* @since 2.1.0 |
9 | 391 |
* @since 5.1.0 Return value modified to indicate success or failure, |
392 |
* {@see 'pre_clear_scheduled_hook'} filter added to short-circuit the function. |
|
0 | 393 |
* |
394 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* @param array $args Optional. Arguments that were to be passed to the hook's callback function. |
9 | 396 |
* @return bool|int On success an integer indicating number of events unscheduled (0 indicates no |
397 |
* events were registered with the hook and arguments combination), false if |
|
398 |
* unscheduling one or more events fail. |
|
0 | 399 |
*/ |
400 |
function wp_clear_scheduled_hook( $hook, $args = array() ) { |
|
401 |
// Backward compatibility |
|
402 |
// Previously this function took the arguments as discrete vars rather than an array like the rest of the API |
|
9 | 403 |
if ( ! is_array( $args ) ) { |
404 |
_deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) ); |
|
0 | 405 |
$args = array_slice( func_get_args(), 1 ); |
406 |
} |
|
407 |
||
9 | 408 |
/** |
409 |
* Filter to preflight or hijack clearing a scheduled hook. |
|
410 |
* |
|
411 |
* Returning a non-null value will short-circuit the normal unscheduling |
|
412 |
* process, causing the function to return the filtered value instead. |
|
413 |
* |
|
414 |
* For plugins replacing wp-cron, return the number of events successfully |
|
415 |
* unscheduled (zero if no events were registered with the hook) or false |
|
416 |
* if unscheduling one or more events fails. |
|
417 |
* |
|
418 |
* @since 5.1.0 |
|
419 |
* |
|
420 |
* @param null|array $pre Value to return instead. Default null to continue unscheduling the event. |
|
421 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
|
422 |
* @param array $args Arguments to pass to the hook's callback function. |
|
423 |
*/ |
|
424 |
$pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args ); |
|
425 |
if ( null !== $pre ) { |
|
426 |
return $pre; |
|
427 |
} |
|
428 |
||
5 | 429 |
// This logic duplicates wp_next_scheduled() |
430 |
// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing, |
|
431 |
// and, wp_next_scheduled() returns the same schedule in an infinite loop. |
|
432 |
$crons = _get_cron_array(); |
|
9 | 433 |
if ( empty( $crons ) ) { |
434 |
return 0; |
|
435 |
} |
|
5 | 436 |
|
9 | 437 |
$results = array(); |
438 |
$key = md5( serialize( $args ) ); |
|
5 | 439 |
foreach ( $crons as $timestamp => $cron ) { |
440 |
if ( isset( $cron[ $hook ][ $key ] ) ) { |
|
9 | 441 |
$results[] = wp_unschedule_event( $timestamp, $hook, $args ); |
5 | 442 |
} |
443 |
} |
|
9 | 444 |
if ( in_array( false, $results, true ) ) { |
445 |
return false; |
|
446 |
} |
|
447 |
return count( $results ); |
|
0 | 448 |
} |
449 |
||
450 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
* Unschedules all events attached to the hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
* Can be useful for plugins when deactivating to clean up the cron queue. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
* |
9 | 455 |
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean |
456 |
* value which evaluates to FALSE. For information about casting to booleans see the |
|
457 |
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use |
|
458 |
* the `===` operator for testing the return value of this function. |
|
459 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
* @since 4.9.0 |
9 | 461 |
* @since 5.1.0 Return value added to indicate success or failure. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
9 | 464 |
* @return bool|int On success an integer indicating number of events unscheduled (0 indicates no |
465 |
* events were registered on the hook), false if unscheduling fails. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
function wp_unschedule_hook( $hook ) { |
9 | 468 |
/** |
469 |
* Filter to preflight or hijack clearing all events attached to the hook. |
|
470 |
* |
|
471 |
* Returning a non-null value will short-circuit the normal unscheduling |
|
472 |
* process, causing the function to return the filtered value instead. |
|
473 |
* |
|
474 |
* For plugins replacing wp-cron, return the number of events successfully |
|
475 |
* unscheduled (zero if no events were registered with the hook) or false |
|
476 |
* if unscheduling one or more events fails. |
|
477 |
* |
|
478 |
* @since 5.1.0 |
|
479 |
* |
|
480 |
* @param null|array $pre Value to return instead. Default null to continue unscheduling the hook. |
|
481 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
|
482 |
*/ |
|
483 |
$pre = apply_filters( 'pre_unschedule_hook', null, $hook ); |
|
484 |
if ( null !== $pre ) { |
|
485 |
return $pre; |
|
486 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
|
9 | 488 |
$crons = _get_cron_array(); |
489 |
if ( empty( $crons ) ) { |
|
490 |
return 0; |
|
491 |
} |
|
492 |
||
493 |
$results = array(); |
|
494 |
foreach ( $crons as $timestamp => $args ) { |
|
495 |
if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) { |
|
496 |
$results[] = count( $crons[ $timestamp ][ $hook ] ); |
|
497 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
unset( $crons[ $timestamp ][ $hook ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
if ( empty( $crons[ $timestamp ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
unset( $crons[ $timestamp ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
|
9 | 505 |
/* |
506 |
* If the results are empty (zero events to unschedule), no attempt |
|
507 |
* to update the cron array is required. |
|
508 |
*/ |
|
509 |
if ( empty( $results ) ) { |
|
510 |
return 0; |
|
511 |
} |
|
512 |
if ( _set_cron_array( $crons ) ) { |
|
513 |
return array_sum( $results ); |
|
514 |
} |
|
515 |
return false; |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* Retrieve a scheduled event. |
|
520 |
* |
|
521 |
* Retrieve the full event object for a given event, if no timestamp is specified the next |
|
522 |
* scheduled event is returned. |
|
523 |
* |
|
524 |
* @since 5.1.0 |
|
525 |
* |
|
526 |
* @param string $hook Action hook of the event. |
|
527 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
528 |
* Although not passed to a callback, these arguments are used to uniquely identify the |
|
529 |
* event, so they should be the same as those used when originally scheduling the event. |
|
530 |
* @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event is returned. |
|
531 |
* @return bool|object The event object. False if the event does not exist. |
|
532 |
*/ |
|
533 |
function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { |
|
534 |
/** |
|
535 |
* Filter to preflight or hijack retrieving a scheduled event. |
|
536 |
* |
|
537 |
* Returning a non-null value will short-circuit the normal process, |
|
538 |
* returning the filtered value instead. |
|
539 |
* |
|
540 |
* Return false if the event does not exist, otherwise an event object |
|
541 |
* should be returned. |
|
542 |
* |
|
543 |
* @since 5.1.0 |
|
544 |
* |
|
545 |
* @param null|bool $pre Value to return instead. Default null to continue retrieving the event. |
|
546 |
* @param string $hook Action hook of the event. |
|
547 |
* @param array $args Array containing each separate argument to pass to the hook's callback function. |
|
548 |
* Although not passed to a callback, these arguments are used to uniquely identify the |
|
549 |
* event. |
|
550 |
* @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event. |
|
551 |
*/ |
|
552 |
$pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp ); |
|
553 |
if ( null !== $pre ) { |
|
554 |
return $pre; |
|
555 |
} |
|
556 |
||
557 |
if ( null !== $timestamp && ! is_numeric( $timestamp ) ) { |
|
558 |
return false; |
|
559 |
} |
|
560 |
||
561 |
$crons = _get_cron_array(); |
|
562 |
if ( empty( $crons ) ) { |
|
563 |
return false; |
|
564 |
} |
|
565 |
||
566 |
$key = md5( serialize( $args ) ); |
|
567 |
||
568 |
if ( ! $timestamp ) { |
|
569 |
// Get next event. |
|
570 |
$next = false; |
|
571 |
foreach ( $crons as $timestamp => $cron ) { |
|
572 |
if ( isset( $cron[ $hook ][ $key ] ) ) { |
|
573 |
$next = $timestamp; |
|
574 |
break; |
|
575 |
} |
|
576 |
} |
|
577 |
if ( ! $next ) { |
|
578 |
return false; |
|
579 |
} |
|
580 |
||
581 |
$timestamp = $next; |
|
582 |
} elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) { |
|
583 |
return false; |
|
584 |
} |
|
585 |
||
586 |
$event = (object) array( |
|
587 |
'hook' => $hook, |
|
588 |
'timestamp' => $timestamp, |
|
589 |
'schedule' => $crons[ $timestamp ][ $hook ][ $key ]['schedule'], |
|
590 |
'args' => $args, |
|
591 |
); |
|
592 |
||
593 |
if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) { |
|
594 |
$event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval']; |
|
595 |
} |
|
596 |
||
597 |
return $event; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
* Retrieve the next timestamp for an event. |
0 | 602 |
* |
603 |
* @since 2.1.0 |
|
604 |
* |
|
9 | 605 |
* @param string $hook Action hook of the event. |
606 |
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. |
|
607 |
* Although not passed to a callback, these arguments are used to uniquely identify the |
|
608 |
* event, so they should be the same as those used when originally scheduling the event. |
|
609 |
* @return false|int The Unix timestamp of the next time the event will occur. False if the event doesn't exist. |
|
0 | 610 |
*/ |
611 |
function wp_next_scheduled( $hook, $args = array() ) { |
|
9 | 612 |
$next_event = wp_get_scheduled_event( $hook, $args ); |
613 |
if ( ! $next_event ) { |
|
0 | 614 |
return false; |
615 |
} |
|
9 | 616 |
|
617 |
return $next_event->timestamp; |
|
0 | 618 |
} |
619 |
||
620 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* Sends a request to run cron through HTTP request that doesn't halt page loading. |
0 | 622 |
* |
623 |
* @since 2.1.0 |
|
9 | 624 |
* @since 5.1.0 Return values added. |
0 | 625 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used). |
9 | 627 |
* @return bool True if spawned, false if no events spawned. |
0 | 628 |
*/ |
629 |
function spawn_cron( $gmt_time = 0 ) { |
|
9 | 630 |
if ( ! $gmt_time ) { |
0 | 631 |
$gmt_time = microtime( true ); |
9 | 632 |
} |
0 | 633 |
|
9 | 634 |
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) { |
635 |
return false; |
|
636 |
} |
|
0 | 637 |
|
638 |
/* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* Get the cron lock, which is a Unix timestamp of when the last cron was spawned |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
* and has not finished running. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
* Multiple processes on multiple web servers can run this code concurrently, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* this lock attempts to make spawning as atomic as possible. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
*/ |
9 | 645 |
$lock = get_transient( 'doing_cron' ); |
0 | 646 |
|
9 | 647 |
if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) { |
0 | 648 |
$lock = 0; |
9 | 649 |
} |
0 | 650 |
|
651 |
// don't run if another process is currently running it or more than once every 60 sec. |
|
9 | 652 |
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) { |
653 |
return false; |
|
654 |
} |
|
0 | 655 |
|
656 |
//sanity check |
|
9 | 657 |
$crons = wp_get_ready_cron_jobs(); |
658 |
if ( empty( $crons ) ) { |
|
659 |
return false; |
|
660 |
} |
|
0 | 661 |
|
662 |
$keys = array_keys( $crons ); |
|
9 | 663 |
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { |
664 |
return false; |
|
665 |
} |
|
0 | 666 |
|
5 | 667 |
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { |
9 | 668 |
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { |
669 |
return false; |
|
5 | 670 |
} |
0 | 671 |
|
672 |
$doing_wp_cron = sprintf( '%.22F', $gmt_time ); |
|
673 |
set_transient( 'doing_cron', $doing_wp_cron ); |
|
674 |
||
675 |
ob_start(); |
|
676 |
wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); |
|
677 |
echo ' '; |
|
678 |
||
679 |
// flush any buffers and send the headers |
|
9 | 680 |
while ( @ob_end_flush() ) { |
681 |
} |
|
0 | 682 |
flush(); |
683 |
||
684 |
WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' ); |
|
9 | 685 |
return true; |
0 | 686 |
} |
687 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
// Set the cron lock with the current unix timestamp, when the cron is being spawned. |
0 | 689 |
$doing_wp_cron = sprintf( '%.22F', $gmt_time ); |
690 |
set_transient( 'doing_cron', $doing_wp_cron ); |
|
691 |
||
5 | 692 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
* Filters the cron request arguments. |
5 | 694 |
* |
695 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
* @since 4.5.0 The `$doing_wp_cron` parameter was added. |
5 | 697 |
* |
698 |
* @param array $cron_request_array { |
|
699 |
* An array of cron request URL arguments. |
|
700 |
* |
|
701 |
* @type string $url The cron request URL. |
|
702 |
* @type int $key The 22 digit GMT microtime. |
|
703 |
* @type array $args { |
|
704 |
* An array of cron request arguments. |
|
705 |
* |
|
706 |
* @type int $timeout The request timeout in seconds. Default .01 seconds. |
|
707 |
* @type bool $blocking Whether to set blocking for the request. Default false. |
|
708 |
* @type bool $sslverify Whether SSL should be verified for the request. Default false. |
|
709 |
* } |
|
710 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
* @param string $doing_wp_cron The unix timestamp of the cron lock. |
5 | 712 |
*/ |
9 | 713 |
$cron_request = apply_filters( |
714 |
'cron_request', |
|
715 |
array( |
|
716 |
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ), |
|
717 |
'key' => $doing_wp_cron, |
|
718 |
'args' => array( |
|
719 |
'timeout' => 0.01, |
|
720 |
'blocking' => false, |
|
721 |
/** This filter is documented in wp-includes/class-wp-http-streams.php */ |
|
722 |
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
723 |
), |
|
724 |
), |
|
725 |
$doing_wp_cron |
|
726 |
); |
|
0 | 727 |
|
9 | 728 |
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] ); |
729 |
return ! is_wp_error( $result ); |
|
0 | 730 |
} |
731 |
||
732 |
/** |
|
733 |
* Run scheduled callbacks or spawn cron for all scheduled events. |
|
734 |
* |
|
9 | 735 |
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean |
736 |
* value which evaluates to FALSE. For information about casting to booleans see the |
|
737 |
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use |
|
738 |
* the `===` operator for testing the return value of this function. |
|
739 |
* |
|
0 | 740 |
* @since 2.1.0 |
9 | 741 |
* @since 5.1.0 Return value added to indicate success or failure. |
742 |
* |
|
743 |
* @return bool|int On success an integer indicating number of events spawned (0 indicates no |
|
744 |
* events needed to be spawned), false if spawning fails for one or more events. |
|
0 | 745 |
*/ |
746 |
function wp_cron() { |
|
747 |
// Prevent infinite loops caused by lack of wp-cron.php |
|
9 | 748 |
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { |
749 |
return 0; |
|
750 |
} |
|
0 | 751 |
|
9 | 752 |
$crons = wp_get_ready_cron_jobs(); |
753 |
if ( empty( $crons ) ) { |
|
754 |
return 0; |
|
755 |
} |
|
0 | 756 |
|
757 |
$gmt_time = microtime( true ); |
|
9 | 758 |
$keys = array_keys( $crons ); |
759 |
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { |
|
760 |
return 0; |
|
761 |
} |
|
0 | 762 |
|
763 |
$schedules = wp_get_schedules(); |
|
9 | 764 |
$results = array(); |
0 | 765 |
foreach ( $crons as $timestamp => $cronhooks ) { |
9 | 766 |
if ( $timestamp > $gmt_time ) { |
767 |
break; |
|
768 |
} |
|
0 | 769 |
foreach ( (array) $cronhooks as $hook => $args ) { |
9 | 770 |
if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) { |
0 | 771 |
continue; |
9 | 772 |
} |
773 |
$results[] = spawn_cron( $gmt_time ); |
|
0 | 774 |
break 2; |
775 |
} |
|
776 |
} |
|
9 | 777 |
|
778 |
if ( in_array( false, $results, true ) ) { |
|
779 |
return false; |
|
780 |
} |
|
781 |
return count( $results ); |
|
0 | 782 |
} |
783 |
||
784 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
* Retrieve supported event recurrence schedules. |
0 | 786 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
* The default supported recurrences are 'hourly', 'twicedaily', and 'daily'. A plugin may |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
* add more by hooking into the {@see 'cron_schedules'} filter. The filter accepts an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
* of arrays. The outer array has a key that is the name of the schedule or for |
0 | 790 |
* example 'weekly'. The value is an array with two keys, one is 'interval' and |
791 |
* the other is 'display'. |
|
792 |
* |
|
793 |
* The 'interval' is a number in seconds of when the cron job should run. So for |
|
794 |
* 'hourly', the time is 3600 or 60*60. For weekly, the value would be |
|
795 |
* 60*60*24*7 or 604800. The value of 'interval' would then be 604800. |
|
796 |
* |
|
797 |
* The 'display' is the description. For the 'weekly' key, the 'display' would |
|
5 | 798 |
* be `__( 'Once Weekly' )`. |
0 | 799 |
* |
800 |
* For your plugin, you will be passed an array. you can easily add your |
|
801 |
* schedule by doing the following. |
|
5 | 802 |
* |
803 |
* // Filter parameter variable name is 'array'. |
|
804 |
* $array['weekly'] = array( |
|
805 |
* 'interval' => 604800, |
|
9 | 806 |
* 'display' => __( 'Once Weekly' ) |
5 | 807 |
* ); |
808 |
* |
|
0 | 809 |
* @since 2.1.0 |
810 |
* |
|
811 |
* @return array |
|
812 |
*/ |
|
813 |
function wp_get_schedules() { |
|
814 |
$schedules = array( |
|
9 | 815 |
'hourly' => array( |
816 |
'interval' => HOUR_IN_SECONDS, |
|
817 |
'display' => __( 'Once Hourly' ), |
|
818 |
), |
|
819 |
'twicedaily' => array( |
|
820 |
'interval' => 12 * HOUR_IN_SECONDS, |
|
821 |
'display' => __( 'Twice Daily' ), |
|
822 |
), |
|
823 |
'daily' => array( |
|
824 |
'interval' => DAY_IN_SECONDS, |
|
825 |
'display' => __( 'Once Daily' ), |
|
826 |
), |
|
0 | 827 |
); |
5 | 828 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
* Filters the non-default cron schedules. |
5 | 830 |
* |
831 |
* @since 2.1.0 |
|
832 |
* |
|
833 |
* @param array $new_schedules An array of non-default cron schedules. Default empty. |
|
834 |
*/ |
|
0 | 835 |
return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); |
836 |
} |
|
837 |
||
838 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
839 |
* Retrieve the recurrence schedule for an event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
840 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
* @see wp_get_schedules() for available schedules. |
0 | 842 |
* |
843 |
* @since 2.1.0 |
|
9 | 844 |
* @since 5.1.0 {@see 'get_schedule'} filter added. |
0 | 845 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
* @param string $hook Action hook to identify the event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
* @param array $args Optional. Arguments passed to the event's callback function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
* @return string|false False, if no schedule. Schedule name on success. |
0 | 849 |
*/ |
9 | 850 |
function wp_get_schedule( $hook, $args = array() ) { |
851 |
$schedule = false; |
|
852 |
$event = wp_get_scheduled_event( $hook, $args ); |
|
853 |
||
854 |
if ( $event ) { |
|
855 |
$schedule = $event->schedule; |
|
856 |
} |
|
857 |
||
858 |
/** |
|
859 |
* Filter the schedule for a hook. |
|
860 |
* |
|
861 |
* @since 5.1.0 |
|
862 |
* |
|
863 |
* @param string|bool $schedule Schedule for the hook. False if not found. |
|
864 |
* @param string $hook Action hook to execute when cron is run. |
|
865 |
* @param array $args Optional. Arguments to pass to the hook's callback function. |
|
866 |
*/ |
|
867 |
return apply_filters( 'get_schedule', $schedule, $hook, $args ); |
|
868 |
} |
|
869 |
||
870 |
/** |
|
871 |
* Retrieve cron jobs ready to be run. |
|
872 |
* |
|
873 |
* Returns the results of _get_cron_array() limited to events ready to be run, |
|
874 |
* ie, with a timestamp in the past. |
|
875 |
* |
|
876 |
* @since 5.1.0 |
|
877 |
* |
|
878 |
* @return array Cron jobs ready to be run. |
|
879 |
*/ |
|
880 |
function wp_get_ready_cron_jobs() { |
|
881 |
/** |
|
882 |
* Filter to preflight or hijack retrieving ready cron jobs. |
|
883 |
* |
|
884 |
* Returning an array will short-circuit the normal retrieval of ready |
|
885 |
* cron jobs, causing the function to return the filtered value instead. |
|
886 |
* |
|
887 |
* @since 5.1.0 |
|
888 |
* |
|
889 |
* @param null|array $pre Array of ready cron tasks to return instead. Default null |
|
890 |
* to continue using results from _get_cron_array(). |
|
891 |
*/ |
|
892 |
$pre = apply_filters( 'pre_get_ready_cron_jobs', null ); |
|
893 |
if ( null !== $pre ) { |
|
894 |
return $pre; |
|
895 |
} |
|
896 |
||
0 | 897 |
$crons = _get_cron_array(); |
9 | 898 |
|
899 |
if ( false === $crons ) { |
|
900 |
return array(); |
|
901 |
} |
|
902 |
||
903 |
$gmt_time = microtime( true ); |
|
904 |
$keys = array_keys( $crons ); |
|
905 |
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { |
|
906 |
return array(); |
|
0 | 907 |
} |
9 | 908 |
|
909 |
$results = array(); |
|
910 |
foreach ( $crons as $timestamp => $cronhooks ) { |
|
911 |
if ( $timestamp > $gmt_time ) { |
|
912 |
break; |
|
913 |
} |
|
914 |
$results[ $timestamp ] = $cronhooks; |
|
915 |
} |
|
916 |
||
917 |
return $results; |
|
0 | 918 |
} |
919 |
||
920 |
// |
|
921 |
// Private functions |
|
922 |
// |
|
923 |
||
924 |
/** |
|
925 |
* Retrieve cron info array option. |
|
926 |
* |
|
927 |
* @since 2.1.0 |
|
928 |
* @access private |
|
929 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
* @return false|array CRON info array. |
0 | 931 |
*/ |
9 | 932 |
function _get_cron_array() { |
933 |
$cron = get_option( 'cron' ); |
|
934 |
if ( ! is_array( $cron ) ) { |
|
0 | 935 |
return false; |
9 | 936 |
} |
0 | 937 |
|
9 | 938 |
if ( ! isset( $cron['version'] ) ) { |
939 |
$cron = _upgrade_cron_array( $cron ); |
|
940 |
} |
|
0 | 941 |
|
9 | 942 |
unset( $cron['version'] ); |
0 | 943 |
|
944 |
return $cron; |
|
945 |
} |
|
946 |
||
947 |
/** |
|
948 |
* Updates the CRON option with the new CRON array. |
|
949 |
* |
|
950 |
* @since 2.1.0 |
|
9 | 951 |
* @since 5.1.0 Return value modified to outcome of update_option(). |
952 |
* |
|
0 | 953 |
* @access private |
954 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
* @param array $cron Cron info array from _get_cron_array(). |
9 | 956 |
* @return bool True if cron array updated, false on failure. |
0 | 957 |
*/ |
9 | 958 |
function _set_cron_array( $cron ) { |
0 | 959 |
$cron['version'] = 2; |
9 | 960 |
return update_option( 'cron', $cron ); |
0 | 961 |
} |
962 |
||
963 |
/** |
|
964 |
* Upgrade a Cron info array. |
|
965 |
* |
|
966 |
* This function upgrades the Cron info array to version 2. |
|
967 |
* |
|
968 |
* @since 2.1.0 |
|
969 |
* @access private |
|
970 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* @param array $cron Cron info array from _get_cron_array(). |
0 | 972 |
* @return array An upgraded Cron info array. |
973 |
*/ |
|
9 | 974 |
function _upgrade_cron_array( $cron ) { |
975 |
if ( isset( $cron['version'] ) && 2 == $cron['version'] ) { |
|
0 | 976 |
return $cron; |
9 | 977 |
} |
0 | 978 |
|
979 |
$new_cron = array(); |
|
980 |
||
9 | 981 |
foreach ( (array) $cron as $timestamp => $hooks ) { |
0 | 982 |
foreach ( (array) $hooks as $hook => $args ) { |
9 | 983 |
$key = md5( serialize( $args['args'] ) ); |
984 |
$new_cron[ $timestamp ][ $hook ][ $key ] = $args; |
|
0 | 985 |
} |
986 |
} |
|
987 |
||
988 |
$new_cron['version'] = 2; |
|
989 |
update_option( 'cron', $new_cron ); |
|
990 |
return $new_cron; |
|
991 |
} |