REST API: Require namespace when registering routes.

Props danielbachhuber.
Fixes #34416.

Built from https://develop.svn.wordpress.org/trunk@35651


git-svn-id: http://core.svn.wordpress.org/trunk@35615 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2015-11-17 02:39:30 +00:00
parent b0ee5efef2
commit b03e036e94
2 changed files with 17 additions and 14 deletions

View File

@ -20,11 +20,25 @@
* multiple methods. Default empty array. * multiple methods. Default empty array.
* @param bool $override Optional. If the route already exists, should we override it? True overrides, * @param bool $override Optional. If the route already exists, should we override it? True overrides,
* false merges (with newer overriding if duplicate keys exist). Default false. * false merges (with newer overriding if duplicate keys exist). Default false.
* @return bool True on success, false on error.
*/ */
function register_rest_route( $namespace, $route, $args = array(), $override = false ) { function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
/** @var WP_REST_Server $wp_rest_server */ /** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server; global $wp_rest_server;
if ( empty( $namespace ) ) {
/*
* Non-namespaced routes are not allowed, with the exception of the main
* and namespace indexes. If you really need to register a
* non-namespaced route, call `WP_REST_Server::register_route` directly.
*/
_doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin or theme name and version.', '4.4.0' );
return false;
} else if ( empty( $route ) ) {
_doing_it_wrong( 'register_rest_route', 'Route must be specified.', '4.4.0' );
return false;
}
if ( isset( $args['callback'] ) ) { if ( isset( $args['callback'] ) ) {
// Upgrade a single set to multiple. // Upgrade a single set to multiple.
$args = array( $args ); $args = array( $args );
@ -44,20 +58,9 @@ function register_rest_route( $namespace, $route, $args = array(), $override = f
$arg_group = array_merge( $defaults, $arg_group ); $arg_group = array_merge( $defaults, $arg_group );
} }
if ( $namespace ) { $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
$full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
} else {
/*
* Non-namespaced routes are not allowed, with the exception of the main
* and namespace indexes. If you really need to register a
* non-namespaced route, call `WP_REST_Server::register_route` directly.
*/
_doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin name and version', '4.4.0' );
$full_route = '/' . trim( $route, '/' );
}
$wp_rest_server->register_route( $namespace, $full_route, $args, $override ); $wp_rest_server->register_route( $namespace, $full_route, $args, $override );
return true;
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-beta4-35650'; $wp_version = '4.4-beta4-35651';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.