[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

/includes/ -> functions.wp-cron.php (source)

   1  <?php
   2  // Last sync [WP12462]
   3  
   4  /**
   5   * WordPress CRON API
   6   *
   7   * @package WordPress
   8   */
   9  
  10  /**
  11   * Schedules a hook to run only once.
  12   *
  13   * Schedules a hook which will be executed once by the WordPress actions core at
  14   * a time which you specify. The action will fire off when someone visits your
  15   * WordPress site, if the schedule time has passed.
  16   *
  17   * @since 2.1.0
  18   * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
  19   *
  20   * @param int $timestamp Timestamp for when to run the event.
  21   * @param string $hook Action hook to execute when cron is run.
  22   * @param array $args Optional. Arguments to pass to the hook's callback function.
  23   */
  24  function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
  25      // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
  26      $next = wp_next_scheduled($hook, $args);
  27      if ( $next && $next <= $timestamp + 600 )
  28          return;
  29  
  30      $crons = _get_cron_array();
  31      $key = md5(serialize($args));
  32      $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
  33      uksort( $crons, "strnatcasecmp" );
  34      _set_cron_array( $crons );
  35  }
  36  
  37  /**
  38   * Schedule a periodic event.
  39   *
  40   * Schedules a hook which will be executed by the WordPress actions core on a
  41   * specific interval, specified by you. The action will trigger when someone
  42   * visits your WordPress site, if the scheduled time has passed.
  43   *
  44   * Valid values for the recurrence are hourly, daily and twicedaily.  These can
  45   * be extended using the cron_schedules filter in wp_get_schedules().
  46   *
  47   * @since 2.1.0
  48   *
  49   * @param int $timestamp Timestamp for when to run the event.
  50   * @param string $recurrence How often the event should recur.
  51   * @param string $hook Action hook to execute when cron is run.
  52   * @param array $args Optional. Arguments to pass to the hook's callback function.
  53   * @return bool|null False on failure, null when complete with scheduling event.
  54   */
  55  function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  56      $crons = _get_cron_array();
  57      $schedules = wp_get_schedules();
  58      $key = md5(serialize($args));
  59      if ( !isset( $schedules[$recurrence] ) )
  60          return false;
  61      $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
  62      uksort( $crons, "strnatcasecmp" );
  63      _set_cron_array( $crons );
  64  }
  65  
  66  /**
  67   * Reschedule a recurring event.
  68   *
  69   * @since 2.1.0
  70   *
  71   * @param int $timestamp Timestamp for when to run the event.
  72   * @param string $recurrence How often the event should recur.
  73   * @param string $hook Action hook to execute when cron is run.
  74   * @param array $args Optional. Arguments to pass to the hook's callback function.
  75   * @return bool|null False on failure. Null when event is rescheduled.
  76   */
  77  function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  78      $crons = _get_cron_array();
  79      $schedules = wp_get_schedules();
  80      $key = md5(serialize($args));
  81      $interval = 0;
  82  
  83      // First we try to get it from the schedule
  84      if ( 0 == $interval )
  85          $interval = $schedules[$recurrence]['interval'];
  86      // Now we try to get it from the saved interval in case the schedule disappears
  87      if ( 0 == $interval )
  88          $interval = $crons[$timestamp][$hook][$key]['interval'];
  89      // Now we assume something is wrong and fail to schedule
  90      if ( 0 == $interval )
  91          return false;
  92  
  93      $now = time();
  94  
  95      if ( $timestamp >= $now )
  96          $timestamp = $now + $interval;
  97      else
  98          $timestamp = $now + ($interval - (($now - $timestamp) % $interval));
  99  
 100      wp_schedule_event( $timestamp, $recurrence, $hook, $args );
 101  }
 102  
 103  /**
 104   * Unschedule a previously scheduled cron job.
 105   *
 106   * The $timestamp and $hook parameters are required, so that the event can be
 107   * identified.
 108   *
 109   * @since 2.1.0
 110   *
 111   * @param int $timestamp Timestamp for when to run the event.
 112   * @param string $hook Action hook, the execution of which will be unscheduled.
 113   * @param array $args Arguments to pass to the hook's callback function.
 114   * Although not passed to a callback function, these arguments are used
 115   * to uniquely identify the scheduled event, so they should be the same
 116   * as those used when originally scheduling the event.
 117   */
 118  function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
 119      $crons = _get_cron_array();
 120      $key = md5(serialize($args));
 121      unset( $crons[$timestamp][$hook][$key] );
 122      if ( empty($crons[$timestamp][$hook]) )
 123          unset( $crons[$timestamp][$hook] );
 124      if ( empty($crons[$timestamp]) )
 125          unset( $crons[$timestamp] );
 126      _set_cron_array( $crons );
 127  }
 128  
 129  /**
 130   * Unschedule all cron jobs attached to a specific hook.
 131   *
 132   * @since 2.1.0
 133   *
 134   * @param string $hook Action hook, the execution of which will be unscheduled.
 135   * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
 136   */
 137  function wp_clear_scheduled_hook( $hook, $args = array() ) {
 138      // Backward compatibility
 139      // Previously this function took the arguments as discrete vars rather than an array like the rest of the API
 140      if ( !is_array($args) )
 141          $args = array_slice( func_get_args(), 1 );
 142  
 143      while ( $timestamp = wp_next_scheduled( $hook, $args ) )
 144          wp_unschedule_event( $timestamp, $hook, $args );
 145  }
 146  
 147  /**
 148   * Retrieve the next timestamp for a cron event.
 149   *
 150   * @since 2.1.0
 151   *
 152   * @param string $hook Action hook to execute when cron is run.
 153   * @param array $args Optional. Arguments to pass to the hook's callback function.
 154   * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
 155   */
 156  function wp_next_scheduled( $hook, $args = array() ) {
 157      $crons = _get_cron_array();
 158      $key = md5(serialize($args));
 159      if ( empty($crons) )
 160          return false;
 161      foreach ( $crons as $timestamp => $cron ) {
 162          if ( isset( $cron[$hook][$key] ) )
 163              return $timestamp;
 164      }
 165      return false;
 166  }
 167  
 168  /**
 169   * Send request to run cron through HTTP request that doesn't halt page loading.
 170   *
 171   * @since 2.1.0
 172   *
 173   * @return null Cron could not be spawned, because it is not needed to run.
 174   */
 175  function spawn_cron( $local_time = 0 ) {
 176  
 177      if ( !$local_time )
 178          $local_time = time();
 179  
 180      if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
 181          return;
 182  
 183      /*
 184       * do not even start the cron if local server timer has drifted
 185       * such as due to power failure, or misconfiguration
 186       */
 187      $timer_accurate = check_server_timer( $local_time );
 188      if ( !$timer_accurate )
 189          return;
 190  
 191      /*
 192      * multiple processes on multiple web servers can run this code concurrently
 193      * try to make this as atomic as possible by setting doing_cron switch
 194      */
 195      $flag = backpress_get_transient('doing_cron');
 196  
 197      if ( $flag > $local_time + 10*60 )
 198          $flag = 0;
 199  
 200      // don't run if another process is currently running it or more than once every 60 sec.
 201      if ( $flag + 60 > $local_time )
 202          return;
 203  
 204      //sanity check
 205      $crons = _get_cron_array();
 206      if ( !is_array($crons) )
 207          return;
 208  
 209      $keys = array_keys( $crons );
 210      if ( isset($keys[0]) && $keys[0] > $local_time )
 211          return;
 212  
 213      if ( defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON ) {
 214          if ( !empty($_POST) || defined('DOING_AJAX') )
 215              return;
 216  
 217          backpress_set_transient( 'doing_cron', $local_time );
 218  
 219          ob_start();
 220          wp_redirect( add_query_arg('doing_wp_cron', '', stripslashes($_SERVER['REQUEST_URI'])) );
 221          echo ' ';
 222  
 223          // flush any buffers and send the headers
 224          while ( @ob_end_flush() );
 225          flush();
 226  
 227          @include_once(ABSPATH . 'wp-cron.php');
 228          return;
 229      }
 230  
 231      backpress_set_transient( 'doing_cron', $local_time );
 232  
 233      $cron_url = remove_query_arg( 'check', backpress_get_option( 'cron_uri' ) );
 234      $cron_url = add_query_arg( 'doing_wp_cron', '', $cron_url );
 235      wp_remote_post( $cron_url, array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)) );
 236  }
 237  
 238  /**
 239   * Run scheduled callbacks or spawn cron for all scheduled events.
 240   *
 241   * @since 2.1.0
 242   *
 243   * @return null When doesn't need to run Cron.
 244   */
 245  function wp_cron() {
 246      // Prevent infinite loops caused by cron page requesting itself
 247      $cron_uri = parse_url( backpress_get_option( 'cron_uri' ) );
 248  
 249      if ( strpos($_SERVER['REQUEST_URI'], $cron_uri['path'] ) !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
 250          return;
 251  
 252      if ( false === $crons = _get_cron_array() )
 253          return;
 254  
 255      $local_time = time();
 256      $keys = array_keys( $crons );
 257      if ( isset($keys[0]) && $keys[0] > $local_time )
 258          return;
 259  
 260      $schedules = wp_get_schedules();
 261      foreach ( $crons as $timestamp => $cronhooks ) {
 262          if ( $timestamp > $local_time ) break;
 263          foreach ( (array) $cronhooks as $hook => $args ) {
 264              if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
 265                  continue;
 266              spawn_cron( $local_time );
 267              break 2;
 268          }
 269      }
 270  }
 271  
 272  /**
 273   * Retrieve supported and filtered Cron recurrences.
 274   *
 275   * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
 276   * hooking into the 'cron_schedules' filter. The filter accepts an array of
 277   * arrays. The outer array has a key that is the name of the schedule or for
 278   * example 'weekly'. The value is an array with two keys, one is 'interval' and
 279   * the other is 'display'.
 280   *
 281   * The 'interval' is a number in seconds of when the cron job should run. So for
 282   * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
 283   * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
 284   *
 285   * The 'display' is the description. For the 'weekly' key, the 'display' would
 286   * be <code>__('Once Weekly')</code>.
 287   *
 288   * For your plugin, you will be passed an array. you can easily add your
 289   * schedule by doing the following.
 290   * <code>
 291   * // filter parameter variable name is 'array'
 292   *    $array['weekly'] = array(
 293   *        'interval' => 604800,
 294   *        'display' => __('Once Weekly')
 295   *    );
 296   * </code>
 297   *
 298   * @since 2.1.0
 299   *
 300   * @return array
 301   */
 302  function wp_get_schedules() {
 303      $schedules = array(
 304          'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
 305          'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
 306          'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
 307      );
 308      return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
 309  }
 310  
 311  /**
 312   * Retrieve Cron schedule for hook with arguments.
 313   *
 314   * @since 2.1.0
 315   *
 316   * @param string $hook Action hook to execute when cron is run.
 317   * @param array $args Optional. Arguments to pass to the hook's callback function.
 318   * @return string|bool False, if no schedule. Schedule on success.
 319   */
 320  function wp_get_schedule($hook, $args = array()) {
 321      $crons = _get_cron_array();
 322      $key = md5(serialize($args));
 323      if ( empty($crons) )
 324          return false;
 325      foreach ( $crons as $timestamp => $cron ) {
 326          if ( isset( $cron[$hook][$key] ) )
 327              return $cron[$hook][$key]['schedule'];
 328      }
 329      return false;
 330  }
 331  
 332  //
 333  // Private functions
 334  //
 335  
 336  /**
 337   * Retrieve cron info array option.
 338   *
 339   * @since 2.1.0
 340   * @access private
 341   *
 342   * @return array CRON info array.
 343   */
 344  function _get_cron_array()  {
 345      $cron = backpress_get_option('cron');
 346      if ( ! is_array($cron) )
 347          return false;
 348  
 349      if ( !isset($cron['version']) )
 350          $cron = _upgrade_cron_array($cron);
 351  
 352      unset($cron['version']);
 353  
 354      return $cron;
 355  }
 356  
 357  /**
 358   * Updates the CRON option with the new CRON array.
 359   *
 360   * @since 2.1.0
 361   * @access private
 362   *
 363   * @param array $cron Cron info array from {@link _get_cron_array()}.
 364   */
 365  function _set_cron_array($cron) {
 366      $cron['version'] = 2;
 367      backpress_update_option( 'cron', $cron );
 368  }
 369  
 370  /**
 371   * Upgrade a Cron info array.
 372   *
 373   * This function upgrades the Cron info array to version 2.
 374   *
 375   * @since 2.1.0
 376   * @access private
 377   *
 378   * @param array $cron Cron info array from {@link _get_cron_array()}.
 379   * @return array An upgraded Cron info array.
 380   */
 381  function _upgrade_cron_array($cron) {
 382      if ( isset($cron['version']) && 2 == $cron['version'])
 383          return $cron;
 384  
 385      $new_cron = array();
 386  
 387      foreach ( (array) $cron as $timestamp => $hooks) {
 388          foreach ( (array) $hooks as $hook => $args ) {
 389              $key = md5(serialize($args['args']));
 390              $new_cron[$timestamp][$hook][$key] = $args;
 391          }
 392      }
 393  
 394      $new_cron['version'] = 2;
 395      backpress_update_option( 'cron', $new_cron );
 396      return $new_cron;
 397  }
 398  
 399  // stub for checking server timer accuracy, using outside standard time sources
 400  function check_server_timer( $local_time ) {
 401      return true;
 402  }


Generated: Tue Mar 19 01:01:07 2024 Cross-referenced by PHPXref 0.7.1