Make WordPress Core

Changeset 50135


Ignore:
Timestamp:
02/02/2021 03:08:53 AM (4 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/cron.php

    r49963 r50135  
    745745
    746746/**
    747  * Run scheduled callbacks or spawn cron for all scheduled events.
     747 * Register _wp_cron() to run on the {@see 'wp_loaded'} action.
     748 *
     749 * If the {@see 'wp_loaded'} action has already fired, this function calls
     750 * _wp_cron() directly.
    748751 *
    749752 * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
     
    754757 * @since 2.1.0
    755758 * @since 5.1.0 Return value added to indicate success or failure.
     759 * @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper.
     760 *
     761 * @return bool|int|void On success an integer indicating number of events spawned (0 indicates no
     762 *                       events needed to be spawned), false if spawning fails for one or more events or
     763 *                       void if the function registered _wp_cron() to run on the action.
     764 */
     765function wp_cron() {
     766    if ( did_action( 'wp_loaded' ) ) {
     767        return _wp_cron();
     768    }
     769
     770    add_action( 'wp_loaded', '_wp_cron', 20 );
     771}
     772
     773/**
     774 * Run scheduled callbacks or spawn cron for all scheduled events.
     775 *
     776 * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
     777 * value which evaluates to FALSE. For information about casting to booleans see the
     778 * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
     779 * the `===` operator for testing the return value of this function.
     780 *
     781 * @since 5.7.0
     782 * @access private
    756783 *
    757784 * @return int|false On success an integer indicating number of events spawned (0 indicates no
    758785 *                   events needed to be spawned), false if spawning fails for one or more events.
    759786 */
    760 function wp_cron() {
     787function _wp_cron() {
    761788    // Prevent infinite loops caused by lack of wp-cron.php.
    762789    if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
Note: See TracChangeset for help on using the changeset viewer.