Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ These fields are optionally available:
Runs the next scheduled cron event for the given hook.

~~~
wp cron event run [<hook>...] [--due-now] [--all]
wp cron event run [<hook>...] [--due-now] [--exclude=<hooks>] [--all]
~~~

**OPTIONS**
Expand All @@ -184,6 +184,9 @@ wp cron event run [<hook>...] [--due-now] [--all]
[--due-now]
Run all hooks due right now.

[--exclude=<hooks>]
Comma-separated list of hooks to exclude.

[--all]
Run all hooks.

Expand Down
16 changes: 16 additions & 0 deletions features/cron-event.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
19 changes: 19 additions & 0 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public function schedule( $args, $assoc_args ) {
* [--due-now]
* : Run all hooks due right now.
*
* [--exclude=<hooks>]
* : Comma-separated list of hooks to exclude.
*
* [--all]
* : Run all hooks.
*
Expand All @@ -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 ) ) {
Expand Down