wp-cron.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 a scheduled cron event runs.
  15. *
  16. * @package WordPress
  17. */
  18. ignore_user_abort( true );
  19. if ( ! headers_sent() ) {
  20. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
  21. header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
  22. }
  23. /* Don't make the request block till we finish, if possible. */
  24. if ( PHP_VERSION_ID >= 70016 && function_exists( 'fastcgi_finish_request' ) ) {
  25. fastcgi_finish_request();
  26. } elseif ( function_exists( 'litespeed_finish_request' ) ) {
  27. litespeed_finish_request();
  28. }
  29. if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
  30. die();
  31. }
  32. /**
  33. * Tell WordPress we are doing the cron task.
  34. *
  35. * @var bool
  36. */
  37. define( 'DOING_CRON', true );
  38. if ( ! defined( 'ABSPATH' ) ) {
  39. /** Set up WordPress environment */
  40. require_once __DIR__ . '/wp-load.php';
  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|int|false Value of the `doing_cron` transient, 0|false otherwise.
  53. */
  54. function _get_cron_lock() {
  55. global $wpdb;
  56. $value = 0;
  57. if ( wp_using_ext_object_cache() ) {
  58. /*
  59. * Skip local cache and force re-fetch of doing_cron transient
  60. * in case another process updated the cache.
  61. */
  62. $value = wp_cache_get( 'doing_cron', 'transient', true );
  63. } else {
  64. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
  65. if ( is_object( $row ) ) {
  66. $value = $row->option_value;
  67. }
  68. }
  69. return $value;
  70. }
  71. $crons = wp_get_ready_cron_jobs();
  72. if ( empty( $crons ) ) {
  73. die();
  74. }
  75. $gmt_time = microtime( true );
  76. // The cron lock: a unix timestamp from when the cron was spawned.
  77. $doing_cron_transient = get_transient( 'doing_cron' );
  78. // Use global $doing_wp_cron lock, otherwise use the GET lock. If no lock, try to grab a new lock.
  79. if ( empty( $doing_wp_cron ) ) {
  80. if ( empty( $_GET['doing_wp_cron'] ) ) {
  81. // Called from external script/job. Try setting a lock.
  82. if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
  83. return;
  84. }
  85. $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
  86. $doing_cron_transient = $doing_wp_cron;
  87. set_transient( 'doing_cron', $doing_wp_cron );
  88. } else {
  89. $doing_wp_cron = $_GET['doing_wp_cron'];
  90. }
  91. }
  92. /*
  93. * The cron lock (a unix timestamp set when the cron was spawned),
  94. * must match $doing_wp_cron (the "key").
  95. */
  96. if ( $doing_cron_transient !== $doing_wp_cron ) {
  97. return;
  98. }
  99. foreach ( $crons as $timestamp => $cronhooks ) {
  100. if ( $timestamp > $gmt_time ) {
  101. break;
  102. }
  103. foreach ( $cronhooks as $hook => $keys ) {
  104. foreach ( $keys as $k => $v ) {
  105. $schedule = $v['schedule'];
  106. if ( $schedule ) {
  107. $result = wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'], true );
  108. if ( is_wp_error( $result ) ) {
  109. error_log(
  110. sprintf(
  111. /* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */
  112. __( 'Cron reschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ),
  113. $hook,
  114. $result->get_error_code(),
  115. $result->get_error_message(),
  116. wp_json_encode( $v )
  117. )
  118. );
  119. /**
  120. * Fires when an error happens rescheduling a cron event.
  121. *
  122. * @since 6.1.0
  123. *
  124. * @param WP_Error $result The WP_Error object.
  125. * @param string $hook Action hook to execute when the event is run.
  126. * @param array $v Event data.
  127. */
  128. do_action( 'cron_reschedule_event_error', $result, $hook, $v );
  129. }
  130. }
  131. $result = wp_unschedule_event( $timestamp, $hook, $v['args'], true );
  132. if ( is_wp_error( $result ) ) {
  133. error_log(
  134. sprintf(
  135. /* translators: 1: Hook name, 2: Error code, 3: Error message, 4: Event data. */
  136. __( 'Cron unschedule event error for hook: %1$s, Error code: %2$s, Error message: %3$s, Data: %4$s' ),
  137. $hook,
  138. $result->get_error_code(),
  139. $result->get_error_message(),
  140. wp_json_encode( $v )
  141. )
  142. );
  143. /**
  144. * Fires when an error happens unscheduling a cron event.
  145. *
  146. * @since 6.1.0
  147. *
  148. * @param WP_Error $result The WP_Error object.
  149. * @param string $hook Action hook to execute when the event is run.
  150. * @param array $v Event data.
  151. */
  152. do_action( 'cron_unschedule_event_error', $result, $hook, $v );
  153. }
  154. /**
  155. * Fires scheduled events.
  156. *
  157. * @ignore
  158. * @since 2.1.0
  159. *
  160. * @param string $hook Name of the hook that was scheduled to be fired.
  161. * @param array $args The arguments to be passed to the hook.
  162. */
  163. do_action_ref_array( $hook, $v['args'] );
  164. // If the hook ran too long and another cron process stole the lock, quit.
  165. if ( _get_cron_lock() !== $doing_wp_cron ) {
  166. return;
  167. }
  168. }
  169. }
  170. }
  171. if ( _get_cron_lock() === $doing_wp_cron ) {
  172. delete_transient( 'doing_cron' );
  173. }
  174. die();