| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | use Elementor\Data\V2\Base\Exceptions\Error_500; |
| 6 | 6 | use WP_REST_Server; |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; // Exit if accessed directly. | |
| 9 | + exit; // Exit if accessed directly | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Class purpose is to separate routing logic into one file. |
| @@ -112,21 +112,21 @@ | ||
| 112 | 112 | * |
| 113 | 113 | * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 114 | 114 | */ |
| 115 | 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 ] ); | |
| 116 | + return $this->controller->get_items( $request ); | |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * Retrieves one item from the collection. |
| 121 | 121 | * |
| 122 | - * @param string $id | |
| 122 | + * @param string $id | |
| 123 | 123 | * @param \WP_REST_Request $request Full data about the request. |
| 124 | 124 | * |
| 125 | 125 | * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 126 | 126 | */ |
| 127 | 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 ] ); | |
| 128 | + return $this->controller->get_item( $request ); | |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * Creates multiple items. |
| @@ -141,15 +141,15 @@ | ||
| 141 | 141 | |
| 142 | 142 | /** |
| 143 | 143 | * Creates one item. |
| 144 | 144 | * |
| 145 | - * @param string $id id of request item. | |
| 145 | + * @param string $id id of request item. | |
| 146 | 146 | * @param \WP_REST_Request $request Full data about the request. |
| 147 | 147 | * |
| 148 | 148 | * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 149 | 149 | */ |
| 150 | 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 ] ); | |
| 151 | + return $this->controller->create_item( $request ); | |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * Updates multiple items. |
| @@ -164,15 +164,15 @@ | ||
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * Updates one item. |
| 167 | 167 | * |
| 168 | - * @param string $id id of request item. | |
| 168 | + * @param string $id id of request item. | |
| 169 | 169 | * @param \WP_REST_Request $request Full data about the request. |
| 170 | 170 | * |
| 171 | 171 | * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 172 | 172 | */ |
| 173 | 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 ] ); | |
| 174 | + return $this->controller->update_item( $request ); | |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
| 178 | 178 | * Delete multiple items. |
| @@ -187,15 +187,15 @@ | ||
| 187 | 187 | |
| 188 | 188 | /** |
| 189 | 189 | * Delete one item. |
| 190 | 190 | * |
| 191 | - * @param string $id id of request item. | |
| 191 | + * @param string $id id of request item. | |
| 192 | 192 | * @param \WP_REST_Request $request Full data about the request. |
| 193 | 193 | * |
| 194 | 194 | * @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. |
| 195 | 195 | */ |
| 196 | 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 ] ); | |
| 197 | + return $this->controller->delete_item( $request ); | |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * Register the endpoint. |
| @@ -205,9 +205,9 @@ | ||
| 205 | 205 | protected function register() { |
| 206 | 206 | $this->register_items_route(); |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | - protected function register_route( $route = '', $methods = WP_REST_Server::READABLE, $args = [] ) { | |
| 209 | + protected function register_route( $route = '', $methods = WP_REST_Server::READABLE, $callback = null, $args = [] ) { | |
| 210 | 210 | if ( ! in_array( $methods, self::AVAILABLE_METHODS, true ) ) { |
| 211 | 211 | trigger_error( "Invalid method: '$methods'.", E_USER_ERROR ); // phpcs:ignore |
| 212 | 212 | } |
| 213 | 213 | |
| @@ -217,23 +217,8 @@ | ||
| 217 | 217 | 'args' => $args, |
| 218 | 218 | 'route' => $route, |
| 219 | 219 | ]; |
| 220 | 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 | 221 | return register_rest_route( $this->controller->get_namespace(), $route, [ |
| 237 | 222 | [ |
| 238 | 223 | 'args' => $args, |
| 239 | 224 | 'methods' => $methods, |
| @@ -248,16 +233,23 @@ | ||
| 248 | 233 | /** |
| 249 | 234 | * Register items route. |
| 250 | 235 | * |
| 251 | 236 | * @param string $methods |
| 252 | - * @param array $args | |
| 237 | + * @param array $args | |
| 253 | 238 | */ |
| 254 | 239 | public function register_items_route( $methods = WP_REST_Server::READABLE, $args = [] ) { |
| 255 | - $args['is_multi'] = true; | |
| 256 | - | |
| 257 | - $this->register_route( '', $methods, $args ); | |
| 240 | + $this->register_route( '', $methods, function ( $request ) use ( $methods ) { | |
| 241 | + return $this->base_callback( $methods, $request, true ); | |
| 242 | + }, $args ); | |
| 258 | 243 | } |
| 259 | 244 | |
| 245 | + /** | |
| 246 | + * Register item route. | |
| 247 | + * | |
| 248 | + * @param string $route | |
| 249 | + * @param array $args | |
| 250 | + * @param string $methods | |
| 251 | + */ | |
| 260 | 252 | public function register_item_route( $methods = WP_REST_Server::READABLE, $args = [], $route = '/' ) { |
| 261 | 253 | if ( ! empty( $args['id_arg_name'] ) ) { |
| 262 | 254 | $this->id_arg_name = $args['id_arg_name']; |
| 263 | 255 | |
| @@ -284,9 +276,11 @@ | ||
| 284 | 276 | 'args' => $args, |
| 285 | 277 | 'route' => $route, |
| 286 | 278 | ]; |
| 287 | 279 | |
| 288 | - $this->register_route( $route, $methods, $args ); | |
| 280 | + $this->register_route( $route, $methods, function ( $request ) use ( $methods ) { | |
| 281 | + return $this->base_callback( $methods, $request ); | |
| 282 | + }, $args ); | |
| 289 | 283 | } |
| 290 | 284 | |
| 291 | 285 | /** |
| 292 | 286 | * Base callback. |
| @@ -291,12 +285,12 @@ | ||
| 291 | 285 | /** |
| 292 | 286 | * Base callback. |
| 293 | 287 | * All reset requests from the client should pass this function. |
| 294 | 288 | * |
| 295 | - * @param string $methods | |
| 289 | + * @param string $methods | |
| 296 | 290 | * @param \WP_REST_Request $request |
| 297 | - * @param bool $is_multi | |
| 298 | - * @param array $args | |
| 291 | + * @param bool $is_multi | |
| 292 | + * @param array $args | |
| 299 | 293 | * |
| 300 | 294 | * @return mixed|\WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 301 | 295 | */ |
| 302 | 296 | public function base_callback( $methods, $request, $is_multi = false, $args = [] ) { |
| @@ -308,49 +302,49 @@ | ||
| 308 | 302 | } |
| 309 | 303 | } |
| 310 | 304 | |
| 311 | 305 | $args = wp_parse_args( $args, [ |
| 312 | - 'is_debug' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), | |
| 306 | + 'is_debug' => defined( 'WP_DEBUG' ), | |
| 313 | 307 | ] ); |
| 314 | 308 | |
| 315 | - $result = new \WP_Error( 'invalid_methods', 'route not supported.' ); | |
| 309 | + $result = []; | |
| 316 | 310 | |
| 317 | - $request->set_param( 'is_multi', $is_multi ); | |
| 311 | + if ( $this->get_permission_callback( $request ) ) { | |
| 312 | + try { | |
| 313 | + switch ( $methods ) { | |
| 314 | + case WP_REST_Server::READABLE: | |
| 315 | + $result = $is_multi ? $this->get_items( $request ) : $this->get_item( $request->get_param( 'id' ), $request ); | |
| 316 | + break; | |
| 318 | 317 | |
| 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; | |
| 318 | + case WP_REST_Server::CREATABLE: | |
| 319 | + $result = $is_multi ? $this->create_items( $request ) : $this->create_item( $request->get_param( 'id' ), $request ); | |
| 320 | + break; | |
| 324 | 321 | |
| 325 | - case WP_REST_Server::CREATABLE: | |
| 326 | - $result = $is_multi ? $this->create_items( $request ) : $this->create_item( $request->get_param( 'id' ), $request ); | |
| 327 | - break; | |
| 322 | + case WP_REST_Server::EDITABLE: | |
| 323 | + $result = $is_multi ? $this->update_items( $request ) : $this->update_item( $request->get_param( 'id' ), $request ); | |
| 324 | + break; | |
| 328 | 325 | |
| 329 | - case WP_REST_Server::EDITABLE: | |
| 330 | - $result = $is_multi ? $this->update_items( $request ) : $this->update_item( $request->get_param( 'id' ), $request ); | |
| 331 | - break; | |
| 326 | + case WP_REST_Server::DELETABLE: | |
| 327 | + $result = $is_multi ? $this->delete_items( $request ) : $this->delete_item( $request->get_param( 'id' ), $request ); | |
| 328 | + break; | |
| 329 | + } | |
| 330 | + } catch ( Data_Exception $e ) { | |
| 331 | + $result = $e->to_wp_error(); | |
| 332 | + } catch ( \Exception $e ) { | |
| 333 | + if ( empty( $args['is_debug'] ) ) { | |
| 334 | + $result = ( new Error_500() )->to_wp_error(); | |
| 335 | + } else { | |
| 336 | + // For frontend. | |
| 337 | + $exception_mapping = [ | |
| 338 | + 'trace' => $e->getTrace(), | |
| 339 | + 'file' => $e->getFile(), | |
| 340 | + 'line' => $e->getLine(), | |
| 341 | + ]; | |
| 332 | 342 | |
| 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 | - ]; | |
| 343 | + $e->debug = $exception_mapping; | |
| 349 | 344 | |
| 350 | - $e->debug = $exception_mapping; | |
| 351 | - | |
| 352 | - $result = ( new Data_Exception( $e->getMessage(), $e->getCode(), $e ) )->to_wp_error(); | |
| 345 | + $result = ( new Data_Exception( $e->getMessage(), $e->getCode(), $e ) )->to_wp_error(); | |
| 346 | + } | |
| 353 | 347 | } |
| 354 | 348 | } |
| 355 | 349 | |
| 356 | 350 | return rest_ensure_response( $result ); |
| @@ -358,12 +352,12 @@ | ||
| 358 | 352 | |
| 359 | 353 | /** |
| 360 | 354 | * Constructor. |
| 361 | 355 | * |
| 362 | - * Run `$this->register()`. | |
| 356 | + * run `$this->register()`. | |
| 363 | 357 | * |
| 364 | 358 | * @param \Elementor\Data\V2\Base\Controller $controller |
| 365 | - * @param string $route | |
| 359 | + * @param string $route | |
| 366 | 360 | */ |
| 367 | 361 | protected function __construct( Controller $controller, $route ) { |
| 368 | 362 | $this->controller = $controller; |
| 369 | 363 | $this->route = $this->ensure_slashes( $route ); |