REST API: Fix namespace shadowing issue in route matching logic.
Following [47260] a namespace such as "test-ns" prevents any namespace such as "test-ns/v1" from being found when matching routes. While not best practice, this was an unintentional back-compat break; this patch restores the original behavior. Props david.binda, TimothyBlynJacobs. Fixes #48530. git-svn-id: https://develop.svn.wordpress.org/trunk@47351 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
74baee2f19
commit
4e18f78ced
@ -879,16 +879,17 @@ class WP_REST_Server {
|
||||
$method = $request->get_method();
|
||||
$path = $request->get_route();
|
||||
|
||||
$routes = array();
|
||||
$with_namespace = array();
|
||||
|
||||
foreach ( $this->get_namespaces() as $namespace ) {
|
||||
if ( 0 === strpos( ltrim( $path, '/' ), $namespace ) ) {
|
||||
$routes = $this->get_routes( $namespace );
|
||||
break;
|
||||
if ( 0 === strpos( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
|
||||
$with_namespace[] = $this->get_routes( $namespace );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $routes ) {
|
||||
if ( $with_namespace ) {
|
||||
$routes = array_merge( ...$with_namespace );
|
||||
} else {
|
||||
$routes = $this->get_routes();
|
||||
}
|
||||
|
||||
|
@ -1465,6 +1465,37 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 48530
|
||||
*/
|
||||
public function test_get_routes_no_namespace_overriding() {
|
||||
register_rest_route(
|
||||
'test-ns',
|
||||
'/test',
|
||||
array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => function() {
|
||||
return new WP_REST_Response( 'data', 204 );
|
||||
},
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
'test-ns/v1',
|
||||
'/test',
|
||||
array(
|
||||
'methods' => array( 'GET' ),
|
||||
'callback' => function() {
|
||||
return new WP_REST_Response( 'data', 204 );
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/test-ns/v1/test' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 204, $response->get_status(), '/test-ns/v1/test' );
|
||||
}
|
||||
|
||||
public function _validate_as_integer_123( $value, $request, $key ) {
|
||||
if ( ! is_int( $value ) ) {
|
||||
return new WP_Error( 'some-error', 'This is not valid!' );
|
||||
|
Loading…
Reference in New Issue
Block a user