Skip to content

Commit 8460962

Browse files
swissspidyCopilotCopilot
authored
Fix tests (#134)
* Pass `$due_now` * Fix `unschedule` command failing on WordPress 4.9 (#135) * Initial plan * Fix unschedule for WP 4.9 compatibility Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * PHPStan fix --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent faebdc0 commit 8460962

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/Cron_Event_Command.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -307,28 +307,44 @@ public function unschedule( $args, $assoc_args ) {
307307

308308
list( $hook ) = $args;
309309

310-
$unscheduled = wp_unschedule_hook( $hook );
311-
312-
if ( empty( $unscheduled ) ) {
313-
$message = 'Failed to unschedule events for hook \'%1\$s.';
314-
315-
// If 0 event found on hook.
316-
if ( 0 === $unscheduled ) {
317-
$message = "No events found for hook '%1\$s'.";
310+
// Count events before unscheduling for WP < 5.1 compatibility where
311+
// wp_unschedule_hook() returns null instead of the event count.
312+
$crons = _get_cron_array();
313+
$event_count = 0;
314+
if ( is_array( $crons ) ) {
315+
foreach ( $crons as $cron ) {
316+
if ( ! is_array( $cron ) ) {
317+
continue;
318+
}
319+
/**
320+
* @var array<string, array<mixed>> $cron
321+
*/
322+
if ( isset( $cron[ $hook ] ) ) {
323+
$event_count += count( $cron[ $hook ] );
324+
}
318325
}
326+
}
327+
328+
if ( 0 === $event_count ) {
329+
WP_CLI::error( sprintf( "No events found for hook '%s'.", $hook ) );
330+
}
319331

320-
WP_CLI::error( sprintf( $message, $hook ) );
332+
$unscheduled = wp_unschedule_hook( $hook );
321333

322-
} else {
323-
WP_CLI::success(
324-
sprintf(
325-
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
326-
$unscheduled,
327-
Utils\pluralize( 'event', $unscheduled ),
328-
$hook
329-
)
330-
);
334+
if ( false === $unscheduled ) {
335+
WP_CLI::error( sprintf( "Failed to unschedule events for hook '%s'.", $hook ) );
331336
}
337+
338+
$count = ( is_int( $unscheduled ) && $unscheduled > 0 ) ? $unscheduled : $event_count;
339+
340+
WP_CLI::success(
341+
sprintf(
342+
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
343+
$count,
344+
Utils\pluralize( 'event', $count ),
345+
$hook
346+
)
347+
);
332348
}
333349

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

565-
$events = self::get_cron_events();
581+
$events = self::get_cron_events( $due_now );
566582

567583
if ( is_wp_error( $events ) ) {
568584
return $events;

0 commit comments

Comments
 (0)