Skip to content

Fix false positive in cron-duplicates check for hooks with unique args#210

Merged
swissspidy merged 5 commits intomainfrom
copilot/fix-duplicate-cron-job-complaint
Mar 20, 2026
Merged

Fix false positive in cron-duplicates check for hooks with unique args#210
swissspidy merged 5 commits intomainfrom
copilot/fix-duplicate-cron-job-complaint

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

The cron-duplicates check grouped jobs solely by hook name, causing false positives for publish_future_post — a WP core hook that schedules one distinct cron job per future post, each with a unique post ID argument.

Changes

  • src/Check/Cron_Duplicates.php: Replace the hook-name-only grouping key with a json_encode([hook, args]) compound key. Jobs are now only counted as duplicates when both the hook name and arguments match.
// Before — unique-arg jobs (e.g. publish_future_post) incorrectly counted as duplicates
$job_counts[ $job['hook'] ]

// After — only true duplicates (same hook AND same args) are counted
$key = json_encode( array( $job['hook'], $job['args'] ) );
$job_counts[ $key ]
  • features/check-cron-duplicates.feature: Add scenario registering 15 publish_future_post-style jobs (same hook, unique args each) and asserting the check reports success.
Original prompt

This section details on the original issue you should resolve

<issue_title>Duplicate-cron complains about WP core functionality (publish_future_post)</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior

wp doctor check --all --spotlight
Complains about duplicate cron-jobs.

The duplicate cronjobs is "publish_future_post"
Which is WP Core generating those "single-events" to publish post in the future.

If a site is using this functionality, and have more than 10 post set to the future, WP Doctor command will give an error.
Even through its expected behavior.

it could be argued that WP Core, should have the post_id as part of the single-cronjob naming, but thats not the case.

Example of WP Core registrering cronjobs using a non-unique key https://github.com/WordPress/WordPress/blob/87cdca8cd61887cd6685c0e11a06e913415524a7/wp-includes/post.php#L4514

maybe a simple solution could be to check if the saved parameter, is the same on multiple jobs.
</issue_description>

Comments on the Issue (you are @copilot in this section)

@danielbachhuber Thanks for the report, @jespervnielsen !

We could include an exclusion for this particular scenario.

Feel free to submit a pull request, if you'd like. Here is some guidance on our pull request best practices.</comment_new>


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…blish_future_post)

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate cron complaints about WP core functionality Fix false positive in cron-duplicates check for hooks with unique args Mar 19, 2026
Copilot AI requested a review from swissspidy March 19, 2026 17:27
@swissspidy
Copy link
Member

@copilot failing test(s):


002 Scenario: Cron check is healthy when same hook has unique args (e.g. publish_future_post) # features/check-cron-duplicates.feature:29
      When I run `wp doctor check cron-duplicates`                                            # features/check-cron-duplicates.feature:38
        $ wp doctor check cron-duplicates
        name	status	message
        cron-duplicates	error	Detected 10 or more of the same cron job.
        
        PHP Notice:  Undefined index: args in src/Check/Cron_Duplicates.php on line 22

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@copilot failing test(s):

...

Fixed in 9bafe58. The root cause was that wp cron event list doesn't include args in its default fields — only hook, next_run_gmt, next_run_relative, and recurrence.

Two changes:

  1. Cron::get_crons() now explicitly requests fields=hook,args so args is always present in the returned data.
  2. Cron_Duplicates::run() also has a defensive isset guard as a safety net for any edge cases.

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Check/Cron_Duplicates.php 80.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy added this to the 2.3.1 milestone Mar 19, 2026
@swissspidy swissspidy marked this pull request as ready for review March 19, 2026 22:39
@swissspidy swissspidy requested a review from a team as a code owner March 19, 2026 22:39
Copilot AI review requested due to automatic review settings March 19, 2026 22:39
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

This PR adjusts the cron-duplicates doctor check to avoid false positives for WordPress core hooks like publish_future_post, where multiple cron events share the same hook name but legitimately differ by arguments.

Changes:

  • Update duplicate detection to group cron events by (hook, args) instead of hook alone.
  • Update cron-event listing to include the args field so the check can differentiate events.
  • Add a Behat scenario verifying that many publish_future_post-style events with unique args do not trigger an error.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Check/Cron_Duplicates.php Changes grouping key to incorporate args to reduce false positives.
src/Check/Cron.php Requests hook,args fields from wp cron event list to support updated grouping.
features/check-cron-duplicates.feature Adds acceptance coverage for “same hook, unique args” cron scheduling.

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

Comment on lines +21 to +25
array(
'format' => 'json',
'fields' => 'hook,args',
)
);
Copy link

Copilot AI Mar 19, 2026

Choose a reason for hiding this comment

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

Cron::get_crons() is shared by Cron_Count and Cron_Duplicates. Requesting args here means the cron-count check now retrieves/serializes args for every event even though it only calls count($crons), which could be a performance/memory regression on sites with large/complex args. Consider keeping get_crons() lightweight and only requesting args for the duplicates check (e.g., accept a $fields parameter and cache per field-set, or add a dedicated method used only by Cron_Duplicates).

Copilot uses AI. Check for mistakes.
swissspidy and others added 2 commits March 19, 2026 23:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy merged commit 54eb4e0 into main Mar 20, 2026
59 checks passed
@swissspidy swissspidy deleted the copilot/fix-duplicate-cron-job-complaint branch March 20, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate-cron complains about WP core functionality (publish_future_post)

3 participants