| 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 |
// Call the callback function with request arguments |
| 65 |
if ( is_callable( $callback['callback'] ) ) { |
| 66 |
if ( is_array( $callback['callback'] ) ) { |
| 67 |
$class = $callback['callback'][0]; |
| 68 |
$method = $callback['callback'][1]; |
| 69 |
|
| 70 |
if ( is_object( $class ) && method_exists( $class, $method ) ) { |
| 71 |
call_user_func_array( [ $class, $method ], [ $request_args ] ); |
| 72 |
} |
| 73 |
} else { |
| 74 |
call_user_func_array( $callback['callback'], [ $request_args ] ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
exit; |
| 79 |
} else { |
| 80 |
wp_send_json_error( 'Method not allowed', 405 ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// If the route doesn't exist, return a 404 response |
| 85 |
wp_send_json_error( 'Invalid API route', 404 ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register a GET route. |
| 91 |
* |
| 92 |
* @param string $route |
| 93 |
* @param mixed $callback |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function get( $route, $callback ) { |
| 98 |
$this->register_route( $route, $callback, 'GET' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register a POST route. |
| 103 |
* |
| 104 |
* @param string $route |
| 105 |
* @param mixed $callback |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function post( $route, $callback ) { |
| 110 |
$this->register_route( $route, $callback, 'POST' ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Register a route. |
| 115 |
* |
| 116 |
* @param string $route |
| 117 |
* @param mixed $callback |
| 118 |
* @param string $method |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
private function register_route( $route, $callback, $method = 'GET' ) { |
| 123 |
$this->routes[ $route ] = [ |
| 124 |
'callback' => $callback, |
| 125 |
'method' => strtoupper( $method ), |
| 126 |
]; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get registered routes. |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function get_routes() { |
| 135 |
return $this->routes; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Check if the route is allowed for the current request method. |
| 140 |
* |
| 141 |
* @param array $callback |
| 142 |
* @param string $request_method |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
private function is_route_method_allowed( $callback, $request_method ) { |
| 147 |
return $callback['method'] === 'ANY' || $callback['method'] === $request_method; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get request arguments. |
| 152 |
* |
| 153 |
* @param string $request_method |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
private function get_request_args( $request_method ) { |
| 158 |
$request_args = []; |
| 159 |
|
| 160 |
if ( 'GET' === $request_method ) { |
| 161 |
$request_args = wp_unslash( $_GET ); |
| 162 |
} elseif ( 'POST' === $request_method ) { |
| 163 |
$request_args = wp_unslash( $_POST ); |
| 164 |
} else { |
| 165 |
$request_args = wp_unslash( $_REQUEST ); |
| 166 |
} |
| 167 |
|
| 168 |
return $request_args; |
| 169 |
} |
| 170 |
} |
| 171 |
|