diff --git a/README.md b/README.md index 702c9b7c..f09baa21 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 hooks to exclude. + [--all] Run all hooks. diff --git a/features/cron-event.feature b/features/cron-event.feature index 5e98968c..a1470b6f 100644 --- a/features/cron-event.feature +++ b/features/cron-event.feature @@ -39,6 +39,22 @@ 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 + """ + + 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` diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index afad964c..4008de0d 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=] + * : Comma-separated list of hooks to exclude. + * * [--all] * : Run all hooks. * @@ -233,6 +236,22 @@ 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 ) { + if ( in_array( $event->hook, $exclude, true ) ) { + return false; + } + return true; + } + ); + } + $hooks = wp_list_pluck( $events, 'hook' ); foreach ( $args as $hook ) { if ( ! in_array( $hook, $hooks, true ) ) {