wp-cli/wp-cli — pull request #1336 by danielbachhuber (2014-08-21 15:59:28+00:00)
Title: Stop breaking at first cron event when looping over events to run/delete

The `wp cron event run` and `wp cron event delete` commands are almost identical, and they both have the problem of doing a `break` after the first matching hook is found. However, you may have scheduled multiple events for the same hook but with different cron arguments associated with it, for example:

```php
wp_schedule_single_event( time(), 'computer_numbers', array( 1, 1 ) );
wp_schedule_single_event( time(), 'computer_numbers', array( 2, 2 ) );
```

Only one of these will get run via WP-CLI currently. So we should instead looping over them all.

So for `wp cron event run`, something like this change:

```diff
diff --git a/php/commands/cron.php b/php/commands/cron.php
index db4e07d..bc67221 100644
--- a/php/commands/cron.php
+++ b/php/commands/cron.php@@ -116,7 +117,7 @@ class Cron_Event_Command extends WP_CLI_Command {
}

/**
- * Run the next scheduled cron event for the given hook.
+ * Run the next scheduled cron event(s) for the given hook.
*
* ## OPTIONS
*
@@ -126,24 +127,27 @@ class Cron_Event_Command extends WP_CLI_Command {
public function run( $args, $assoc_args ) {

$hook = $args[0];
- $result = false;
$events = self::get_cron_events();

if ( is_wp_error( $events ) ) {
WP_CLI::error( $events );
}

+ $found = 0;
foreach ( $events as $id => $event ) {
- if ( $event->hook == $hook ) {
+ if ( $event->hook === $hook ) {
$result = self::run_event( $event );
- break;
+ if ( $result ) {
+ WP_CLI::success( sprintf( "Successfully executed the cron event '%s'", $hook ) );
+ } else {
+ WP_CLI::error( sprintf( "Failed to the execute the cron event '%s'", $hook ) );
+ }
+ $found += 1;
}
}

- if ( $result ) {
- WP_CLI::success( sprintf( "Successfully executed the cron event '%s'", $hook ) );
- } else {
- WP_CLI::error( sprintf( "Failed to the execute the cron event '%s'", $hook ) );
+ if ( 0 === $found ) {
+ WP_CLI::warning( sprintf( "Cron event '%s' not found", $hook ) );
}

}
```