From 3d9fa1def8c4100eda7156e96b63b5a438c2f80e Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 30 Jan 2023 10:41:10 -0800 Subject: [PATCH 1/2] Warn instead of fatal when invalid cron event is detected --- composer.json | 1 + features/cron.feature | 38 ++++++++++++++++++++++++++++++++++++++ src/Cron_Event_Command.php | 7 +++++++ 3 files changed, 46 insertions(+) diff --git a/composer.json b/composer.json index 969d20a8..37127c09 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ }, "require-dev": { "wp-cli/entity-command": "^1.3 || ^2", + "wp-cli/eval-command": "^2.0", "wp-cli/server-command": "^2.0", "wp-cli/wp-cli-tests": "^3.1" }, diff --git a/features/cron.feature b/features/cron.feature index e605bcfd..c66d534a 100644 --- a/features/cron.feature +++ b/features/cron.feature @@ -360,3 +360,41 @@ Feature: Manage WP-Cron events and schedules Then STDOUT should be CSV containing: | hook | recurrence | args | | wp_cli_test_args_event | Non-repeating | {"foo":"banana","bar":"apple"} | + + Scenario: Warn when an invalid cron event is detected + Given a WP install + And a update.php file: + """ + array( + 'postindexer_secondpass_cron' => array( + '40cd750bba9870f18aada2478b24840a' => array( + 'schedule' => '5mins', + 'args' => array(), + 'interval' => 100, + ), + ), + ), + 'wp_batch_split_terms' => array( + 1442323165 => array( + '40cd750bba9870f18aada2478b24840a' => array( + 'schedule' => false, + 'args' => array() + ) + ) + ) + ); + update_option( 'cron', $val ); + """ + And I run `wp eval-file update.php` + + When I run `wp cron event list` + Then STDOUT should contain: + """ + postindexer_secondpass_cron + """ + And STDERR should contain: + """ + Warning: Ignoring incorrectly registered cron event "wp_batch_split_terms". + """ diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index 3665b144..cd8817b9 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -449,6 +449,13 @@ protected static function get_cron_events( $is_due_now = false ) { } foreach ( $crons as $time => $hooks ) { + + // Incorrectly registered cron events can produce a string key. + if ( is_string( $time ) ) { + WP_CLI::warning( sprintf( 'Ignoring invalid cron event ("%s").', $time ) ); + continue; + } + foreach ( $hooks as $hook => $hook_events ) { foreach ( $hook_events as $sig => $data ) { From e8ac7233b1c06ce6bcf6531b4cb0e6b6d3d30810 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 30 Jan 2023 10:51:51 -0800 Subject: [PATCH 2/2] Clean up output and test --- features/cron.feature | 2 +- src/Cron_Event_Command.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/features/cron.feature b/features/cron.feature index c66d534a..86981138 100644 --- a/features/cron.feature +++ b/features/cron.feature @@ -389,7 +389,7 @@ Feature: Manage WP-Cron events and schedules """ And I run `wp eval-file update.php` - When I run `wp cron event list` + When I try `wp cron event list` Then STDOUT should contain: """ postindexer_secondpass_cron diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index cd8817b9..afad964c 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -452,7 +452,7 @@ protected static function get_cron_events( $is_due_now = false ) { // Incorrectly registered cron events can produce a string key. if ( is_string( $time ) ) { - WP_CLI::warning( sprintf( 'Ignoring invalid cron event ("%s").', $time ) ); + WP_CLI::warning( sprintf( 'Ignoring incorrectly registered cron event "%s".', $time ) ); continue; }