[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/ -> wp-cron.php (source)

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


Generated: Sun Jun 16 01:00:07 2019 Cross-referenced by PHPXref 0.7.1