| @@ -1,21 +1,21 @@ | ||
| 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 Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; | |
| 6 | +use Exception; | |
| 6 | 7 | use WP_Error; |
| 7 | -use Exception; | |
| 8 | +use WP_REST_Response; | |
| 9 | +use WP_REST_Request; | |
| 8 | 10 | use WP_REST_Server; |
| 9 | -use WP_REST_Request; | |
| 11 | + | |
| 10 | 12 | use Elementor\Plugin; |
| 11 | -use WP_REST_Response; | |
| 12 | -use Elementor\Modules\Variables\Services\Variables_Service; | |
| 13 | -use Elementor\Modules\Variables\Module as Variables_Module; | |
| 13 | +use Elementor\Modules\Variables\PropTypes\Color_Variable_Prop_Type; | |
| 14 | +use Elementor\Modules\Variables\PropTypes\Font_Variable_Prop_Type; | |
| 15 | +use Elementor\Modules\Variables\Storage\Repository as Variables_Repository; | |
| 14 | 16 | use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 15 | 17 | use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 16 | -use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; | |
| 17 | -use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed; | |
| 18 | 18 | |
| 19 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | 20 | exit; // Exit if accessed directly. |
| 21 | 21 | } |
| @@ -22,21 +22,23 @@ | ||
| 22 | 22 | |
| 23 | 23 | class Rest_Api { |
| 24 | 24 | const API_NAMESPACE = 'elementor/v1'; |
| 25 | 25 | const API_BASE = 'variables'; |
| 26 | + | |
| 26 | 27 | const HTTP_OK = 200; |
| 27 | 28 | const HTTP_CREATED = 201; |
| 28 | 29 | const HTTP_BAD_REQUEST = 400; |
| 29 | 30 | const HTTP_NOT_FOUND = 404; |
| 30 | 31 | const HTTP_SERVER_ERROR = 500; |
| 32 | + | |
| 31 | 33 | const MAX_ID_LENGTH = 64; |
| 32 | 34 | const MAX_LABEL_LENGTH = 50; |
| 33 | 35 | const MAX_VALUE_LENGTH = 512; |
| 34 | 36 | |
| 35 | - private Variables_Service $service; | |
| 37 | + private Variables_Repository $variables_repository; | |
| 36 | 38 | |
| 37 | - public function __construct( Variables_Service $service ) { | |
| 38 | - $this->service = $service; | |
| 39 | + public function __construct( Variables_Repository $variables_repository ) { | |
| 40 | + $this->variables_repository = $variables_repository; | |
| 39 | 41 | } |
| 40 | 42 | |
| 41 | 43 | public function enough_permissions_to_perform_ro_action() { |
| 42 | 44 | return current_user_can( 'edit_posts' ); |
| @@ -101,19 +103,8 @@ | ||
| 101 | 103 | 'type' => 'string', |
| 102 | 104 | 'validate_callback' => [ $this, 'is_valid_variable_value' ], |
| 103 | 105 | 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], |
| 104 | 106 | ], |
| 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 | 107 | ], |
| 117 | 108 | ] ); |
| 118 | 109 | |
| 119 | 110 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/delete', [ |
| @@ -152,38 +143,13 @@ | ||
| 152 | 143 | 'type' => 'string', |
| 153 | 144 | 'validate_callback' => [ $this, 'is_valid_variable_value' ], |
| 154 | 145 | 'sanitize_callback' => [ $this, 'trim_and_sanitize_text_field' ], |
| 155 | 146 | ], |
| 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 | 147 | ], |
| 163 | 148 | ] ); |
| 164 | - | |
| 165 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/batch', [ | |
| 166 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 167 | - 'callback' => [ $this, 'process_batch' ], | |
| 168 | - 'permission_callback' => [ $this, 'enough_permissions_to_perform_rw_action' ], | |
| 169 | - 'args' => [ | |
| 170 | - 'watermark' => [ | |
| 171 | - 'required' => true, | |
| 172 | - 'type' => 'integer', | |
| 173 | - 'validate_callback' => [ $this, 'is_valid_watermark' ], | |
| 174 | - ], | |
| 175 | - 'operations' => [ | |
| 176 | - 'required' => true, | |
| 177 | - 'type' => 'array', | |
| 178 | - 'validate_callback' => [ $this, 'is_valid_operations_array' ], | |
| 179 | - ], | |
| 180 | - ], | |
| 181 | - ] ); | |
| 182 | 149 | } |
| 183 | 150 | |
| 184 | 151 | public function trim_and_sanitize_text_field( $value ) { |
| 185 | - | |
| 186 | 152 | return trim( sanitize_text_field( $value ) ); |
| 187 | 153 | } |
| 188 | 154 | |
| 189 | 155 | public function is_valid_variable_id( $id ) { |
| @@ -197,9 +163,8 @@ | ||
| 197 | 163 | } |
| 198 | 164 | |
| 199 | 165 | if ( self::MAX_ID_LENGTH < strlen( $id ) ) { |
| 200 | 166 | return new WP_Error( 'invalid_variable_id_length', sprintf( |
| 201 | - /* translators: %d: Maximum ID length. */ | |
| 202 | 167 | __( 'ID cannot exceed %d characters', 'elementor' ), |
| 203 | 168 | self::MAX_ID_LENGTH |
| 204 | 169 | ) ); |
| 205 | 170 | } |
| @@ -207,11 +172,12 @@ | ||
| 207 | 172 | return true; |
| 208 | 173 | } |
| 209 | 174 | |
| 210 | 175 | public function is_valid_variable_type( $type ) { |
| 211 | - $allowed_types = array_keys( Variables_Module::instance()->get_variable_types_registry()->all() ); | |
| 212 | - | |
| 213 | - return in_array( $type, $allowed_types, true ); | |
| 176 | + return in_array( $type, [ | |
| 177 | + Color_Variable_Prop_Type::get_key(), | |
| 178 | + Font_Variable_Prop_Type::get_key(), | |
| 179 | + ], true ); | |
| 214 | 180 | } |
| 215 | 181 | |
| 216 | 182 | public function is_valid_variable_label( $label ) { |
| 217 | 183 | $label = trim( $label ); |
| @@ -224,9 +190,8 @@ | ||
| 224 | 190 | } |
| 225 | 191 | |
| 226 | 192 | if ( self::MAX_LABEL_LENGTH < strlen( $label ) ) { |
| 227 | 193 | return new WP_Error( 'invalid_variable_label_length', sprintf( |
| 228 | - /* translators: %d: Maximum label length. */ | |
| 229 | 194 | __( 'Label cannot exceed %d characters', 'elementor' ), |
| 230 | 195 | self::MAX_LABEL_LENGTH |
| 231 | 196 | ) ); |
| 232 | 197 | } |
| @@ -233,19 +198,8 @@ | ||
| 233 | 198 | |
| 234 | 199 | return true; |
| 235 | 200 | } |
| 236 | 201 | |
| 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 | 202 | public function is_valid_variable_value( $value ) { |
| 249 | 203 | $value = trim( $value ); |
| 250 | 204 | |
| 251 | 205 | if ( empty( $value ) ) { |
| @@ -256,9 +210,8 @@ | ||
| 256 | 210 | } |
| 257 | 211 | |
| 258 | 212 | if ( self::MAX_VALUE_LENGTH < strlen( $value ) ) { |
| 259 | 213 | return new WP_Error( 'invalid_variable_value_length', sprintf( |
| 260 | - /* translators: %d: Maximum value length. */ | |
| 261 | 214 | __( 'Value cannot exceed %d characters', 'elementor' ), |
| 262 | 215 | self::MAX_VALUE_LENGTH |
| 263 | 216 | ) ); |
| 264 | 217 | } |
| @@ -282,9 +235,9 @@ | ||
| 282 | 235 | $type = $request->get_param( 'type' ); |
| 283 | 236 | $label = $request->get_param( 'label' ); |
| 284 | 237 | $value = $request->get_param( 'value' ); |
| 285 | 238 | |
| 286 | - $result = $this->service->create( [ | |
| 239 | + $result = $this->variables_repository->create( [ | |
| 287 | 240 | 'type' => $type, |
| 288 | 241 | 'label' => $label, |
| 289 | 242 | 'value' => $value, |
| 290 | 243 | ] ); |
| @@ -308,26 +261,14 @@ | ||
| 308 | 261 | private function update_existing_variable( WP_REST_Request $request ) { |
| 309 | 262 | $id = $request->get_param( 'id' ); |
| 310 | 263 | $label = $request->get_param( 'label' ); |
| 311 | 264 | $value = $request->get_param( 'value' ); |
| 312 | - $order = $request->get_param( 'order' ); | |
| 313 | - $type = $request->get_param( 'type' ); | |
| 314 | 265 | |
| 315 | - $update_data = [ | |
| 266 | + $result = $this->variables_repository->update( $id, [ | |
| 316 | 267 | 'label' => $label, |
| 317 | 268 | 'value' => $value, |
| 318 | - ]; | |
| 269 | + ] ); | |
| 319 | 270 | |
| 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 | 271 | $this->clear_cache(); |
| 331 | 272 | |
| 332 | 273 | return $this->success_response( [ |
| 333 | 274 | 'variable' => $result['variable'], |
| @@ -345,9 +286,9 @@ | ||
| 345 | 286 | |
| 346 | 287 | private function delete_existing_variable( WP_REST_Request $request ) { |
| 347 | 288 | $id = $request->get_param( 'id' ); |
| 348 | 289 | |
| 349 | - $result = $this->service->delete( $id ); | |
| 290 | + $result = $this->variables_repository->delete( $id ); | |
| 350 | 291 | |
| 351 | 292 | $this->clear_cache(); |
| 352 | 293 | |
| 353 | 294 | return $this->success_response( [ |
| @@ -369,27 +310,19 @@ | ||
| 369 | 310 | |
| 370 | 311 | $overrides = []; |
| 371 | 312 | |
| 372 | 313 | $label = $request->get_param( 'label' ); |
| 373 | - | |
| 374 | 314 | if ( $label ) { |
| 375 | 315 | $overrides['label'] = $label; |
| 376 | 316 | } |
| 377 | 317 | |
| 378 | 318 | $value = $request->get_param( 'value' ); |
| 379 | - | |
| 380 | 319 | if ( $value ) { |
| 381 | 320 | $overrides['value'] = $value; |
| 382 | 321 | } |
| 383 | 322 | |
| 384 | - $type = $request->get_param( 'type' ); | |
| 323 | + $result = $this->variables_repository->restore( $id, $overrides ); | |
| 385 | 324 | |
| 386 | - if ( $type ) { | |
| 387 | - $overrides['type'] = $type; | |
| 388 | - } | |
| 389 | - | |
| 390 | - $result = $this->service->restore( $id, $overrides ); | |
| 391 | - | |
| 392 | 325 | $this->clear_cache(); |
| 393 | 326 | |
| 394 | 327 | return $this->success_response( [ |
| 395 | 328 | 'variable' => $result['variable'], |
| @@ -405,12 +338,12 @@ | ||
| 405 | 338 | } |
| 406 | 339 | } |
| 407 | 340 | |
| 408 | 341 | private function list_of_variables() { |
| 409 | - $db_record = $this->service->load(); | |
| 342 | + $db_record = $this->variables_repository->load(); | |
| 410 | 343 | |
| 411 | 344 | return $this->success_response( [ |
| 412 | - 'variables' => $db_record['data'] ?? [], | |
| 345 | + 'variables' => $db_record['data'], | |
| 413 | 346 | 'total' => count( $db_record['data'] ), |
| 414 | 347 | 'watermark' => $db_record['watermark'], |
| 415 | 348 | ] ); |
| 416 | 349 | } |
| @@ -446,16 +379,8 @@ | ||
| 446 | 379 | __( 'Variable not found', 'elementor' ) |
| 447 | 380 | ); |
| 448 | 381 | } |
| 449 | 382 | |
| 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 | 383 | return $this->prepare_error_response( |
| 459 | 384 | self::HTTP_SERVER_ERROR, |
| 460 | 385 | 'unexpected_server_error', |
| 461 | 386 | __( 'Unexpected server error', 'elementor' ) |
| @@ -469,149 +394,6 @@ | ||
| 469 | 394 | 'data' => [ |
| 470 | 395 | 'status' => $status_code, |
| 471 | 396 | ], |
| 472 | 397 | ], $status_code ); |
| 473 | - } | |
| 474 | - | |
| 475 | - public function is_valid_watermark( $watermark ) { | |
| 476 | - if ( ! is_numeric( $watermark ) || $watermark < 0 ) { | |
| 477 | - return new WP_Error( | |
| 478 | - 'invalid_watermark', | |
| 479 | - __( 'Watermark must be a non-negative integer', 'elementor' ) | |
| 480 | - ); | |
| 481 | - } | |
| 482 | - | |
| 483 | - return true; | |
| 484 | - } | |
| 485 | - | |
| 486 | - public function is_valid_operations_array( $operations ) { | |
| 487 | - if ( ! is_array( $operations ) || empty( $operations ) ) { | |
| 488 | - return new WP_Error( | |
| 489 | - 'invalid_operations_empty', | |
| 490 | - __( 'Operations array cannot be empty', 'elementor' ) | |
| 491 | - ); | |
| 492 | - } | |
| 493 | - | |
| 494 | - foreach ( $operations as $index => $operation ) { | |
| 495 | - if ( ! is_array( $operation ) || ! isset( $operation['type'] ) ) { | |
| 496 | - $sanitized_index = absint( $index ); | |
| 497 | - return new WP_Error( | |
| 498 | - 'invalid_operation_structure', | |
| 499 | - sprintf( | |
| 500 | - /* translators: %d: operation index */ | |
| 501 | - __( 'Invalid operation structure at index %d', 'elementor' ), | |
| 502 | - $sanitized_index | |
| 503 | - ) | |
| 504 | - ); | |
| 505 | - } | |
| 506 | - | |
| 507 | - $allowed_types = [ 'create', 'update', 'delete', 'restore', 'reorder' ]; | |
| 508 | - | |
| 509 | - if ( ! in_array( $operation['type'], $allowed_types, true ) ) { | |
| 510 | - $sanitized_index = absint( $index ); | |
| 511 | - return new WP_Error( | |
| 512 | - 'invalid_operation_type', | |
| 513 | - sprintf( | |
| 514 | - /* translators: %d: operation index */ | |
| 515 | - __( 'Invalid operation type at index %d', 'elementor' ), | |
| 516 | - $sanitized_index | |
| 517 | - ) | |
| 518 | - ); | |
| 519 | - } | |
| 520 | - } | |
| 521 | - | |
| 522 | - return true; | |
| 523 | - } | |
| 524 | - | |
| 525 | - public function process_batch( WP_REST_Request $request ) { | |
| 526 | - try { | |
| 527 | - return $this->process_batch_operations( $request ); | |
| 528 | - } catch ( Exception $e ) { | |
| 529 | - return $this->batch_error_response( $e ); | |
| 530 | - } | |
| 531 | - } | |
| 532 | - | |
| 533 | - private function process_batch_operations( WP_REST_Request $request ) { | |
| 534 | - $operations = $request->get_param( 'operations' ); | |
| 535 | - | |
| 536 | - $result = $this->service->process_batch( $operations ); | |
| 537 | - | |
| 538 | - $this->clear_cache(); | |
| 539 | - | |
| 540 | - return $this->success_response( $result ); | |
| 541 | - } | |
| 542 | - | |
| 543 | - | |
| 544 | - private function batch_error_response( Exception $e ) { | |
| 545 | - if ( $e instanceof BatchOperationFailed ) { | |
| 546 | - $error_details = $e->getErrorDetails(); | |
| 547 | - $batch_error_context = $this->determine_batch_error_context( $error_details ); | |
| 548 | - | |
| 549 | - return new WP_REST_Response( [ | |
| 550 | - 'success' => false, | |
| 551 | - 'code' => $batch_error_context['code'], | |
| 552 | - 'message' => $batch_error_context['message'], | |
| 553 | - 'data' => $batch_error_context['filtered_errors'], | |
| 554 | - ], self::HTTP_BAD_REQUEST ); | |
| 555 | - } | |
| 556 | - | |
| 557 | - 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 | 398 | } |
| 617 | 399 | } |