Skip to content
Merged
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
54 changes: 35 additions & 19 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,44 @@ public function unschedule( $args, $assoc_args ) {

list( $hook ) = $args;

$unscheduled = wp_unschedule_hook( $hook );

if ( empty( $unscheduled ) ) {
$message = 'Failed to unschedule events for hook \'%1\$s.';

// If 0 event found on hook.
if ( 0 === $unscheduled ) {
$message = "No events found for hook '%1\$s'.";
// Count events before unscheduling for WP < 5.1 compatibility where
// wp_unschedule_hook() returns null instead of the event count.
$crons = _get_cron_array();
$event_count = 0;
if ( is_array( $crons ) ) {
foreach ( $crons as $cron ) {
if ( ! is_array( $cron ) ) {
continue;
}
/**
* @var array<string, array<mixed>> $cron
*/
if ( isset( $cron[ $hook ] ) ) {
$event_count += count( $cron[ $hook ] );
}
}
}

if ( 0 === $event_count ) {
WP_CLI::error( sprintf( "No events found for hook '%s'.", $hook ) );
}

WP_CLI::error( sprintf( $message, $hook ) );
$unscheduled = wp_unschedule_hook( $hook );

} else {
WP_CLI::success(
sprintf(
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
$unscheduled,
Utils\pluralize( 'event', $unscheduled ),
$hook
)
);
if ( false === $unscheduled ) {
WP_CLI::error( sprintf( "Failed to unschedule events for hook '%s'.", $hook ) );
}

$count = ( is_int( $unscheduled ) && $unscheduled > 0 ) ? $unscheduled : $event_count;

WP_CLI::success(
sprintf(
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
$count,
Utils\pluralize( 'event', $count ),
$hook
)
);
}

/**
Expand Down Expand Up @@ -562,7 +578,7 @@ protected static function get_selected_cron_events( $args, $assoc_args ) {
WP_CLI::error( 'Please use either --due-now or --all.' );
}

$events = self::get_cron_events();
$events = self::get_cron_events( $due_now );

if ( is_wp_error( $events ) ) {
return $events;
Expand Down
Loading