PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.2.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.2.1
1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 1.5.1 All 26 releases
flywp / includes / Router.php

Router.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 0.2.1, at includes/Router.php

174 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FlyWP;
4
5 use WP;
6
7 class Router {
8
9 /**
10 * Base route for API.
11 *
12 * @var string
13 */
14 private $base_route = 'fly-api';
15
16 /**
17 * Registered routes.
18 *
19 * @var array
20 */
21 private $routes = [];
22
23 /**
24 * Initialize the class.
25 *
26 * @return void
27 */
28 public function init() {
29 add_action( 'init', [ $this, 'register_routes' ], 0 );
30 add_action( 'wp', [ $this, 'handle_request' ], 0 );
31 }
32
33 /**
34 * Register routes.
35 *
36 * @return void
37 */
38 public function register_routes() {
39 add_rewrite_endpoint( $this->base_route, EP_ROOT );
40 }
41
42 /**
43 * Handle API request.
44 *
45 * @param WP $wp
46 *
47 * @return void
48 */
49 public function handle_request() {
50 global $wp;
51
52 if ( isset( $wp->query_vars[ $this->base_route ] ) ) {
53 $route = $wp->query_vars[ $this->base_route ];
54
55 // Check if the requested route exists
56 if ( isset( $this->routes[ $route ] ) ) {
57 $callback = $this->routes[ $route ];
58 $request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET';
59
60 // Check if the route is allowed for the current request method
61 if ( $this->is_route_method_allowed( $callback, $request_method ) ) {
62 $request_args = $this->get_request_args( $request_method );
63
64 // var_dump( $request_args );
65 // die;
66
67 // Call the callback function with request arguments
68 if ( is_callable( $callback['callback'] ) ) {
69 if ( is_array( $callback['callback'] ) ) {
70 $class = $callback['callback'][0];
71 $method = $callback['callback'][1];
72
73 if ( is_object( $class ) && method_exists( $class, $method ) ) {
74 call_user_func_array( [ $class, $method ], [ $request_args ] );
75 }
76 } else {
77 call_user_func_array( $callback['callback'], [ $request_args ] );
78 }
79 }
80
81 exit;
82 } else {
83 wp_send_json_error( 'Method not allowed', 405 );
84 }
85 }
86
87 // If the route doesn't exist, return a 404 response
88 wp_send_json_error( 'Invalid API route', 404 );
89 }
90 }
91
92 /**
93 * Register a GET route.
94 *
95 * @param string $route
96 * @param mixed $callback
97 *
98 * @return void
99 */
100 public function get( $route, $callback ) {
101 $this->register_route( $route, $callback, 'GET' );
102 }
103
104 /**
105 * Register a POST route.
106 *
107 * @param string $route
108 * @param mixed $callback
109 *
110 * @return void
111 */
112 public function post( $route, $callback ) {
113 $this->register_route( $route, $callback, 'POST' );
114 }
115
116 /**
117 * Register a route.
118 *
119 * @param string $route
120 * @param mixed $callback
121 * @param string $method
122 *
123 * @return void
124 */
125 private function register_route( $route, $callback, $method = 'GET' ) {
126 $this->routes[ $route ] = [
127 'callback' => $callback,
128 'method' => strtoupper( $method ),
129 ];
130 }
131
132 /**
133 * Get registered routes.
134 *
135 * @return array
136 */
137 public function get_routes() {
138 return $this->routes;
139 }
140
141 /**
142 * Check if the route is allowed for the current request method.
143 *
144 * @param array $callback
145 * @param string $request_method
146 *
147 * @return bool
148 */
149 private function is_route_method_allowed( $callback, $request_method ) {
150 return $callback['method'] === 'ANY' || $callback['method'] === $request_method;
151 }
152
153 /**
154 * Get request arguments.
155 *
156 * @param string $request_method
157 *
158 * @return array
159 */
160 private function get_request_args( $request_method ) {
161 $request_args = [];
162
163 if ( 'GET' === $request_method ) {
164 $request_args = wp_unslash( $_GET );
165 } elseif ( 'POST' === $request_method ) {
166 $request_args = wp_unslash( $_POST );
167 } else {
168 $request_args = wp_unslash( $_REQUEST );
169 }
170
171 return $request_args;
172 }
173 }
174