WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/24/2020 06:05:12 PM (11 months ago)
Author:
kadamwhite
Message:

REST API: Fix namespace shadowing issue in route matching logic.

Following [47260] a namespace such as "test-ns" prevents any namespace such as "test-ns/v1" from being found when matching routes.
While not best practice, this was an unintentional back-compat break; this patch restores the original behavior.

Props david.binda, TimothyBlynJacobs.
Fixes #48530.

File:
1 edited

Legend:

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

    r47326 r47351  
    880880        $path   = $request->get_route();
    881881
    882         $routes = array();
     882        $with_namespace = array();
    883883
    884884        foreach ( $this->get_namespaces() as $namespace ) {
    885             if ( 0 === strpos( ltrim( $path, '/' ), $namespace ) ) {
    886                 $routes = $this->get_routes( $namespace );
    887                 break;
    888             }
    889         }
    890 
    891         if ( ! $routes ) {
     885            if ( 0 === strpos( trailingslashit( ltrim( $path, '/' ) ), $namespace ) ) {
     886                $with_namespace[] = $this->get_routes( $namespace );
     887            }
     888        }
     889
     890        if ( $with_namespace ) {
     891            $routes = array_merge( ...$with_namespace );
     892        } else {
    892893            $routes = $this->get_routes();
    893894        }
Note: See TracChangeset for help on using the changeset viewer.