Make WordPress Core


Ignore:
Timestamp:
11/17/2015 02:38:31 AM (9 years ago)
Author:
rmccue
Message:

REST API: Require namespace when registering routes.

Props danielbachhuber.
Fixes #34416.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/rest-functions.php

    r35650 r35651  
    2121 * @param bool   $override  Optional. If the route already exists, should we override it? True overrides,
    2222 *                          false merges (with newer overriding if duplicate keys exist). Default false.
     23 * @return bool True on success, false on error.
    2324 */
    2425function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
    2526    /** @var WP_REST_Server $wp_rest_server */
    2627    global $wp_rest_server;
     28
     29    if ( empty( $namespace ) ) {
     30        /*
     31         * Non-namespaced routes are not allowed, with the exception of the main
     32         * and namespace indexes. If you really need to register a
     33         * non-namespaced route, call `WP_REST_Server::register_route` directly.
     34         */
     35        _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin or theme name and version.', '4.4.0' );
     36        return false;
     37    } else if ( empty( $route ) ) {
     38        _doing_it_wrong( 'register_rest_route', 'Route must be specified.', '4.4.0' );
     39        return false;
     40    }
    2741
    2842    if ( isset( $args['callback'] ) ) {
     
    4559    }
    4660
    47     if ( $namespace ) {
    48         $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    49     } else {
    50         /*
    51          * Non-namespaced routes are not allowed, with the exception of the main
    52          * and namespace indexes. If you really need to register a
    53          * non-namespaced route, call `WP_REST_Server::register_route` directly.
    54          */
    55         _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin name and version', '4.4.0' );
    56 
    57         $full_route = '/' . trim( $route, '/' );
    58     }
    59 
     61    $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    6062    $wp_rest_server->register_route( $namespace, $full_route, $args, $override );
     63    return true;
    6164}
    6265
Note: See TracChangeset for help on using the changeset viewer.