wp-cron.php 3.6 KB

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