web/wp-cron.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress Cron Implementation for hosts, which do not offer CRON or for which
     3  * WordPress Cron Implementation for hosts, which do not offer CRON or for which
     4  * the user has not setup a CRON job pointing to this file.
     4  * the user has not set up a CRON job pointing to this file.
     5  *
     5  *
     6  * The HTTP request to this file will not slow down the visitor who happens to
     6  * The HTTP request to this file will not slow down the visitor who happens to
     7  * visit when the cron job is needed to run.
     7  * visit when the cron job is needed to run.
     8  *
     8  *
     9  * @package WordPress
     9  * @package WordPress
    20  * @var bool
    20  * @var bool
    21  */
    21  */
    22 define('DOING_CRON', true);
    22 define('DOING_CRON', true);
    23 
    23 
    24 if ( !defined('ABSPATH') ) {
    24 if ( !defined('ABSPATH') ) {
    25 	/** Setup WordPress environment */
    25 	/** Set up WordPress environment */
    26 	require_once('./wp-load.php');
    26 	require_once('./wp-load.php');
       
    27 }
       
    28 
       
    29 // Uncached doing_cron transient fetch
       
    30 function _get_cron_lock() {
       
    31 	global $_wp_using_ext_object_cache, $wpdb;
       
    32 
       
    33 	$value = 0;
       
    34 	if ( $_wp_using_ext_object_cache ) {
       
    35 		// Skip local cache and force refetch of doing_cron transient in case
       
    36 		// another processs updated the cache
       
    37 		$value = wp_cache_get( 'doing_cron', 'transient', true );
       
    38 	} else {
       
    39 		$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
       
    40 		if ( is_object( $row ) )
       
    41 			$value = $row->option_value;
       
    42 	}
       
    43 
       
    44 	return $value;
    27 }
    45 }
    28 
    46 
    29 if ( false === $crons = _get_cron_array() )
    47 if ( false === $crons = _get_cron_array() )
    30 	die();
    48 	die();
    31 
    49 
    32 $keys = array_keys( $crons );
    50 $keys = array_keys( $crons );
    33 $local_time = time();
    51 $local_time = microtime( true );
    34 
    52 
    35 if ( isset($keys[0]) && $keys[0] > $local_time )
    53 if ( isset($keys[0]) && $keys[0] > $local_time )
    36 	die();
    54 	die();
    37 
    55 
    38 foreach ($crons as $timestamp => $cronhooks) {
    56 $doing_cron_transient = get_transient( 'doing_cron');
       
    57 
       
    58 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
       
    59 if ( empty( $doing_wp_cron ) ) {
       
    60 	if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
       
    61 		// Called from external script/job. Try setting a lock.
       
    62 		if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $local_time ) )
       
    63 			return;
       
    64 		$doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
       
    65 		set_transient( 'doing_cron', $doing_wp_cron );
       
    66 	} else {
       
    67 		$doing_wp_cron = $_GET[ 'doing_wp_cron' ];
       
    68 	}
       
    69 }
       
    70 
       
    71 // Check lock
       
    72 if ( $doing_cron_transient != $doing_wp_cron )
       
    73 	return;
       
    74 
       
    75 foreach ( $crons as $timestamp => $cronhooks ) {
    39 	if ( $timestamp > $local_time )
    76 	if ( $timestamp > $local_time )
    40 		break;
    77 		break;
    41 
    78 
    42 	foreach ($cronhooks as $hook => $keys) {
    79 	foreach ( $cronhooks as $hook => $keys ) {
    43 
    80 
    44 		foreach ($keys as $k => $v) {
    81 		foreach ( $keys as $k => $v ) {
    45 
    82 
    46 			$schedule = $v['schedule'];
    83 			$schedule = $v['schedule'];
    47 
    84 
    48 			if ($schedule != false) {
    85 			if ( $schedule != false ) {
    49 				$new_args = array($timestamp, $schedule, $hook, $v['args']);
    86 				$new_args = array($timestamp, $schedule, $hook, $v['args']);
    50 				call_user_func_array('wp_reschedule_event', $new_args);
    87 				call_user_func_array('wp_reschedule_event', $new_args);
    51 			}
    88 			}
    52 
    89 
    53 			wp_unschedule_event($timestamp, $hook, $v['args']);
    90 			wp_unschedule_event( $timestamp, $hook, $v['args'] );
    54 
    91 
    55  			do_action_ref_array($hook, $v['args']);
    92  			do_action_ref_array( $hook, $v['args'] );
       
    93 
       
    94 			// If the hook ran too long and another cron process stole the lock, quit.
       
    95 			if ( _get_cron_lock() != $doing_wp_cron )
       
    96 				return;
    56 		}
    97 		}
    57 	}
    98 	}
    58 }
    99 }
    59 
   100 
       
   101 if ( _get_cron_lock() == $doing_wp_cron )
       
   102 	delete_transient( 'doing_cron' );
       
   103 
    60 die();
   104 die();