From 3ece01ad157afa7fc4e6a16bf2925921507fd569 Mon Sep 17 00:00:00 2001 From: rutviksavsani Date: Wed, 24 May 2023 18:55:50 +0530 Subject: [PATCH 1/7] Add exclude flag in wp cron event run command. --- src/Cron_Event_Command.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index afad964c..f79da477 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -210,6 +210,9 @@ public function schedule( $args, $assoc_args ) { * [--due-now] * : Run all hooks due right now. * + * [--exclude=] + * : Exclude events of a comma-separated list of hooks. + * * [--all] * : Run all hooks. * @@ -233,6 +236,20 @@ public function run( $args, $assoc_args ) { WP_CLI::error( $events ); } + $exclude = Utils\get_flag_value( $assoc_args, 'exclude' ); + + if ( ! empty( $exclude ) ) { + $exclude = explode( ',', $exclude ); + } + + $events = array_filter( + $events, + function ( $event ) use ( $args, $exclude ) { + return ( empty( $args ) || in_array( $event->hook, $args, true ) ) && + ( empty( $exclude ) || ! in_array( $event->hook, $exclude, true ) ); + } + ); + $hooks = wp_list_pluck( $events, 'hook' ); foreach ( $args as $hook ) { if ( ! in_array( $hook, $hooks, true ) ) { From 9c85184cf12c7c9d5be1e048a84b5571991c493b Mon Sep 17 00:00:00 2001 From: rutviksavsani Date: Wed, 24 May 2023 20:15:40 +0530 Subject: [PATCH 2/7] Add behat test. --- features/cron-event.feature | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/features/cron-event.feature b/features/cron-event.feature index 5e98968c..af7c7c09 100644 --- a/features/cron-event.feature +++ b/features/cron-event.feature @@ -39,6 +39,12 @@ Feature: Manage WP Cron events Executed a total of 1 cron event """ + When I run `wp cron event run --due-now --exclude=wp_cli_test_event_2` + Then STDOUT should contain: + """ + Executed a total of 0 cron events + """ + @require-wp-4.9.0 Scenario: Unschedule cron event When I run `wp cron event schedule wp_cli_test_event_1 now hourly` From d8df3d00291af48651310b1faf0ea0147b6e223e Mon Sep 17 00:00:00 2001 From: Rutvik Savsani <53530700+rutviksavsani@users.noreply.github.com> Date: Thu, 25 May 2023 18:48:55 +0530 Subject: [PATCH 3/7] Update flag description. Co-authored-by: Daniel Bachhuber --- src/Cron_Event_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index f79da477..2734a85a 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -211,7 +211,7 @@ public function schedule( $args, $assoc_args ) { * : Run all hooks due right now. * * [--exclude=] - * : Exclude events of a comma-separated list of hooks. + * : Comma-separated list of events to exclude. * * [--all] * : Run all hooks. From c1c07d11ecc487339b43cb1eb3e5a23a5adccdf2 Mon Sep 17 00:00:00 2001 From: rutviksavsani Date: Thu, 25 May 2023 19:06:53 +0530 Subject: [PATCH 4/7] Increase readability. --- src/Cron_Event_Command.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index 2734a85a..4f682b37 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -240,15 +240,17 @@ public function run( $args, $assoc_args ) { if ( ! empty( $exclude ) ) { $exclude = explode( ',', $exclude ); - } - $events = array_filter( - $events, - function ( $event ) use ( $args, $exclude ) { - return ( empty( $args ) || in_array( $event->hook, $args, true ) ) && - ( empty( $exclude ) || ! in_array( $event->hook, $exclude, true ) ); - } - ); + $events = array_filter( + $events, + function ( $event ) use ( $args, $exclude ) { + if ( in_array( $event->hook, $exclude, true ) ) { + return false; + } + return true; + } + ); + } $hooks = wp_list_pluck( $events, 'hook' ); foreach ( $args as $hook ) { From f879760018a99996b89714be646a0ef154f44082 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 25 May 2023 09:05:15 -0700 Subject: [PATCH 5/7] One more test for this scenario --- features/cron-event.feature | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/features/cron-event.feature b/features/cron-event.feature index af7c7c09..a1470b6f 100644 --- a/features/cron-event.feature +++ b/features/cron-event.feature @@ -45,6 +45,16 @@ Feature: Manage WP Cron events Executed a total of 0 cron events """ + When I run `wp cron event run wp_cli_test_event_2 --due-now` + Then STDOUT should contain: + """ + Executed the cron event 'wp_cli_test_event_2' + """ + And STDOUT should contain: + """ + Executed a total of 1 cron event + """ + @require-wp-4.9.0 Scenario: Unschedule cron event When I run `wp cron event schedule wp_cli_test_event_1 now hourly` From 300d99aa9c2c9a034f52ec192c016a2ca3ffdd9f Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 25 May 2023 09:05:47 -0700 Subject: [PATCH 6/7] Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 702c9b7c..817e05b3 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ These fields are optionally available: Runs the next scheduled cron event for the given hook. ~~~ -wp cron event run [...] [--due-now] [--all] +wp cron event run [...] [--due-now] [--exclude=] [--all] ~~~ **OPTIONS** @@ -184,6 +184,9 @@ wp cron event run [...] [--due-now] [--all] [--due-now] Run all hooks due right now. + [--exclude=] + Comma-separated list of events to exclude. + [--all] Run all hooks. From 783b6f398ae259b2faaa94fa8d143c4b4f656cbf Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 25 May 2023 09:06:53 -0700 Subject: [PATCH 7/7] Tweak argument description slightly --- README.md | 2 +- src/Cron_Event_Command.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 817e05b3..f09baa21 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ wp cron event run [...] [--due-now] [--exclude=] [--all] Run all hooks due right now. [--exclude=] - Comma-separated list of events to exclude. + Comma-separated list of hooks to exclude. [--all] Run all hooks. diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index 4f682b37..4008de0d 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -211,7 +211,7 @@ public function schedule( $args, $assoc_args ) { * : Run all hooks due right now. * * [--exclude=] - * : Comma-separated list of events to exclude. + * : Comma-separated list of hooks to exclude. * * [--all] * : Run all hooks.