Advertisement
Hectorss1

Untitled

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