Skip to content

Commit 86c2b76

Browse files
Copilotswissspidy
andauthored
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>
1 parent db11c6f commit 86c2b76

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/Cron_Event_Command.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -307,28 +307,38 @@ 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 ( isset( $cron[ $hook ] ) ) {
317+
$event_count += count( $cron[ $hook ] );
318+
}
318319
}
320+
}
319321

320-
WP_CLI::error( sprintf( $message, $hook ) );
322+
if ( 0 === $event_count ) {
323+
WP_CLI::error( sprintf( "No events found for hook '%s'.", $hook ) );
324+
}
321325

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-
);
326+
$unscheduled = wp_unschedule_hook( $hook );
327+
328+
if ( false === $unscheduled ) {
329+
WP_CLI::error( sprintf( "Failed to unschedule events for hook '%s'.", $hook ) );
331330
}
331+
332+
$count = ( is_int( $unscheduled ) && $unscheduled > 0 ) ? $unscheduled : $event_count;
333+
334+
WP_CLI::success(
335+
sprintf(
336+
'Unscheduled %1$d %2$s for hook \'%3$s\'.',
337+
$count,
338+
Utils\pluralize( 'event', $count ),
339+
$hook
340+
)
341+
);
332342
}
333343

334344
/**

0 commit comments

Comments
 (0)