Skip to content

Rewrite Rules: Check the post type before reading the random base - #16066

Open
mukeshpanchal27 wants to merge 2 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/random-base-check-order
Open

mukeshpanchal27 wants to merge 2 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/random-base-check-order

Conversation

@mukeshpanchal27

Copy link
Copy Markdown
Member

wp_random_content_flush_rewrite_rules_for_post() is new in 7.2 and runs on wp_after_insert_post and after_delete_post. Its first check was $wp_rewrite->get_random_base() === $page->post_name, and the post type check came second.

The default random base is translatable. So get_random_base() calls switch_to_locale( get_locale() ), reads the string, and switches back. When the user locale differs from the site locale, each call fully switches the locale and back, which fires switch_locale and restore_previous_locale and resets text domains. This happened for every revision, autosave, attachment and menu item, although only top-level pages and posts can ever match.

This PR checks the post type and parent first, so only top-level pages and posts read the random base. The behavior does not change: the same posts still flush the rewrite rules.

Tests

  • I added test_saving_posts_that_cannot_be_at_the_random_base_does_not_read_the_random_base(). It counts reads of the translatable random base while saving a child page, an attachment and a revision, all with the slug random.
  • The new test fails without the fix and passes with it.
  • All 339 tests in Tests_Query_RandomContentRedirect pass, including the existing flush tests.

Trac ticket: https://core.trac.wordpress.org/ticket/64498

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the issue while reviewing 7.2 changes, drafting the fix and test, and running the tests. I reviewed the final change.


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.

🤖 Generated with Claude Code

`wp_random_content_flush_rewrite_rules_for_post()` runs on every post insert and delete. It called `WP_Rewrite::get_random_base()` before checking the post type, so every revision, autosave, attachment and menu item read the random base. Reading the default base switches to the site locale and back, which is costly when the user locale differs from the site locale.

This checks the post type and parent first, so only top-level pages and posts read the random base.

Follow-up to [64402].

See #64498.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

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.

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

Props mukesh27.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@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.

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.

1 participant