/wp-cron.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 152 lines · 72 code · 24 blank · 56 comment · 21 complexity · 0cdc26ef7f3e46926d381ec9834b60d9 MD5 · raw file

  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. /* Don't make the request block till we finish, if possible. */
  20. if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
  21. if ( ! headers_sent() ) {
  22. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
  23. header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
  24. }
  25. fastcgi_finish_request();
  26. }
  27. if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
  28. die();
  29. }
  30. /**
  31. * Tell WordPress we are doing the cron task.
  32. *
  33. * @var bool
  34. */
  35. define( 'DOING_CRON', true );
  36. if ( ! defined( 'ABSPATH' ) ) {
  37. /** Set up WordPress environment */
  38. require_once __DIR__ . '/wp-load.php';
  39. }
  40. /**
  41. * Retrieves the cron lock.
  42. *
  43. * Returns the uncached `doing_cron` transient.
  44. *
  45. * @ignore
  46. * @since 3.3.0
  47. *
  48. * @global wpdb $wpdb WordPress database abstraction object.
  49. *
  50. * @return string|false Value of the `doing_cron` transient, 0|false otherwise.
  51. */
  52. function _get_cron_lock() {
  53. global $wpdb;
  54. $value = 0;
  55. if ( wp_using_ext_object_cache() ) {
  56. /*
  57. * Skip local cache and force re-fetch of doing_cron transient
  58. * in case another process updated the cache.
  59. */
  60. $value = wp_cache_get( 'doing_cron', 'transient', true );
  61. } else {
  62. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
  63. if ( is_object( $row ) ) {
  64. $value = $row->option_value;
  65. }
  66. }
  67. return $value;
  68. }
  69. $crons = wp_get_ready_cron_jobs();
  70. if ( empty( $crons ) ) {
  71. die();
  72. }
  73. $gmt_time = microtime( true );
  74. // The cron lock: a unix timestamp from when the cron was spawned.
  75. $doing_cron_transient = get_transient( 'doing_cron' );
  76. // Use global $doing_wp_cron lock, otherwise use the GET lock. If no lock, try to grab a new lock.
  77. if ( empty( $doing_wp_cron ) ) {
  78. if ( empty( $_GET['doing_wp_cron'] ) ) {
  79. // Called from external script/job. Try setting a lock.
  80. if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
  81. return;
  82. }
  83. $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
  84. $doing_cron_transient = $doing_wp_cron;
  85. set_transient( 'doing_cron', $doing_wp_cron );
  86. } else {
  87. $doing_wp_cron = $_GET['doing_wp_cron'];
  88. }
  89. }
  90. /*
  91. * The cron lock (a unix timestamp set when the cron was spawned),
  92. * must match $doing_wp_cron (the "key").
  93. */
  94. if ( $doing_cron_transient !== $doing_wp_cron ) {
  95. return;
  96. }
  97. foreach ( $crons as $timestamp => $cronhooks ) {
  98. if ( $timestamp > $gmt_time ) {
  99. break;
  100. }
  101. foreach ( $cronhooks as $hook => $keys ) {
  102. foreach ( $keys as $k => $v ) {
  103. $schedule = $v['schedule'];
  104. if ( $schedule ) {
  105. wp_reschedule_event( $timestamp, $schedule, $hook, $v['args'] );
  106. }
  107. wp_unschedule_event( $timestamp, $hook, $v['args'] );
  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. // If the hook ran too long and another cron process stole the lock, quit.
  119. if ( _get_cron_lock() !== $doing_wp_cron ) {
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. if ( _get_cron_lock() === $doing_wp_cron ) {
  126. delete_transient( 'doing_cron' );
  127. }
  128. die();