Skip to content

Don't fatal when invalid arguements are passed to wp_after_insert_post or after_delete_post. - #14356

Open
aaronjorbin wants to merge 4 commits into
WordPress:trunkfrom
aaronjorbin:followup/trac-64498/1
Open

aaronjorbin wants to merge 4 commits into
WordPress:trunkfrom
aaronjorbin:followup/trac-64498/1

Conversation

@aaronjorbin

Copy link
Copy Markdown
Member

Trac Ticket: https://core.trac.wordpress.org/ticket/64498#comment

Props bradshawtm

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Fable 5.1
Used for: Initial code skeleton and test suggestions; final implementation and tests were reviewed and edited by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jorbin, therssoftware.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…ility

Cause: multisite hooks _update_posts_count_on_delete() onto after_delete_post too (src/wp-includes/ms-default-filters.php:78). It reads $post->post_status on string 'random' → warning → test error. Not our function's bug. Other core callbacks choking on junk are out of scope.

Fix: tests now call wp_random_content_flush_rewrite_rules_for_post() directly instead of do_action(). Isolates unit under test from unrelated callbacks on same hook. Data provider renamed data_invalid_arguments, test test_invalid_arguments_do_not_flush_rewrite_rules. Same five cases, now keyed by value shape rather than hook name. _doing_it_wrong() still asserted on every case.
@robelsust

Copy link
Copy Markdown

Thanks for working on this follow-up, @aaronjorbin! Removing the scalar and class type declarations on wp_random_content_flush_rewrite_rules_for_post() is definitely the right approach to prevent fatals when wp_after_insert_post or after_delete_post are triggered by third-party plugins.

Testing this locally revealed a critical edge case regarding default parameters and ArgumentCountError:

  1. ArgumentCountError on 1-argument after_delete_post calls:
    The after_delete_post action was introduced in WP 3.2.0 with only $post_id. The second parameter $post was only added in WP 5.5.0 (do_action( 'after_delete_post', $post_id, $post )). Third-party plugins still invoke do_action( 'after_delete_post', $post_id ) with a single argument.
    Because wp_random_content_flush_rewrite_rules_for_post( $post_id, $post, ... ) currently has no default for $post, any single-argument call still fatals:

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function wp_random_content_flush_rewrite_rules_for_post(), 1 passed and at least 2 expected
    

    Providing defaults $post_id = 0, $post = null completely resolves this.

  2. Gracefully handling null === $post:
    When a caller fires do_action( 'after_delete_post', $post_id ) (or passes null), $post is null. Bailing with if ( null === $post ) { return; } before _doing_it_wrong() avoids firing false notices for valid legacy action invocations, while still preserving _doing_it_wrong() for genuinely invalid types (e.g. strings, arrays).

  3. Global variable placement:
    global $wp_rewrite; can be moved down immediately before its first usage on line 1582.

  4. Test coverage:
    Added test cases for single-argument and empty-argument calls to guarantee no ArgumentCountError can occur.

I've left inline code suggestions below for convenience!

…onal post parameter and improve error handling for invalid arguments
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.

2 participants