Repository navigation
Conversation
…arrays. Adds static methods get_readable_methods(), get_creatable_methods(), get_editable_methods(), and get_deletable_methods() to WP_REST_Server that return sets of HTTP methods as string arrays. Soft-deprecates the comma-separated string constants READABLE, CREATABLE, EDITABLE, DELETABLE, and ALLMETHODS via DocBlocks. Props moonmeister, westonruter, therssoftware. Fixes #66583.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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/66583
Description
WP_REST_Servercurrently defines HTTP method sets as comma-separated string constants:READABLE = 'GET'CREATABLE = 'POST'EDITABLE = 'POST, PUT, PATCH'DELETABLE = 'DELETE'ALLMETHODS = 'GET, POST, PUT, PATCH, DELETE'Because three of these happen to contain a single method, extensive ecosystem code (including WooCommerce, Elementor, and Jetpack) performs strict or loose equality checks such as
$request->get_method() === WP_REST_Server::READABLEor passes the constant directly to outbound HTTP requests. Consequently, these constants are effectively frozen and cannot gain new HTTP methods (such as RFC 10008 HTTPQUERYmethod from #65616) without breaking existing routes across the ecosystem.This PR addresses the issue by introducing dedicated static accessor methods on
WP_REST_Serverthat return sets of methods asstring[]:WP_REST_Server::get_readable_methods()->array( 'GET' )WP_REST_Server::get_creatable_methods()->array( 'POST' )WP_REST_Server::get_editable_methods()->array( 'POST', 'PUT', 'PATCH' )WP_REST_Server::get_deletable_methods()->array( 'DELETE' )Returning arrays establishes the correct type semantics (a set of HTTP methods is a list, preventing incorrect string equality checks like
===), allows the method sets to safely expand in future WordPress versions, and seamlessly integrates withregister_rest_route(), which already natively accepts arrays of methods.The legacy constants are soft-deprecated via
@deprecated 7.3.0docblocks without triggering runtime notices, keeping existing plugins 100% functional.Changes
get_readable_methods(),get_creatable_methods(),get_editable_methods(), andget_deletable_methods()toWP_REST_Server.@deprecated 7.3.0tags toREADABLE,CREATABLE,EDITABLE,DELETABLE, andALLMETHODS.tests/phpunit/tests/rest-api/wpRestServerGetMethods.php.Testing Instructions
Attribution
Props @moonmeister, @westonruter, @therssoftware.
AI Usage Disclosure:
Assisted by generative AI (Antigravity) to prepare the implementation, DocBlocks, and PHPUnit test suite in accordance with WordPress coding guidelines. All code and test coverage were reviewed and verified.