| @@ -1,17 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Variables\Classes; |
| 4 | 4 | |
| 5 | -use Elementor\Modules\Variables\Storage\Exceptions\Type_Mismatch; | |
| 5 | +use Exception; | |
| 6 | 6 | use WP_Error; |
| 7 | -use Exception; | |
| 7 | +use WP_REST_Response; | |
| 8 | +use WP_REST_Request; | |
| 8 | 9 | use WP_REST_Server; |
| 9 | -use WP_REST_Request; | |
| 10 | 10 | use Elementor\Plugin; |
| 11 | -use WP_REST_Response; | |
| 12 | -use Elementor\Modules\Variables\Services\Variables_Service; | |
| 13 | 11 | use Elementor\Modules\Variables\Module as Variables_Module; |
| 12 | +use Elementor\Modules\Variables\Storage\Repository as Variables_Repository; | |
| 14 | 13 | use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 15 | 14 | use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 16 | 15 | use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; |
| 17 | 16 | use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed; |
| @@ -30,13 +29,12 @@ | ||
| 30 | 29 | const HTTP_SERVER_ERROR = 500; |
| 31 | 30 | const MAX_ID_LENGTH = 64; |
| 32 | 31 | const MAX_LABEL_LENGTH = 50; |
| 33 | 32 | const MAX_VALUE_LENGTH = 512; |
| 33 | + private Variables_Repository $variables_repository; | |
| 34 | 34 | |
| 35 | - private Variables_Service $service; | |
| 36 | - | |
| 37 | - public function __construct( Variables_Service $service ) { | |
| 38 | - $this->service = $service; | |
| 35 | + public function __construct( Variables_Repository $variables_repository ) { | |
| 36 | + $this->variables_repository = $variables_repository; | |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | 39 | public function enough_permissions_to_perform_ro_action() { |
| 42 | 40 | return current_user_can( 'edit_posts' ); |
| @@ -106,14 +104,8 @@ | ||
| 106 | 104 | 'required' => false, |
| 107 | 105 | 'type' => 'integer', |
| 108 | 106 | 'validate_callback' => [ $this, 'is_valid_order' ], |
| 109 | 107 | ], |
| 110 | - 'type' => [ | |
| 111 | - 'required' => false, | |
| 112 | - 'type' => 'string', | |
| 113 | - 'validate_callback' => [ $this, 'is_valid_variable_type' ], | |
| 114 | - 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], | |
| 115 | - ], | |
| 116 | 108 | ], |
| 117 | 109 | ] ); |
| 118 | 110 | |
| 119 | 111 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/delete', [ |
| @@ -152,14 +144,8 @@ | ||
| 152 | 144 | 'type' => 'string', |
| 153 | 145 | 'validate_callback' => [ $this, 'is_valid_variable_value' ], |
| 154 | 146 | 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], |
| 155 | 147 | ], |
| 156 | - 'type' => [ | |
| 157 | - 'required' => false, | |
| 158 | - 'type' => 'string', | |
| 159 | - 'validate_callback' => [ $this, 'is_valid_variable_type' ], | |
| 160 | - 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], | |
| 161 | - ], | |
| 162 | 148 | ], |
| 163 | 149 | ] ); |
| 164 | 150 | |
| 165 | 151 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/batch', [ |
| @@ -181,9 +167,8 @@ | ||
| 181 | 167 | ] ); |
| 182 | 168 | } |
| 183 | 169 | |
| 184 | 170 | public function trim_and_sanitize_text_field( $value ) { |
| 185 | - | |
| 186 | 171 | return trim( sanitize_text_field( $value ) ); |
| 187 | 172 | } |
| 188 | 173 | |
| 189 | 174 | public function is_valid_variable_id( $id ) { |
| @@ -282,9 +267,9 @@ | ||
| 282 | 267 | $type = $request->get_param( 'type' ); |
| 283 | 268 | $label = $request->get_param( 'label' ); |
| 284 | 269 | $value = $request->get_param( 'value' ); |
| 285 | 270 | |
| 286 | - $result = $this->service->create( [ | |
| 271 | + $result = $this->variables_repository->create( [ | |
| 287 | 272 | 'type' => $type, |
| 288 | 273 | 'label' => $label, |
| 289 | 274 | 'value' => $value, |
| 290 | 275 | ] ); |
| @@ -309,9 +294,8 @@ | ||
| 309 | 294 | $id = $request->get_param( 'id' ); |
| 310 | 295 | $label = $request->get_param( 'label' ); |
| 311 | 296 | $value = $request->get_param( 'value' ); |
| 312 | 297 | $order = $request->get_param( 'order' ); |
| 313 | - $type = $request->get_param( 'type' ); | |
| 314 | 298 | |
| 315 | 299 | $update_data = [ |
| 316 | 300 | 'label' => $label, |
| 317 | 301 | 'value' => $value, |
| @@ -316,17 +300,13 @@ | ||
| 316 | 300 | 'label' => $label, |
| 317 | 301 | 'value' => $value, |
| 318 | 302 | ]; |
| 319 | 303 | |
| 320 | - if ( $type ) { | |
| 321 | - $update_data['type'] = $type; | |
| 322 | - } | |
| 323 | - | |
| 324 | 304 | if ( null !== $order ) { |
| 325 | 305 | $update_data['order'] = $order; |
| 326 | 306 | } |
| 327 | 307 | |
| 328 | - $result = $this->service->update( $id, $update_data ); | |
| 308 | + $result = $this->variables_repository->update( $id, $update_data ); | |
| 329 | 309 | |
| 330 | 310 | $this->clear_cache(); |
| 331 | 311 | |
| 332 | 312 | return $this->success_response( [ |
| @@ -345,9 +325,9 @@ | ||
| 345 | 325 | |
| 346 | 326 | private function delete_existing_variable( WP_REST_Request $request ) { |
| 347 | 327 | $id = $request->get_param( 'id' ); |
| 348 | 328 | |
| 349 | - $result = $this->service->delete( $id ); | |
| 329 | + $result = $this->variables_repository->delete( $id ); | |
| 350 | 330 | |
| 351 | 331 | $this->clear_cache(); |
| 352 | 332 | |
| 353 | 333 | return $this->success_response( [ |
| @@ -380,16 +360,10 @@ | ||
| 380 | 360 | if ( $value ) { |
| 381 | 361 | $overrides['value'] = $value; |
| 382 | 362 | } |
| 383 | 363 | |
| 384 | - $type = $request->get_param( 'type' ); | |
| 364 | + $result = $this->variables_repository->restore( $id, $overrides ); | |
| 385 | 365 | |
| 386 | - if ( $type ) { | |
| 387 | - $overrides['type'] = $type; | |
| 388 | - } | |
| 389 | - | |
| 390 | - $result = $this->service->restore( $id, $overrides ); | |
| 391 | - | |
| 392 | 366 | $this->clear_cache(); |
| 393 | 367 | |
| 394 | 368 | return $this->success_response( [ |
| 395 | 369 | 'variable' => $result['variable'], |
| @@ -405,9 +379,9 @@ | ||
| 405 | 379 | } |
| 406 | 380 | } |
| 407 | 381 | |
| 408 | 382 | private function list_of_variables() { |
| 409 | - $db_record = $this->service->load(); | |
| 383 | + $db_record = $this->variables_repository->load(); | |
| 410 | 384 | |
| 411 | 385 | return $this->success_response( [ |
| 412 | 386 | 'variables' => $db_record['data'] ?? [], |
| 413 | 387 | 'total' => count( $db_record['data'] ), |
| @@ -446,16 +420,8 @@ | ||
| 446 | 420 | __( 'Variable not found', 'elementor' ) |
| 447 | 421 | ); |
| 448 | 422 | } |
| 449 | 423 | |
| 450 | - if ( $e instanceof Type_Mismatch ) { | |
| 451 | - return $this->prepare_error_response( | |
| 452 | - self::HTTP_BAD_REQUEST, | |
| 453 | - 'type_mismatch', | |
| 454 | - $e->getMessage() | |
| 455 | - ); | |
| 456 | - } | |
| 457 | - | |
| 458 | 424 | return $this->prepare_error_response( |
| 459 | 425 | self::HTTP_SERVER_ERROR, |
| 460 | 426 | 'unexpected_server_error', |
| 461 | 427 | __( 'Unexpected server error', 'elementor' ) |
| @@ -482,8 +448,9 @@ | ||
| 482 | 448 | |
| 483 | 449 | return true; |
| 484 | 450 | } |
| 485 | 451 | |
| 452 | + | |
| 486 | 453 | public function is_valid_operations_array( $operations ) { |
| 487 | 454 | if ( ! is_array( $operations ) || empty( $operations ) ) { |
| 488 | 455 | return new WP_Error( |
| 489 | 456 | 'invalid_operations_empty', |
| @@ -492,15 +459,14 @@ | ||
| 492 | 459 | } |
| 493 | 460 | |
| 494 | 461 | foreach ( $operations as $index => $operation ) { |
| 495 | 462 | if ( ! is_array( $operation ) || ! isset( $operation['type'] ) ) { |
| 496 | - $sanitized_index = absint( $index ); | |
| 497 | 463 | return new WP_Error( |
| 498 | 464 | 'invalid_operation_structure', |
| 499 | 465 | sprintf( |
| 500 | 466 | /* translators: %d: operation index */ |
| 501 | 467 | __( 'Invalid operation structure at index %d', 'elementor' ), |
| 502 | - $sanitized_index | |
| 468 | + $index | |
| 503 | 469 | ) |
| 504 | 470 | ); |
| 505 | 471 | } |
| 506 | 472 | |
| @@ -506,15 +472,14 @@ | ||
| 506 | 472 | |
| 507 | 473 | $allowed_types = [ 'create', 'update', 'delete', 'restore', 'reorder' ]; |
| 508 | 474 | |
| 509 | 475 | if ( ! in_array( $operation['type'], $allowed_types, true ) ) { |
| 510 | - $sanitized_index = absint( $index ); | |
| 511 | 476 | return new WP_Error( |
| 512 | 477 | 'invalid_operation_type', |
| 513 | 478 | sprintf( |
| 514 | 479 | /* translators: %d: operation index */ |
| 515 | 480 | __( 'Invalid operation type at index %d', 'elementor' ), |
| 516 | - $sanitized_index | |
| 481 | + $index | |
| 517 | 482 | ) |
| 518 | 483 | ); |
| 519 | 484 | } |
| 520 | 485 | } |
| @@ -530,11 +495,12 @@ | ||
| 530 | 495 | } |
| 531 | 496 | } |
| 532 | 497 | |
| 533 | 498 | private function process_batch_operations( WP_REST_Request $request ) { |
| 499 | + $watermark = $request->get_param( 'watermark' ); | |
| 534 | 500 | $operations = $request->get_param( 'operations' ); |
| 535 | 501 | |
| 536 | - $result = $this->service->process_batch( $operations ); | |
| 502 | + $result = $this->variables_repository->process_atomic_batch( $operations, $watermark ); | |
| 537 | 503 | |
| 538 | 504 | $this->clear_cache(); |
| 539 | 505 | |
| 540 | 506 | return $this->success_response( $result ); |