| 1 |
<?php |
| 2 |
namespace Elementor\Data\V2\Base; |
| 3 |
|
| 4 |
use Elementor\Data\V2\Base\Exceptions\Data_Exception; |
| 5 |
use Elementor\Data\V2\Base\Exceptions\Error_500; |
| 6 |
use WP_REST_Server; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class purpose is to separate routing logic into one file. |
| 14 |
*/ |
| 15 |
abstract class Base_Route { |
| 16 |
const AVAILABLE_METHODS = [ |
| 17 |
WP_REST_Server::READABLE, |
| 18 |
WP_REST_Server::CREATABLE, |
| 19 |
WP_REST_Server::EDITABLE, |
| 20 |
WP_REST_Server::DELETABLE, |
| 21 |
WP_REST_Server::ALLMETHODS, |
| 22 |
]; |
| 23 |
|
| 24 |
/** |
| 25 |
* Controller of current endpoint. |
| 26 |
* |
| 27 |
* @var \Elementor\Data\V2\Base\Controller |
| 28 |
*/ |
| 29 |
protected $controller; |
| 30 |
|
| 31 |
/** |
| 32 |
* Current route, effect only in case the endpoint behave like sub-endpoint. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $route; |
| 37 |
|
| 38 |
/** |
| 39 |
* All register routes. |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $routes = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* Registered item route. |
| 47 |
* |
| 48 |
* @var array|null |
| 49 |
*/ |
| 50 |
protected $item_route = null; |
| 51 |
|
| 52 |
protected $id_arg_name = 'id'; |
| 53 |
protected $id_arg_type_regex = '[\d]+'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Ensure start-with and end-with slashes. |
| 57 |
* |
| 58 |
* '/' => '/' |
| 59 |
* 'abc' => '/abc/' |
| 60 |
* '/abc' => '/abc/' |
| 61 |
* 'abc/' => '/abc/' |
| 62 |
* '/abc/' => '/abc/' |
| 63 |
* |
| 64 |
* @param string $route |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
private function ensure_slashes( $route ) { |
| 69 |
if ( '/' !== $route[0] ) { |
| 70 |
$route = '/' . $route; |
| 71 |
} |
| 72 |
|
| 73 |
return trailingslashit( $route ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get base route. |
| 78 |
* This method should always return the base route starts with '/' and ends without '/'. |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get_base_route() { |
| 83 |
$name = $this->get_public_name(); |
| 84 |
$parent = $this->get_parent(); |
| 85 |
$parent_base = $parent->get_base_route(); |
| 86 |
$route = '/'; |
| 87 |
|
| 88 |
if ( ! ( $parent instanceof Controller ) ) { |
| 89 |
$route = $parent->item_route ? $parent->item_route['route'] . '/' : $this->route; |
| 90 |
} |
| 91 |
|
| 92 |
return untrailingslashit( '/' . trim( $parent_base . $route . $name, '/' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get permission callback. |
| 97 |
* |
| 98 |
* By default get permission callback from the controller. |
| 99 |
* |
| 100 |
* @param \WP_REST_Request $request Full data about the request. |
| 101 |
* |
| 102 |
* @return boolean |
| 103 |
*/ |
| 104 |
public function get_permission_callback( $request ) { |
| 105 |
return $this->controller->get_permission_callback( $request ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieves a collection of items. |
| 110 |
* |
| 111 |
* @param \WP_REST_Request $request Full data about the request. |
| 112 |
* |
| 113 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 114 |
*/ |
| 115 |
protected function get_items( $request ) { |
| 116 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Retrieves one item from the collection. |
| 121 |
* |
| 122 |
* @param string $id |
| 123 |
* @param \WP_REST_Request $request Full data about the request. |
| 124 |
* |
| 125 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 126 |
*/ |
| 127 |
protected function get_item( $id, $request ) { |
| 128 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Creates multiple items. |
| 133 |
* |
| 134 |
* @param \WP_REST_Request $request Full data about the request. |
| 135 |
* |
| 136 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 137 |
*/ |
| 138 |
protected function create_items( $request ) { |
| 139 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Creates one item. |
| 144 |
* |
| 145 |
* @param string $id id of request item. |
| 146 |
* @param \WP_REST_Request $request Full data about the request. |
| 147 |
* |
| 148 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 149 |
*/ |
| 150 |
protected function create_item( $id, $request ) { |
| 151 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Updates multiple items. |
| 156 |
* |
| 157 |
* @param \WP_REST_Request $request Full data about the request. |
| 158 |
* |
| 159 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 160 |
*/ |
| 161 |
protected function update_items( $request ) { |
| 162 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Updates one item. |
| 167 |
* |
| 168 |
* @param string $id id of request item. |
| 169 |
* @param \WP_REST_Request $request Full data about the request. |
| 170 |
* |
| 171 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 172 |
*/ |
| 173 |
protected function update_item( $id, $request ) { |
| 174 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Delete multiple items. |
| 179 |
* |
| 180 |
* @param \WP_REST_Request $request Full data about the request. |
| 181 |
* |
| 182 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 183 |
*/ |
| 184 |
protected function delete_items( $request ) { |
| 185 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Delete one item. |
| 190 |
* |
| 191 |
* @param string $id id of request item. |
| 192 |
* @param \WP_REST_Request $request Full data about the request. |
| 193 |
* |
| 194 |
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 195 |
*/ |
| 196 |
protected function delete_item( $id, $request ) { |
| 197 |
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Register the endpoint. |
| 202 |
* |
| 203 |
* By default: register get items route. |
| 204 |
*/ |
| 205 |
protected function register() { |
| 206 |
$this->register_items_route(); |
| 207 |
} |
| 208 |
|
| 209 |
protected function register_route( $route = '', $methods = WP_REST_Server::READABLE, $args = [] ) { |
| 210 |
if ( ! in_array( $methods, self::AVAILABLE_METHODS, true ) ) { |
| 211 |
trigger_error( "Invalid method: '$methods'.", E_USER_ERROR ); // phpcs:ignore |
| 212 |
} |
| 213 |
|
| 214 |
$route = $this->get_base_route() . $route; |
| 215 |
|
| 216 |
$this->routes [] = [ |
| 217 |
'args' => $args, |
| 218 |
'route' => $route, |
| 219 |
]; |
| 220 |
|
| 221 |
/** |
| 222 |
* Determine behaviour of `base_callback()` and `get_permission_callback()`: |
| 223 |
* For `base_callback()` which applying the action. |
| 224 |
* Whether it's a one item request and should call `get_item_permission_callback()` or it's mutil items request and should call `get_items_permission_callback()`. |
| 225 |
*/ |
| 226 |
$is_multi = ! empty( $args['is_multi'] ); |
| 227 |
|
| 228 |
if ( $is_multi ) { |
| 229 |
unset( $args['is_multi'] ); |
| 230 |
} |
| 231 |
|
| 232 |
$callback = function ( $request ) use ( $methods, $args, $is_multi ) { |
| 233 |
return $this->base_callback( $methods, $request, $is_multi ); |
| 234 |
}; |
| 235 |
|
| 236 |
return register_rest_route( $this->controller->get_namespace(), $route, [ |
| 237 |
[ |
| 238 |
'args' => $args, |
| 239 |
'methods' => $methods, |
| 240 |
'callback' => $callback, |
| 241 |
'permission_callback' => function ( $request ) { |
| 242 |
return $this->get_permission_callback( $request ); |
| 243 |
}, |
| 244 |
], |
| 245 |
] ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Register items route. |
| 250 |
* |
| 251 |
* @param string $methods |
| 252 |
* @param array $args |
| 253 |
*/ |
| 254 |
public function register_items_route( $methods = WP_REST_Server::READABLE, $args = [] ) { |
| 255 |
$args['is_multi'] = true; |
| 256 |
|
| 257 |
$this->register_route( '', $methods, $args ); |
| 258 |
} |
| 259 |
|
| 260 |
public function register_item_route( $methods = WP_REST_Server::READABLE, $args = [], $route = '/' ) { |
| 261 |
if ( ! empty( $args['id_arg_name'] ) ) { |
| 262 |
$this->id_arg_name = $args['id_arg_name']; |
| 263 |
|
| 264 |
unset( $args['id_arg_name'] ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! empty( $args['id_arg_type_regex'] ) ) { |
| 268 |
$this->id_arg_type_regex = $args['id_arg_type_regex']; |
| 269 |
|
| 270 |
unset( $args['id_arg_type_regex'] ); |
| 271 |
} |
| 272 |
|
| 273 |
$args = array_merge( [ |
| 274 |
$this->id_arg_name => [ |
| 275 |
'description' => 'Unique identifier for the object.', |
| 276 |
'type' => 'string', |
| 277 |
'required' => true, |
| 278 |
], |
| 279 |
], $args ); |
| 280 |
|
| 281 |
$route .= '(?P<' . $this->id_arg_name . '>' . $this->id_arg_type_regex . ')'; |
| 282 |
|
| 283 |
$this->item_route = [ |
| 284 |
'args' => $args, |
| 285 |
'route' => $route, |
| 286 |
]; |
| 287 |
|
| 288 |
$this->register_route( $route, $methods, $args ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Base callback. |
| 293 |
* All reset requests from the client should pass this function. |
| 294 |
* |
| 295 |
* @param string $methods |
| 296 |
* @param \WP_REST_Request $request |
| 297 |
* @param bool $is_multi |
| 298 |
* @param array $args |
| 299 |
* |
| 300 |
* @return mixed|\WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 301 |
*/ |
| 302 |
public function base_callback( $methods, $request, $is_multi = false, $args = [] ) { |
| 303 |
if ( $request ) { |
| 304 |
$json_params = $request->get_json_params(); |
| 305 |
|
| 306 |
if ( $json_params ) { |
| 307 |
$request->set_body_params( $json_params ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
$args = wp_parse_args( $args, [ |
| 312 |
'is_debug' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), |
| 313 |
] ); |
| 314 |
|
| 315 |
$result = new \WP_Error( 'invalid_methods', 'route not supported.' ); |
| 316 |
|
| 317 |
$request->set_param( 'is_multi', $is_multi ); |
| 318 |
|
| 319 |
try { |
| 320 |
switch ( $methods ) { |
| 321 |
case WP_REST_Server::READABLE: |
| 322 |
$result = $is_multi ? $this->get_items( $request ) : $this->get_item( $request->get_param( 'id' ), $request ); |
| 323 |
break; |
| 324 |
|
| 325 |
case WP_REST_Server::CREATABLE: |
| 326 |
$result = $is_multi ? $this->create_items( $request ) : $this->create_item( $request->get_param( 'id' ), $request ); |
| 327 |
break; |
| 328 |
|
| 329 |
case WP_REST_Server::EDITABLE: |
| 330 |
$result = $is_multi ? $this->update_items( $request ) : $this->update_item( $request->get_param( 'id' ), $request ); |
| 331 |
break; |
| 332 |
|
| 333 |
case WP_REST_Server::DELETABLE: |
| 334 |
$result = $is_multi ? $this->delete_items( $request ) : $this->delete_item( $request->get_param( 'id' ), $request ); |
| 335 |
break; |
| 336 |
} |
| 337 |
} catch ( Data_Exception $e ) { |
| 338 |
$result = $e->to_wp_error(); |
| 339 |
} catch ( \Exception $e ) { |
| 340 |
if ( empty( $args['is_debug'] ) ) { |
| 341 |
$result = ( new Error_500() )->to_wp_error(); |
| 342 |
} else { |
| 343 |
// For frontend. |
| 344 |
$exception_mapping = [ |
| 345 |
'trace' => $e->getTrace(), |
| 346 |
'file' => $e->getFile(), |
| 347 |
'line' => $e->getLine(), |
| 348 |
]; |
| 349 |
|
| 350 |
$e->debug = $exception_mapping; |
| 351 |
|
| 352 |
$result = ( new Data_Exception( $e->getMessage(), $e->getCode(), $e ) )->to_wp_error(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
return rest_ensure_response( $result ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Constructor. |
| 361 |
* |
| 362 |
* Run `$this->register()`. |
| 363 |
* |
| 364 |
* @param \Elementor\Data\V2\Base\Controller $controller |
| 365 |
* @param string $route |
| 366 |
*/ |
| 367 |
protected function __construct( Controller $controller, $route ) { |
| 368 |
$this->controller = $controller; |
| 369 |
$this->route = $this->ensure_slashes( $route ); |
| 370 |
|
| 371 |
$this->register(); |
| 372 |
} |
| 373 |
} |
| 374 |
|