raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress CRON API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 */
mp-wp_genesis 7
mp-wp_genesis 8 /**
mp-wp_genesis 9 * Schedules a hook to run only once.
mp-wp_genesis 10 *
mp-wp_genesis 11 * Schedules a hook which will be executed once by the Wordpress actions core at
mp-wp_genesis 12 * a time which you specify. The action will fire off when someone visits your
mp-wp_genesis 13 * WordPress site, if the schedule time has passed.
mp-wp_genesis 14 *
mp-wp_genesis 15 * @since 2.1.0
mp-wp_genesis 16 * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
mp-wp_genesis 17 *
mp-wp_genesis 18 * @param int $timestamp Timestamp for when to run the event.
mp-wp_genesis 19 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 20 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 21 */
mp-wp_genesis 22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
mp-wp_genesis 23 // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
mp-wp_genesis 24 $next = wp_next_scheduled($hook, $args);
mp-wp_genesis 25 if ( $next && $next <= $timestamp + 600 )
mp-wp_genesis 26 return;
mp-wp_genesis 27
mp-wp_genesis 28 $crons = _get_cron_array();
mp-wp_genesis 29 $key = md5(serialize($args));
mp-wp_genesis 30 $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
mp-wp_genesis 31 uksort( $crons, "strnatcasecmp" );
mp-wp_genesis 32 _set_cron_array( $crons );
mp-wp_genesis 33 }
mp-wp_genesis 34
mp-wp_genesis 35 /**
mp-wp_genesis 36 * Schedule a periodic event.
mp-wp_genesis 37 *
mp-wp_genesis 38 * Schedules a hook which will be executed by the WordPress actions core on a
mp-wp_genesis 39 * specific interval, specified by you. The action will trigger when someone
mp-wp_genesis 40 * visits your WordPress site, if the scheduled time has passed.
mp-wp_genesis 41 *
mp-wp_genesis 42 * @since 2.1.0
mp-wp_genesis 43 *
mp-wp_genesis 44 * @param int $timestamp Timestamp for when to run the event.
mp-wp_genesis 45 * @param string $recurrence How often the event should recur.
mp-wp_genesis 46 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 47 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 48 * @return bool|null False on failure, null when complete with scheduling event.
mp-wp_genesis 49 */
mp-wp_genesis 50 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
mp-wp_genesis 51 $crons = _get_cron_array();
mp-wp_genesis 52 $schedules = wp_get_schedules();
mp-wp_genesis 53 $key = md5(serialize($args));
mp-wp_genesis 54 if ( !isset( $schedules[$recurrence] ) )
mp-wp_genesis 55 return false;
mp-wp_genesis 56 $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
mp-wp_genesis 57 uksort( $crons, "strnatcasecmp" );
mp-wp_genesis 58 _set_cron_array( $crons );
mp-wp_genesis 59 }
mp-wp_genesis 60
mp-wp_genesis 61 /**
mp-wp_genesis 62 * Reschedule a recurring event.
mp-wp_genesis 63 *
mp-wp_genesis 64 * @since 2.1.0
mp-wp_genesis 65 *
mp-wp_genesis 66 * @param int $timestamp Timestamp for when to run the event.
mp-wp_genesis 67 * @param string $recurrence How often the event should recur.
mp-wp_genesis 68 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 69 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 70 * @return bool|null False on failure. Null when event is rescheduled.
mp-wp_genesis 71 */
mp-wp_genesis 72 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
mp-wp_genesis 73 $crons = _get_cron_array();
mp-wp_genesis 74 $schedules = wp_get_schedules();
mp-wp_genesis 75 $key = md5(serialize($args));
mp-wp_genesis 76 $interval = 0;
mp-wp_genesis 77
mp-wp_genesis 78 // First we try to get it from the schedule
mp-wp_genesis 79 if ( 0 == $interval )
mp-wp_genesis 80 $interval = $schedules[$recurrence]['interval'];
mp-wp_genesis 81 // Now we try to get it from the saved interval in case the schedule disappears
mp-wp_genesis 82 if ( 0 == $interval )
mp-wp_genesis 83 $interval = $crons[$timestamp][$hook][$key]['interval'];
mp-wp_genesis 84 // Now we assume something is wrong and fail to schedule
mp-wp_genesis 85 if ( 0 == $interval )
mp-wp_genesis 86 return false;
mp-wp_genesis 87
mp-wp_genesis 88 while ( $timestamp < time() + 1 )
mp-wp_genesis 89 $timestamp += $interval;
mp-wp_genesis 90
mp-wp_genesis 91 wp_schedule_event( $timestamp, $recurrence, $hook, $args );
mp-wp_genesis 92 }
mp-wp_genesis 93
mp-wp_genesis 94 /**
mp-wp_genesis 95 * Unschedule a previously scheduled cron job.
mp-wp_genesis 96 *
mp-wp_genesis 97 * The $timestamp and $hook parameters are required, so that the event can be
mp-wp_genesis 98 * identified.
mp-wp_genesis 99 *
mp-wp_genesis 100 * @since 2.1.0
mp-wp_genesis 101 *
mp-wp_genesis 102 * @param int $timestamp Timestamp for when to run the event.
mp-wp_genesis 103 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 104 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 105 */
mp-wp_genesis 106 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
mp-wp_genesis 107 $crons = _get_cron_array();
mp-wp_genesis 108 $key = md5(serialize($args));
mp-wp_genesis 109 unset( $crons[$timestamp][$hook][$key] );
mp-wp_genesis 110 if ( empty($crons[$timestamp][$hook]) )
mp-wp_genesis 111 unset( $crons[$timestamp][$hook] );
mp-wp_genesis 112 if ( empty($crons[$timestamp]) )
mp-wp_genesis 113 unset( $crons[$timestamp] );
mp-wp_genesis 114 _set_cron_array( $crons );
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 /**
mp-wp_genesis 118 * Unschedule all cron jobs attached to a specific hook.
mp-wp_genesis 119 *
mp-wp_genesis 120 * @since 2.1.0
mp-wp_genesis 121 *
mp-wp_genesis 122 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 123 * @param mixed $args,... Optional. Event arguments.
mp-wp_genesis 124 */
mp-wp_genesis 125 function wp_clear_scheduled_hook( $hook ) {
mp-wp_genesis 126 $args = array_slice( func_get_args(), 1 );
mp-wp_genesis 127
mp-wp_genesis 128 while ( $timestamp = wp_next_scheduled( $hook, $args ) )
mp-wp_genesis 129 wp_unschedule_event( $timestamp, $hook, $args );
mp-wp_genesis 130 }
mp-wp_genesis 131
mp-wp_genesis 132 /**
mp-wp_genesis 133 * Retrieve the next timestamp for a cron event.
mp-wp_genesis 134 *
mp-wp_genesis 135 * @since 2.1.0
mp-wp_genesis 136 *
mp-wp_genesis 137 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 138 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 139 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
mp-wp_genesis 140 */
mp-wp_genesis 141 function wp_next_scheduled( $hook, $args = array() ) {
mp-wp_genesis 142 $crons = _get_cron_array();
mp-wp_genesis 143 $key = md5(serialize($args));
mp-wp_genesis 144 if ( empty($crons) )
mp-wp_genesis 145 return false;
mp-wp_genesis 146 foreach ( $crons as $timestamp => $cron ) {
mp-wp_genesis 147 if ( isset( $cron[$hook][$key] ) )
mp-wp_genesis 148 return $timestamp;
mp-wp_genesis 149 }
mp-wp_genesis 150 return false;
mp-wp_genesis 151 }
mp-wp_genesis 152
mp-wp_genesis 153 /**
mp-wp_genesis 154 * Send request to run cron through HTTP request that doesn't halt page loading.
mp-wp_genesis 155 *
mp-wp_genesis 156 * @since 2.1.0
mp-wp_genesis 157 *
mp-wp_genesis 158 * @return null Cron could not be spawned, because it is not needed to run.
mp-wp_genesis 159 */
mp-wp_genesis 160 function spawn_cron( $local_time ) {
mp-wp_genesis 161
mp-wp_genesis 162 /*
mp-wp_genesis 163 * do not even start the cron if local server timer has drifted
mp-wp_genesis 164 * such as due to power failure, or misconfiguration
mp-wp_genesis 165 */
mp-wp_genesis 166 $timer_accurate = check_server_timer( $local_time );
mp-wp_genesis 167 if ( !$timer_accurate )
mp-wp_genesis 168 return;
mp-wp_genesis 169
mp-wp_genesis 170 //sanity check
mp-wp_genesis 171 $crons = _get_cron_array();
mp-wp_genesis 172 if ( !is_array($crons) )
mp-wp_genesis 173 return;
mp-wp_genesis 174
mp-wp_genesis 175 $keys = array_keys( $crons );
mp-wp_genesis 176 $timestamp = $keys[0];
mp-wp_genesis 177 if ( $timestamp > $local_time )
mp-wp_genesis 178 return;
mp-wp_genesis 179
mp-wp_genesis 180 $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
mp-wp_genesis 181 /*
mp-wp_genesis 182 * multiple processes on multiple web servers can run this code concurrently
mp-wp_genesis 183 * try to make this as atomic as possible by setting doing_cron switch
mp-wp_genesis 184 */
mp-wp_genesis 185 $flag = get_option('doing_cron');
mp-wp_genesis 186
mp-wp_genesis 187 // clean up potential invalid value resulted from various system chaos
mp-wp_genesis 188 if ( $flag != 0 ) {
mp-wp_genesis 189 if ( $flag > $local_time + 10*60 || $flag < $local_time - 10*60 ) {
mp-wp_genesis 190 update_option('doing_cron', 0);
mp-wp_genesis 191 $flag = 0;
mp-wp_genesis 192 }
mp-wp_genesis 193 }
mp-wp_genesis 194
mp-wp_genesis 195 //don't run if another process is currently running it
mp-wp_genesis 196 if ( $flag > $local_time )
mp-wp_genesis 197 return;
mp-wp_genesis 198
mp-wp_genesis 199 update_option( 'doing_cron', $local_time + 30 );
mp-wp_genesis 200
mp-wp_genesis 201 wp_remote_post($cron_url, array('timeout' => 0.01, 'blocking' => false));
mp-wp_genesis 202 }
mp-wp_genesis 203
mp-wp_genesis 204 /**
mp-wp_genesis 205 * Run scheduled callbacks or spawn cron for all scheduled events.
mp-wp_genesis 206 *
mp-wp_genesis 207 * @since 2.1.0
mp-wp_genesis 208 *
mp-wp_genesis 209 * @return null When doesn't need to run Cron.
mp-wp_genesis 210 */
mp-wp_genesis 211 function wp_cron() {
mp-wp_genesis 212
mp-wp_genesis 213 // Prevent infinite loops caused by lack of wp-cron.php
mp-wp_genesis 214 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
mp-wp_genesis 215 return;
mp-wp_genesis 216
mp-wp_genesis 217 $crons = _get_cron_array();
mp-wp_genesis 218
mp-wp_genesis 219 if ( !is_array($crons) )
mp-wp_genesis 220 return;
mp-wp_genesis 221
mp-wp_genesis 222 $keys = array_keys( $crons );
mp-wp_genesis 223 if ( isset($keys[0]) && $keys[0] > time() )
mp-wp_genesis 224 return;
mp-wp_genesis 225
mp-wp_genesis 226 $local_time = time();
mp-wp_genesis 227 $schedules = wp_get_schedules();
mp-wp_genesis 228 foreach ( $crons as $timestamp => $cronhooks ) {
mp-wp_genesis 229 if ( $timestamp > $local_time ) break;
mp-wp_genesis 230 foreach ( (array) $cronhooks as $hook => $args ) {
mp-wp_genesis 231 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
mp-wp_genesis 232 continue;
mp-wp_genesis 233 spawn_cron( $local_time );
mp-wp_genesis 234 break 2;
mp-wp_genesis 235 }
mp-wp_genesis 236 }
mp-wp_genesis 237 }
mp-wp_genesis 238
mp-wp_genesis 239 /**
mp-wp_genesis 240 * Retrieve supported and filtered Cron recurrences.
mp-wp_genesis 241 *
mp-wp_genesis 242 * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
mp-wp_genesis 243 * hooking into the 'cron_schedules' filter. The filter accepts an array of
mp-wp_genesis 244 * arrays. The outer array has a key that is the name of the schedule or for
mp-wp_genesis 245 * example 'weekly'. The value is an array with two keys, one is 'interval' and
mp-wp_genesis 246 * the other is 'display'.
mp-wp_genesis 247 *
mp-wp_genesis 248 * The 'interval' is a number in seconds of when the cron job should run. So for
mp-wp_genesis 249 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
mp-wp_genesis 250 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
mp-wp_genesis 251 *
mp-wp_genesis 252 * The 'display' is the description. For the 'weekly' key, the 'display' would
mp-wp_genesis 253 * be <code>__('Once Weekly')</code>.
mp-wp_genesis 254 *
mp-wp_genesis 255 * For your plugin, you will be passed an array. you can easily add your
mp-wp_genesis 256 * schedule by doing the following.
mp-wp_genesis 257 * <code>
mp-wp_genesis 258 * // filter parameter variable name is 'array'
mp-wp_genesis 259 * $array['weekly'] = array(
mp-wp_genesis 260 * 'interval' => 604800,
mp-wp_genesis 261 * 'display' => __('Once Weekly')
mp-wp_genesis 262 * );
mp-wp_genesis 263 * </code>
mp-wp_genesis 264 *
mp-wp_genesis 265 * @since 2.1.0
mp-wp_genesis 266 *
mp-wp_genesis 267 * @return array
mp-wp_genesis 268 */
mp-wp_genesis 269 function wp_get_schedules() {
mp-wp_genesis 270 $schedules = array(
mp-wp_genesis 271 'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
mp-wp_genesis 272 'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
mp-wp_genesis 273 'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
mp-wp_genesis 274 );
mp-wp_genesis 275 return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
mp-wp_genesis 276 }
mp-wp_genesis 277
mp-wp_genesis 278 /**
mp-wp_genesis 279 * Retrieve Cron schedule for hook with arguments.
mp-wp_genesis 280 *
mp-wp_genesis 281 * @since 2.1.0
mp-wp_genesis 282 *
mp-wp_genesis 283 * @param callback $hook Function or method to call, when cron is run.
mp-wp_genesis 284 * @param array $args Optional. Arguments to pass to the hook function.
mp-wp_genesis 285 * @return string|bool False, if no schedule. Schedule on success.
mp-wp_genesis 286 */
mp-wp_genesis 287 function wp_get_schedule($hook, $args = array()) {
mp-wp_genesis 288 $crons = _get_cron_array();
mp-wp_genesis 289 $key = md5(serialize($args));
mp-wp_genesis 290 if ( empty($crons) )
mp-wp_genesis 291 return false;
mp-wp_genesis 292 foreach ( $crons as $timestamp => $cron ) {
mp-wp_genesis 293 if ( isset( $cron[$hook][$key] ) )
mp-wp_genesis 294 return $cron[$hook][$key]['schedule'];
mp-wp_genesis 295 }
mp-wp_genesis 296 return false;
mp-wp_genesis 297 }
mp-wp_genesis 298
mp-wp_genesis 299 //
mp-wp_genesis 300 // Private functions
mp-wp_genesis 301 //
mp-wp_genesis 302
mp-wp_genesis 303 /**
mp-wp_genesis 304 * Retrieve cron info array option.
mp-wp_genesis 305 *
mp-wp_genesis 306 * @since 2.1.0
mp-wp_genesis 307 * @access private
mp-wp_genesis 308 *
mp-wp_genesis 309 * @return array CRON info array.
mp-wp_genesis 310 */
mp-wp_genesis 311 function _get_cron_array() {
mp-wp_genesis 312 $cron = get_option('cron');
mp-wp_genesis 313 if ( ! is_array($cron) )
mp-wp_genesis 314 return false;
mp-wp_genesis 315
mp-wp_genesis 316 if ( !isset($cron['version']) )
mp-wp_genesis 317 $cron = _upgrade_cron_array($cron);
mp-wp_genesis 318
mp-wp_genesis 319 unset($cron['version']);
mp-wp_genesis 320
mp-wp_genesis 321 return $cron;
mp-wp_genesis 322 }
mp-wp_genesis 323
mp-wp_genesis 324 /**
mp-wp_genesis 325 * Updates the CRON option with the new CRON array.
mp-wp_genesis 326 *
mp-wp_genesis 327 * @since 2.1.0
mp-wp_genesis 328 * @access private
mp-wp_genesis 329 *
mp-wp_genesis 330 * @param array $cron Cron info array from {@link _get_cron_array()}.
mp-wp_genesis 331 */
mp-wp_genesis 332 function _set_cron_array($cron) {
mp-wp_genesis 333 $cron['version'] = 2;
mp-wp_genesis 334 update_option( 'cron', $cron );
mp-wp_genesis 335 }
mp-wp_genesis 336
mp-wp_genesis 337 /**
mp-wp_genesis 338 * Upgrade a Cron info array.
mp-wp_genesis 339 *
mp-wp_genesis 340 * This function upgrades the Cron info array to version 2.
mp-wp_genesis 341 *
mp-wp_genesis 342 * @since 2.1.0
mp-wp_genesis 343 * @access private
mp-wp_genesis 344 *
mp-wp_genesis 345 * @param array $cron Cron info array from {@link _get_cron_array()}.
mp-wp_genesis 346 * @return array An upgraded Cron info array.
mp-wp_genesis 347 */
mp-wp_genesis 348 function _upgrade_cron_array($cron) {
mp-wp_genesis 349 if ( isset($cron['version']) && 2 == $cron['version'])
mp-wp_genesis 350 return $cron;
mp-wp_genesis 351
mp-wp_genesis 352 $new_cron = array();
mp-wp_genesis 353
mp-wp_genesis 354 foreach ( (array) $cron as $timestamp => $hooks) {
mp-wp_genesis 355 foreach ( (array) $hooks as $hook => $args ) {
mp-wp_genesis 356 $key = md5(serialize($args['args']));
mp-wp_genesis 357 $new_cron[$timestamp][$hook][$key] = $args;
mp-wp_genesis 358 }
mp-wp_genesis 359 }
mp-wp_genesis 360
mp-wp_genesis 361 $new_cron['version'] = 2;
mp-wp_genesis 362 update_option( 'cron', $new_cron );
mp-wp_genesis 363 return $new_cron;
mp-wp_genesis 364 }
mp-wp_genesis 365
mp-wp_genesis 366 // stub for checking server timer accuracy, using outside standard time sources
mp-wp_genesis 367 function check_server_timer( $local_time ) {
mp-wp_genesis 368 return true;
mp-wp_genesis 369 }
mp-wp_genesis 370
mp-wp_genesis 371 ?>