| @@ -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' ); |
| @@ -101,19 +99,8 @@ | ||
| 101 | 99 | 'type' => 'string', |
| 102 | 100 | 'validate_callback' => [ $this, 'is_valid_variable_value' ], |
| 103 | 101 | 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], |
| 104 | 102 | ], |
| 105 | - 'order' => [ | |
| 106 | - 'required' => false, | |
| 107 | - 'type' => 'integer', | |
| 108 | - 'validate_callback' => [ $this, 'is_valid_order' ], | |
| 109 | - ], | |
| 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 | 103 | ], |
| 117 | 104 | ] ); |
| 118 | 105 | |
| 119 | 106 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/delete', [ |
| @@ -152,14 +139,8 @@ | ||
| 152 | 139 | 'type' => 'string', |
| 153 | 140 | 'validate_callback' => [ $this, 'is_valid_variable_value' ], |
| 154 | 141 | 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], |
| 155 | 142 | ], |
| 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 | 143 | ], |
| 163 | 144 | ] ); |
| 164 | 145 | |
| 165 | 146 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/batch', [ |
| @@ -181,9 +162,8 @@ | ||
| 181 | 162 | ] ); |
| 182 | 163 | } |
| 183 | 164 | |
| 184 | 165 | public function trim_and_sanitize_text_field( $value ) { |
| 185 | - | |
| 186 | 166 | return trim( sanitize_text_field( $value ) ); |
| 187 | 167 | } |
| 188 | 168 | |
| 189 | 169 | public function is_valid_variable_id( $id ) { |
| @@ -233,19 +213,8 @@ | ||
| 233 | 213 | |
| 234 | 214 | return true; |
| 235 | 215 | } |
| 236 | 216 | |
| 237 | - public function is_valid_order( $order ) { | |
| 238 | - if ( ! is_numeric( $order ) || $order < 0 ) { | |
| 239 | - return new WP_Error( | |
| 240 | - 'invalid_order', | |
| 241 | - __( 'Order must be a non-negative integer', 'elementor' ) | |
| 242 | - ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - return true; | |
| 246 | - } | |
| 247 | - | |
| 248 | 217 | public function is_valid_variable_value( $value ) { |
| 249 | 218 | $value = trim( $value ); |
| 250 | 219 | |
| 251 | 220 | if ( empty( $value ) ) { |
| @@ -282,9 +251,9 @@ | ||
| 282 | 251 | $type = $request->get_param( 'type' ); |
| 283 | 252 | $label = $request->get_param( 'label' ); |
| 284 | 253 | $value = $request->get_param( 'value' ); |
| 285 | 254 | |
| 286 | - $result = $this->service->create( [ | |
| 255 | + $result = $this->variables_repository->create( [ | |
| 287 | 256 | 'type' => $type, |
| 288 | 257 | 'label' => $label, |
| 289 | 258 | 'value' => $value, |
| 290 | 259 | ] ); |
| @@ -308,26 +277,14 @@ | ||
| 308 | 277 | private function update_existing_variable( WP_REST_Request $request ) { |
| 309 | 278 | $id = $request->get_param( 'id' ); |
| 310 | 279 | $label = $request->get_param( 'label' ); |
| 311 | 280 | $value = $request->get_param( 'value' ); |
| 312 | - $order = $request->get_param( 'order' ); | |
| 313 | - $type = $request->get_param( 'type' ); | |
| 314 | 281 | |
| 315 | - $update_data = [ | |
| 282 | + $result = $this->variables_repository->update( $id, [ | |
| 316 | 283 | 'label' => $label, |
| 317 | 284 | 'value' => $value, |
| 318 | - ]; | |
| 285 | + ] ); | |
| 319 | 286 | |
| 320 | - if ( $type ) { | |
| 321 | - $update_data['type'] = $type; | |
| 322 | - } | |
| 323 | - | |
| 324 | - if ( null !== $order ) { | |
| 325 | - $update_data['order'] = $order; | |
| 326 | - } | |
| 327 | - | |
| 328 | - $result = $this->service->update( $id, $update_data ); | |
| 329 | - | |
| 330 | 287 | $this->clear_cache(); |
| 331 | 288 | |
| 332 | 289 | return $this->success_response( [ |
| 333 | 290 | 'variable' => $result['variable'], |
| @@ -345,9 +302,9 @@ | ||
| 345 | 302 | |
| 346 | 303 | private function delete_existing_variable( WP_REST_Request $request ) { |
| 347 | 304 | $id = $request->get_param( 'id' ); |
| 348 | 305 | |
| 349 | - $result = $this->service->delete( $id ); | |
| 306 | + $result = $this->variables_repository->delete( $id ); | |
| 350 | 307 | |
| 351 | 308 | $this->clear_cache(); |
| 352 | 309 | |
| 353 | 310 | return $this->success_response( [ |
| @@ -380,16 +337,10 @@ | ||
| 380 | 337 | if ( $value ) { |
| 381 | 338 | $overrides['value'] = $value; |
| 382 | 339 | } |
| 383 | 340 | |
| 384 | - $type = $request->get_param( 'type' ); | |
| 341 | + $result = $this->variables_repository->restore( $id, $overrides ); | |
| 385 | 342 | |
| 386 | - if ( $type ) { | |
| 387 | - $overrides['type'] = $type; | |
| 388 | - } | |
| 389 | - | |
| 390 | - $result = $this->service->restore( $id, $overrides ); | |
| 391 | - | |
| 392 | 343 | $this->clear_cache(); |
| 393 | 344 | |
| 394 | 345 | return $this->success_response( [ |
| 395 | 346 | 'variable' => $result['variable'], |
| @@ -405,12 +356,12 @@ | ||
| 405 | 356 | } |
| 406 | 357 | } |
| 407 | 358 | |
| 408 | 359 | private function list_of_variables() { |
| 409 | - $db_record = $this->service->load(); | |
| 360 | + $db_record = $this->variables_repository->load(); | |
| 410 | 361 | |
| 411 | 362 | return $this->success_response( [ |
| 412 | - 'variables' => $db_record['data'] ?? [], | |
| 363 | + 'variables' => $db_record['data'], | |
| 413 | 364 | 'total' => count( $db_record['data'] ), |
| 414 | 365 | 'watermark' => $db_record['watermark'], |
| 415 | 366 | ] ); |
| 416 | 367 | } |
| @@ -446,16 +397,8 @@ | ||
| 446 | 397 | __( 'Variable not found', 'elementor' ) |
| 447 | 398 | ); |
| 448 | 399 | } |
| 449 | 400 | |
| 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 | 401 | return $this->prepare_error_response( |
| 459 | 402 | self::HTTP_SERVER_ERROR, |
| 460 | 403 | 'unexpected_server_error', |
| 461 | 404 | __( 'Unexpected server error', 'elementor' ) |
| @@ -492,29 +435,27 @@ | ||
| 492 | 435 | } |
| 493 | 436 | |
| 494 | 437 | foreach ( $operations as $index => $operation ) { |
| 495 | 438 | if ( ! is_array( $operation ) || ! isset( $operation['type'] ) ) { |
| 496 | - $sanitized_index = absint( $index ); | |
| 497 | 439 | return new WP_Error( |
| 498 | 440 | 'invalid_operation_structure', |
| 499 | 441 | sprintf( |
| 500 | 442 | /* translators: %d: operation index */ |
| 501 | 443 | __( 'Invalid operation structure at index %d', 'elementor' ), |
| 502 | - $sanitized_index | |
| 444 | + $index | |
| 503 | 445 | ) |
| 504 | 446 | ); |
| 505 | 447 | } |
| 506 | 448 | |
| 507 | - $allowed_types = [ 'create', 'update', 'delete', 'restore', 'reorder' ]; | |
| 449 | + $allowed_types = [ 'create', 'update', 'delete', 'restore' ]; | |
| 508 | 450 | |
| 509 | 451 | if ( ! in_array( $operation['type'], $allowed_types, true ) ) { |
| 510 | - $sanitized_index = absint( $index ); | |
| 511 | 452 | return new WP_Error( |
| 512 | 453 | 'invalid_operation_type', |
| 513 | 454 | sprintf( |
| 514 | 455 | /* translators: %d: operation index */ |
| 515 | 456 | __( 'Invalid operation type at index %d', 'elementor' ), |
| 516 | - $sanitized_index | |
| 457 | + $index | |
| 517 | 458 | ) |
| 518 | 459 | ); |
| 519 | 460 | } |
| 520 | 461 | } |
| @@ -530,11 +471,12 @@ | ||
| 530 | 471 | } |
| 531 | 472 | } |
| 532 | 473 | |
| 533 | 474 | private function process_batch_operations( WP_REST_Request $request ) { |
| 475 | + $watermark = $request->get_param( 'watermark' ); | |
| 534 | 476 | $operations = $request->get_param( 'operations' ); |
| 535 | 477 | |
| 536 | - $result = $this->service->process_batch( $operations ); | |
| 478 | + $result = $this->variables_repository->process_atomic_batch( $operations, $watermark ); | |
| 537 | 479 | |
| 538 | 480 | $this->clear_cache(); |
| 539 | 481 | |
| 540 | 482 | return $this->success_response( $result ); |
| @@ -539,79 +481,17 @@ | ||
| 539 | 481 | |
| 540 | 482 | return $this->success_response( $result ); |
| 541 | 483 | } |
| 542 | 484 | |
| 543 | - | |
| 544 | 485 | private function batch_error_response( Exception $e ) { |
| 545 | 486 | if ( $e instanceof BatchOperationFailed ) { |
| 546 | - $error_details = $e->getErrorDetails(); | |
| 547 | - $batch_error_context = $this->determine_batch_error_context( $error_details ); | |
| 548 | - | |
| 549 | 487 | return new WP_REST_Response( [ |
| 550 | 488 | 'success' => false, |
| 551 | - 'code' => $batch_error_context['code'], | |
| 552 | - 'message' => $batch_error_context['message'], | |
| 553 | - 'data' => $batch_error_context['filtered_errors'], | |
| 489 | + 'code' => 'atomic_operation_failed', | |
| 490 | + 'message' => __( 'Batch operation failed', 'elementor' ), | |
| 491 | + 'data' => $e->getErrorDetails(), | |
| 554 | 492 | ], self::HTTP_BAD_REQUEST ); |
| 555 | 493 | } |
| 556 | 494 | |
| 557 | 495 | return $this->error_response( $e ); |
| 558 | - } | |
| 559 | - | |
| 560 | - private function determine_batch_error_context( array $error_details ) { | |
| 561 | - $error_config = [ | |
| 562 | - 'invalid_variable_limit_reached' => [ | |
| 563 | - 'batch_code' => 'batch_variables_limit_reached', | |
| 564 | - 'batch_message' => __( 'Batch operation failed: Reached the maximum number of variables', 'elementor' ), | |
| 565 | - 'status' => self::HTTP_BAD_REQUEST, | |
| 566 | - 'message' => __( 'Reached the maximum number of variables', 'elementor' ), | |
| 567 | - ], | |
| 568 | - 'duplicated_label' => [ | |
| 569 | - 'batch_code' => 'batch_duplicated_label', | |
| 570 | - 'batch_message' => __( 'Batch operation failed: Variable labels already exist', 'elementor' ), | |
| 571 | - 'status' => self::HTTP_BAD_REQUEST, | |
| 572 | - 'message' => __( 'Variable label already exists', 'elementor' ), | |
| 573 | - ], | |
| 574 | - 'variable_not_found' => [ | |
| 575 | - 'batch_code' => 'batch_variables_not_found', | |
| 576 | - 'batch_message' => __( 'Batch operation failed: Variables not found', 'elementor' ), | |
| 577 | - 'status' => self::HTTP_NOT_FOUND, | |
| 578 | - 'message' => __( 'Variable not found', 'elementor' ), | |
| 579 | - ], | |
| 580 | - ]; | |
| 581 | - | |
| 582 | - $grouped_errors = []; | |
| 583 | - | |
| 584 | - foreach ( $error_details as $id => $error_detail ) { | |
| 585 | - $error_code = $error_detail['code'] ?? ''; | |
| 586 | - | |
| 587 | - if ( isset( $error_config[ $error_code ] ) ) { | |
| 588 | - $config = $error_config[ $error_code ]; | |
| 589 | - $grouped_errors[ $error_code ][ $id ] = [ | |
| 590 | - 'status' => $config['status'], | |
| 591 | - 'message' => $config['message'], | |
| 592 | - ]; | |
| 593 | - } else { | |
| 594 | - $grouped_errors['unknown'][ $id ] = [ | |
| 595 | - 'status' => self::HTTP_SERVER_ERROR, | |
| 596 | - 'message' => $error_detail['message'] ?? __( 'Unexpected error', 'elementor' ), | |
| 597 | - ]; | |
| 598 | - } | |
| 599 | - } | |
| 600 | - | |
| 601 | - foreach ( $error_config as $error_code => $config ) { | |
| 602 | - if ( ! empty( $grouped_errors[ $error_code ] ) ) { | |
| 603 | - return [ | |
| 604 | - 'code' => $config['batch_code'], | |
| 605 | - 'message' => $config['batch_message'], | |
| 606 | - 'filtered_errors' => $grouped_errors[ $error_code ], | |
| 607 | - ]; | |
| 608 | - } | |
| 609 | - } | |
| 610 | - | |
| 611 | - return [ | |
| 612 | - 'code' => 'batch_operation_failed', | |
| 613 | - 'message' => __( 'Batch operation failed', 'elementor' ), | |
| 614 | - 'filtered_errors' => $grouped_errors['unknown'] ?? [], | |
| 615 | - ]; | |
| 616 | 496 | } |
| 617 | 497 | } |