wp/wp-cron.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress Cron Implementation for hosts, which do not offer CRON or for which
     3  * A pseudo-CRON daemon for scheduling WordPress tasks
     4  * the user has not set up a CRON job pointing to this file.
     4  *
       
     5  * WP Cron is triggered when the site receives a visit. In the scenario
       
     6  * where a site may not receive enough visits to execute scheduled tasks
       
     7  * in a timely manner, this file can be called directly or via a server
       
     8  * CRON daemon for X number of times.
       
     9  *
       
    10  * Defining DISABLE_WP_CRON as true and calling this file directly are
       
    11  * mutually exclusive and the latter does not rely on the former to work.
     5  *
    12  *
     6  * The HTTP request to this file will not slow down the visitor who happens to
    13  * 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.
    14  * visit when the cron job is needed to run.
     8  *
    15  *
     9  * @package WordPress
    16  * @package WordPress
    32  * Returns the uncached `doing_cron` transient.
    39  * Returns the uncached `doing_cron` transient.
    33  *
    40  *
    34  * @ignore
    41  * @ignore
    35  * @since 3.3.0
    42  * @since 3.3.0
    36  *
    43  *
       
    44  * @global wpdb $wpdb WordPress database abstraction object.
       
    45  *
    37  * @return string|false Value of the `doing_cron` transient, 0|false otherwise.
    46  * @return string|false Value of the `doing_cron` transient, 0|false otherwise.
    38  */
    47  */
    39 function _get_cron_lock() {
    48 function _get_cron_lock() {
    40 	global $wpdb;
    49 	global $wpdb;
    41 
    50 
    62 $gmt_time = microtime( true );
    71 $gmt_time = microtime( true );
    63 
    72 
    64 if ( isset($keys[0]) && $keys[0] > $gmt_time )
    73 if ( isset($keys[0]) && $keys[0] > $gmt_time )
    65 	die();
    74 	die();
    66 
    75 
    67 $doing_cron_transient = get_transient( 'doing_cron');
    76 
       
    77 // The cron lock: a unix timestamp from when the cron was spawned.
       
    78 $doing_cron_transient = get_transient( 'doing_cron' );
    68 
    79 
    69 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
    80 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
    70 if ( empty( $doing_wp_cron ) ) {
    81 if ( empty( $doing_wp_cron ) ) {
    71 	if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
    82 	if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
    72 		// Called from external script/job. Try setting a lock.
    83 		// Called from external script/job. Try setting a lock.
    77 	} else {
    88 	} else {
    78 		$doing_wp_cron = $_GET[ 'doing_wp_cron' ];
    89 		$doing_wp_cron = $_GET[ 'doing_wp_cron' ];
    79 	}
    90 	}
    80 }
    91 }
    81 
    92 
    82 // Check lock
    93 /*
       
    94  * The cron lock (a unix timestamp set when the cron was spawned),
       
    95  * must match $doing_wp_cron (the "key").
       
    96  */
    83 if ( $doing_cron_transient != $doing_wp_cron )
    97 if ( $doing_cron_transient != $doing_wp_cron )
    84 	return;
    98 	return;
    85 
    99 
    86 foreach ( $crons as $timestamp => $cronhooks ) {
   100 foreach ( $crons as $timestamp => $cronhooks ) {
    87 	if ( $timestamp > $gmt_time )
   101 	if ( $timestamp > $gmt_time )