Abilities API: Avoid _doing_it_wrong() on REST lookups of unknown abilities.#12555
Abilities API: Avoid _doing_it_wrong() on REST lookups of unknown abilities.#12555dhrupo wants to merge 1 commit into
Conversation
…lities. The Abilities REST controllers look up an ability (or category) by the name/slug supplied in the request URL and return a 404 when it is not found. They called wp_get_ability() / wp_get_ability_category() directly, which route through WP_Abilities_Registry::get_registered() — a method that intentionally triggers _doing_it_wrong() for an unknown name to catch developer misuse. Because the request name is client-controlled, a routine 404 for a non-existent ability fired a developer notice on every such request (even via the run endpoint's permission callback), polluting debug logs, surfacing in Query Monitor, and potentially corrupting the JSON response under a custom doing_it_wrong handler. Guard each lookup with the non-warning wp_has_ability() / wp_has_ability_category() existence check before calling the getter, so a missing ability yields a clean 404 without a _doing_it_wrong() notice. The registry getters continue to warn on direct misuse. Removes the now-unnecessary @expectedIncorrectUsage annotations from the affected not-found REST tests, which now assert a clean 404. Fixes #65644.
|
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. |
Trac ticket: https://core.trac.wordpress.org/ticket/65644
Summary
The Abilities REST controllers resolve an ability (or category) from the name/slug in the request URL and return a 404 when it does not exist — calling
wp_get_ability()/wp_get_ability_category()directly:Those functions delegate to
WP_Abilities_Registry::get_registered()/WP_Ability_Categories_Registry::get_registered(), which deliberately call_doing_it_wrong()for an unknown name to flag developer misuse. Because the name is client-controlled, a routine 404 for a non-existent ability fires_doing_it_wrong()on every such request — including via the run endpoint'spermission_callback, so unauthenticated requests trigger it too.Core's
rest_api_default_filters()suppresses thetrigger_error()output during REST requests (see #64260), but thedoing_it_wrong_runaction still fires, so the notice surfaces in Query Monitor, indoing_it_wrong_runlisteners / logging plugins, and in the test suite — the not-found REST controller tests currently carried@expectedIncorrectUsage. Existence-probing a public endpoint should not emit a developer "doing it wrong" warning.Reproduction
POST /wp-json/wp-abilities/v1/abilities/non/existent/runreturns the expected 404, but (surfaced via Query Monitor / thedoing_it_wrong_runaction)_doing_it_wrong()fires:See the before/after screenshots on the Trac ticket.
Fix
Guard each REST lookup with the non-warning existence helper before calling the getter:
A missing ability then yields a clean 404 with no
_doing_it_wrong(). The registry getters keep warning on direct misuse (that behavior is intentional and separately tested — unchanged here). Applied to all five lookup sites across the run, list, and categories controllers.Testing
Removed the now-unnecessary
@expectedIncorrectUsageannotations from the four affected not-found REST tests, which now assert a clean 404.Unexpected incorrect usage notice for WP_Abilities_Registry::get_registered.Full suites green:
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Locating the affected call sites, reproducing the notice, implementing the guards, and updating the tests. All changes were reviewed by me.