WP_Route
WP_Route copied to clipboard
Routes are not matched correctly to the request
Hi!
I was just giving this a try today and noticed something quite odd - route handlers were being called when the request uri did not match the route.
This appears to be because of the following in handle()
:
$method = strtoupper($_SERVER['REQUEST_METHOD']);
$routes = array_merge($this->routes[$method], $this->routes['ANY']);
$requestURI = $this->requestURI();
$tokenizedRequestURI = $this->tokenize($requestURI);
foreach($routes as $key => $route){
// First, filter routes that do not have equal tokenized lengths
if(count($this->tokenize($route->route)) !== count($tokenizedRequestURI)){
unset($routes[$key]);
continue;
}
// Add more filtering here as routing gets more complex.
}
The routes are only matched based on the number of tokens in use (URLs are tokenised by being split using "/" as the delimiter). So, I could go to /documents/generate/pdf
, or /wp/wp-admin/admin.php
and it will call the handler for /documents/generate/pdf
.
I might submit a PR for this if I get time, but just thought it was worth flagging.
This library is basically unusable.