← All changes
|
modules/components/components-rest-api.php
+21
-76
4.1.0-dev3
→
3.35.0-dev2
View file →
| @@ -147,15 +147,12 @@ | ||
| 147 | 147 | 'methods' => 'GET', |
| 148 | 148 | 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->get_overridable_props( $request ) ), |
| 149 | 149 | 'permission_callback' => fn() => current_user_can( 'edit_posts' ), |
| 150 | 150 | 'args' => [ |
| 151 | - 'componentIds' => [ | |
| 152 | - 'type' => 'array', | |
| 153 | - 'items' => [ | |
| 154 | - 'type' => 'integer', | |
| 155 | - ], | |
| 151 | + 'componentId' => [ | |
| 152 | + 'type' => 'integer', | |
| 156 | 153 | 'required' => true, |
| 157 | - 'description' => 'The component IDs to get overridable props for', | |
| 154 | + 'description' => 'The component ID to get overridable props for', | |
| 158 | 155 | ], |
| 159 | 156 | ], |
| 160 | 157 | ], |
| 161 | 158 | ] ); |
| @@ -313,49 +310,36 @@ | ||
| 313 | 310 | return Response_Builder::make( $styles )->build(); |
| 314 | 311 | } |
| 315 | 312 | |
| 316 | 313 | private function get_overridable_props( \WP_REST_Request $request ) { |
| 317 | - $component_ids = $request->get_param( 'componentIds' ); | |
| 314 | + $component_id = (int) $request->get_param( 'componentId' ); | |
| 318 | 315 | |
| 319 | - $data = []; | |
| 320 | - $errors = []; | |
| 316 | + if ( ! $component_id ) { | |
| 317 | + return Error_Builder::make( 'invalid_component_id' ) | |
| 318 | + ->set_status( 400 ) | |
| 319 | + ->set_message( __( 'Invalid component ID', 'elementor' ) ) | |
| 320 | + ->build(); | |
| 321 | + } | |
| 321 | 322 | |
| 322 | - foreach ( $component_ids as $component_id ) { | |
| 323 | - $component_id = (int) $component_id; | |
| 323 | + $document = $this->get_repository()->get( $component_id ); | |
| 324 | 324 | |
| 325 | - /** @var Component $document */ | |
| 326 | - $document = $this->get_repository()->get( $component_id ); | |
| 325 | + if ( ! $document ) { | |
| 326 | + return Error_Builder::make( 'component_not_found' ) | |
| 327 | + ->set_status( 404 ) | |
| 328 | + ->set_message( __( 'Component not found', 'elementor' ) ) | |
| 329 | + ->build(); | |
| 330 | + } | |
| 327 | 331 | |
| 328 | - if ( ! $document ) { | |
| 329 | - $errors[ $component_id ] = 'component_not_found'; | |
| 330 | - continue; | |
| 331 | - } | |
| 332 | + $overridable = $document->get_json_meta( Component::OVERRIDABLE_PROPS_META_KEY ) ?? null; | |
| 332 | 333 | |
| 333 | - // This is a fix for the case where overridable props in element settings where migrated | |
| 334 | - // but the overridable props metadata were not aligned with the new origin values. | |
| 335 | - // In version 4.0.1, we fixed this by running the align_overridable_props_with_elements method after the migration. | |
| 336 | - $document_version = $document->get_elementor_version(); | |
| 337 | - $overridable_props_migration_fix_version = '4.0.1'; | |
| 338 | - $should_align_overridable_props = version_compare( $document_version, $overridable_props_migration_fix_version, '<=' ); | |
| 339 | - if ( $should_align_overridable_props ) { | |
| 340 | - $document->align_overridable_props_with_elements(); | |
| 341 | - } | |
| 342 | - | |
| 343 | - $overridable = $document->get_json_meta( Component::OVERRIDABLE_PROPS_META_KEY ); | |
| 344 | - | |
| 345 | - $data[ $component_id ] = empty( $overridable ) ? null : $overridable; | |
| 334 | + if ( empty( $overridable ) ) { | |
| 335 | + $overridable = null; | |
| 346 | 336 | } |
| 347 | 337 | |
| 348 | - return Response_Builder::make( $data ) | |
| 349 | - ->set_meta( [ 'errors' => $errors ] ) | |
| 350 | - ->build(); | |
| 338 | + return Response_Builder::make( $overridable )->build(); | |
| 351 | 339 | } |
| 352 | 340 | |
| 353 | 341 | private function create_components( \WP_REST_Request $request ) { |
| 354 | - if ( ! Components_Access_Controller::can_create() ) { | |
| 355 | - return $this->get_insufficient_permissions_error( 'create' ); | |
| 356 | - } | |
| 357 | - | |
| 358 | 342 | $save_status = $request->get_param( 'status' ); |
| 359 | 343 | |
| 360 | 344 | $items = Collection::make( $request->get_param( 'items' ) ); |
| 361 | 345 | $components = $this->get_repository()->all(); |
| @@ -424,12 +408,8 @@ | ||
| 424 | 408 | ->build(); |
| 425 | 409 | } |
| 426 | 410 | |
| 427 | 411 | private function update_statuses( \WP_REST_Request $request ) { |
| 428 | - if ( ! Components_Access_Controller::can_publish() ) { | |
| 429 | - return $this->get_insufficient_permissions_error( 'publish' ); | |
| 430 | - } | |
| 431 | - | |
| 432 | 412 | $result = Collection::make( $request->get_param( 'ids' ) ) |
| 433 | 413 | ->reduce( |
| 434 | 414 | function ( $result, int $component_id ) { |
| 435 | 415 | $component = $this->get_repository()->get( $component_id ); |
| @@ -454,12 +434,8 @@ | ||
| 454 | 434 | return Response_Builder::make( $result )->build(); |
| 455 | 435 | } |
| 456 | 436 | |
| 457 | 437 | private function lock_component( \WP_REST_Request $request ) { |
| 458 | - if ( ! Components_Access_Controller::can_lock() ) { | |
| 459 | - return $this->get_insufficient_permissions_error( 'lock' ); | |
| 460 | - } | |
| 461 | - | |
| 462 | 438 | $component_id = $request->get_param( 'componentId' ); |
| 463 | 439 | try { |
| 464 | 440 | $success = $this->get_component_lock_manager()->lock( $component_id ); |
| 465 | 441 | } catch ( \Exception $e ) { |
| @@ -480,12 +456,8 @@ | ||
| 480 | 456 | return Response_Builder::make( [ 'locked' => $success ] )->build(); |
| 481 | 457 | } |
| 482 | 458 | |
| 483 | 459 | private function unlock_component( \WP_REST_Request $request ) { |
| 484 | - if ( ! Components_Access_Controller::can_lock() ) { | |
| 485 | - return $this->get_insufficient_permissions_error( 'unlock' ); | |
| 486 | - } | |
| 487 | - | |
| 488 | 460 | $component_id = $request->get_param( 'componentId' ); |
| 489 | 461 | try { |
| 490 | 462 | $success = $this->get_component_lock_manager()->unlock( $component_id ); |
| 491 | 463 | } catch ( \Exception $e ) { |
| @@ -505,12 +477,8 @@ | ||
| 505 | 477 | return Response_Builder::make( [ 'unlocked' => $success ] )->build(); |
| 506 | 478 | } |
| 507 | 479 | |
| 508 | 480 | private function get_lock_status( \WP_REST_Request $request ) { |
| 509 | - if ( ! Components_Access_Controller::can_lock() ) { | |
| 510 | - return $this->get_insufficient_permissions_error( 'lock_status' ); | |
| 511 | - } | |
| 512 | - | |
| 513 | 481 | $component_id = (int) $request->get_param( 'componentId' ); |
| 514 | 482 | try { |
| 515 | 483 | $lock_manager = $this->get_component_lock_manager(); |
| 516 | 484 | if ( $lock_manager->is_lock_expired( $component_id ) ) { |
| @@ -552,12 +520,8 @@ | ||
| 552 | 520 | } |
| 553 | 521 | } |
| 554 | 522 | |
| 555 | 523 | private function archive_components( \WP_REST_Request $request ) { |
| 556 | - if ( ! Components_Access_Controller::can_delete() ) { | |
| 557 | - return $this->get_insufficient_permissions_error( 'delete' ); | |
| 558 | - } | |
| 559 | - | |
| 560 | 524 | $component_ids = $request->get_param( 'componentIds' ); |
| 561 | 525 | $status = $request->get_param( 'status' ); |
| 562 | 526 | |
| 563 | 527 | try { |
| @@ -573,12 +537,8 @@ | ||
| 573 | 537 | return Response_Builder::make( $result )->build(); |
| 574 | 538 | } |
| 575 | 539 | |
| 576 | 540 | private function update_components_title( \WP_REST_Request $request ) { |
| 577 | - if ( ! Components_Access_Controller::can_rename() ) { | |
| 578 | - return $this->get_insufficient_permissions_error( 'rename' ); | |
| 579 | - } | |
| 580 | - | |
| 581 | 541 | $failed_ids = []; |
| 582 | 542 | $success_ids = []; |
| 583 | 543 | $components = $request->get_param( 'components' ); |
| 584 | 544 | $status = $request->get_param( 'status' ); |
| @@ -599,12 +559,8 @@ | ||
| 599 | 559 | ] )->build(); |
| 600 | 560 | } |
| 601 | 561 | |
| 602 | 562 | private function create_validate_components( \WP_REST_Request $request ) { |
| 603 | - if ( ! Components_Access_Controller::can_create() ) { | |
| 604 | - return $this->get_insufficient_permissions_error( 'create' ); | |
| 605 | - } | |
| 606 | - | |
| 607 | 563 | $items = Collection::make( $request->get_param( 'items' ) ); |
| 608 | 564 | $components = $this->get_repository()->all(); |
| 609 | 565 | |
| 610 | 566 | $result = Save_Components_Validator::make( $components )->validate( $items ); |
| @@ -693,17 +649,6 @@ | ||
| 693 | 649 | ->build(); |
| 694 | 650 | } |
| 695 | 651 | |
| 696 | 652 | return $response; |
| 697 | - } | |
| 698 | - | |
| 699 | - private function get_insufficient_permissions_error( string $action ) { | |
| 700 | - return Error_Builder::make( 'insufficient_permissions' ) | |
| 701 | - ->set_status( 403 ) | |
| 702 | - ->set_message( __( 'You do not have permission to perform this action.', 'elementor' ) ) | |
| 703 | - ->set_meta( [ | |
| 704 | - 'action' => $action, | |
| 705 | - 'tier' => Components_Access_Controller::get_access_tier(), | |
| 706 | - ] ) | |
| 707 | - ->build(); | |
| 708 | 653 | } |
| 709 | 654 | } |