From 7a8a7fdcd40e3e2eec392719d129858303d9e806 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 2 Feb 2021 03:10:08 +0000 Subject: [PATCH] Cron API: Run alternative wp-cron later, do not run on archived blogs. Runs cron jobs later on sites using alternative cron, ie the `ALTERNATE_WP_CRON` constant is true, to more closely match when standard cron jobs are run. Jobs now run on the `wp_loaded` hook at priority `20`. Prior to this change they would run on the `init` hook. This ensures custom post types and taxonomies are registered prior to the jobs running. This change also prevents alternative wp-cron from running on archived or suspended multisite blogs as these are shut down prior to the `wp_loaded` hook from running. Moves the existing functionality of `wp_cron()` in to a new private function `_wp_cron()`. Props flixos90, jeremyfelt, johnbillion, jrf, kurtpayne, nacin, peterwilsoncc, prettyboymp, r-a-y, ryan, stevenkword, swissspidy. Fixes #20537, #24160. Built from https://develop.svn.wordpress.org/trunk@50135 git-svn-id: http://core.svn.wordpress.org/trunk@49814 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/cron.php | 31 +++++++++++++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/wp-includes/cron.php b/wp-includes/cron.php index e3bd917e25..a1d33ff58d 100644 --- a/wp-includes/cron.php +++ b/wp-includes/cron.php @@ -744,7 +744,10 @@ function spawn_cron( $gmt_time = 0 ) { } /** - * Run scheduled callbacks or spawn cron for all scheduled events. + * Register _wp_cron() to run on the {@see 'wp_loaded'} action. + * + * If the {@see 'wp_loaded'} action has already fired, this function calls + * _wp_cron() directly. * * Warning: This function may return Boolean FALSE, but may also return a non-Boolean * value which evaluates to FALSE. For information about casting to booleans see the @@ -753,11 +756,35 @@ function spawn_cron( $gmt_time = 0 ) { * * @since 2.1.0 * @since 5.1.0 Return value added to indicate success or failure. + * @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper. + * + * @return bool|int|void On success an integer indicating number of events spawned (0 indicates no + * events needed to be spawned), false if spawning fails for one or more events or + * void if the function registered _wp_cron() to run on the action. + */ +function wp_cron() { + if ( did_action( 'wp_loaded' ) ) { + return _wp_cron(); + } + + add_action( 'wp_loaded', '_wp_cron', 20 ); +} + +/** + * Run scheduled callbacks or spawn cron for all scheduled events. + * + * Warning: This function may return Boolean FALSE, but may also return a non-Boolean + * value which evaluates to FALSE. For information about casting to booleans see the + * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use + * the `===` operator for testing the return value of this function. + * + * @since 5.7.0 + * @access private * * @return int|false On success an integer indicating number of events spawned (0 indicates no * events needed to be spawned), false if spawning fails for one or more events. */ -function wp_cron() { +function _wp_cron() { // Prevent infinite loops caused by lack of wp-cron.php. if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { return 0; diff --git a/wp-includes/version.php b/wp-includes/version.php index 3b52bfca0b..ea7f042b19 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.7-alpha-50134'; +$wp_version = '5.7-alpha-50135'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.