diff --git a/src/wp-cron.php b/src/wp-cron.php index 417dcce375849..edb9ce3475516 100644 --- a/src/wp-cron.php +++ b/src/wp-cron.php @@ -18,19 +18,11 @@ ignore_user_abort( true ); -if ( ! headers_sent() ) { - header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); - header( 'Cache-Control: no-cache, must-revalidate, max-age=0' ); -} - -// Don't run cron until the request finishes, if possible. -if ( function_exists( 'fastcgi_finish_request' ) ) { - fastcgi_finish_request(); -} elseif ( function_exists( 'litespeed_finish_request' ) ) { - litespeed_finish_request(); -} - if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) { + if ( ! headers_sent() ) { + header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); + header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); + } die(); } @@ -46,6 +38,27 @@ require_once __DIR__ . '/wp-load.php'; } +/** This filter is documented in wp-includes/default-constants.php */ +if ( ! apply_filters( 'wp_cron_endpoint_enabled', true ) ) { + if ( ! headers_sent() ) { + header( 'X-WP-Cron: Bypass' ); + } + die(); +} + +if ( ! headers_sent() ) { + header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); + header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); + header( 'X-WP-Cron: Spawned' ); +} + +// Don't run cron until the request finishes, if possible. +if ( function_exists( 'fastcgi_finish_request' ) ) { + fastcgi_finish_request(); +} elseif ( function_exists( 'litespeed_finish_request' ) ) { + litespeed_finish_request(); +} + // Attempt to raise the PHP memory limit for cron event processing. wp_raise_memory_limit( 'cron' ); diff --git a/src/wp-includes/default-constants.php b/src/wp-includes/default-constants.php index acfc878fb7138..272ef12a2328e 100644 --- a/src/wp-includes/default-constants.php +++ b/src/wp-includes/default-constants.php @@ -398,6 +398,34 @@ function wp_functionality_constants() { if ( ! defined( 'WP_CRON_LOCK_TIMEOUT' ) ) { define( 'WP_CRON_LOCK_TIMEOUT', MINUTE_IN_SECONDS ); } + + if ( ! defined( 'DISABLE_WP_CRON' ) ) { + /** + * Filters whether the wp-cron.php endpoint spawns the WP-Cron process. + * + * Use the filter to disable the wp-cron.php endpoint from spawning cron jobs. This + * filter must only be used if an alternative approach to firing cron jobs is enabled + * on the server. + * + * When disabling the wp-cron.php endpoint, you must ensure that cron jobs are still + * fired by using an alternative method such as WP CLI. + * + * For single site installs, the following WP CLI command can be scheduled via a system + * cron job: `wp cron event run --due-now`. For multisite installs, the command must be + * run for each site by specifying the global `--url` parameter. + * + * When disabling the wp-cron.php endpoint via the filter, the filter must be added on + * or prior to the `plugins_loaded` hook to ensure it takes effect. + * + * @since x.x.x + * + * @link https://developer.wordpress.org/cli/commands/cron/event/ + * + * @param bool $wp_cron_endpoint_enabled Whether to enable the wp-cron.php endpoint. Default true. + */ + $wp_cron_endpoint_enabled = (bool) apply_filters( 'wp_cron_endpoint_enabled', true ); + define( 'DISABLE_WP_CRON', ! $wp_cron_endpoint_enabled ); + } } /**