Skip to content
Prev Previous commit
Next Next commit
Add support for --exclude
  • Loading branch information
Mike-Hermans committed Jun 8, 2023
commit 70ac6cab3ffb12587a492211749195545dfcbb9e
16 changes: 16 additions & 0 deletions features/cron.feature
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,22 @@ Feature: Manage WP-Cron events and schedules
Deleted a total of
"""

When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty

When I run `wp cron event schedule wp_cli_test_event_2 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty

When I run `wp cron event delete --all --exclude=wp_cli_test_event_1`
Then STDOUT should contain:
"""
Deleted the cron event 'wp_cli_test_event_2'
"""
And STDOUT should contain:
"""
Deleted a total of
"""

Scenario: A valid combination of parameters should be present
When I try `wp cron event delete --due-now --all`
Then STDERR should be:
Expand Down
35 changes: 19 additions & 16 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,6 @@ 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;
}
);
}

$executed = 0;
foreach ( $events as $event ) {
$start = microtime( true );
Expand Down Expand Up @@ -317,6 +301,9 @@ public function unschedule( $args, $assoc_args ) {
* [--due-now]
* : Delete all hooks due right now.
*
* [--exclude=<hooks>]
* : Comma-separated list of hooks to exclude.
*
* [--all]
* : Delete all hooks.
*
Expand Down Expand Up @@ -469,6 +456,7 @@ protected static function get_cron_events( $is_due_now = false ) {
protected static function get_selected_cron_events( $args, $assoc_args ) {
$due_now = Utils\get_flag_value( $assoc_args, 'due-now' );
$all = Utils\get_flag_value( $assoc_args, 'all' );
$exclude = Utils\get_flag_value( $assoc_args, 'exclude' );

if ( empty( $args ) && ! $due_now && ! $all ) {
WP_CLI::error( 'Please specify one or more cron events, or use --due-now/--all.' );
Expand All @@ -495,6 +483,19 @@ protected static function get_selected_cron_events( $args, $assoc_args ) {
}
}

// Remove all excluded hooks
if ( ! empty( $exclude ) ) {
$exclude = explode( ',', $exclude );
$events = array_filter(
$events,
function ( $event ) use ( $exclude ) {
return ! in_array( $event->hook, $exclude, true );
}
);
}

// If --due-now is specified, take only the events that have 'now' as
// their next_run_relative time
if ( $due_now ) {
$due_events = array();
foreach ( $events as $event ) {
Expand All @@ -507,6 +508,8 @@ protected static function get_selected_cron_events( $args, $assoc_args ) {
}
$events = $due_events;
} elseif ( ! $all ) {
// IF --all is not specified, take only the events that have been
// given as $args.
$due_events = array();
foreach ( $events as $event ) {
if ( in_array( $event->hook, $args, true ) ) {
Expand Down