spawn_cron

Sends a request to run cron through HTTP request that doesn't halt page loading.

Description

spawn_cron( (int) $gmt_time = 0 ); 

Parameters (1)

0. $gmt_time — Optional. (int)
Unix timestamp (UTC). Default 0 (current time is used).

Usage

  1. if ( !function_exists( 'spawn_cron' ) ) { 
  2. require_once ABSPATH . WPINC . '/cron.php'; 
  3.  
  4. // Optional. Unix timestamp (UTC). Default 0 (current time is used). 
  5. $gmt_time = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = spawn_cron($gmt_time); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/cron.php  
  1. function spawn_cron( $gmt_time = 0 ) { 
  2. if ( ! $gmt_time ) 
  3. $gmt_time = microtime( true ); 
  4.  
  5. if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) 
  6. return; 
  7.  
  8. /** 
  9. * Get the cron lock, which is a Unix timestamp of when the last cron was spawned 
  10. * and has not finished running. 
  11. * Multiple processes on multiple web servers can run this code concurrently,  
  12. * this lock attempts to make spawning as atomic as possible. 
  13. */ 
  14. $lock = get_transient('doing_cron'); 
  15.  
  16. if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS
  17. $lock = 0; 
  18.  
  19. // don't run if another process is currently running it or more than once every 60 sec. 
  20. if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) 
  21. return; 
  22.  
  23. //sanity check 
  24. $crons = _get_cron_array(); 
  25. if ( !is_array($crons) ) 
  26. return; 
  27.  
  28. $keys = array_keys( $crons ); 
  29. if ( isset($keys[0]) && $keys[0] > $gmt_time ) 
  30. return; 
  31.  
  32. if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { 
  33. if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { 
  34. return; 
  35.  
  36. $doing_wp_cron = sprintf( '%.22F', $gmt_time ); 
  37. set_transient( 'doing_cron', $doing_wp_cron ); 
  38.  
  39. ob_start(); 
  40. wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); 
  41. echo ' '; 
  42.  
  43. // flush any buffers and send the headers 
  44. while ( @ob_end_flush() ); 
  45. flush(); 
  46.  
  47. WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' ); 
  48. return; 
  49.  
  50. // Set the cron lock with the current unix timestamp, when the cron is being spawned. 
  51. $doing_wp_cron = sprintf( '%.22F', $gmt_time ); 
  52. set_transient( 'doing_cron', $doing_wp_cron ); 
  53.  
  54. /** 
  55. * Filters the cron request arguments. 
  56. * @since 3.5.0 
  57. * @since 4.5.0 The `$doing_wp_cron` parameter was added. 
  58. * @param array $cron_request_array { 
  59. * An array of cron request URL arguments. 
  60. * @type string $url The cron request URL. 
  61. * @type int $key The 22 digit GMT microtime. 
  62. * @type array $args { 
  63. * An array of cron request arguments. 
  64. * @type int $timeout The request timeout in seconds. Default .01 seconds. 
  65. * @type bool $blocking Whether to set blocking for the request. Default false. 
  66. * @type bool $sslverify Whether SSL should be verified for the request. Default false. 
  67. * } 
  68. * } 
  69. * @param string $doing_wp_cron The unix timestamp of the cron lock. 
  70. */ 
  71. $cron_request = apply_filters( 'cron_request', array( 
  72. 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),  
  73. 'key' => $doing_wp_cron,  
  74. 'args' => array( 
  75. 'timeout' => 0.01,  
  76. 'blocking' => false,  
  77. /** This filter is documented in wp-includes/class-wp-http-streams.php */ 
  78. 'sslverify' => apply_filters( 'https_local_ssl_verify', false ) 
  79. ), $doing_wp_cron ); 
  80.  
  81. wp_remote_post( $cron_request['url'], $cron_request['args'] );