[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/ -> wp-cron.php (source)

   1  <?php
   2  /**
   3   * WordPress Cron Implementation for hosts, which do not offer CRON or for which
   4   * the user has not set up a CRON job pointing to this file.
   5   *
   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.
   8   *
   9   * @package WordPress
  10   */
  11  
  12  ignore_user_abort(true);
  13  
  14  if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
  15      die();
  16  
  17  /**
  18   * Tell WordPress we are doing the CRON task.
  19   *
  20   * @var bool
  21   */
  22  define('DOING_CRON', true);
  23  
  24  if ( !defined('ABSPATH') ) {
  25      /** Set up WordPress environment */
  26      require_once( dirname( __FILE__ ) . '/wp-load.php' );
  27  }
  28  
  29  /**
  30   * Retrieves the cron lock.
  31   *
  32   * Returns the uncached `doing_cron` transient.
  33   *
  34   * @ignore
  35   * @since 3.3.0
  36   *
  37   * @return string|false Value of the `doing_cron` transient, 0|false otherwise.
  38   */
  39  function _get_cron_lock() {
  40      global $wpdb;
  41  
  42      $value = 0;
  43      if ( wp_using_ext_object_cache() ) {
  44          /*
  45           * Skip local cache and force re-fetch of doing_cron transient
  46           * in case another process updated the cache.
  47           */
  48          $value = wp_cache_get( 'doing_cron', 'transient', true );
  49      } else {
  50          $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
  51          if ( is_object( $row ) )
  52              $value = $row->option_value;
  53      }
  54  
  55      return $value;
  56  }
  57  
  58  if ( false === $crons = _get_cron_array() )
  59      die();
  60  
  61  $keys = array_keys( $crons );
  62  $gmt_time = microtime( true );
  63  
  64  if ( isset($keys[0]) && $keys[0] > $gmt_time )
  65      die();
  66  
  67  
  68  // The cron lock: a unix timestamp from when the cron was spawned.
  69  $doing_cron_transient = get_transient( 'doing_cron' );
  70  
  71  // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
  72  if ( empty( $doing_wp_cron ) ) {
  73      if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
  74          // Called from external script/job. Try setting a lock.
  75          if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) )
  76              return;
  77          $doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
  78          set_transient( 'doing_cron', $doing_wp_cron );
  79      } else {
  80          $doing_wp_cron = $_GET[ 'doing_wp_cron' ];
  81      }
  82  }
  83  
  84  /*
  85   * The cron lock (a unix timestamp set when the cron was spawned),
  86   * must match $doing_wp_cron (the "key").
  87   */
  88  if ( $doing_cron_transient != $doing_wp_cron )
  89      return;
  90  
  91  foreach ( $crons as $timestamp => $cronhooks ) {
  92      if ( $timestamp > $gmt_time )
  93          break;
  94  
  95      foreach ( $cronhooks as $hook => $keys ) {
  96  
  97          foreach ( $keys as $k => $v ) {
  98  
  99              $schedule = $v['schedule'];
 100  
 101              if ( $schedule != false ) {
 102                  $new_args = array($timestamp, $schedule, $hook, $v['args']);
 103                  call_user_func_array('wp_reschedule_event', $new_args);
 104              }
 105  
 106              wp_unschedule_event( $timestamp, $hook, $v['args'] );
 107  
 108              /**
 109               * Fires scheduled events.
 110               *
 111               * @ignore
 112               * @since 2.1.0
 113               *
 114               * @param string $hook Name of the hook that was scheduled to be fired.
 115               * @param array  $args The arguments to be passed to the hook.
 116               */
 117               do_action_ref_array( $hook, $v['args'] );
 118  
 119              // If the hook ran too long and another cron process stole the lock, quit.
 120              if ( _get_cron_lock() != $doing_wp_cron )
 121                  return;
 122          }
 123      }
 124  }
 125  
 126  if ( _get_cron_lock() == $doing_wp_cron )
 127      delete_transient( 'doing_cron' );
 128  
 129  die();


Generated: Sun Mar 26 01:00:24 2017 Cross-referenced by PHPXref 0.7.1