PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/cron.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 998 lines | 409 code | 94 blank | 495 comment | 87 complexity | 7eba6075d76101561f2f5e969d791962 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Cron API
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Schedules an event to run only once.
  9. *
  10. * Schedules a hook which will be triggered by WordPress at the specified time.
  11. * The action will trigger when someone visits your WordPress site if the scheduled
  12. * time has passed.
  13. *
  14. * Note that scheduling an event to occur within 10 minutes of an existing event
  15. * with the same action hook will be ignored unless you pass unique `$args` values
  16. * for each scheduled event.
  17. *
  18. * Use wp_next_scheduled() to prevent duplicate events.
  19. *
  20. * Use wp_schedule_event() to schedule a recurring event.
  21. *
  22. * @since 2.1.0
  23. * @since 5.1.0 Return value modified to boolean indicating success or failure,
  24. * {@see 'pre_schedule_event'} filter added to short-circuit the function.
  25. *
  26. * @link https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
  27. *
  28. * @param int $timestamp Unix timestamp (UTC) for when to next run the event.
  29. * @param string $hook Action hook to execute when the event is run.
  30. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  31. * @return bool True if event successfully scheduled. False for failure.
  32. */
  33. function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
  34. // Make sure timestamp is a positive integer.
  35. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
  36. return false;
  37. }
  38. $event = (object) array(
  39. 'hook' => $hook,
  40. 'timestamp' => $timestamp,
  41. 'schedule' => false,
  42. 'args' => $args,
  43. );
  44. /**
  45. * Filter to preflight or hijack scheduling an event.
  46. *
  47. * Returning a non-null value will short-circuit adding the event to the
  48. * cron array, causing the function to return the filtered value instead.
  49. *
  50. * Both single events and recurring events are passed through this filter;
  51. * single events have `$event->schedule` as false, whereas recurring events
  52. * have this set to a recurrence from wp_get_schedules(). Recurring
  53. * events also have the integer recurrence interval set as `$event->interval`.
  54. *
  55. * For plugins replacing wp-cron, it is recommended you check for an
  56. * identical event within ten minutes and apply the {@see 'schedule_event'}
  57. * filter to check if another plugin has disallowed the event before scheduling.
  58. *
  59. * Return true if the event was scheduled, false if not.
  60. *
  61. * @since 5.1.0
  62. *
  63. * @param null|bool $pre Value to return instead. Default null to continue adding the event.
  64. * @param stdClass $event {
  65. * An object containing an event's data.
  66. *
  67. * @type string $hook Action hook to execute when the event is run.
  68. * @type int $timestamp Unix timestamp (UTC) for when to next run the event.
  69. * @type string|false $schedule How often the event should subsequently recur.
  70. * @type array $args Array containing each separate argument to pass to the hook's callback function.
  71. * @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
  72. * }
  73. */
  74. $pre = apply_filters( 'pre_schedule_event', null, $event );
  75. if ( null !== $pre ) {
  76. return $pre;
  77. }
  78. /*
  79. * Check for a duplicated event.
  80. *
  81. * Don't schedule an event if there's already an identical event
  82. * within 10 minutes.
  83. *
  84. * When scheduling events within ten minutes of the current time,
  85. * all past identical events are considered duplicates.
  86. *
  87. * When scheduling an event with a past timestamp (ie, before the
  88. * current time) all events scheduled within the next ten minutes
  89. * are considered duplicates.
  90. */
  91. $crons = (array) _get_cron_array();
  92. $key = md5( serialize( $event->args ) );
  93. $duplicate = false;
  94. if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) {
  95. $min_timestamp = 0;
  96. } else {
  97. $min_timestamp = $event->timestamp - 10 * MINUTE_IN_SECONDS;
  98. }
  99. if ( $event->timestamp < time() ) {
  100. $max_timestamp = time() + 10 * MINUTE_IN_SECONDS;
  101. } else {
  102. $max_timestamp = $event->timestamp + 10 * MINUTE_IN_SECONDS;
  103. }
  104. foreach ( $crons as $event_timestamp => $cron ) {
  105. if ( $event_timestamp < $min_timestamp ) {
  106. continue;
  107. }
  108. if ( $event_timestamp > $max_timestamp ) {
  109. break;
  110. }
  111. if ( isset( $cron[ $event->hook ][ $key ] ) ) {
  112. $duplicate = true;
  113. break;
  114. }
  115. }
  116. if ( $duplicate ) {
  117. return false;
  118. }
  119. /**
  120. * Modify an event before it is scheduled.
  121. *
  122. * @since 3.1.0
  123. *
  124. * @param stdClass $event {
  125. * An object containing an event's data.
  126. *
  127. * @type string $hook Action hook to execute when the event is run.
  128. * @type int $timestamp Unix timestamp (UTC) for when to next run the event.
  129. * @type string|false $schedule How often the event should subsequently recur.
  130. * @type array $args Array containing each separate argument to pass to the hook's callback function.
  131. * @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
  132. * }
  133. */
  134. $event = apply_filters( 'schedule_event', $event );
  135. // A plugin disallowed this event.
  136. if ( ! $event ) {
  137. return false;
  138. }
  139. $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
  140. 'schedule' => $event->schedule,
  141. 'args' => $event->args,
  142. );
  143. uksort( $crons, 'strnatcasecmp' );
  144. return _set_cron_array( $crons );
  145. }
  146. /**
  147. * Schedules a recurring event.
  148. *
  149. * Schedules a hook which will be triggered by WordPress at the specified interval.
  150. * The action will trigger when someone visits your WordPress site if the scheduled
  151. * time has passed.
  152. *
  153. * Valid values for the recurrence are 'hourly', 'daily', and 'twicedaily'. These can
  154. * be extended using the {@see 'cron_schedules'} filter in wp_get_schedules().
  155. *
  156. * Note that scheduling an event to occur within 10 minutes of an existing event
  157. * with the same action hook will be ignored unless you pass unique `$args` values
  158. * for each scheduled event.
  159. *
  160. * Use wp_next_scheduled() to prevent duplicate events.
  161. *
  162. * Use wp_schedule_single_event() to schedule a non-recurring event.
  163. *
  164. * @since 2.1.0
  165. * @since 5.1.0 Return value modified to boolean indicating success or failure,
  166. * {@see 'pre_schedule_event'} filter added to short-circuit the function.
  167. *
  168. * @link https://developer.wordpress.org/reference/functions/wp_schedule_event/
  169. *
  170. * @param int $timestamp Unix timestamp (UTC) for when to next run the event.
  171. * @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
  172. * @param string $hook Action hook to execute when the event is run.
  173. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  174. * @return bool True if event successfully scheduled. False for failure.
  175. */
  176. function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
  177. // Make sure timestamp is a positive integer.
  178. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
  179. return false;
  180. }
  181. $schedules = wp_get_schedules();
  182. if ( ! isset( $schedules[ $recurrence ] ) ) {
  183. return false;
  184. }
  185. $event = (object) array(
  186. 'hook' => $hook,
  187. 'timestamp' => $timestamp,
  188. 'schedule' => $recurrence,
  189. 'args' => $args,
  190. 'interval' => $schedules[ $recurrence ]['interval'],
  191. );
  192. /** This filter is documented in wp-includes/cron.php */
  193. $pre = apply_filters( 'pre_schedule_event', null, $event );
  194. if ( null !== $pre ) {
  195. return $pre;
  196. }
  197. /** This filter is documented in wp-includes/cron.php */
  198. $event = apply_filters( 'schedule_event', $event );
  199. // A plugin disallowed this event.
  200. if ( ! $event ) {
  201. return false;
  202. }
  203. $key = md5( serialize( $event->args ) );
  204. $crons = _get_cron_array();
  205. $crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
  206. 'schedule' => $event->schedule,
  207. 'args' => $event->args,
  208. 'interval' => $event->interval,
  209. );
  210. uksort( $crons, 'strnatcasecmp' );
  211. return _set_cron_array( $crons );
  212. }
  213. /**
  214. * Reschedules a recurring event.
  215. *
  216. * Mainly for internal use, this takes the time stamp of a previously run
  217. * recurring event and reschedules it for its next run.
  218. *
  219. * To change upcoming scheduled events, use wp_schedule_event() to
  220. * change the recurrence frequency.
  221. *
  222. * @since 2.1.0
  223. * @since 5.1.0 Return value modified to boolean indicating success or failure,
  224. * {@see 'pre_reschedule_event'} filter added to short-circuit the function.
  225. *
  226. * @param int $timestamp Unix timestamp (UTC) for when the event was scheduled.
  227. * @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
  228. * @param string $hook Action hook to execute when the event is run.
  229. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  230. * @return bool True if event successfully rescheduled. False for failure.
  231. */
  232. function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
  233. // Make sure timestamp is a positive integer.
  234. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
  235. return false;
  236. }
  237. $schedules = wp_get_schedules();
  238. $interval = 0;
  239. // First we try to get the interval from the schedule.
  240. if ( isset( $schedules[ $recurrence ] ) ) {
  241. $interval = $schedules[ $recurrence ]['interval'];
  242. }
  243. // Now we try to get it from the saved interval in case the schedule disappears.
  244. if ( 0 === $interval ) {
  245. $scheduled_event = wp_get_scheduled_event( $hook, $args, $timestamp );
  246. if ( $scheduled_event && isset( $scheduled_event->interval ) ) {
  247. $interval = $scheduled_event->interval;
  248. }
  249. }
  250. $event = (object) array(
  251. 'hook' => $hook,
  252. 'timestamp' => $timestamp,
  253. 'schedule' => $recurrence,
  254. 'args' => $args,
  255. 'interval' => $interval,
  256. );
  257. /**
  258. * Filter to preflight or hijack rescheduling of events.
  259. *
  260. * Returning a non-null value will short-circuit the normal rescheduling
  261. * process, causing the function to return the filtered value instead.
  262. *
  263. * For plugins replacing wp-cron, return true if the event was successfully
  264. * rescheduled, false if not.
  265. *
  266. * @since 5.1.0
  267. *
  268. * @param null|bool $pre Value to return instead. Default null to continue adding the event.
  269. * @param stdClass $event {
  270. * An object containing an event's data.
  271. *
  272. * @type string $hook Action hook to execute when the event is run.
  273. * @type int $timestamp Unix timestamp (UTC) for when to next run the event.
  274. * @type string|false $schedule How often the event should subsequently recur.
  275. * @type array $args Array containing each separate argument to pass to the hook's callback function.
  276. * @type int $interval The interval time in seconds for the schedule. Only present for recurring events.
  277. * }
  278. */
  279. $pre = apply_filters( 'pre_reschedule_event', null, $event );
  280. if ( null !== $pre ) {
  281. return $pre;
  282. }
  283. // Now we assume something is wrong and fail to schedule.
  284. if ( 0 == $interval ) {
  285. return false;
  286. }
  287. $now = time();
  288. if ( $timestamp >= $now ) {
  289. $timestamp = $now + $interval;
  290. } else {
  291. $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
  292. }
  293. return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
  294. }
  295. /**
  296. * Unschedule a previously scheduled event.
  297. *
  298. * The $timestamp and $hook parameters are required so that the event can be
  299. * identified.
  300. *
  301. * @since 2.1.0
  302. * @since 5.1.0 Return value modified to boolean indicating success or failure,
  303. * {@see 'pre_unschedule_event'} filter added to short-circuit the function.
  304. *
  305. * @param int $timestamp Unix timestamp (UTC) of the event.
  306. * @param string $hook Action hook of the event.
  307. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  308. * Although not passed to a callback, these arguments are used to uniquely identify the
  309. * event, so they should be the same as those used when originally scheduling the event.
  310. * @return bool True if event successfully unscheduled. False for failure.
  311. */
  312. function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
  313. // Make sure timestamp is a positive integer.
  314. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
  315. return false;
  316. }
  317. /**
  318. * Filter to preflight or hijack unscheduling of events.
  319. *
  320. * Returning a non-null value will short-circuit the normal unscheduling
  321. * process, causing the function to return the filtered value instead.
  322. *
  323. * For plugins replacing wp-cron, return true if the event was successfully
  324. * unscheduled, false if not.
  325. *
  326. * @since 5.1.0
  327. *
  328. * @param null|bool $pre Value to return instead. Default null to continue unscheduling the event.
  329. * @param int $timestamp Timestamp for when to run the event.
  330. * @param string $hook Action hook, the execution of which will be unscheduled.
  331. * @param array $args Arguments to pass to the hook's callback function.
  332. */
  333. $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args );
  334. if ( null !== $pre ) {
  335. return $pre;
  336. }
  337. $crons = _get_cron_array();
  338. $key = md5( serialize( $args ) );
  339. unset( $crons[ $timestamp ][ $hook ][ $key ] );
  340. if ( empty( $crons[ $timestamp ][ $hook ] ) ) {
  341. unset( $crons[ $timestamp ][ $hook ] );
  342. }
  343. if ( empty( $crons[ $timestamp ] ) ) {
  344. unset( $crons[ $timestamp ] );
  345. }
  346. return _set_cron_array( $crons );
  347. }
  348. /**
  349. * Unschedules all events attached to the hook with the specified arguments.
  350. *
  351. * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
  352. * value which evaluates to FALSE. For information about casting to booleans see the
  353. * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
  354. * the `===` operator for testing the return value of this function.
  355. *
  356. * @since 2.1.0
  357. * @since 5.1.0 Return value modified to indicate success or failure,
  358. * {@see 'pre_clear_scheduled_hook'} filter added to short-circuit the function.
  359. *
  360. * @param string $hook Action hook, the execution of which will be unscheduled.
  361. * @param array $args Optional. Arguments that were to be passed to the hook's callback function.
  362. * @return int|false On success an integer indicating number of events unscheduled (0 indicates no
  363. * events were registered with the hook and arguments combination), false if
  364. * unscheduling one or more events fail.
  365. */
  366. function wp_clear_scheduled_hook( $hook, $args = array() ) {
  367. // Backward compatibility.
  368. // Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.
  369. if ( ! is_array( $args ) ) {
  370. _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) );
  371. $args = array_slice( func_get_args(), 1 );
  372. }
  373. /**
  374. * Filter to preflight or hijack clearing a scheduled hook.
  375. *
  376. * Returning a non-null value will short-circuit the normal unscheduling
  377. * process, causing the function to return the filtered value instead.
  378. *
  379. * For plugins replacing wp-cron, return the number of events successfully
  380. * unscheduled (zero if no events were registered with the hook) or false
  381. * if unscheduling one or more events fails.
  382. *
  383. * @since 5.1.0
  384. *
  385. * @param null|int|false $pre Value to return instead. Default null to continue unscheduling the event.
  386. * @param string $hook Action hook, the execution of which will be unscheduled.
  387. * @param array $args Arguments to pass to the hook's callback function.
  388. */
  389. $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args );
  390. if ( null !== $pre ) {
  391. return $pre;
  392. }
  393. /*
  394. * This logic duplicates wp_next_scheduled().
  395. * It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
  396. * and, wp_next_scheduled() returns the same schedule in an infinite loop.
  397. */
  398. $crons = _get_cron_array();
  399. if ( empty( $crons ) ) {
  400. return 0;
  401. }
  402. $results = array();
  403. $key = md5( serialize( $args ) );
  404. foreach ( $crons as $timestamp => $cron ) {
  405. if ( isset( $cron[ $hook ][ $key ] ) ) {
  406. $results[] = wp_unschedule_event( $timestamp, $hook, $args );
  407. }
  408. }
  409. if ( in_array( false, $results, true ) ) {
  410. return false;
  411. }
  412. return count( $results );
  413. }
  414. /**
  415. * Unschedules all events attached to the hook.
  416. *
  417. * Can be useful for plugins when deactivating to clean up the cron queue.
  418. *
  419. * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
  420. * value which evaluates to FALSE. For information about casting to booleans see the
  421. * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
  422. * the `===` operator for testing the return value of this function.
  423. *
  424. * @since 4.9.0
  425. * @since 5.1.0 Return value added to indicate success or failure.
  426. *
  427. * @param string $hook Action hook, the execution of which will be unscheduled.
  428. * @return int|false On success an integer indicating number of events unscheduled (0 indicates no
  429. * events were registered on the hook), false if unscheduling fails.
  430. */
  431. function wp_unschedule_hook( $hook ) {
  432. /**
  433. * Filter to preflight or hijack clearing all events attached to the hook.
  434. *
  435. * Returning a non-null value will short-circuit the normal unscheduling
  436. * process, causing the function to return the filtered value instead.
  437. *
  438. * For plugins replacing wp-cron, return the number of events successfully
  439. * unscheduled (zero if no events were registered with the hook) or false
  440. * if unscheduling one or more events fails.
  441. *
  442. * @since 5.1.0
  443. *
  444. * @param null|int|false $pre Value to return instead. Default null to continue unscheduling the hook.
  445. * @param string $hook Action hook, the execution of which will be unscheduled.
  446. */
  447. $pre = apply_filters( 'pre_unschedule_hook', null, $hook );
  448. if ( null !== $pre ) {
  449. return $pre;
  450. }
  451. $crons = _get_cron_array();
  452. if ( empty( $crons ) ) {
  453. return 0;
  454. }
  455. $results = array();
  456. foreach ( $crons as $timestamp => $args ) {
  457. if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
  458. $results[] = count( $crons[ $timestamp ][ $hook ] );
  459. }
  460. unset( $crons[ $timestamp ][ $hook ] );
  461. if ( empty( $crons[ $timestamp ] ) ) {
  462. unset( $crons[ $timestamp ] );
  463. }
  464. }
  465. /*
  466. * If the results are empty (zero events to unschedule), no attempt
  467. * to update the cron array is required.
  468. */
  469. if ( empty( $results ) ) {
  470. return 0;
  471. }
  472. if ( _set_cron_array( $crons ) ) {
  473. return array_sum( $results );
  474. }
  475. return false;
  476. }
  477. /**
  478. * Retrieve a scheduled event.
  479. *
  480. * Retrieve the full event object for a given event, if no timestamp is specified the next
  481. * scheduled event is returned.
  482. *
  483. * @since 5.1.0
  484. *
  485. * @param string $hook Action hook of the event.
  486. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  487. * Although not passed to a callback, these arguments are used to uniquely identify the
  488. * event, so they should be the same as those used when originally scheduling the event.
  489. * @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event is returned.
  490. * @return object|false The event object. False if the event does not exist.
  491. */
  492. function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
  493. /**
  494. * Filter to preflight or hijack retrieving a scheduled event.
  495. *
  496. * Returning a non-null value will short-circuit the normal process,
  497. * returning the filtered value instead.
  498. *
  499. * Return false if the event does not exist, otherwise an event object
  500. * should be returned.
  501. *
  502. * @since 5.1.0
  503. *
  504. * @param null|false|object $pre Value to return instead. Default null to continue retrieving the event.
  505. * @param string $hook Action hook of the event.
  506. * @param array $args Array containing each separate argument to pass to the hook's callback function.
  507. * Although not passed to a callback, these arguments are used to uniquely identify
  508. * the event.
  509. * @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event.
  510. */
  511. $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp );
  512. if ( null !== $pre ) {
  513. return $pre;
  514. }
  515. if ( null !== $timestamp && ! is_numeric( $timestamp ) ) {
  516. return false;
  517. }
  518. $crons = _get_cron_array();
  519. if ( empty( $crons ) ) {
  520. return false;
  521. }
  522. $key = md5( serialize( $args ) );
  523. if ( ! $timestamp ) {
  524. // Get next event.
  525. $next = false;
  526. foreach ( $crons as $timestamp => $cron ) {
  527. if ( isset( $cron[ $hook ][ $key ] ) ) {
  528. $next = $timestamp;
  529. break;
  530. }
  531. }
  532. if ( ! $next ) {
  533. return false;
  534. }
  535. $timestamp = $next;
  536. } elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {
  537. return false;
  538. }
  539. $event = (object) array(
  540. 'hook' => $hook,
  541. 'timestamp' => $timestamp,
  542. 'schedule' => $crons[ $timestamp ][ $hook ][ $key ]['schedule'],
  543. 'args' => $args,
  544. );
  545. if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) {
  546. $event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
  547. }
  548. return $event;
  549. }
  550. /**
  551. * Retrieve the next timestamp for an event.
  552. *
  553. * @since 2.1.0
  554. *
  555. * @param string $hook Action hook of the event.
  556. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
  557. * Although not passed to a callback, these arguments are used to uniquely identify the
  558. * event, so they should be the same as those used when originally scheduling the event.
  559. * @return int|false The Unix timestamp of the next time the event will occur. False if the event doesn't exist.
  560. */
  561. function wp_next_scheduled( $hook, $args = array() ) {
  562. $next_event = wp_get_scheduled_event( $hook, $args );
  563. if ( ! $next_event ) {
  564. return false;
  565. }
  566. return $next_event->timestamp;
  567. }
  568. /**
  569. * Sends a request to run cron through HTTP request that doesn't halt page loading.
  570. *
  571. * @since 2.1.0
  572. * @since 5.1.0 Return values added.
  573. *
  574. * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
  575. * @return bool True if spawned, false if no events spawned.
  576. */
  577. function spawn_cron( $gmt_time = 0 ) {
  578. if ( ! $gmt_time ) {
  579. $gmt_time = microtime( true );
  580. }
  581. if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
  582. return false;
  583. }
  584. /*
  585. * Get the cron lock, which is a Unix timestamp of when the last cron was spawned
  586. * and has not finished running.
  587. *
  588. * Multiple processes on multiple web servers can run this code concurrently,
  589. * this lock attempts to make spawning as atomic as possible.
  590. */
  591. $lock = get_transient( 'doing_cron' );
  592. if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
  593. $lock = 0;
  594. }
  595. // Don't run if another process is currently running it or more than once every 60 sec.
  596. if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
  597. return false;
  598. }
  599. // Sanity check.
  600. $crons = wp_get_ready_cron_jobs();
  601. if ( empty( $crons ) ) {
  602. return false;
  603. }
  604. $keys = array_keys( $crons );
  605. if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
  606. return false;
  607. }
  608. if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
  609. if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
  610. return false;
  611. }
  612. $doing_wp_cron = sprintf( '%.22F', $gmt_time );
  613. set_transient( 'doing_cron', $doing_wp_cron );
  614. ob_start();
  615. wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
  616. echo ' ';
  617. // Flush any buffers and send the headers.
  618. wp_ob_end_flush_all();
  619. flush();
  620. include_once ABSPATH . 'wp-cron.php';
  621. return true;
  622. }
  623. // Set the cron lock with the current unix timestamp, when the cron is being spawned.
  624. $doing_wp_cron = sprintf( '%.22F', $gmt_time );
  625. set_transient( 'doing_cron', $doing_wp_cron );
  626. /**
  627. * Filters the cron request arguments.
  628. *
  629. * @since 3.5.0
  630. * @since 4.5.0 The `$doing_wp_cron` parameter was added.
  631. *
  632. * @param array $cron_request_array {
  633. * An array of cron request URL arguments.
  634. *
  635. * @type string $url The cron request URL.
  636. * @type int $key The 22 digit GMT microtime.
  637. * @type array $args {
  638. * An array of cron request arguments.
  639. *
  640. * @type int $timeout The request timeout in seconds. Default .01 seconds.
  641. * @type bool $blocking Whether to set blocking for the request. Default false.
  642. * @type bool $sslverify Whether SSL should be verified for the request. Default false.
  643. * }
  644. * }
  645. * @param string $doing_wp_cron The unix timestamp of the cron lock.
  646. */
  647. $cron_request = apply_filters(
  648. 'cron_request',
  649. array(
  650. 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
  651. 'key' => $doing_wp_cron,
  652. 'args' => array(
  653. 'timeout' => 0.01,
  654. 'blocking' => false,
  655. /** This filter is documented in wp-includes/class-wp-http-streams.php */
  656. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
  657. ),
  658. ),
  659. $doing_wp_cron
  660. );
  661. $result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
  662. return ! is_wp_error( $result );
  663. }
  664. /**
  665. * Run scheduled callbacks or spawn cron for all scheduled events.
  666. *
  667. * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
  668. * value which evaluates to FALSE. For information about casting to booleans see the
  669. * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
  670. * the `===` operator for testing the return value of this function.
  671. *
  672. * @since 2.1.0
  673. * @since 5.1.0 Return value added to indicate success or failure.
  674. *
  675. * @return bool|int On success an integer indicating number of events spawned (0 indicates no
  676. * events needed to be spawned), false if spawning fails for one or more events.
  677. */
  678. function wp_cron() {
  679. // Prevent infinite loops caused by lack of wp-cron.php.
  680. if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
  681. return 0;
  682. }
  683. $crons = wp_get_ready_cron_jobs();
  684. if ( empty( $crons ) ) {
  685. return 0;
  686. }
  687. $gmt_time = microtime( true );
  688. $keys = array_keys( $crons );
  689. if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
  690. return 0;
  691. }
  692. $schedules = wp_get_schedules();
  693. $results = array();
  694. foreach ( $crons as $timestamp => $cronhooks ) {
  695. if ( $timestamp > $gmt_time ) {
  696. break;
  697. }
  698. foreach ( (array) $cronhooks as $hook => $args ) {
  699. if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) {
  700. continue;
  701. }
  702. $results[] = spawn_cron( $gmt_time );
  703. break 2;
  704. }
  705. }
  706. if ( in_array( false, $results, true ) ) {
  707. return false;
  708. }
  709. return count( $results );
  710. }
  711. /**
  712. * Retrieve supported event recurrence schedules.
  713. *
  714. * The default supported recurrences are 'hourly', 'twicedaily', 'daily', and 'weekly'.
  715. * A plugin may add more by hooking into the {@see 'cron_schedules'} filter.
  716. * The filter accepts an array of arrays. The outer array has a key that is the name
  717. * of the schedule, for example 'monthly'. The value is an array with two keys,
  718. * one is 'interval' and the other is 'display'.
  719. *
  720. * The 'interval' is a number in seconds of when the cron job should run.
  721. * So for 'hourly' the time is `HOUR_IN_SECONDS` (60 * 60 or 3600). For 'monthly',
  722. * the value would be `MONTH_IN_SECONDS` (30 * 24 * 60 * 60 or 2592000).
  723. *
  724. * The 'display' is the description. For the 'monthly' key, the 'display'
  725. * would be `__( 'Once Monthly' )`.
  726. *
  727. * For your plugin, you will be passed an array. You can easily add your
  728. * schedule by doing the following.
  729. *
  730. * // Filter parameter variable name is 'array'.
  731. * $array['monthly'] = array(
  732. * 'interval' => MONTH_IN_SECONDS,
  733. * 'display' => __( 'Once Monthly' )
  734. * );
  735. *
  736. * @since 2.1.0
  737. * @since 5.4.0 The 'weekly' schedule was added.
  738. *
  739. * @return array
  740. */
  741. function wp_get_schedules() {
  742. $schedules = array(
  743. 'hourly' => array(
  744. 'interval' => HOUR_IN_SECONDS,
  745. 'display' => __( 'Once Hourly' ),
  746. ),
  747. 'twicedaily' => array(
  748. 'interval' => 12 * HOUR_IN_SECONDS,
  749. 'display' => __( 'Twice Daily' ),
  750. ),
  751. 'daily' => array(
  752. 'interval' => DAY_IN_SECONDS,
  753. 'display' => __( 'Once Daily' ),
  754. ),
  755. 'weekly' => array(
  756. 'interval' => WEEK_IN_SECONDS,
  757. 'display' => __( 'Once Weekly' ),
  758. ),
  759. );
  760. /**
  761. * Filters the non-default cron schedules.
  762. *
  763. * @since 2.1.0
  764. *
  765. * @param array $new_schedules An array of non-default cron schedules. Default empty.
  766. */
  767. return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
  768. }
  769. /**
  770. * Retrieve the recurrence schedule for an event.
  771. *
  772. * @see wp_get_schedules() for available schedules.
  773. *
  774. * @since 2.1.0
  775. * @since 5.1.0 {@see 'get_schedule'} filter added.
  776. *
  777. * @param string $hook Action hook to identify the event.
  778. * @param array $args Optional. Arguments passed to the event's callback function.
  779. * @return string|false False, if no schedule. Schedule name on success.
  780. */
  781. function wp_get_schedule( $hook, $args = array() ) {
  782. $schedule = false;
  783. $event = wp_get_scheduled_event( $hook, $args );
  784. if ( $event ) {
  785. $schedule = $event->schedule;
  786. }
  787. /**
  788. * Filter the schedule for a hook.
  789. *
  790. * @since 5.1.0
  791. *
  792. * @param string|bool $schedule Schedule for the hook. False if not found.
  793. * @param string $hook Action hook to execute when cron is run.
  794. * @param array $args Optional. Arguments to pass to the hook's callback function.
  795. */
  796. return apply_filters( 'get_schedule', $schedule, $hook, $args );
  797. }
  798. /**
  799. * Retrieve cron jobs ready to be run.
  800. *
  801. * Returns the results of _get_cron_array() limited to events ready to be run,
  802. * ie, with a timestamp in the past.
  803. *
  804. * @since 5.1.0
  805. *
  806. * @return array Cron jobs ready to be run.
  807. */
  808. function wp_get_ready_cron_jobs() {
  809. /**
  810. * Filter to preflight or hijack retrieving ready cron jobs.
  811. *
  812. * Returning an array will short-circuit the normal retrieval of ready
  813. * cron jobs, causing the function to return the filtered value instead.
  814. *
  815. * @since 5.1.0
  816. *
  817. * @param null|array $pre Array of ready cron tasks to return instead. Default null
  818. * to continue using results from _get_cron_array().
  819. */
  820. $pre = apply_filters( 'pre_get_ready_cron_jobs', null );
  821. if ( null !== $pre ) {
  822. return $pre;
  823. }
  824. $crons = _get_cron_array();
  825. if ( false === $crons ) {
  826. return array();
  827. }
  828. $gmt_time = microtime( true );
  829. $keys = array_keys( $crons );
  830. if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
  831. return array();
  832. }
  833. $results = array();
  834. foreach ( $crons as $timestamp => $cronhooks ) {
  835. if ( $timestamp > $gmt_time ) {
  836. break;
  837. }
  838. $results[ $timestamp ] = $cronhooks;
  839. }
  840. return $results;
  841. }
  842. //
  843. // Private functions.
  844. //
  845. /**
  846. * Retrieve cron info array option.
  847. *
  848. * @since 2.1.0
  849. * @access private
  850. *
  851. * @return array|false CRON info array.
  852. */
  853. function _get_cron_array() {
  854. $cron = get_option( 'cron' );
  855. if ( ! is_array( $cron ) ) {
  856. return false;
  857. }
  858. if ( ! isset( $cron['version'] ) ) {
  859. $cron = _upgrade_cron_array( $cron );
  860. }
  861. unset( $cron['version'] );
  862. return $cron;
  863. }
  864. /**
  865. * Updates the CRON option with the new CRON array.
  866. *
  867. * @since 2.1.0
  868. * @since 5.1.0 Return value modified to outcome of update_option().
  869. *
  870. * @access private
  871. *
  872. * @param array $cron Cron info array from _get_cron_array().
  873. * @return bool True if cron array updated, false on failure.
  874. */
  875. function _set_cron_array( $cron ) {
  876. $cron['version'] = 2;
  877. return update_option( 'cron', $cron );
  878. }
  879. /**
  880. * Upgrade a Cron info array.
  881. *
  882. * This function upgrades the Cron info array to version 2.
  883. *
  884. * @since 2.1.0
  885. * @access private
  886. *
  887. * @param array $cron Cron info array from _get_cron_array().
  888. * @return array An upgraded Cron info array.
  889. */
  890. function _upgrade_cron_array( $cron ) {
  891. if ( isset( $cron['version'] ) && 2 == $cron['version'] ) {
  892. return $cron;
  893. }
  894. $new_cron = array();
  895. foreach ( (array) $cron as $timestamp => $hooks ) {
  896. foreach ( (array) $hooks as $hook => $args ) {
  897. $key = md5( serialize( $args['args'] ) );
  898. $new_cron[ $timestamp ][ $hook ][ $key ] = $args;
  899. }
  900. }
  901. $new_cron['version'] = 2;
  902. update_option( 'cron', $new_cron );
  903. return $new_cron;
  904. }