Skip to content

Commit e9c6e22

Browse files
committed
First draft at hook to control wp-cron.php endpoint.
1 parent d5fed6b commit e9c6e22

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

src/wp-cron.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@
1818

1919
ignore_user_abort( true );
2020

21-
if ( ! headers_sent() ) {
22-
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
23-
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
24-
}
25-
26-
// Don't run cron until the request finishes, if possible.
27-
if ( function_exists( 'fastcgi_finish_request' ) ) {
28-
fastcgi_finish_request();
29-
} elseif ( function_exists( 'litespeed_finish_request' ) ) {
30-
litespeed_finish_request();
31-
}
32-
3321
if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
22+
if ( ! headers_sent() ) {
23+
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
24+
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
25+
}
3426
die();
3527
}
3628

@@ -46,6 +38,29 @@
4638
require_once __DIR__ . '/wp-load.php';
4739
}
4840

41+
/** This filter is documented in wp-includes/default-constants.php */
42+
if ( ! apply_filters( 'wp_cron_endpoint_enabled', true ) ) {
43+
if ( ! headers_sent() ) {
44+
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
45+
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
46+
header( 'X-WP-Cron: Bypass' );
47+
}
48+
die();
49+
}
50+
51+
if ( ! headers_sent() ) {
52+
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
53+
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
54+
header( 'X-WP-Cron: Spawned' );
55+
}
56+
57+
// Don't run cron until the request finishes, if possible.
58+
if ( function_exists( 'fastcgi_finish_request' ) ) {
59+
fastcgi_finish_request();
60+
} elseif ( function_exists( 'litespeed_finish_request' ) ) {
61+
litespeed_finish_request();
62+
}
63+
4964
// Attempt to raise the PHP memory limit for cron event processing.
5065
wp_raise_memory_limit( 'cron' );
5166

src/wp-includes/default-constants.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,34 @@ function wp_functionality_constants() {
398398
if ( ! defined( 'WP_CRON_LOCK_TIMEOUT' ) ) {
399399
define( 'WP_CRON_LOCK_TIMEOUT', MINUTE_IN_SECONDS );
400400
}
401+
402+
if ( ! defined( 'DISABLE_WP_CRON' ) ) {
403+
/**
404+
* Filters whether the wp-cron.php endpoint spawns the WP-Cron process.
405+
*
406+
* Use the filter to disable the wp-cron.php endpoint from spawning cron jobs. This
407+
* filter must only be used if an alternative approach to firing cron jobs is enabled
408+
* on the server.
409+
*
410+
* When disabling the wp-cron.php endpoint, you must ensure that cron jobs are still
411+
* fired by using an alternative method such as WP CLI.
412+
*
413+
* For single site installs, the following WP CLI command can be scheduled via a system
414+
* cron job: `wp cron event run --due-now`. For multi site installs, the command must be
415+
* run for each site by specifying the global `--url` parameter.
416+
*
417+
* When disabling the wp-cron.php endpoint via the filter, the filter must be added on
418+
* or prior to the `plugins_loaded` hook to ensure it takes effect.
419+
*
420+
* @since x.x.x
421+
*
422+
* @link https://developer.wordpress.org/cli/commands/cron/event/
423+
*
424+
* @param bool $wp_cron_endpoint_enabled Whether to enable the wp-cron.php endpoint. Default true.
425+
*/
426+
$wp_cron_endpoint_enabled = (bool) apply_filters( 'wp_cron_endpoint_enabled', true );
427+
define( 'DISABLE_WP_CRON', ! $wp_cron_endpoint_enabled );
428+
}
401429
}
402430

403431
/**

0 commit comments

Comments
 (0)