web/wp-includes/cron.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    20  * @param array $args Optional. Arguments to pass to the hook's callback function.
    20  * @param array $args Optional. Arguments to pass to the hook's callback function.
    21  */
    21  */
    22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
    22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
    23 	// don't schedule a duplicate if there's already an identical event due in the next 10 minutes
    23 	// don't schedule a duplicate if there's already an identical event due in the next 10 minutes
    24 	$next = wp_next_scheduled($hook, $args);
    24 	$next = wp_next_scheduled($hook, $args);
    25 	if ( $next && $next <= $timestamp + 600 )
    25 	if ( $next && $next <= $timestamp + 10 * MINUTE_IN_SECONDS )
    26 		return;
    26 		return;
    27 
    27 
    28 	$crons = _get_cron_array();
    28 	$crons = _get_cron_array();
    29 	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
    29 	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
    30 	$event = apply_filters('schedule_event', $event);
    30 	$event = apply_filters('schedule_event', $event);
   190  *
   190  *
   191  * @since 2.1.0
   191  * @since 2.1.0
   192  *
   192  *
   193  * @return null Cron could not be spawned, because it is not needed to run.
   193  * @return null Cron could not be spawned, because it is not needed to run.
   194  */
   194  */
   195 function spawn_cron( $local_time = 0 ) {
   195 function spawn_cron( $gmt_time = 0 ) {
   196 
   196 
   197 	if ( ! $local_time )
   197 	if ( ! $gmt_time )
   198 		$local_time = microtime( true );
   198 		$gmt_time = microtime( true );
   199 
   199 
   200 	if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
   200 	if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
   201 		return;
   201 		return;
   202 
   202 
   203 	/*
   203 	/*
   204 	* multiple processes on multiple web servers can run this code concurrently
   204 	* multiple processes on multiple web servers can run this code concurrently
   205 	* try to make this as atomic as possible by setting doing_cron switch
   205 	* try to make this as atomic as possible by setting doing_cron switch
   206 	*/
   206 	*/
   207 	$lock = get_transient('doing_cron');
   207 	$lock = get_transient('doing_cron');
   208 
   208 
   209 	if ( $lock > $local_time + 10*60 )
   209 	if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
   210 		$lock = 0;
   210 		$lock = 0;
   211 
   211 
   212 	// don't run if another process is currently running it or more than once every 60 sec.
   212 	// don't run if another process is currently running it or more than once every 60 sec.
   213 	if ( $lock + WP_CRON_LOCK_TIMEOUT > $local_time )
   213 	if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
   214 		return;
   214 		return;
   215 
   215 
   216 	//sanity check
   216 	//sanity check
   217 	$crons = _get_cron_array();
   217 	$crons = _get_cron_array();
   218 	if ( !is_array($crons) )
   218 	if ( !is_array($crons) )
   219 		return;
   219 		return;
   220 
   220 
   221 	$keys = array_keys( $crons );
   221 	$keys = array_keys( $crons );
   222 	if ( isset($keys[0]) && $keys[0] > $local_time )
   222 	if ( isset($keys[0]) && $keys[0] > $gmt_time )
   223 		return;
   223 		return;
   224 
   224 
   225 	if ( defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON ) {
   225 	if ( defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON ) {
   226 		if ( !empty($_POST) || defined('DOING_AJAX') )
   226 		if ( !empty($_POST) || defined('DOING_AJAX') )
   227 			return;
   227 			return;
   228 
   228 
   229 		$doing_wp_cron = sprintf( '%.22F', $local_time );
   229 		$doing_wp_cron = sprintf( '%.22F', $gmt_time );
   230 		set_transient( 'doing_cron', $doing_wp_cron );
   230 		set_transient( 'doing_cron', $doing_wp_cron );
   231 
   231 
   232 		ob_start();
   232 		ob_start();
   233 		wp_redirect( add_query_arg('doing_wp_cron', $doing_wp_cron, stripslashes($_SERVER['REQUEST_URI'])) );
   233 		wp_redirect( add_query_arg('doing_wp_cron', $doing_wp_cron, stripslashes($_SERVER['REQUEST_URI'])) );
   234 		echo ' ';
   234 		echo ' ';
   239 
   239 
   240 		WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
   240 		WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
   241 		return;
   241 		return;
   242 	}
   242 	}
   243 
   243 
   244 	$doing_wp_cron = sprintf( '%.22F', $local_time );
   244 	$doing_wp_cron = sprintf( '%.22F', $gmt_time );
   245 	set_transient( 'doing_cron', $doing_wp_cron );
   245 	set_transient( 'doing_cron', $doing_wp_cron );
   246 
   246 
   247 	$cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
   247 	$cron_request = apply_filters( 'cron_request', array(
   248 	wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
   248 		'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
       
   249 		'key' => $doing_wp_cron,
       
   250 		'args' => array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) )
       
   251 	) );
       
   252 
       
   253 	wp_remote_post( $cron_request['url'], $cron_request['args'] );
   249 }
   254 }
   250 
   255 
   251 /**
   256 /**
   252  * Run scheduled callbacks or spawn cron for all scheduled events.
   257  * Run scheduled callbacks or spawn cron for all scheduled events.
   253  *
   258  *
   262 		return;
   267 		return;
   263 
   268 
   264 	if ( false === $crons = _get_cron_array() )
   269 	if ( false === $crons = _get_cron_array() )
   265 		return;
   270 		return;
   266 
   271 
   267 	$local_time = microtime( true );
   272 	$gmt_time = microtime( true );
   268 	$keys = array_keys( $crons );
   273 	$keys = array_keys( $crons );
   269 	if ( isset($keys[0]) && $keys[0] > $local_time )
   274 	if ( isset($keys[0]) && $keys[0] > $gmt_time )
   270 		return;
   275 		return;
   271 
   276 
   272 	$schedules = wp_get_schedules();
   277 	$schedules = wp_get_schedules();
   273 	foreach ( $crons as $timestamp => $cronhooks ) {
   278 	foreach ( $crons as $timestamp => $cronhooks ) {
   274 		if ( $timestamp > $local_time ) break;
   279 		if ( $timestamp > $gmt_time ) break;
   275 		foreach ( (array) $cronhooks as $hook => $args ) {
   280 		foreach ( (array) $cronhooks as $hook => $args ) {
   276 			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
   281 			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
   277 				continue;
   282 				continue;
   278 			spawn_cron( $local_time );
   283 			spawn_cron( $gmt_time );
   279 			break 2;
   284 			break 2;
   280 		}
   285 		}
   281 	}
   286 	}
   282 }
   287 }
   283 
   288 
   311  *
   316  *
   312  * @return array
   317  * @return array
   313  */
   318  */
   314 function wp_get_schedules() {
   319 function wp_get_schedules() {
   315 	$schedules = array(
   320 	$schedules = array(
   316 		'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
   321 		'hourly'     => array( 'interval' => HOUR_IN_SECONDS,      'display' => __( 'Once Hourly' ) ),
   317 		'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
   322 		'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
   318 		'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
   323 		'daily'      => array( 'interval' => DAY_IN_SECONDS,       'display' => __( 'Once Daily' ) ),
   319 	);
   324 	);
   320 	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
   325 	return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
   321 }
   326 }
   322 
   327 
   323 /**
   328 /**