forked from Automattic/wp-openid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
121 lines (96 loc) · 2.99 KB
/
Copy pathRouter.php
File metadata and controls
121 lines (96 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace OpenIDConnectServer\Http;
use OAuth2\Request;
use OAuth2\Response;
class Router {
private const PREFIX = 'openid-connect';
private array $routes = array();
private array $rest_routes = array();
public static function make_url( string $route = '' ): string {
return home_url( "/$route" );
}
public static function make_rest_url( string $route ): string {
return rest_url( self::PREFIX . "/$route" );
}
public function __construct() {
add_action( 'template_redirect', array( $this, 'handle_request' ) );
}
public function add_route( string $route, RequestHandler $handler ) {
if ( isset( $this->rest_routes[ $route ] ) ) {
return;
}
$this->routes[ $route ] = $handler;
}
public function add_rest_route( string $route, RequestHandler $handler, array $methods = array( 'GET' ), array $args = array() ) {
$route_with_prefix = self::PREFIX . "/$route";
if ( isset( $this->rest_routes[ $route_with_prefix ] ) ) {
return;
}
$this->rest_routes[ $route_with_prefix ] = $handler;
add_action(
'rest_api_init',
function () use ( $route, $methods, $args ) {
register_rest_route(
self::PREFIX,
$route,
array(
'methods' => $methods,
'permission_callback' => '__return_true',
'callback' => array( $this, 'handle_rest_request' ),
'args' => $args,
)
);
}
);
}
private function get_current_route(): string {
$wp_url = get_site_url();
$installed_dir = wp_parse_url( $wp_url, PHP_URL_PATH );
// Requested URI relative to WP install.
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$uri = $installed_dir ? str_replace( $installed_dir, '', $uri ) : $uri;
$route = strtok( $uri, '?' );
return trim( $route, '/' );
}
/**
* This method is meant for internal use in this class only.
* It must not be used elsewhere.
* It's only public since it's used as a callback.
*/
public function handle_request() {
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$route = $this->get_current_route();
if ( ! isset( $this->routes[ $route ] ) ) {
return;
}
$handler = $this->routes[ $route ];
$this->do_handle_request( $handler );
}
/**
* This method is meant for internal use in this class only.
* It must not be used elsewhere.
* It's only public since it's used as a callback.
*/
public function handle_rest_request( $wp_request ) {
$route = $wp_request->get_route();
// Remove leading slashes.
$route = ltrim( $route, '/' );
if ( ! isset( $this->rest_routes[ $route ] ) ) {
$response = new Response();
$response->setStatusCode( 404 );
$response->send();
exit;
}
$handler = $this->rest_routes[ $route ];
$this->do_handle_request( $handler );
}
private function do_handle_request( RequestHandler $handler ) {
$request = Request::createFromGlobals();
$response = new Response();
$response = $handler->handle( $request, $response );
$response->send();
exit();
}
}