wp/wp-cron.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    18 
    18 
    19 ignore_user_abort( true );
    19 ignore_user_abort( true );
    20 
    20 
    21 /* Don't make the request block till we finish, if possible. */
    21 /* Don't make the request block till we finish, if possible. */
    22 if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
    22 if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
       
    23 	if ( ! headers_sent() ) {
       
    24 		header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
       
    25 		header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
       
    26 	}
       
    27 
    23 	fastcgi_finish_request();
    28 	fastcgi_finish_request();
    24 }
    29 }
    25 
    30 
    26 if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
    31 if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
    27 	die();
    32 	die();
    34  */
    39  */
    35 define( 'DOING_CRON', true );
    40 define( 'DOING_CRON', true );
    36 
    41 
    37 if ( ! defined( 'ABSPATH' ) ) {
    42 if ( ! defined( 'ABSPATH' ) ) {
    38 	/** Set up WordPress environment */
    43 	/** Set up WordPress environment */
    39 	require_once( dirname( __FILE__ ) . '/wp-load.php' );
    44 	require_once __DIR__ . '/wp-load.php';
    40 }
    45 }
    41 
    46 
    42 /**
    47 /**
    43  * Retrieves the cron lock.
    48  * Retrieves the cron lock.
    44  *
    49  *
    79 $gmt_time = microtime( true );
    84 $gmt_time = microtime( true );
    80 
    85 
    81 // The cron lock: a unix timestamp from when the cron was spawned.
    86 // The cron lock: a unix timestamp from when the cron was spawned.
    82 $doing_cron_transient = get_transient( 'doing_cron' );
    87 $doing_cron_transient = get_transient( 'doing_cron' );
    83 
    88 
    84 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
    89 // Use global $doing_wp_cron lock, otherwise use the GET lock. If no lock, try to grab a new lock.
    85 if ( empty( $doing_wp_cron ) ) {
    90 if ( empty( $doing_wp_cron ) ) {
    86 	if ( empty( $_GET['doing_wp_cron'] ) ) {
    91 	if ( empty( $_GET['doing_wp_cron'] ) ) {
    87 		// Called from external script/job. Try setting a lock.
    92 		// Called from external script/job. Try setting a lock.
    88 		if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
    93 		if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
    89 			return;
    94 			return;
    90 		}
    95 		}
    91 		$doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
    96 		$doing_wp_cron        = sprintf( '%.22F', microtime( true ) );
       
    97 		$doing_cron_transient = $doing_wp_cron;
    92 		set_transient( 'doing_cron', $doing_wp_cron );
    98 		set_transient( 'doing_cron', $doing_wp_cron );
    93 	} else {
    99 	} else {
    94 		$doing_wp_cron = $_GET['doing_wp_cron'];
   100 		$doing_wp_cron = $_GET['doing_wp_cron'];
    95 	}
   101 	}
    96 }
   102 }
    97 
   103 
    98 /*
   104 /*
    99  * The cron lock (a unix timestamp set when the cron was spawned),
   105  * The cron lock (a unix timestamp set when the cron was spawned),
   100  * must match $doing_wp_cron (the "key").
   106  * must match $doing_wp_cron (the "key").
   101  */
   107  */
   102 if ( $doing_cron_transient != $doing_wp_cron ) {
   108 if ( $doing_cron_transient !== $doing_wp_cron ) {
   103 	return;
   109 	return;
   104 }
   110 }
   105 
   111 
   106 foreach ( $crons as $timestamp => $cronhooks ) {
   112 foreach ( $crons as $timestamp => $cronhooks ) {
   107 	if ( $timestamp > $gmt_time ) {
   113 	if ( $timestamp > $gmt_time ) {
   112 
   118 
   113 		foreach ( $keys as $k => $v ) {
   119 		foreach ( $keys as $k => $v ) {
   114 
   120 
   115 			$schedule = $v['schedule'];
   121 			$schedule = $v['schedule'];
   116 
   122 
   117 			if ( $schedule != false ) {
   123 			if ( $schedule ) {
   118 				$new_args = array( $timestamp, $schedule, $hook, $v['args'] );
   124 				wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'] );
   119 				call_user_func_array( 'wp_reschedule_event', $new_args );
       
   120 			}
   125 			}
   121 
   126 
   122 			wp_unschedule_event( $timestamp, $hook, $v['args'] );
   127 			wp_unschedule_event( $timestamp, $hook, $v['args'] );
   123 
   128 
   124 			/**
   129 			/**
   131 			 * @param array  $args The arguments to be passed to the hook.
   136 			 * @param array  $args The arguments to be passed to the hook.
   132 			 */
   137 			 */
   133 			do_action_ref_array( $hook, $v['args'] );
   138 			do_action_ref_array( $hook, $v['args'] );
   134 
   139 
   135 			// If the hook ran too long and another cron process stole the lock, quit.
   140 			// If the hook ran too long and another cron process stole the lock, quit.
   136 			if ( _get_cron_lock() != $doing_wp_cron ) {
   141 			if ( _get_cron_lock() !== $doing_wp_cron ) {
   137 				return;
   142 				return;
   138 			}
   143 			}
   139 		}
   144 		}
   140 	}
   145 	}
   141 }
   146 }
   142 
   147 
   143 if ( _get_cron_lock() == $doing_wp_cron ) {
   148 if ( _get_cron_lock() === $doing_wp_cron ) {
   144 	delete_transient( 'doing_cron' );
   149 	delete_transient( 'doing_cron' );
   145 }
   150 }
   146 
   151 
   147 die();
   152 die();