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
46 changes: 28 additions & 18 deletions src/Cron_Event_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,38 @@ 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 ( isset( $cron[ $hook ] ) ) {
$event_count += count( $cron[ $hook ] );
}
Comment on lines +315 to +318
}
}

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

} else {
WP_CLI::success(
sprintf(
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
$unscheduled,
Utils\pluralize( 'event', $unscheduled ),
$hook
)
);
$unscheduled = wp_unschedule_hook( $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
Loading