| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | use Elementor\Core\Utils\Api\Error_Builder; |
| 7 | 7 | use Elementor\Core\Utils\Api\Response_Builder; |
| 8 | 8 | use Elementor\Core\Utils\Collection; |
| 9 | 9 | use Elementor\Modules\Components\Documents\Component; |
| 10 | -use Elementor\Modules\Components\OverridableProps\Component_Overridable_Props_Parser; | |
| 11 | 10 | |
| 12 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 12 | exit; // Exit if accessed directly. |
| 14 | 13 | } |
| @@ -17,9 +16,9 @@ | ||
| 17 | 16 | const API_NAMESPACE = 'elementor/v1'; |
| 18 | 17 | const API_BASE = 'components'; |
| 19 | 18 | const LOCK_DOCUMENT_TYPE_NAME = 'components'; |
| 20 | 19 | const STYLES_ROUTE = 'styles'; |
| 21 | - const MAX_COMPONENTS = 100; | |
| 20 | + const MAX_COMPONENTS = 50; | |
| 22 | 21 | |
| 23 | 22 | private $repository = null; |
| 24 | 23 | public function register_hooks() { |
| 25 | 24 | add_action( 'rest_api_init', fn() => $this->register_routes() ); |
| @@ -90,12 +89,8 @@ | ||
| 90 | 89 | 'items' => [ |
| 91 | 90 | 'type' => 'object', |
| 92 | 91 | ], |
| 93 | 92 | ], |
| 94 | - 'settings' => [ | |
| 95 | - 'type' => 'object', | |
| 96 | - 'required' => false, | |
| 97 | - ], | |
| 98 | 93 | ], |
| 99 | 94 | ], |
| 100 | 95 | ], |
| 101 | 96 | ], |
| @@ -101,66 +96,8 @@ | ||
| 101 | 96 | ], |
| 102 | 97 | ], |
| 103 | 98 | ] ); |
| 104 | 99 | |
| 105 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/create-validate', [ | |
| 106 | - [ | |
| 107 | - 'methods' => 'POST', | |
| 108 | - 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->create_validate_components( $request ) ), | |
| 109 | - 'permission_callback' => fn() => current_user_can( 'manage_options' ), | |
| 110 | - 'args' => [ | |
| 111 | - 'items' => [ | |
| 112 | - 'type' => 'array', | |
| 113 | - 'required' => true, | |
| 114 | - 'items' => [ | |
| 115 | - 'type' => 'object', | |
| 116 | - 'properties' => [ | |
| 117 | - 'uid' => [ | |
| 118 | - 'type' => 'string', | |
| 119 | - 'required' => true, | |
| 120 | - ], | |
| 121 | - 'title' => [ | |
| 122 | - 'type' => 'string', | |
| 123 | - 'required' => true, | |
| 124 | - 'minLength' => 2, | |
| 125 | - 'maxLength' => 200, | |
| 126 | - ], | |
| 127 | - 'elements' => [ | |
| 128 | - 'type' => 'array', | |
| 129 | - 'required' => true, | |
| 130 | - 'items' => [ | |
| 131 | - 'type' => 'object', | |
| 132 | - ], | |
| 133 | - ], | |
| 134 | - 'settings' => [ | |
| 135 | - 'type' => 'object', | |
| 136 | - 'required' => false, | |
| 137 | - ], | |
| 138 | - ], | |
| 139 | - ], | |
| 140 | - ], | |
| 141 | - ], | |
| 142 | - ], | |
| 143 | - ] ); | |
| 144 | - | |
| 145 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/overridable-props', [ | |
| 146 | - [ | |
| 147 | - 'methods' => 'GET', | |
| 148 | - 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->get_overridable_props( $request ) ), | |
| 149 | - 'permission_callback' => fn() => current_user_can( 'edit_posts' ), | |
| 150 | - 'args' => [ | |
| 151 | - 'componentIds' => [ | |
| 152 | - 'type' => 'array', | |
| 153 | - 'items' => [ | |
| 154 | - 'type' => 'integer', | |
| 155 | - ], | |
| 156 | - 'required' => true, | |
| 157 | - 'description' => 'The component IDs to get overridable props for', | |
| 158 | - ], | |
| 159 | - ], | |
| 160 | - ], | |
| 161 | - ] ); | |
| 162 | - | |
| 163 | 100 | register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/status', [ |
| 164 | 101 | [ |
| 165 | 102 | 'methods' => 'PUT', |
| 166 | 103 | 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->update_statuses( $request ) ), |
| @@ -226,79 +163,18 @@ | ||
| 226 | 163 | ], |
| 227 | 164 | ], |
| 228 | 165 | ], |
| 229 | 166 | ] ); |
| 230 | - | |
| 231 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/archive', [ | |
| 232 | - [ | |
| 233 | - 'methods' => 'POST', | |
| 234 | - 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->archive_components( $request ) ), | |
| 235 | - 'permission_callback' => fn() => current_user_can( 'manage_options' ), | |
| 236 | - 'args' => [ | |
| 237 | - 'componentIds' => [ | |
| 238 | - 'type' => 'array', | |
| 239 | - 'items' => [ | |
| 240 | - 'type' => 'number', | |
| 241 | - 'required' => true, | |
| 242 | - ], | |
| 243 | - 'required' => true, | |
| 244 | - 'description' => 'The component IDs to archive', | |
| 245 | - ], | |
| 246 | - 'status' => [ | |
| 247 | - 'type' => 'string', | |
| 248 | - 'enum' => [ Document::STATUS_PUBLISH, Document::STATUS_DRAFT, Document::STATUS_AUTOSAVE ], | |
| 249 | - 'required' => true, | |
| 250 | - ], | |
| 251 | - ], | |
| 252 | - ], | |
| 253 | - ] ); | |
| 254 | - | |
| 255 | - register_rest_route( self::API_NAMESPACE, '/' . self::API_BASE . '/update-titles', [ | |
| 256 | - [ | |
| 257 | - 'methods' => 'POST', | |
| 258 | - 'callback' => fn( $request ) => $this->route_wrapper( fn() => $this->update_components_title( $request ) ), | |
| 259 | - 'permission_callback' => fn() => current_user_can( 'manage_options' ), | |
| 260 | - 'args' => [ | |
| 261 | - 'components' => [ | |
| 262 | - 'type' => 'array', | |
| 263 | - 'required' => true, | |
| 264 | - 'items' => [ | |
| 265 | - 'type' => 'object', | |
| 266 | - 'properties' => [ | |
| 267 | - 'componentId' => [ | |
| 268 | - 'type' => 'number', | |
| 269 | - 'required' => true, | |
| 270 | - 'description' => 'The component ID to update title', | |
| 271 | - ], | |
| 272 | - 'title' => [ | |
| 273 | - 'type' => 'string', | |
| 274 | - 'required' => true, | |
| 275 | - 'description' => 'The new title for the component', | |
| 276 | - ], | |
| 277 | - ], | |
| 278 | - ], | |
| 279 | - ], | |
| 280 | - 'status' => [ | |
| 281 | - 'type' => 'string', | |
| 282 | - 'enum' => [ Document::STATUS_PUBLISH, Document::STATUS_DRAFT, Document::STATUS_AUTOSAVE ], | |
| 283 | - 'required' => true, | |
| 284 | - ], | |
| 285 | - ], | |
| 286 | - ], | |
| 287 | - ] ); | |
| 288 | 167 | } |
| 289 | 168 | |
| 290 | 169 | private function get_components() { |
| 291 | 170 | $components = $this->get_repository()->all(); |
| 292 | 171 | |
| 293 | - $components_list = array_values( $components | |
| 294 | - ->map( fn( $component ) => [ | |
| 295 | - 'id' => $component['id'], | |
| 296 | - 'name' => $component['title'], | |
| 297 | - 'uid' => $component['uid'], | |
| 298 | - 'isArchived' => $component['is_archived'] ?? false, | |
| 299 | - ] ) | |
| 300 | - ->all() ); | |
| 172 | + $components_list = $components->map( fn( $component ) => [ | |
| 173 | + 'id' => $component['id'], | |
| 174 | + 'name' => $component['title'], | |
| 175 | + 'uid' => $component['uid'], | |
| 176 | + ])->all(); | |
| 301 | 177 | |
| 302 | 178 | return Response_Builder::make( $components_list )->build(); |
| 303 | 179 | } |
| 304 | 180 | |
| @@ -312,50 +188,9 @@ | ||
| 312 | 188 | |
| 313 | 189 | return Response_Builder::make( $styles )->build(); |
| 314 | 190 | } |
| 315 | 191 | |
| 316 | - private function get_overridable_props( \WP_REST_Request $request ) { | |
| 317 | - $component_ids = $request->get_param( 'componentIds' ); | |
| 318 | - | |
| 319 | - $data = []; | |
| 320 | - $errors = []; | |
| 321 | - | |
| 322 | - foreach ( $component_ids as $component_id ) { | |
| 323 | - $component_id = (int) $component_id; | |
| 324 | - | |
| 325 | - /** @var Component $document */ | |
| 326 | - $document = $this->get_repository()->get( $component_id ); | |
| 327 | - | |
| 328 | - if ( ! $document ) { | |
| 329 | - $errors[ $component_id ] = 'component_not_found'; | |
| 330 | - continue; | |
| 331 | - } | |
| 332 | - | |
| 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; | |
| 346 | - } | |
| 347 | - | |
| 348 | - return Response_Builder::make( $data ) | |
| 349 | - ->set_meta( [ 'errors' => $errors ] ) | |
| 350 | - ->build(); | |
| 351 | - } | |
| 352 | - | |
| 353 | 192 | 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 | 193 | $save_status = $request->get_param( 'status' ); |
| 359 | 194 | |
| 360 | 195 | $items = Collection::make( $request->get_param( 'items' ) ); |
| 361 | 196 | $components = $this->get_repository()->all(); |
| @@ -363,86 +198,53 @@ | ||
| 363 | 198 | $result = Save_Components_Validator::make( $components )->validate( $items ); |
| 364 | 199 | |
| 365 | 200 | if ( ! $result['success'] ) { |
| 366 | 201 | return Error_Builder::make( 'components_validation_failed' ) |
| 367 | - ->set_status( 422 ) | |
| 202 | + ->set_status( 400 ) | |
| 368 | 203 | ->set_message( 'Validation failed: ' . implode( ', ', $result['messages'] ) ) |
| 369 | 204 | ->build(); |
| 370 | 205 | } |
| 371 | 206 | |
| 372 | - $circular_result = Circular_Dependency_Validator::make()->validate_new_components( $items ); | |
| 373 | - | |
| 374 | - if ( ! $circular_result['success'] ) { | |
| 375 | - return Error_Builder::make( 'circular_dependency_detected' ) | |
| 376 | - ->set_status( 422 ) | |
| 377 | - ->set_message( __( "Can't add this component - components that contain each other can't be nested.", 'elementor' ) ) | |
| 378 | - ->set_meta( [ 'caused_by' => $circular_result['messages'] ] ) | |
| 379 | - ->build(); | |
| 380 | - } | |
| 381 | - | |
| 382 | - $non_atomic_result = Non_Atomic_Widget_Validator::make()->validate_items( $items ); | |
| 383 | - | |
| 384 | - if ( ! $non_atomic_result['success'] ) { | |
| 385 | - return Error_Builder::make( Non_Atomic_Widget_Validator::ERROR_CODE ) | |
| 386 | - ->set_status( 422 ) | |
| 387 | - ->set_message( __( 'Components require atomic elements only. Remove widgets to create this component.', 'elementor' ) ) | |
| 388 | - ->set_meta( [ 'non_atomic_elements' => $non_atomic_result['non_atomic_elements'] ] ) | |
| 389 | - ->build(); | |
| 390 | - } | |
| 391 | - | |
| 392 | - $validation_errors = []; | |
| 393 | - | |
| 394 | - $created = $items->map_with_keys( function ( $item ) use ( $save_status, &$validation_errors ) { | |
| 207 | + $created = $items->map_with_keys( function ( $item ) use ( $save_status ) { | |
| 395 | 208 | $title = sanitize_text_field( $item['title'] ); |
| 396 | 209 | $content = $item['elements']; |
| 397 | 210 | $uid = $item['uid']; |
| 398 | 211 | |
| 399 | - try { | |
| 400 | - $settings = isset( $item['settings'] ) ? $this->parse_settings( $item['settings'] ) : []; | |
| 212 | + $status = Document::STATUS_AUTOSAVE === $save_status | |
| 213 | + ? Document::STATUS_DRAFT | |
| 214 | + : $save_status; | |
| 401 | 215 | |
| 402 | - $status = Document::STATUS_AUTOSAVE === $save_status | |
| 403 | - ? Document::STATUS_DRAFT | |
| 404 | - : $save_status; | |
| 216 | + $component_id = $this->get_repository()->create( $title, $content, $status, $uid ); | |
| 405 | 217 | |
| 406 | - $component_id = $this->get_repository()->create( $title, $content, $status, $uid, $settings ); | |
| 407 | - | |
| 408 | - return [ $uid => $component_id ]; | |
| 409 | - } catch ( \Exception $e ) { | |
| 410 | - $validation_errors[ $uid ] = $e->getMessage(); | |
| 411 | - return [ $uid => null ]; | |
| 412 | - } | |
| 218 | + return [ $uid => $component_id ]; | |
| 413 | 219 | } ); |
| 414 | 220 | |
| 415 | - if ( ! empty( $validation_errors ) ) { | |
| 416 | - return Error_Builder::make( 'settings_validation_failed' ) | |
| 417 | - ->set_status( 422 ) | |
| 418 | - ->set_message( 'Settings validation failed: ' . json_encode( $validation_errors ) ) | |
| 419 | - ->build(); | |
| 420 | - } | |
| 421 | - | |
| 422 | - return Response_Builder::make( $created->all() ) | |
| 221 | + return Response_Builder::make( (object) $created->all() ) | |
| 423 | 222 | ->set_status( 201 ) |
| 424 | 223 | ->build(); |
| 425 | 224 | } |
| 426 | 225 | |
| 427 | 226 | private function update_statuses( \WP_REST_Request $request ) { |
| 428 | - if ( ! Components_Access_Controller::can_publish() ) { | |
| 429 | - return $this->get_insufficient_permissions_error( 'publish' ); | |
| 430 | - } | |
| 227 | + $status = $request->get_param( 'status' ); | |
| 431 | 228 | |
| 432 | 229 | $result = Collection::make( $request->get_param( 'ids' ) ) |
| 230 | + ->map( fn( $id ) => $this->get_repository()->get( $id ) ) | |
| 231 | + ->filter( fn( $component ) => (bool) $component ) | |
| 433 | 232 | ->reduce( |
| 434 | - function ( $result, int $component_id ) { | |
| 435 | - $component = $this->get_repository()->get( $component_id ); | |
| 233 | + function ( $result, Component $component ) use ( $status ) { | |
| 234 | + $post = $component->get_post(); | |
| 235 | + $autosave = $component->get_newer_autosave(); | |
| 436 | 236 | |
| 437 | - if ( ! $component ) { | |
| 438 | - $result['failed'][] = $component_id; | |
| 439 | - return $result; | |
| 440 | - } | |
| 237 | + $elements = $autosave | |
| 238 | + ? $autosave->get_json_meta( Document::ELEMENTOR_DATA_META_KEY ) | |
| 239 | + : $component->get_json_meta( Document::ELEMENTOR_DATA_META_KEY ); | |
| 441 | 240 | |
| 442 | - $publish_result = $this->get_repository()->publish_component( $component ); | |
| 241 | + $is_updated = $component->save( [ | |
| 242 | + 'settings' => [ 'post_status' => $status ], | |
| 243 | + 'elements' => $elements, | |
| 244 | + ] ); | |
| 443 | 245 | |
| 444 | - $result[ $publish_result ? 'success' : 'failed' ][] = $component_id; | |
| 246 | + $result[ $is_updated ? 'success' : 'failed' ][] = $post->ID; | |
| 445 | 247 | |
| 446 | 248 | return $result; |
| 447 | 249 | }, |
| 448 | 250 | [ |
| @@ -454,12 +256,8 @@ | ||
| 454 | 256 | return Response_Builder::make( $result )->build(); |
| 455 | 257 | } |
| 456 | 258 | |
| 457 | 259 | 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 | 260 | $component_id = $request->get_param( 'componentId' ); |
| 463 | 261 | try { |
| 464 | 262 | $success = $this->get_component_lock_manager()->lock( $component_id ); |
| 465 | 263 | } catch ( \Exception $e ) { |
| @@ -480,12 +278,8 @@ | ||
| 480 | 278 | return Response_Builder::make( [ 'locked' => $success ] )->build(); |
| 481 | 279 | } |
| 482 | 280 | |
| 483 | 281 | 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 | 282 | $component_id = $request->get_param( 'componentId' ); |
| 489 | 283 | try { |
| 490 | 284 | $success = $this->get_component_lock_manager()->unlock( $component_id ); |
| 491 | 285 | } catch ( \Exception $e ) { |
| @@ -505,12 +299,8 @@ | ||
| 505 | 299 | return Response_Builder::make( [ 'unlocked' => $success ] )->build(); |
| 506 | 300 | } |
| 507 | 301 | |
| 508 | 302 | 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 | 303 | $component_id = (int) $request->get_param( 'componentId' ); |
| 514 | 304 | try { |
| 515 | 305 | $lock_manager = $this->get_component_lock_manager(); |
| 516 | 306 | if ( $lock_manager->is_lock_expired( $component_id ) ) { |
| @@ -551,159 +341,16 @@ | ||
| 551 | 341 | ->build(); |
| 552 | 342 | } |
| 553 | 343 | } |
| 554 | 344 | |
| 555 | - 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 | - $component_ids = $request->get_param( 'componentIds' ); | |
| 561 | - $status = $request->get_param( 'status' ); | |
| 562 | - | |
| 563 | - try { | |
| 564 | - $result = $this->get_repository()->archive( $component_ids, $status ); | |
| 565 | - } catch ( \Exception $e ) { | |
| 566 | - error_log( 'Components REST API archive_components error: ' . $e->getMessage() ); | |
| 567 | - return Error_Builder::make( 'archive_failed' ) | |
| 568 | - ->set_meta( [ 'error' => $e->getMessage() ] ) | |
| 569 | - ->set_status( 500 ) | |
| 570 | - ->set_message( __( 'Failed to archive components', 'elementor' ) ) | |
| 571 | - ->build(); | |
| 572 | - } | |
| 573 | - return Response_Builder::make( $result )->build(); | |
| 574 | - } | |
| 575 | - | |
| 576 | - 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 | - $failed_ids = []; | |
| 582 | - $success_ids = []; | |
| 583 | - $components = $request->get_param( 'components' ); | |
| 584 | - $status = $request->get_param( 'status' ); | |
| 585 | - | |
| 586 | - foreach ( $components as $component ) { | |
| 587 | - $is_success = $this->get_repository()->update_title( $component['componentId'], $component['title'], $status ); | |
| 588 | - | |
| 589 | - if ( ! $is_success ) { | |
| 590 | - $failed_ids[] = $component['componentId']; | |
| 591 | - continue; | |
| 592 | - } | |
| 593 | - $success_ids[] = $component['componentId']; | |
| 594 | - | |
| 595 | - } | |
| 596 | - return Response_Builder::make( [ | |
| 597 | - 'failedIds' => $failed_ids, | |
| 598 | - 'successIds' => $success_ids, | |
| 599 | - ] )->build(); | |
| 600 | - } | |
| 601 | - | |
| 602 | - 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 | - $items = Collection::make( $request->get_param( 'items' ) ); | |
| 608 | - $components = $this->get_repository()->all(); | |
| 609 | - | |
| 610 | - $result = Save_Components_Validator::make( $components )->validate( $items ); | |
| 611 | - | |
| 612 | - if ( ! $result['success'] ) { | |
| 613 | - return Error_Builder::make( 'components_validation_failed' ) | |
| 614 | - ->set_status( 422 ) | |
| 615 | - ->set_message( 'Validation failed: ' . implode( ', ', $result['messages'] ) ) | |
| 616 | - ->build(); | |
| 617 | - } | |
| 618 | - | |
| 619 | - $circular_result = Circular_Dependency_Validator::make()->validate_new_components( $items ); | |
| 620 | - | |
| 621 | - if ( ! $circular_result['success'] ) { | |
| 622 | - return Error_Builder::make( 'circular_dependency_detected' ) | |
| 623 | - ->set_status( 422 ) | |
| 624 | - ->set_message( __( "Can't add this component - components that contain each other can't be nested.", 'elementor' ) ) | |
| 625 | - ->set_meta( [ 'caused_by' => $circular_result['messages'] ] ) | |
| 626 | - ->build(); | |
| 627 | - } | |
| 628 | - | |
| 629 | - $non_atomic_result = Non_Atomic_Widget_Validator::make()->validate_items( $items ); | |
| 630 | - | |
| 631 | - if ( ! $non_atomic_result['success'] ) { | |
| 632 | - return Error_Builder::make( Non_Atomic_Widget_Validator::ERROR_CODE ) | |
| 633 | - ->set_status( 422 ) | |
| 634 | - ->set_message( __( 'Components require atomic elements only. Remove widgets to create this component.', 'elementor' ) ) | |
| 635 | - ->set_meta( [ 'non_atomic_elements' => $non_atomic_result['non_atomic_elements'] ] ) | |
| 636 | - ->build(); | |
| 637 | - } | |
| 638 | - | |
| 639 | - $validation_errors = $items->map_with_keys( function ( $item ) { | |
| 640 | - try { | |
| 641 | - if ( isset( $item['settings'] ) ) { | |
| 642 | - $this->parse_settings( $item['settings'] ); | |
| 643 | - } | |
| 644 | - } catch ( \Exception $e ) { | |
| 645 | - return [ $item['uid'] => $e->getMessage() ]; | |
| 646 | - } | |
| 647 | - | |
| 648 | - return [ $item['uid'] => null ]; | |
| 649 | - } ) | |
| 650 | - ->filter( fn( $value ) => null !== $value ); | |
| 651 | - | |
| 652 | - if ( ! $validation_errors->is_empty() ) { | |
| 653 | - return Error_Builder::make( 'settings_validation_failed' ) | |
| 654 | - ->set_status( 422 ) | |
| 655 | - ->set_message( 'Settings validation failed: ' . json_encode( $validation_errors->all() ) ) | |
| 656 | - ->build(); | |
| 657 | - } | |
| 658 | - | |
| 659 | - return Response_Builder::make() | |
| 660 | - ->set_status( 200 ) | |
| 661 | - ->build(); | |
| 662 | - } | |
| 663 | - | |
| 664 | - private function parse_settings( array $settings ): array { | |
| 665 | - $result = []; | |
| 666 | - | |
| 667 | - if ( empty( $settings ) ) { | |
| 668 | - return $result; | |
| 669 | - } | |
| 670 | - | |
| 671 | - if ( isset( $settings['overridable_props'] ) ) { | |
| 672 | - $parser = Component_Overridable_Props_Parser::make(); | |
| 673 | - $overridable_props_result = $parser->parse( $settings['overridable_props'] ); | |
| 674 | - | |
| 675 | - if ( ! $overridable_props_result->is_valid() ) { | |
| 676 | - throw new \Exception( | |
| 677 | - esc_html( 'Validation failed for overridable_props: ' . $overridable_props_result->errors()->to_string() ) | |
| 678 | - ); | |
| 679 | - } | |
| 680 | - | |
| 681 | - $result['overridable_props'] = $overridable_props_result->unwrap(); | |
| 682 | - } | |
| 683 | - | |
| 684 | - return $result; | |
| 685 | - } | |
| 686 | - | |
| 687 | 345 | private function route_wrapper( callable $cb ) { |
| 688 | 346 | try { |
| 689 | 347 | $response = $cb(); |
| 690 | 348 | } catch ( \Exception $e ) { |
| 691 | 349 | return Error_Builder::make( 'unexpected_error' ) |
| 692 | - ->set_message( __( 'Something went wrong', 'elementor' ) ) | |
| 693 | - ->build(); | |
| 350 | + ->set_message( __( 'Something went wrong', 'elementor' ) ) | |
| 351 | + ->build(); | |
| 694 | 352 | } |
| 695 | 353 | |
| 696 | 354 | 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 | 355 | } |
| 709 | 356 | } |