get_routes() публичный Метод

The route map is an associative array with path regexes as the keys. The value is an indexed array with the callback function/method as the first item, and a bitmask of HTTP methods as the second item (see the class constants). Each route can be mapped to more than one callback by using an array of the indexed arrays. This allows mapping e.g. GET requests to one callback and POST requests to another. Note that the path regexes (array keys) must have @ escaped, as this is used as the delimiter with preg_match()
С версии: 4.4.0
public get_routes ( ) : array
Результат array `'/path/regex' => array( $callback, $bitmask )` or `'/path/regex' => array( array( $callback, $bitmask ), ...)`.
 /**
  * Test if our route has been registerd correctly.
  *
  * Borrowed from the REST API.
  */
 public function test_route_availability()
 {
     // Check the route was registered correctly.
     $filtered_routes = $this->server->get_routes();
     $this->assertArrayHasKey('/wp/v2/oembed', $filtered_routes);
     $route = $filtered_routes['/wp/v2/oembed'];
     $this->assertCount(1, $route);
     $this->assertArrayHasKey('callback', $route[0]);
     $this->assertArrayHasKey('methods', $route[0]);
     $this->assertArrayHasKey('args', $route[0]);
 }
Пример #2
0
 /**
  * Test endpoint registration
  *
  * @since 0.2.0
  *
  * @group rest
  * @group settings_rest
  * @group group_rest
  * @group tracking_rest
  * @group session_rest
  * @group variant_rest
  */
 public function test_endpoints()
 {
     $the_route = $this->namespaced_route;
     $routes = $this->server->get_routes();
     foreach ($routes as $route => $route_config) {
         if (0 === strpos($the_route, $route)) {
             $this->assertTrue(is_array($route_config));
             foreach ($route_config as $i => $endpoint) {
                 $this->assertArrayHasKey('callback', $endpoint, get_class($this));
                 $this->assertArrayHasKey(0, $endpoint['callback'], get_class($this));
                 $this->assertArrayHasKey(1, $endpoint['callback'], get_class($this));
                 $this->assertTrue(is_callable(array($endpoint['callback'][0], $endpoint['callback'][1])), get_class($this));
             }
         }
     }
 }
Пример #3
0
/**
 * Sends the "Allow" header to state all methods that can be sent to the current route.
 *
 * @since 4.4.0
 *
 * @param WP_REST_Response $response Current response being served.
 * @param WP_REST_Server   $server   ResponseHandler instance (usually WP_REST_Server).
 * @param WP_REST_Request  $request  The request that was used to make current response.
 * @return WP_REST_Response Response to be served, with "Allow" header if route has allowed methods.
 */
function rest_send_allow_header($response, $server, $request)
{
    $matched_route = $response->get_matched_route();
    if (!$matched_route) {
        return $response;
    }
    $routes = $server->get_routes();
    $allowed_methods = array();
    // Get the allowed methods across the routes.
    foreach ($routes[$matched_route] as $_handler) {
        foreach ($_handler['methods'] as $handler_method => $value) {
            if (!empty($_handler['permission_callback'])) {
                $permission = call_user_func($_handler['permission_callback'], $request);
                $allowed_methods[$handler_method] = true === $permission;
            } else {
                $allowed_methods[$handler_method] = true;
            }
        }
    }
    // Strip out all the methods that are not allowed (false values).
    $allowed_methods = array_filter($allowed_methods);
    if ($allowed_methods) {
        $response->header('Allow', implode(', ', array_map('strtoupper', array_keys($allowed_methods))));
    }
    return $response;
}