Repository navigation
Don't fatal when invalid arguements are passed to wp_after_insert_post or after_delete_post. - #14356
aaronjorbin wants to merge 5 commits into
Conversation
…t or after_delete_post. Trac Ticket: https://core.trac.wordpress.org/ticket/64498#comment Props bradshawtm
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…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.
|
Thanks for working on this follow-up, @aaronjorbin! Removing the scalar and class type declarations on Testing this locally revealed a critical edge case regarding default parameters and
I've left inline code suggestions below for convenience! |
…onal post parameter and improve error handling for invalid arguments
robelsust
left a comment
There was a problem hiding this comment.
Thanks for addressing this so quickly, @aaronjorbin! Handling the fallback lookup via $post = get_post( (int) $post_id ) when $post is omitted and updating _update_posts_count_on_delete() in multisite cleanly resolves the ArgumentCountError regression. The test coverage is comprehensive and all CI checks pass.
Left a couple of minor notes/suggestions on the diff. Looks great!
| * @see WP_Rewrite::random_rewrite_rules() | ||
| * @global WP_Rewrite $wp_rewrite WordPress rewrite component. | ||
| * | ||
| * @param int $post_id Post ID. |
There was a problem hiding this comment.
| * @param int $post_id Optional. Post ID. Default 0. | |
| * @param WP_Post|null $post Optional. Post object. Default null. | |
| * @param bool $update Optional. Whether this is an existing post being updated. Default false. | |
| * @param WP_Post|null $post_before Optional. Post object before the update, null for new posts. Default null. | |
| */ | |
| function wp_random_content_flush_rewrite_rules_for_post( $post_id = 0, $post = null, $update = false, $post_before = null ): void { | |
| if ( null === $post ) { | |
| return; | |
| } | |
| if ( ! $post instanceof WP_Post ) { | |
| _doing_it_wrong( | |
| __FUNCTION__, | |
| sprintf( | |
| /* translators: 1: $post, 2: WP_Post */ | |
| __( 'The %1$s argument must be a %2$s object.' ), | |
| '<code>$post</code>', | |
| '<code>WP_Post</code>' | |
| ), | |
| '7.2.0' | |
| ); | |
| return; | |
| } | |
| if ( null !== $post_before && ! $post_before instanceof WP_Post ) { | |
| _doing_it_wrong( | |
| __FUNCTION__, | |
| sprintf( | |
| /* translators: 1: $post_before, 2: WP_Post */ | |
| __( 'The %1$s argument must be a %2$s object or null.' ), | |
| '<code>$post_before</code>', | |
| '<code>WP_Post</code>' | |
| ), | |
| '7.2.0' | |
| ); | |
| $post_before = null; | |
| } | |
| global $wp_rewrite; | |
| if ( ! $wp_rewrite->using_permalinks() || ! wp_is_random_content_redirect_enabled() ) { | |
| return; | |
| } |
| $this->assertSame( 0, $flushes->get_call_count(), 'Only pages and posts at the random base should flush the rewrite rules.' ); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
| /** | |
| /** | |
| * Tests that missing or null post arguments (such as legacy after_delete_post calls with 1 argument) | |
| * do not fatal, do not trigger doing_it_wrong, and do not flush rewrite rules. | |
| * | |
| * @dataProvider data_missing_or_null_post_arguments | |
| * | |
| * @param array $args The arguments to call the function with. | |
| */ | |
| public function test_missing_or_null_post_arguments_do_not_flush_rewrite_rules( array $args ): void { | |
| $flushes = new MockAction(); | |
| add_filter( 'pre_update_option_rewrite_rules', array( $flushes, 'filter' ) ); | |
| wp_random_content_flush_rewrite_rules_for_post( ...$args ); | |
| $this->assertSame( 0, $flushes->get_call_count(), 'Missing or null arguments should not flush the rewrite rules.' ); | |
| } | |
| /** | |
| * Data provider for test_missing_or_null_post_arguments_do_not_flush_rewrite_rules. | |
| * | |
| * @return array<string, array{array}> | |
| */ | |
| public function data_missing_or_null_post_arguments(): array { | |
| return array( | |
| 'single argument (legacy after_delete_post)' => array( array( 123 ) ), | |
| 'no arguments' => array( array() ), | |
| 'nulls' => array( array( null, null, false, null ) ), | |
| 'nulls without optional values' => array( array( null, null ) ), | |
| ); | |
| } | |
| /** | |
| * @dataProvider data_invalid_arguments | |
| * | |
| * @param array $args The arguments to call the function with. | |
| */ | |
| public function test_invalid_arguments_do_not_flush_rewrite_rules( array $args ): void { | |
| $this->setExpectedIncorrectUsage( 'wp_random_content_flush_rewrite_rules_for_post' ); | |
| $flushes = new MockAction(); | |
| add_filter( 'pre_update_option_rewrite_rules', array( $flushes, 'filter' ) ); | |
| wp_random_content_flush_rewrite_rules_for_post( ...$args ); | |
| $this->assertSame( 0, $flushes->get_call_count(), 'Invalid arguments should not flush the rewrite rules.' ); | |
| } | |
| /** | |
| * Data provider. | |
| * | |
| * Core never fires the `wp_after_insert_post` and `after_delete_post` actions with invalid | |
| * arguments, but third-party code can. Doing so is incorrect usage and must not cause a | |
| * fatal error. | |
| * | |
| * @return array<string, array{array}> | |
| */ | |
| public function data_invalid_arguments(): array { | |
| return array( | |
| 'strings' => array( array( 'random', 'random', 'yes', 'random' ) ), | |
| 'arrays' => array( array( array(), array( 'post_name' => 'random' ), 1, array() ) ), | |
| 'strings without optional values' => array( array( 'random', 'random' ) ), | |
| ); | |
| } |
tbradsha
left a comment
There was a problem hiding this comment.
Thanks for your patience! I would probably just return early rather than doing a lookup if encountering an unexpected type. That'll allow it to handle it gracefully without the added complexity/overhead of a lookup.
But I'll defer to others more familiar with this code.
Co-authored-by: tbradsha <32492176+tbradsha@users.noreply.github.com>
|
I strongly agree with @tbradsha's suggestion to return early rather than falling back to There is an important lifecycle reason for this on
Returning early if |
|
There are four plugins that include references to |
I agree that the sample size there doesn't turn much up. That said, it's only representative of the most recent versions of plugins. Also, in the wild people install all sorts of plugins/themes that aren't in the WP.org repo, and also manually patch things and add code snippets all over the place. Without this PR, any such instances would fatal when updating WordPress, so thanks for taking this on. :^) |
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.