Skip to content

Taxonomy: Ensure _pad_term_counts() handles post types safely.#11579

Open
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/65055-sql-filter
Open

Taxonomy: Ensure _pad_term_counts() handles post types safely.#11579
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/65055-sql-filter

Conversation

@liaisontw
Copy link
Copy Markdown

@liaisontw liaisontw commented Apr 15, 2026

This commit updates _pad_term_counts() to use $wpdb->prepare() for all database queries, improving security and consistency. Additionally, it introduces a filter using post_type_exists() to ensure that only registered post types are queried, aligning the behavior with _update_post_term_count().Includes comprehensive unit tests to verify the logic and prevent regressions.Fixes #65055.See #11542.

Description
This PR builds upon the work by @rajeshcp in #11542. It implements the $wpdb->prepare() fix for #65055 and adds comprehensive unit tests to ensure the logic is robust.

While drafting the unit tests, I identified a scenario where _pad_term_counts() could query non-existent post types (see test_pad_term_counts_with_invalid_post_types). Following @ABCD95's suggestion, I've aligned the logic with _update_post_term_count() by filtering via post_type_exists().

Key Changes:

Security: Refactored direct SQL string interpolation to use $wpdb->prepare() to prevent potential SQL injection and ensure standard database interaction.

Consistency: Integrated array_filter( $object_types, 'post_type_exists' ) before the query execution. This aligns _pad_term_counts() with the logic found in _update_post_term_count(), preventing queries from running against invalid or ghost post types that may still exist in the taxonomy's object_type array.

Robustness: Added an early exit if no valid post types remain after filtering, saving unnecessary database calls.

Unit Testing: Introduced comprehensive test cases in Tests_Taxonomy (including a specific test for invalid post types) to ensure the logic works as expected and to prevent future regressions.

I have verified these changes with a zero-dependency local test script, confirming that the SQL arguments are correctly filtered and parameterized compared to the unpatched version.

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

Use of AI Tools
AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 3 Flash
Used for: Brainstorming the zero-dependency test script to isolate the core logic during local verification; assisting in drafting the technical description and PR documentation based on my local test results. All code changes and logic were manually implemented and verified 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
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 liaison.

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.

@liaisontw liaisontw force-pushed the fix/65055-sql-filter branch 9 times, most recently from 90627cc to 83744c4 Compare April 16, 2026 06:27
…commit updates `_pad_term_counts()` to use `$wpdb->prepare()` for all database queries, improving security and consistency. Additionally, it introduces a filter using `post_type_exists()` to ensure that only registered post types are queried, aligning the behavior with `_update_post_term_count()`.Includes comprehensive unit tests to verify the logic and prevent regressions.Fixes #65055.See WordPress#11542.
@liaisontw liaisontw force-pushed the fix/65055-sql-filter branch from 83744c4 to 4b98249 Compare April 16, 2026 06:32
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