Skip to content

Commit 6b8dc35

Browse files
Copilotswissspidy
andcommitted
Refactor: Extract event execution logic to private helper method
- Created run_events() helper method to eliminate code duplication - Removed unused $result variable assignments - Both network and single-site execution now use the same helper Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c832e58 commit 6b8dc35

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/Cron_Event_Command.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,7 @@ public function run( $args, $assoc_args ) {
275275
$events = self::get_selected_cron_events( $args, $network_assoc_args );
276276

277277
if ( ! is_wp_error( $events ) ) {
278-
foreach ( $events as $event ) {
279-
$start = microtime( true );
280-
$result = self::run_event( $event );
281-
$total = round( microtime( true ) - $start, 3 );
282-
++$total_executed;
283-
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
284-
if ( ! empty( $event->args ) ) {
285-
WP_CLI::debug( sprintf( 'Arguments: %s', wp_json_encode( $event->args ) ), 'cron' );
286-
}
287-
}
278+
$total_executed += self::run_events( $events );
288279
} else {
289280
WP_CLI::debug( sprintf( 'No events found for site %d: %s', $site_id, $events->get_error_message() ), 'cron' );
290281
}
@@ -309,17 +300,7 @@ public function run( $args, $assoc_args ) {
309300
WP_CLI::error( $events );
310301
}
311302

312-
$executed = 0;
313-
foreach ( $events as $event ) {
314-
$start = microtime( true );
315-
$result = self::run_event( $event );
316-
$total = round( microtime( true ) - $start, 3 );
317-
++$executed;
318-
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
319-
if ( ! empty( $event->args ) ) {
320-
WP_CLI::debug( sprintf( 'Arguments: %s', wp_json_encode( $event->args ) ), 'cron' );
321-
}
322-
}
303+
$executed = self::run_events( $events );
323304

324305
$message = ( 1 === $executed ) ? 'Executed a total of %d cron event.' : 'Executed a total of %d cron events.';
325306
WP_CLI::success( sprintf( $message, $executed ) );
@@ -411,6 +392,29 @@ public function delete( $args, $assoc_args ) {
411392
WP_CLI::success( sprintf( $message, $deleted ) );
412393
}
413394

395+
/**
396+
* Runs multiple cron events and logs their execution.
397+
*
398+
* @param array $events Array of event objects to run.
399+
* @return int The number of events executed.
400+
*/
401+
private static function run_events( array $events ) {
402+
$executed = 0;
403+
404+
foreach ( $events as $event ) {
405+
$start = microtime( true );
406+
self::run_event( $event );
407+
$total = round( microtime( true ) - $start, 3 );
408+
++$executed;
409+
WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) );
410+
if ( ! empty( $event->args ) ) {
411+
WP_CLI::debug( sprintf( 'Arguments: %s', wp_json_encode( $event->args ) ), 'cron' );
412+
}
413+
}
414+
415+
return $executed;
416+
}
417+
414418
/**
415419
* Executes an event immediately.
416420
*

0 commit comments

Comments
 (0)