Taxonomy: Ensure _pad_term_counts() handles post types safely.#11579
Taxonomy: Ensure _pad_term_counts() handles post types safely.#11579liaisontw wants to merge 1 commit intoWordPress:trunkfrom
_pad_term_counts() handles post types safely.#11579Conversation
|
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. |
90627cc to
83744c4
Compare
…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.
83744c4 to
4b98249
Compare
This commit updates
_pad_term_counts()to use$wpdb->prepare()for all database queries, improving security and consistency. Additionally, it introduces a filter usingpost_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.