Commit 9e754d92 authored by Erick Hitter's avatar Erick Hitter
Browse files

Improve `DOING_CRON` consistency and leverage new filter from 4.8

parent ddf19012
......@@ -47,7 +47,7 @@ class Events extends Singleton {
}
// Flag is used in many contexts, so should be set for all of our requests, regardless of the action
define( 'DOING_CRON', true );
set_doing_cron();
// When running events, allow for long-running ones, and non-blocking trigger requests
if ( REST_API::ENDPOINT_RUN === $endpoint ) {
......
......@@ -115,3 +115,16 @@ function parse_request() {
return $parsed_request;
}
/**
* Consistently set flag Core uses to indicate cron execution is ongoing
*/
function set_doing_cron() {
if ( ! defined( 'DOING_CRON' ) ) {
define( 'DOING_CRON', true );
}
// WP 4.8 introduced the `wp_doing_cron()` function and filter
// These can be used to override the `DOING_CRON` constant, which may cause problems for plugin's requests
add_filter( 'wp_doing_cron', '__return_true', 99999 );
}
......@@ -29,9 +29,7 @@ function prepare_environment() {
// Set DOING_CRON when appropriate
if ( isset( $cmd[1] ) && 'orchestrate' === $cmd[1] ) {
if ( ! defined( 'DOING_CRON' ) ) {
define( 'DOING_CRON', true );
}
\Automattic\WP\Cron_Control\set_doing_cron();
}
}
prepare_environment();
......
......@@ -128,9 +128,7 @@ class Events extends \WP_CLI_Command {
\WP_CLI::confirm( sprintf( __( 'Run this event?', 'automattic-cron-control' ) ) );
// Environment preparation
if ( ! defined( 'DOING_CRON' ) ) {
define( 'DOING_CRON', true );
}
\Automattic\WP\Cron_Control\set_doing_cron();
// Run the event
$run = \Automattic\WP\Cron_Control\run_event( $event->timestamp, $event->action_hashed, $event->instance, true );
......
......@@ -60,16 +60,14 @@ class Orchestrate_Runner extends \WP_CLI_Command {
\WP_CLI::error( __( 'Invalid instance', 'automattic-cron-control' ) );
}
// Prepare environment
if ( ! defined( 'DOING_CRON' ) ) {
define( 'DOING_CRON', true );
}
$now = time();
if ( $timestamp > $now ) {
\WP_CLI::error( sprintf( __( 'Given timestamp is for %1$s GMT, %2$s from now. The event\'s existence was not confirmed, and no attempt was made to execute it.', 'automattic-cron-control' ), date_i18n( TIME_FORMAT, $timestamp ), human_time_diff( $now, $timestamp ) ) );
}
// Prepare environment
\Automattic\WP\Cron_Control\set_doing_cron();
// Run the event
$run = \Automattic\WP\Cron_Control\run_event( $timestamp, $action, $instance );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment