/wp-includes/cron.php

  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 an event which will execute once by the WordPress actions core at 
  12. * a time which you specify. The action will fire off when someone visits your 
  13. * WordPress site, if the schedule 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. * @since 2.1.0 
  20. * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event 
  21. * 
  22. * @param int $timestamp Unix timestamp (UTC) for when to run the event. 
  23. * @param string $hook Action hook to execute when event is run. 
  24. * @param array $args Optional. Arguments to pass to the hook's callback function. 
  25. * @return false|void False if the event does not get scheduled. 
  26. */ 
  27. function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
  28. // Make sure timestamp is a positive integer 
  29. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { 
  30. return false; 
  31.  
  32. // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it 
  33. $next = wp_next_scheduled($hook, $args); 
  34. if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) { 
  35. return false; 
  36.  
  37. $crons = _get_cron_array(); 
  38. $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args ); 
  39. /** 
  40. * Filters a single event before it is scheduled. 
  41. * 
  42. * @since 3.1.0 
  43. * 
  44. * @param stdClass $event { 
  45. * An object containing an event's data. 
  46. * 
  47. * @type string $hook Action hook to execute when event is run. 
  48. * @type int $timestamp Unix timestamp (UTC) for when to run the event. 
  49. * @type string|false $schedule How often the event should recur. See `wp_get_schedules()`. 
  50. * @type array $args Arguments to pass to the hook's callback function. 
  51. * } 
  52. */ 
  53. $event = apply_filters( 'schedule_event', $event ); 
  54.  
  55. // A plugin disallowed this event 
  56. if ( ! $event ) 
  57. return false; 
  58.  
  59. $key = md5(serialize($event->args)); 
  60.  
  61. $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); 
  62. uksort( $crons, "strnatcasecmp" ); 
  63. _set_cron_array( $crons ); 
  64.  
  65. /** 
  66. * Schedule a recurring event. 
  67. * 
  68. * Schedules a hook which will be executed by the WordPress actions core on a 
  69. * specific interval, specified by you. The action will trigger when someone 
  70. * visits your WordPress site, if the scheduled time has passed. 
  71. * 
  72. * Valid values for the recurrence are hourly, daily, and twicedaily. These can 
  73. * be extended using the {@see 'cron_schedules'} filter in wp_get_schedules(). 
  74. * 
  75. * Use wp_next_scheduled() to prevent duplicates 
  76. * 
  77. * @since 2.1.0 
  78. * 
  79. * @param int $timestamp Unix timestamp (UTC) for when to run the event. 
  80. * @param string $recurrence How often the event should recur. 
  81. * @param string $hook Action hook to execute when event is run. 
  82. * @param array $args Optional. Arguments to pass to the hook's callback function. 
  83. * @return false|void False if the event does not get scheduled. 
  84. */ 
  85. function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 
  86. // Make sure timestamp is a positive integer 
  87. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { 
  88. return false; 
  89.  
  90. $crons = _get_cron_array(); 
  91. $schedules = wp_get_schedules(); 
  92.  
  93. if ( !isset( $schedules[$recurrence] ) ) 
  94. return false; 
  95.  
  96. $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); 
  97. /** This filter is documented in wp-includes/cron.php */ 
  98. $event = apply_filters( 'schedule_event', $event ); 
  99.  
  100. // A plugin disallowed this event 
  101. if ( ! $event ) 
  102. return false; 
  103.  
  104. $key = md5(serialize($event->args)); 
  105.  
  106. $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval ); 
  107. uksort( $crons, "strnatcasecmp" ); 
  108. _set_cron_array( $crons ); 
  109.  
  110. /** 
  111. * Reschedule a recurring event. 
  112. * 
  113. * @since 2.1.0 
  114. * 
  115. * @param int $timestamp Unix timestamp (UTC) for when to run the event. 
  116. * @param string $recurrence How often the event should recur. 
  117. * @param string $hook Action hook to execute when event is run. 
  118. * @param array $args Optional. Arguments to pass to the hook's callback function. 
  119. * @return false|void False if the event does not get rescheduled. 
  120. */ 
  121. function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) { 
  122. // Make sure timestamp is a positive integer 
  123. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { 
  124. return false; 
  125.  
  126. $crons = _get_cron_array(); 
  127. $schedules = wp_get_schedules(); 
  128. $key = md5( serialize( $args ) ); 
  129. $interval = 0; 
  130.  
  131. // First we try to get it from the schedule 
  132. if ( isset( $schedules[ $recurrence ] ) ) { 
  133. $interval = $schedules[ $recurrence ]['interval']; 
  134. // Now we try to get it from the saved interval in case the schedule disappears 
  135. if ( 0 == $interval ) { 
  136. $interval = $crons[ $timestamp ][ $hook ][ $key ]['interval']; 
  137. // Now we assume something is wrong and fail to schedule 
  138. if ( 0 == $interval ) { 
  139. return false; 
  140.  
  141. $now = time(); 
  142.  
  143. if ( $timestamp >= $now ) { 
  144. $timestamp = $now + $interval; 
  145. } else { 
  146. $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); 
  147.  
  148. wp_schedule_event( $timestamp, $recurrence, $hook, $args ); 
  149.  
  150. /** 
  151. * Unschedule a previously scheduled event. 
  152. * 
  153. * The $timestamp and $hook parameters are required so that the event can be 
  154. * identified. 
  155. * 
  156. * @since 2.1.0 
  157. * 
  158. * @param int $timestamp Unix timestamp (UTC) for when to run the event. 
  159. * @param string $hook Action hook, the execution of which will be unscheduled. 
  160. * @param array $args Arguments to pass to the hook's callback function. 
  161. * Although not passed to a callback function, these arguments are used 
  162. * to uniquely identify the scheduled event, so they should be the same 
  163. * as those used when originally scheduling the event. 
  164. * @return false|void False if the event does not get unscheduled. 
  165. */ 
  166. function wp_unschedule_event( $timestamp, $hook, $args = array() ) { 
  167. // Make sure timestamp is a positive integer 
  168. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { 
  169. return false; 
  170.  
  171. $crons = _get_cron_array(); 
  172. $key = md5(serialize($args)); 
  173. unset( $crons[$timestamp][$hook][$key] ); 
  174. if ( empty($crons[$timestamp][$hook]) ) 
  175. unset( $crons[$timestamp][$hook] ); 
  176. if ( empty($crons[$timestamp]) ) 
  177. unset( $crons[$timestamp] ); 
  178. _set_cron_array( $crons ); 
  179.  
  180. /** 
  181. * Unschedule all events attached to the specified hook. 
  182. * 
  183. * @since 2.1.0 
  184. * 
  185. * @param string $hook Action hook, the execution of which will be unscheduled. 
  186. * @param array $args Optional. Arguments that were to be passed to the hook's callback function. 
  187. */ 
  188. function wp_clear_scheduled_hook( $hook, $args = array() ) { 
  189. // Backward compatibility 
  190. // Previously this function took the arguments as discrete vars rather than an array like the rest of the API 
  191. if ( !is_array($args) ) { 
  192. _deprecated_argument( __FUNCTION__, '3.0.0', __('This argument has changed to an array to match the behavior of the other cron functions.') ); 
  193. $args = array_slice( func_get_args(), 1 ); 
  194.  
  195. // This logic duplicates wp_next_scheduled() 
  196. // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,  
  197. // and, wp_next_scheduled() returns the same schedule in an infinite loop. 
  198. $crons = _get_cron_array(); 
  199. if ( empty( $crons ) ) 
  200. return; 
  201.  
  202. $key = md5( serialize( $args ) ); 
  203. foreach ( $crons as $timestamp => $cron ) { 
  204. if ( isset( $cron[ $hook ][ $key ] ) ) { 
  205. wp_unschedule_event( $timestamp, $hook, $args ); 
  206.  
  207. /** 
  208. * Retrieve the next timestamp for an event. 
  209. * 
  210. * @since 2.1.0 
  211. * 
  212. * @param string $hook Action hook to execute when event is run. 
  213. * @param array $args Optional. Arguments to pass to the hook's callback function. 
  214. * @return false|int The Unix timestamp of the next time the scheduled event will occur. 
  215. */ 
  216. function wp_next_scheduled( $hook, $args = array() ) { 
  217. $crons = _get_cron_array(); 
  218. $key = md5(serialize($args)); 
  219. if ( empty($crons) ) 
  220. return false; 
  221. foreach ( $crons as $timestamp => $cron ) { 
  222. if ( isset( $cron[$hook][$key] ) ) 
  223. return $timestamp; 
  224. return false; 
  225.  
  226. /** 
  227. * Sends a request to run cron through HTTP request that doesn't halt page loading. 
  228. * 
  229. * @since 2.1.0 
  230. * 
  231. * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used). 
  232. */ 
  233. function spawn_cron( $gmt_time = 0 ) { 
  234. if ( ! $gmt_time ) 
  235. $gmt_time = microtime( true ); 
  236.  
  237. if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) 
  238. return; 
  239.  
  240. /** 
  241. * Get the cron lock, which is a Unix timestamp of when the last cron was spawned 
  242. * and has not finished running. 
  243. * 
  244. * Multiple processes on multiple web servers can run this code concurrently,  
  245. * this lock attempts to make spawning as atomic as possible. 
  246. */ 
  247. $lock = get_transient('doing_cron'); 
  248.  
  249. if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) 
  250. $lock = 0; 
  251.  
  252. // don't run if another process is currently running it or more than once every 60 sec. 
  253. if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) 
  254. return; 
  255.  
  256. //sanity check 
  257. $crons = _get_cron_array(); 
  258. if ( !is_array($crons) ) 
  259. return; 
  260.  
  261. $keys = array_keys( $crons ); 
  262. if ( isset($keys[0]) && $keys[0] > $gmt_time ) 
  263. return; 
  264.  
  265. if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { 
  266. if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { 
  267. return; 
  268.  
  269. $doing_wp_cron = sprintf( '%.22F', $gmt_time ); 
  270. set_transient( 'doing_cron', $doing_wp_cron ); 
  271.  
  272. ob_start(); 
  273. wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); 
  274. echo ' '; 
  275.  
  276. // flush any buffers and send the headers 
  277. while ( @ob_end_flush() ); 
  278. flush(); 
  279.  
  280. WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' ); 
  281. return; 
  282.  
  283. // Set the cron lock with the current unix timestamp, when the cron is being spawned. 
  284. $doing_wp_cron = sprintf( '%.22F', $gmt_time ); 
  285. set_transient( 'doing_cron', $doing_wp_cron ); 
  286.  
  287. /** 
  288. * Filters the cron request arguments. 
  289. * 
  290. * @since 3.5.0 
  291. * @since 4.5.0 The `$doing_wp_cron` parameter was added. 
  292. * 
  293. * @param array $cron_request_array { 
  294. * An array of cron request URL arguments. 
  295. * 
  296. * @type string $url The cron request URL. 
  297. * @type int $key The 22 digit GMT microtime. 
  298. * @type array $args { 
  299. * An array of cron request arguments. 
  300. * 
  301. * @type int $timeout The request timeout in seconds. Default .01 seconds. 
  302. * @type bool $blocking Whether to set blocking for the request. Default false. 
  303. * @type bool $sslverify Whether SSL should be verified for the request. Default false. 
  304. * } 
  305. * } 
  306. * @param string $doing_wp_cron The unix timestamp of the cron lock. 
  307. */ 
  308. $cron_request = apply_filters( 'cron_request', array( 
  309. 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),  
  310. 'key' => $doing_wp_cron,  
  311. 'args' => array( 
  312. 'timeout' => 0.01,  
  313. 'blocking' => false,  
  314. /** This filter is documented in wp-includes/class-wp-http-streams.php */ 
  315. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ) 
  316. ), $doing_wp_cron ); 
  317.  
  318. wp_remote_post( $cron_request['url'], $cron_request['args'] ); 
  319.  
  320. /** 
  321. * Run scheduled callbacks or spawn cron for all scheduled events. 
  322. * 
  323. * @since 2.1.0 
  324. */ 
  325. function wp_cron() { 
  326. // Prevent infinite loops caused by lack of wp-cron.php 
  327. if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) 
  328. return; 
  329.  
  330. if ( false === $crons = _get_cron_array() ) 
  331. return; 
  332.  
  333. $gmt_time = microtime( true ); 
  334. $keys = array_keys( $crons ); 
  335. if ( isset($keys[0]) && $keys[0] > $gmt_time ) 
  336. return; 
  337.  
  338. $schedules = wp_get_schedules(); 
  339. foreach ( $crons as $timestamp => $cronhooks ) { 
  340. if ( $timestamp > $gmt_time ) break; 
  341. foreach ( (array) $cronhooks as $hook => $args ) { 
  342. if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) 
  343. continue; 
  344. spawn_cron( $gmt_time ); 
  345. break 2; 
  346.  
  347. /** 
  348. * Retrieve supported event recurrence schedules. 
  349. * 
  350. * The default supported recurrences are 'hourly', 'twicedaily', and 'daily'. A plugin may 
  351. * add more by hooking into the {@see 'cron_schedules'} filter. The filter accepts an array 
  352. * of arrays. The outer array has a key that is the name of the schedule or for 
  353. * example 'weekly'. The value is an array with two keys, one is 'interval' and 
  354. * the other is 'display'. 
  355. * 
  356. * The 'interval' is a number in seconds of when the cron job should run. So for 
  357. * 'hourly', the time is 3600 or 60*60. For weekly, the value would be 
  358. * 60*60*24*7 or 604800. The value of 'interval' would then be 604800. 
  359. * 
  360. * The 'display' is the description. For the 'weekly' key, the 'display' would 
  361. * be `__( 'Once Weekly' )`. 
  362. * 
  363. * For your plugin, you will be passed an array. you can easily add your 
  364. * schedule by doing the following. 
  365. * 
  366. * // Filter parameter variable name is 'array'. 
  367. * $array['weekly'] = array( 
  368. * 'interval' => 604800,  
  369. * 'display' => __( 'Once Weekly' ) 
  370. * ); 
  371. * 
  372. * 
  373. * @since 2.1.0 
  374. * 
  375. * @return array 
  376. */ 
  377. function wp_get_schedules() { 
  378. $schedules = array( 
  379. 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),  
  380. 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),  
  381. 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),  
  382. ); 
  383. /** 
  384. * Filters the non-default cron schedules. 
  385. * 
  386. * @since 2.1.0 
  387. * 
  388. * @param array $new_schedules An array of non-default cron schedules. Default empty. 
  389. */ 
  390. return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); 
  391.  
  392. /** 
  393. * Retrieve the recurrence schedule for an event. 
  394. * 
  395. * @see wp_get_schedules() for available schedules. 
  396. * 
  397. * @since 2.1.0 
  398. * 
  399. * @param string $hook Action hook to identify the event. 
  400. * @param array $args Optional. Arguments passed to the event's callback function. 
  401. * @return string|false False, if no schedule. Schedule name on success. 
  402. */ 
  403. function wp_get_schedule($hook, $args = array()) { 
  404. $crons = _get_cron_array(); 
  405. $key = md5(serialize($args)); 
  406. if ( empty($crons) ) 
  407. return false; 
  408. foreach ( $crons as $timestamp => $cron ) { 
  409. if ( isset( $cron[$hook][$key] ) ) 
  410. return $cron[$hook][$key]['schedule']; 
  411. return false; 
  412.  
  413. // 
  414. // Private functions 
  415. // 
  416.   
  417. /** 
  418. * Retrieve cron info array option. 
  419. * 
  420. * @since 2.1.0 
  421. * @access private 
  422. * 
  423. * @return false|array CRON info array. 
  424. */ 
  425. function _get_cron_array() { 
  426. $cron = get_option('cron'); 
  427. if ( ! is_array($cron) ) 
  428. return false; 
  429.  
  430. if ( !isset($cron['version']) ) 
  431. $cron = _upgrade_cron_array($cron); 
  432.  
  433. unset($cron['version']); 
  434.  
  435. return $cron; 
  436.  
  437. /** 
  438. * Updates the CRON option with the new CRON array. 
  439. * 
  440. * @since 2.1.0 
  441. * @access private 
  442. * 
  443. * @param array $cron Cron info array from _get_cron_array(). 
  444. */ 
  445. function _set_cron_array($cron) { 
  446. $cron['version'] = 2; 
  447. update_option( 'cron', $cron ); 
  448.  
  449. /** 
  450. * Upgrade a Cron info array. 
  451. * 
  452. * This function upgrades the Cron info array to version 2. 
  453. * 
  454. * @since 2.1.0 
  455. * @access private 
  456. * 
  457. * @param array $cron Cron info array from _get_cron_array(). 
  458. * @return array An upgraded Cron info array. 
  459. */ 
  460. function _upgrade_cron_array($cron) { 
  461. if ( isset($cron['version']) && 2 == $cron['version']) 
  462. return $cron; 
  463.  
  464. $new_cron = array(); 
  465.  
  466. foreach ( (array) $cron as $timestamp => $hooks) { 
  467. foreach ( (array) $hooks as $hook => $args ) { 
  468. $key = md5(serialize($args['args'])); 
  469. $new_cron[$timestamp][$hook][$key] = $args; 
  470.  
  471. $new_cron['version'] = 2; 
  472. update_option( 'cron', $new_cron ); 
  473. return $new_cron; 
.