REST API: Improve error messages when registering an invalid route.

The error messages now include the REST API namespace and route to help identify the offending code.

Props lwangaman, timothyblynjacobs.
Fixes #50493.

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


git-svn-id: http://core.svn.wordpress.org/trunk@58329 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2024-08-25 22:51:14 +00:00
parent a45c25d272
commit a0e8d31590
2 changed files with 37 additions and 8 deletions

View File

@ -38,26 +38,55 @@ function register_rest_route( $route_namespace, $route, $args = array(), $overri
* 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' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Routes must be namespaced with plugin or theme name and version. Instead there seems to be an empty namespace \'%1$s\' for route \'%2$s\'.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
),
'4.4.0'
);
return false;
} elseif ( empty( $route ) ) {
_doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Route must be specified. Instead within the namespace \'%1$s\', there seems to be an empty route \'%2$s\'.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
),
'4.4.0'
);
return false;
}
$clean_namespace = trim( $route_namespace, '/' );
if ( $clean_namespace !== $route_namespace ) {
_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' );
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: string value of the namespace, 2: string value of the route. */
__( 'Namespace must not start or end with a slash. Instead namespace \'%1$s\' for route \'%2$s\' seems to contain a slash.' ),
'<code>' . $route_namespace . '</code>',
'<code>' . $route . '</code>'
),
'5.4.2'
);
}
if ( ! did_action( 'rest_api_init' ) ) {
_doing_it_wrong(
'register_rest_route',
__FUNCTION__,
sprintf(
/* translators: %s: rest_api_init */
__( 'REST API routes must be registered on the %s action.' ),
'<code>rest_api_init</code>'
/* translators: 1: rest_api_init, 2: string value of the route, 3: string value of the namespace. */
__( 'REST API routes must be registered on the %1$s action. Instead route \'%2$s\' with namespace \'%3$s\' was not registered on this action.' ),
'<code>rest_api_init</code>',
'<code>' . $route . '</code>',
'<code>' . $route_namespace . '</code>'
),
'5.1.0'
);

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-alpha-58932';
$wp_version = '6.7-alpha-58933';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.