From f34232edafc152a1005d1bf321ccf1cda9515534 Mon Sep 17 00:00:00 2001 From: whyisjake Date: Fri, 22 May 2020 21:00:07 +0000 Subject: [PATCH] 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. This brings the changes from [47842] to the 5.4 branch. Fixes #49749. Props TimothyBlynJacobs, skarabeq, afercia, skithund. Built from https://develop.svn.wordpress.org/branches/5.4@47843 git-svn-id: http://core.svn.wordpress.org/branches/5.4@47619 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 10 ++++++++-- wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index bdfd8fac42..4b9da0acb5 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 44b19f8a90..43440b7f1e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.4.2-alpha-47839'; +$wp_version = '5.4.2-alpha-47843'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.