Skip to content

Commit c63ac49

Browse files
Add --exclude=<hooks> argument to wp cron event run (#97)
* Add exclude flag in wp cron event run command. * Add behat test. * Update flag description. Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co> * Increase readability. * One more test for this scenario * Update README * Tweak argument description slightly --------- Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co> Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
1 parent a72c43d commit c63ac49

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ These fields are optionally available:
173173
Runs the next scheduled cron event for the given hook.
174174

175175
~~~
176-
wp cron event run [<hook>...] [--due-now] [--all]
176+
wp cron event run [<hook>...] [--due-now] [--exclude=<hooks>] [--all]
177177
~~~
178178

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

187+
[--exclude=<hooks>]
188+
Comma-separated list of hooks to exclude.
189+
187190
[--all]
188191
Run all hooks.
189192

features/cron-event.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ Feature: Manage WP Cron events
3939
Executed a total of 1 cron event
4040
"""
4141

42+
When I run `wp cron event run --due-now --exclude=wp_cli_test_event_2`
43+
Then STDOUT should contain:
44+
"""
45+
Executed a total of 0 cron events
46+
"""
47+
48+
When I run `wp cron event run wp_cli_test_event_2 --due-now`
49+
Then STDOUT should contain:
50+
"""
51+
Executed the cron event 'wp_cli_test_event_2'
52+
"""
53+
And STDOUT should contain:
54+
"""
55+
Executed a total of 1 cron event
56+
"""
57+
4258
@require-wp-4.9.0
4359
Scenario: Unschedule cron event
4460
When I run `wp cron event schedule wp_cli_test_event_1 now hourly`

src/Cron_Event_Command.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ public function schedule( $args, $assoc_args ) {
210210
* [--due-now]
211211
* : Run all hooks due right now.
212212
*
213+
* [--exclude=<hooks>]
214+
* : Comma-separated list of hooks to exclude.
215+
*
213216
* [--all]
214217
* : Run all hooks.
215218
*
@@ -233,6 +236,22 @@ public function run( $args, $assoc_args ) {
233236
WP_CLI::error( $events );
234237
}
235238

239+
$exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
240+
241+
if ( ! empty( $exclude ) ) {
242+
$exclude = explode( ',', $exclude );
243+
244+
$events = array_filter(
245+
$events,
246+
function ( $event ) use ( $args, $exclude ) {
247+
if ( in_array( $event->hook, $exclude, true ) ) {
248+
return false;
249+
}
250+
return true;
251+
}
252+
);
253+
}
254+
236255
$hooks = wp_list_pluck( $events, 'hook' );
237256
foreach ( $args as $hook ) {
238257
if ( ! in_array( $hook, $hooks, true ) ) {

0 commit comments

Comments
 (0)