}> */ private static $continuations = array(); /** * Waits the given number of seconds. * * @var callable */ private $sleep; /** * @param callable|null $sleep Waits the given number of seconds; sleep() by default. */ public function __construct( ?callable $sleep = null ) { $this->sleep = $sleep ?? static function ( int $seconds ): void { sleep( $seconds ); }; } /** * Start the next cron run for this continuation once this request ends. * * Does nothing outside a cron run, or when page views do not start cron * (DISABLE_WP_CRON) or start it by redirect (ALTERNATE_WP_CRON). * * @param string $hook Continuation hook. * @param array $args Its arguments. * @return void */ public static function afterThisRun( string $hook, array $args ): void { if ( ! wp_doing_cron() || self::constantIsTrue( 'DISABLE_WP_CRON' ) || self::constantIsTrue( 'ALTERNATE_WP_CRON' ) ) { return; } if ( array() === self::$continuations ) { add_action( 'shutdown', array( self::class, 'onShutdown' ) ); } self::$continuations[ $hook . '|' . md5( serialize( $args ) ) ] = array( 'hook' => $hook, 'args' => $args, ); } /** * Shutdown callback: start the next run for what this request asked for. * * @return void */ public static function onShutdown(): void { $continuations = self::$continuations; self::$continuations = array(); ( new self() )->kick( $continuations ); } /** * Wait until the last continuation still scheduled within MAX_WAIT_SECONDS * is due, then start a cron run. * * The next run lists its ready events once, when it starts. Waiting for the * earliest continuation only would leave a later one out of that list, and * it would wait for a page view if the job whose continuation ran finished * there and asked for no further run. * * @param array}> $continuations Hook and arguments of each. * @return void */ public function kick( array $continuations ): void { $now = time(); $due = null; foreach ( $continuations as $continuation ) { $at = wp_next_scheduled( $continuation['hook'], $continuation['args'] ); if ( false === $at || (int) $at - $now > self::MAX_WAIT_SECONDS ) { continue; } $due = null === $due ? (int) $at : max( $due, (int) $at ); } if ( null === $due ) { return; } $wait = $due - $now; if ( $wait > 0 ) { ( $this->sleep )( $wait ); } $url = site_url( 'wp-cron.php' ); $request = apply_filters( 'cron_request', array( 'url' => $url, 'key' => '', 'args' => array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', false, $url ), ), ), '' ); if ( ! is_array( $request ) || empty( $request['url'] ) || ! is_string( $request['url'] ) ) { Logger::warn( 'automation', 'The next cron run could not be started after a tick', array( 'error' => 'cron_request returned no URL' ) ); return; } $result = wp_remote_post( $request['url'], isset( $request['args'] ) && is_array( $request['args'] ) ? $request['args'] : array() ); if ( is_wp_error( $result ) ) { Logger::warn( 'automation', 'The next cron run could not be started after a tick', array( 'error' => $result->get_error_message() ) ); } } /** * Whether a constant is defined and true. * * @param string $name Constant name. * @return bool */ private static function constantIsTrue( string $name ): bool { return defined( $name ) && constant( $name ); } }