author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* Schedules an event which will execute once by the WordPress actions core at |
0 | 12 |
* a time which you specify. The action will fire off when someone visits your |
13 |
* WordPress site, if the schedule time has passed. |
|
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 |
* |
0 | 19 |
* @since 2.1.0 |
5 | 20 |
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event |
0 | 21 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @param int $timestamp Unix timestamp (UTC) for when to run the event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @param string $hook Action hook to execute when event is run. |
0 | 24 |
* @param array $args Optional. Arguments to pass to the hook's callback function. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* @return false|void False if the event does not get scheduled. |
0 | 26 |
*/ |
27 |
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
|
28 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
// Don't schedule a duplicate if there's already an identical event due within 10 minutes of it |
0 | 34 |
$next = wp_next_scheduled($hook, $args); |
5 | 35 |
if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
return false; |
5 | 37 |
} |
0 | 38 |
|
39 |
$crons = _get_cron_array(); |
|
40 |
$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args ); |
|
5 | 41 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
* Filters a single event before it is scheduled. |
5 | 43 |
* |
44 |
* @since 3.1.0 |
|
45 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* @param stdClass $event { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* An object containing an event's data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @type string $hook Action hook to execute when event is run. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* @type int $timestamp Unix timestamp (UTC) for when to run the event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* @type string|false $schedule How often the event should recur. See `wp_get_schedules()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @type array $args Arguments to pass to the hook's callback function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* } |
5 | 54 |
*/ |
55 |
$event = apply_filters( 'schedule_event', $event ); |
|
0 | 56 |
|
57 |
// A plugin disallowed this event |
|
58 |
if ( ! $event ) |
|
59 |
return false; |
|
60 |
||
61 |
$key = md5(serialize($event->args)); |
|
62 |
||
63 |
$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); |
|
64 |
uksort( $crons, "strnatcasecmp" ); |
|
65 |
_set_cron_array( $crons ); |
|
66 |
} |
|
67 |
||
68 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* Schedule a recurring event. |
0 | 70 |
* |
71 |
* Schedules a hook which will be executed by the WordPress actions core on a |
|
72 |
* specific interval, specified by you. The action will trigger when someone |
|
73 |
* visits your WordPress site, if the scheduled time has passed. |
|
74 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* Valid values for the recurrence are hourly, daily, and twicedaily. These can |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* be extended using the {@see 'cron_schedules'} filter in wp_get_schedules(). |
0 | 77 |
* |
78 |
* Use wp_next_scheduled() to prevent duplicates |
|
79 |
* |
|
80 |
* @since 2.1.0 |
|
81 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* @param int $timestamp Unix timestamp (UTC) for when to run the event. |
0 | 83 |
* @param string $recurrence How often the event should recur. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* @param string $hook Action hook to execute when event is run. |
0 | 85 |
* @param array $args Optional. Arguments to pass to the hook's callback function. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* @return false|void False if the event does not get scheduled. |
0 | 87 |
*/ |
88 |
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
|
89 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
|
0 | 94 |
$crons = _get_cron_array(); |
95 |
$schedules = wp_get_schedules(); |
|
96 |
||
97 |
if ( !isset( $schedules[$recurrence] ) ) |
|
98 |
return false; |
|
99 |
||
100 |
$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); |
|
5 | 101 |
/** This filter is documented in wp-includes/cron.php */ |
102 |
$event = apply_filters( 'schedule_event', $event ); |
|
0 | 103 |
|
104 |
// A plugin disallowed this event |
|
105 |
if ( ! $event ) |
|
106 |
return false; |
|
107 |
||
108 |
$key = md5(serialize($event->args)); |
|
109 |
||
110 |
$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval ); |
|
111 |
uksort( $crons, "strnatcasecmp" ); |
|
112 |
_set_cron_array( $crons ); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Reschedule a recurring event. |
|
117 |
* |
|
118 |
* @since 2.1.0 |
|
119 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* @param int $timestamp Unix timestamp (UTC) for when to run the event. |
0 | 121 |
* @param string $recurrence How often the event should recur. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* @param string $hook Action hook to execute when event is run. |
0 | 123 |
* @param array $args Optional. Arguments to pass to the hook's callback function. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @return false|void False if the event does not get rescheduled. |
0 | 125 |
*/ |
5 | 126 |
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
|
127 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
|
0 | 132 |
$crons = _get_cron_array(); |
133 |
$schedules = wp_get_schedules(); |
|
5 | 134 |
$key = md5( serialize( $args ) ); |
0 | 135 |
$interval = 0; |
136 |
||
137 |
// First we try to get it from the schedule |
|
5 | 138 |
if ( isset( $schedules[ $recurrence ] ) ) { |
139 |
$interval = $schedules[ $recurrence ]['interval']; |
|
140 |
} |
|
0 | 141 |
// Now we try to get it from the saved interval in case the schedule disappears |
5 | 142 |
if ( 0 == $interval ) { |
143 |
$interval = $crons[ $timestamp ][ $hook ][ $key ]['interval']; |
|
144 |
} |
|
0 | 145 |
// Now we assume something is wrong and fail to schedule |
5 | 146 |
if ( 0 == $interval ) { |
0 | 147 |
return false; |
5 | 148 |
} |
0 | 149 |
|
150 |
$now = time(); |
|
151 |
||
5 | 152 |
if ( $timestamp >= $now ) { |
0 | 153 |
$timestamp = $now + $interval; |
5 | 154 |
} else { |
155 |
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); |
|
156 |
} |
|
0 | 157 |
|
158 |
wp_schedule_event( $timestamp, $recurrence, $hook, $args ); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
* Unschedule a previously scheduled event. |
0 | 163 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* The $timestamp and $hook parameters are required so that the event can be |
0 | 165 |
* identified. |
166 |
* |
|
167 |
* @since 2.1.0 |
|
168 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
* @param int $timestamp Unix timestamp (UTC) for when to run the event. |
0 | 170 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
171 |
* @param array $args Arguments to pass to the hook's callback function. |
|
172 |
* Although not passed to a callback function, these arguments are used |
|
173 |
* to uniquely identify the scheduled event, so they should be the same |
|
174 |
* as those used when originally scheduling the event. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* @return false|void False if the event does not get unscheduled. |
0 | 176 |
*/ |
177 |
function wp_unschedule_event( $timestamp, $hook, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
// Make sure timestamp is a positive integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
|
0 | 183 |
$crons = _get_cron_array(); |
184 |
$key = md5(serialize($args)); |
|
185 |
unset( $crons[$timestamp][$hook][$key] ); |
|
186 |
if ( empty($crons[$timestamp][$hook]) ) |
|
187 |
unset( $crons[$timestamp][$hook] ); |
|
188 |
if ( empty($crons[$timestamp]) ) |
|
189 |
unset( $crons[$timestamp] ); |
|
190 |
_set_cron_array( $crons ); |
|
191 |
} |
|
192 |
||
193 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
* Unschedules all events attached to the hook with the specified arguments. |
0 | 195 |
* |
196 |
* @since 2.1.0 |
|
197 |
* |
|
198 |
* @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
|
199 |
* @param array $args Optional. Arguments that were to be passed to the hook's callback function. |
0 | 200 |
*/ |
201 |
function wp_clear_scheduled_hook( $hook, $args = array() ) { |
|
202 |
// Backward compatibility |
|
203 |
// Previously this function took the arguments as discrete vars rather than an array like the rest of the API |
|
204 |
if ( !is_array($args) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
_deprecated_argument( __FUNCTION__, '3.0.0', __('This argument has changed to an array to match the behavior of the other cron functions.') ); |
0 | 206 |
$args = array_slice( func_get_args(), 1 ); |
207 |
} |
|
208 |
||
5 | 209 |
// This logic duplicates wp_next_scheduled() |
210 |
// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing, |
|
211 |
// and, wp_next_scheduled() returns the same schedule in an infinite loop. |
|
212 |
$crons = _get_cron_array(); |
|
213 |
if ( empty( $crons ) ) |
|
214 |
return; |
|
215 |
||
216 |
$key = md5( serialize( $args ) ); |
|
217 |
foreach ( $crons as $timestamp => $cron ) { |
|
218 |
if ( isset( $cron[ $hook ][ $key ] ) ) { |
|
219 |
wp_unschedule_event( $timestamp, $hook, $args ); |
|
220 |
} |
|
221 |
} |
|
0 | 222 |
} |
223 |
||
224 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* Unschedules all events attached to the hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* 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
|
228 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
* @param string $hook Action hook, the execution of which will be unscheduled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
function wp_unschedule_hook( $hook ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
$crons = _get_cron_array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
foreach( $crons as $timestamp => $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
unset( $crons[ $timestamp ][ $hook ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
if ( empty( $crons[ $timestamp ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
unset( $crons[ $timestamp ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
_set_cron_array( $crons ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
* Retrieve the next timestamp for an event. |
0 | 249 |
* |
250 |
* @since 2.1.0 |
|
251 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* @param string $hook Action hook to execute when event is run. |
0 | 253 |
* @param array $args Optional. Arguments to pass to the hook's callback function. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @return false|int The Unix timestamp of the next time the scheduled event will occur. |
0 | 255 |
*/ |
256 |
function wp_next_scheduled( $hook, $args = array() ) { |
|
257 |
$crons = _get_cron_array(); |
|
258 |
$key = md5(serialize($args)); |
|
259 |
if ( empty($crons) ) |
|
260 |
return false; |
|
261 |
foreach ( $crons as $timestamp => $cron ) { |
|
262 |
if ( isset( $cron[$hook][$key] ) ) |
|
263 |
return $timestamp; |
|
264 |
} |
|
265 |
return false; |
|
266 |
} |
|
267 |
||
268 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* Sends a request to run cron through HTTP request that doesn't halt page loading. |
0 | 270 |
* |
271 |
* @since 2.1.0 |
|
272 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used). |
0 | 274 |
*/ |
275 |
function spawn_cron( $gmt_time = 0 ) { |
|
276 |
if ( ! $gmt_time ) |
|
277 |
$gmt_time = microtime( true ); |
|
278 |
||
279 |
if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) |
|
280 |
return; |
|
281 |
||
282 |
/* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
* 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
|
284 |
* and has not finished running. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* 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
|
287 |
* 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
|
288 |
*/ |
0 | 289 |
$lock = get_transient('doing_cron'); |
290 |
||
291 |
if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) |
|
292 |
$lock = 0; |
|
293 |
||
294 |
// don't run if another process is currently running it or more than once every 60 sec. |
|
295 |
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) |
|
296 |
return; |
|
297 |
||
298 |
//sanity check |
|
299 |
$crons = _get_cron_array(); |
|
300 |
if ( !is_array($crons) ) |
|
301 |
return; |
|
302 |
||
303 |
$keys = array_keys( $crons ); |
|
304 |
if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
|
305 |
return; |
|
306 |
||
5 | 307 |
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { |
0 | 309 |
return; |
5 | 310 |
} |
0 | 311 |
|
312 |
$doing_wp_cron = sprintf( '%.22F', $gmt_time ); |
|
313 |
set_transient( 'doing_cron', $doing_wp_cron ); |
|
314 |
||
315 |
ob_start(); |
|
316 |
wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); |
|
317 |
echo ' '; |
|
318 |
||
319 |
// flush any buffers and send the headers |
|
320 |
while ( @ob_end_flush() ); |
|
321 |
flush(); |
|
322 |
||
323 |
WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' ); |
|
324 |
return; |
|
325 |
} |
|
326 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
// Set the cron lock with the current unix timestamp, when the cron is being spawned. |
0 | 328 |
$doing_wp_cron = sprintf( '%.22F', $gmt_time ); |
329 |
set_transient( 'doing_cron', $doing_wp_cron ); |
|
330 |
||
5 | 331 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* Filters the cron request arguments. |
5 | 333 |
* |
334 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
* @since 4.5.0 The `$doing_wp_cron` parameter was added. |
5 | 336 |
* |
337 |
* @param array $cron_request_array { |
|
338 |
* An array of cron request URL arguments. |
|
339 |
* |
|
340 |
* @type string $url The cron request URL. |
|
341 |
* @type int $key The 22 digit GMT microtime. |
|
342 |
* @type array $args { |
|
343 |
* An array of cron request arguments. |
|
344 |
* |
|
345 |
* @type int $timeout The request timeout in seconds. Default .01 seconds. |
|
346 |
* @type bool $blocking Whether to set blocking for the request. Default false. |
|
347 |
* @type bool $sslverify Whether SSL should be verified for the request. Default false. |
|
348 |
* } |
|
349 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* @param string $doing_wp_cron The unix timestamp of the cron lock. |
5 | 351 |
*/ |
0 | 352 |
$cron_request = apply_filters( 'cron_request', array( |
5 | 353 |
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ), |
354 |
'key' => $doing_wp_cron, |
|
355 |
'args' => array( |
|
356 |
'timeout' => 0.01, |
|
357 |
'blocking' => false, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
/** This filter is documented in wp-includes/class-wp-http-streams.php */ |
5 | 359 |
'sslverify' => apply_filters( 'https_local_ssl_verify', false ) |
360 |
) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
), $doing_wp_cron ); |
0 | 362 |
|
363 |
wp_remote_post( $cron_request['url'], $cron_request['args'] ); |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Run scheduled callbacks or spawn cron for all scheduled events. |
|
368 |
* |
|
369 |
* @since 2.1.0 |
|
370 |
*/ |
|
371 |
function wp_cron() { |
|
372 |
// Prevent infinite loops caused by lack of wp-cron.php |
|
373 |
if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) |
|
374 |
return; |
|
375 |
||
376 |
if ( false === $crons = _get_cron_array() ) |
|
377 |
return; |
|
378 |
||
379 |
$gmt_time = microtime( true ); |
|
380 |
$keys = array_keys( $crons ); |
|
381 |
if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
|
382 |
return; |
|
383 |
||
384 |
$schedules = wp_get_schedules(); |
|
385 |
foreach ( $crons as $timestamp => $cronhooks ) { |
|
386 |
if ( $timestamp > $gmt_time ) break; |
|
387 |
foreach ( (array) $cronhooks as $hook => $args ) { |
|
388 |
if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) |
|
389 |
continue; |
|
390 |
spawn_cron( $gmt_time ); |
|
391 |
break 2; |
|
392 |
} |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* Retrieve supported event recurrence schedules. |
0 | 398 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* 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
|
400 |
* 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
|
401 |
* of arrays. The outer array has a key that is the name of the schedule or for |
0 | 402 |
* example 'weekly'. The value is an array with two keys, one is 'interval' and |
403 |
* the other is 'display'. |
|
404 |
* |
|
405 |
* The 'interval' is a number in seconds of when the cron job should run. So for |
|
406 |
* 'hourly', the time is 3600 or 60*60. For weekly, the value would be |
|
407 |
* 60*60*24*7 or 604800. The value of 'interval' would then be 604800. |
|
408 |
* |
|
409 |
* The 'display' is the description. For the 'weekly' key, the 'display' would |
|
5 | 410 |
* be `__( 'Once Weekly' )`. |
0 | 411 |
* |
412 |
* For your plugin, you will be passed an array. you can easily add your |
|
413 |
* schedule by doing the following. |
|
5 | 414 |
* |
415 |
* // Filter parameter variable name is 'array'. |
|
416 |
* $array['weekly'] = array( |
|
417 |
* 'interval' => 604800, |
|
418 |
* 'display' => __( 'Once Weekly' ) |
|
419 |
* ); |
|
420 |
* |
|
0 | 421 |
* |
422 |
* @since 2.1.0 |
|
423 |
* |
|
424 |
* @return array |
|
425 |
*/ |
|
426 |
function wp_get_schedules() { |
|
427 |
$schedules = array( |
|
428 |
'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ), |
|
429 |
'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ), |
|
430 |
'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ), |
|
431 |
); |
|
5 | 432 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* Filters the non-default cron schedules. |
5 | 434 |
* |
435 |
* @since 2.1.0 |
|
436 |
* |
|
437 |
* @param array $new_schedules An array of non-default cron schedules. Default empty. |
|
438 |
*/ |
|
0 | 439 |
return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); |
440 |
} |
|
441 |
||
442 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* Retrieve the recurrence schedule for an event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* @see wp_get_schedules() for available schedules. |
0 | 446 |
* |
447 |
* @since 2.1.0 |
|
448 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
* @param string $hook Action hook to identify the event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
* @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
|
451 |
* @return string|false False, if no schedule. Schedule name on success. |
0 | 452 |
*/ |
453 |
function wp_get_schedule($hook, $args = array()) { |
|
454 |
$crons = _get_cron_array(); |
|
455 |
$key = md5(serialize($args)); |
|
456 |
if ( empty($crons) ) |
|
457 |
return false; |
|
458 |
foreach ( $crons as $timestamp => $cron ) { |
|
459 |
if ( isset( $cron[$hook][$key] ) ) |
|
460 |
return $cron[$hook][$key]['schedule']; |
|
461 |
} |
|
462 |
return false; |
|
463 |
} |
|
464 |
||
465 |
// |
|
466 |
// Private functions |
|
467 |
// |
|
468 |
||
469 |
/** |
|
470 |
* Retrieve cron info array option. |
|
471 |
* |
|
472 |
* @since 2.1.0 |
|
473 |
* @access private |
|
474 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
* @return false|array CRON info array. |
0 | 476 |
*/ |
477 |
function _get_cron_array() { |
|
478 |
$cron = get_option('cron'); |
|
479 |
if ( ! is_array($cron) ) |
|
480 |
return false; |
|
481 |
||
482 |
if ( !isset($cron['version']) ) |
|
483 |
$cron = _upgrade_cron_array($cron); |
|
484 |
||
485 |
unset($cron['version']); |
|
486 |
||
487 |
return $cron; |
|
488 |
} |
|
489 |
||
490 |
/** |
|
491 |
* Updates the CRON option with the new CRON array. |
|
492 |
* |
|
493 |
* @since 2.1.0 |
|
494 |
* @access private |
|
495 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
* @param array $cron Cron info array from _get_cron_array(). |
0 | 497 |
*/ |
498 |
function _set_cron_array($cron) { |
|
499 |
$cron['version'] = 2; |
|
500 |
update_option( 'cron', $cron ); |
|
501 |
} |
|
502 |
||
503 |
/** |
|
504 |
* Upgrade a Cron info array. |
|
505 |
* |
|
506 |
* This function upgrades the Cron info array to version 2. |
|
507 |
* |
|
508 |
* @since 2.1.0 |
|
509 |
* @access private |
|
510 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* @param array $cron Cron info array from _get_cron_array(). |
0 | 512 |
* @return array An upgraded Cron info array. |
513 |
*/ |
|
514 |
function _upgrade_cron_array($cron) { |
|
515 |
if ( isset($cron['version']) && 2 == $cron['version']) |
|
516 |
return $cron; |
|
517 |
||
518 |
$new_cron = array(); |
|
519 |
||
520 |
foreach ( (array) $cron as $timestamp => $hooks) { |
|
521 |
foreach ( (array) $hooks as $hook => $args ) { |
|
522 |
$key = md5(serialize($args['args'])); |
|
523 |
$new_cron[$timestamp][$hook][$key] = $args; |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
$new_cron['version'] = 2; |
|
528 |
update_option( 'cron', $new_cron ); |
|
529 |
return $new_cron; |
|
530 |
} |