REST API: Ensure proper namespacing when registering routes.

The PR will corerce routes that have a leading slash and throwing a `_doing_it_wrong` notice while ensuring a proper namespace.

Fixes #49749.
Props TimothyBlynJacobs, skarabeq, afercia.



git-svn-id: https://develop.svn.wordpress.org/trunk@47842 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock
2020-05-22 20:46:03 +00:00
parent 3fe58b173f
commit 3ab28fd579
2 changed files with 28 additions and 2 deletions

View File

@@ -44,6 +44,12 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f
return false;
}
$clean_namespace = trim( $namespace, '/' );
if ( $clean_namespace !== $namespace ) {
_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' );
}
if ( ! did_action( 'rest_api_init' ) ) {
_doing_it_wrong(
'register_rest_route',
@@ -84,8 +90,8 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f
$arg_group['args'] = array_merge( $common_args, $arg_group['args'] );
}
$full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
rest_get_server()->register_route( $namespace, $full_route, $args, $override );
$full_route = '/' . $clean_namespace . '/' . trim( $route, '/' );
rest_get_server()->register_route( $clean_namespace, $full_route, $args, $override );
return true;
}