Skip to content

Fix unschedule command failing on WordPress 4.9#135

Merged
swissspidy merged 2 commits intotry/fix-testsfrom
copilot/sub-pr-134
Mar 17, 2026
Merged

Fix unschedule command failing on WordPress 4.9#135
swissspidy merged 2 commits intotry/fix-testsfrom
copilot/sub-pr-134

Conversation

Copy link
Contributor

Copilot AI commented Mar 16, 2026

wp cron event unschedule always errored on WP 4.9 because wp_unschedule_hook() returns null on success in that version (since _set_cron_array() was void). The empty() check treated null as failure, and a format string bug (\'%1\$s — literal backslash in single-quoted string) garbled the error message further.

Changes

  • Pre-count events via _get_cron_array() before calling wp_unschedule_hook(), providing a reliable count across all WP versions
  • Replace empty() with false === $unscheduled — only an explicit false return indicates failure; null (WP 4.9) and positive integers (WP 5.1+) both mean success
  • Fall back to pre-count when wp_unschedule_hook() returns null (WP < 5.1)
  • Fix error message format string — replaced broken \'%1\$s in single-quoted string with a clean double-quoted equivalent

WP version behavior:

WP version wp_unschedule_hook() return on success
4.9.x null (void _set_cron_array)
5.1+ integer count

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tests Fix unschedule command failing on WordPress 4.9 Mar 16, 2026
Copilot AI requested a review from swissspidy March 16, 2026 22:02
@codecov
Copy link

codecov bot commented Mar 17, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Cron_Event_Command.php 94.73% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 17, 2026 07:19
@swissspidy swissspidy requested a review from a team as a code owner March 17, 2026 07:19
Copilot AI review requested due to automatic review settings March 17, 2026 07:19
@swissspidy swissspidy merged commit 86c2b76 into try/fix-tests Mar 17, 2026
62 checks passed
@swissspidy swissspidy deleted the copilot/sub-pr-134 branch March 17, 2026 07:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes wp cron event unschedule incorrectly erroring on WordPress 4.9 by avoiding empty() checks against wp_unschedule_hook() (which returns null on success in WP 4.9), and by correcting a broken error message format string.

Changes:

  • Pre-counts scheduled events for a hook via _get_cron_array() to support WP versions where wp_unschedule_hook() doesn’t return a count.
  • Updates failure detection to only treat an explicit false return from wp_unschedule_hook() as failure.
  • Fixes/standardizes the “No events found…” and “Failed to unschedule…” error messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +315 to +318
foreach ( $crons as $cron ) {
if ( isset( $cron[ $hook ] ) ) {
$event_count += count( $cron[ $hook ] );
}
WP_CLI::error( sprintf( "Failed to unschedule events for hook '%s'.", $hook ) );
}

$count = ( is_int( $unscheduled ) && $unscheduled > 0 ) ? $unscheduled : $event_count;
swissspidy added a commit that referenced this pull request Mar 17, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants