← All changes
|
includes/template-library/sources/cloud.php
+8
-294
4.2.0-dev2
→
3.28.0-dev2
View file →
| @@ -1,11 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\TemplateLibrary; |
| 3 | 3 | |
| 4 | -use Elementor\Core\Base\Document; | |
| 5 | 4 | use Elementor\Core\Utils\Exceptions; |
| 6 | 5 | use Elementor\Modules\CloudLibrary\Connect\Cloud_Library; |
| 7 | -use Elementor\Modules\CloudLibrary\Documents\Cloud_Template_Preview; | |
| 8 | 6 | use Elementor\Plugin; |
| 9 | 7 | use Elementor\DB; |
| 10 | 8 | |
| 11 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -37,12 +35,8 @@ | ||
| 37 | 35 | } |
| 38 | 36 | |
| 39 | 37 | public function register_data() {} |
| 40 | 38 | |
| 41 | - public function supports_quota(): bool { | |
| 42 | - return true; | |
| 43 | - } | |
| 44 | - | |
| 45 | 39 | public function get_items( $args = [] ) { |
| 46 | 40 | return $this->get_app()->get_resources( $args ); |
| 47 | 41 | } |
| 48 | 42 | |
| @@ -80,17 +74,8 @@ | ||
| 80 | 74 | if ( ! empty( $args['with_page_settings'] ) ) { |
| 81 | 75 | $data['page_settings'] = $decoded_data['page_settings']; |
| 82 | 76 | } |
| 83 | 77 | |
| 84 | - $snapshots = apply_filters( | |
| 85 | - 'elementor/template_library/get_data/extract_snapshots', | |
| 86 | - [], | |
| 87 | - $decoded_data, | |
| 88 | - $data | |
| 89 | - ); | |
| 90 | - | |
| 91 | - static::attach_global_styles_to_data_static( $data, $snapshots ); | |
| 92 | - | |
| 93 | 78 | // After the upload complete, set the elementor upload state back to false |
| 94 | 79 | Plugin::$instance->uploads_manager->set_elementor_upload_state( false ); |
| 95 | 80 | |
| 96 | 81 | return $data; |
| @@ -102,9 +87,15 @@ | ||
| 102 | 87 | |
| 103 | 88 | public function save_item( $template_data ): int { |
| 104 | 89 | $app = $this->get_app(); |
| 105 | 90 | |
| 106 | - $resource_data = $this->format_resource_item_for_create( $template_data ); | |
| 91 | + $resource_data = [ | |
| 92 | + 'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ), | |
| 93 | + 'type' => self::TEMPLATE_RESOURCE_TYPE, | |
| 94 | + 'templateType' => $template_data['type'], | |
| 95 | + 'parentId' => $template_data['parentId'] ?? null, | |
| 96 | + 'content' => wp_json_encode( $template_data['content'] ), | |
| 97 | + ]; | |
| 107 | 98 | |
| 108 | 99 | $response = $app->post_resource( $resource_data ); |
| 109 | 100 | |
| 110 | 101 | return (int) $response['id']; |
| @@ -109,28 +100,8 @@ | ||
| 109 | 100 | |
| 110 | 101 | return (int) $response['id']; |
| 111 | 102 | } |
| 112 | 103 | |
| 113 | - private function format_resource_item_for_create( $template_data ) { | |
| 114 | - $content_payload = [ | |
| 115 | - 'content' => $template_data['content'], | |
| 116 | - 'page_settings' => $template_data['page_settings'], | |
| 117 | - ]; | |
| 118 | - | |
| 119 | - if ( is_array( $template_data['content'] ?? null ) ) { | |
| 120 | - $this->attach_global_styles_to_data( $content_payload, $template_data['content'], 0 ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - return [ | |
| 124 | - 'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ), | |
| 125 | - 'type' => self::TEMPLATE_RESOURCE_TYPE, | |
| 126 | - 'templateType' => $template_data['type'], | |
| 127 | - 'parentId' => ! empty( $template_data['parentId'] ) ? (int) $template_data['parentId'] : null, | |
| 128 | - 'content' => wp_json_encode( $content_payload ), | |
| 129 | - 'hasPageSettings' => ! empty( $template_data['page_settings'] ), | |
| 130 | - ]; | |
| 131 | - } | |
| 132 | - | |
| 133 | 104 | public function save_folder( array $folder_data = [] ) { |
| 134 | 105 | $app = $this->get_app(); |
| 135 | 106 | |
| 136 | 107 | $resource_data = [ |
| @@ -156,9 +127,9 @@ | ||
| 156 | 127 | public function export_template( $id ) { |
| 157 | 128 | $data = $this->get_app()->get_resource( [ 'id' => $id ] ); |
| 158 | 129 | |
| 159 | 130 | if ( is_wp_error( $data ) ) { |
| 160 | - return new \WP_Error( 'export_template_error', 'An error has occurred' ); | |
| 131 | + return new \WP_Error( 'export_template_error', 'An error has occured' ); | |
| 161 | 132 | } |
| 162 | 133 | |
| 163 | 134 | if ( static::TEMPLATE_RESOURCE_TYPE === $data['type'] ) { |
| 164 | 135 | $this->handle_export_file( $data ); |
| @@ -211,18 +182,8 @@ | ||
| 211 | 182 | 'title' => $data['title'], |
| 212 | 183 | 'type' => $data['templateType'], |
| 213 | 184 | ]; |
| 214 | 185 | |
| 215 | - $this->attach_global_styles_to_data( | |
| 216 | - $export_data, | |
| 217 | - $export_data['content'], | |
| 218 | - (int) ( $data['id'] ?? 0 ), | |
| 219 | - [ | |
| 220 | - 'global_classes' => $data['content']['global_classes'] ?? null, | |
| 221 | - 'global_variables' => $data['content']['global_variables'] ?? null, | |
| 222 | - ] | |
| 223 | - ); | |
| 224 | - | |
| 225 | 186 | return [ |
| 226 | 187 | 'name' => 'elementor-' . $data['id'] . '-' . gmdate( 'Y-m-d' ) . '.json', |
| 227 | 188 | 'content' => wp_json_encode( $export_data ), |
| 228 | 189 | ]; |
| @@ -290,253 +251,6 @@ | ||
| 290 | 251 | return [ |
| 291 | 252 | 'path' => $complete_path, |
| 292 | 253 | 'name' => $file_data['name'], |
| 293 | 254 | ]; |
| 294 | - } | |
| 295 | - | |
| 296 | - public function move_template_to_folder( array $args = [] ) { | |
| 297 | - $move_args = [ | |
| 298 | - 'title' => $args['title'], | |
| 299 | - 'id' => $args['from_template_id'], | |
| 300 | - 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : '', | |
| 301 | - ]; | |
| 302 | - | |
| 303 | - return $this->update_item( $move_args ); | |
| 304 | - } | |
| 305 | - | |
| 306 | - public function move_bulk_templates_to_folder( array $args = [] ) { | |
| 307 | - $move_args = [ | |
| 308 | - 'ids' => $args['from_template_id'], | |
| 309 | - 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : null, | |
| 310 | - ]; | |
| 311 | - | |
| 312 | - return $this->get_app()->bulk_move_templates( $move_args ); | |
| 313 | - } | |
| 314 | - | |
| 315 | - public function save_item_preview( $template_id, $data ) { | |
| 316 | - return $this->get_app()->update_resource_preview( $template_id, $data ); | |
| 317 | - } | |
| 318 | - | |
| 319 | - public function mark_preview_as_failed( $template_id, $data ) { | |
| 320 | - return $this->get_app()->mark_preview_as_failed( $template_id, $data ); | |
| 321 | - } | |
| 322 | - | |
| 323 | - /** | |
| 324 | - * @param int $template_id | |
| 325 | - * @return Document|\WP_Error | |
| 326 | - * @throws \Exception If the user has no permission or the post is not found. | |
| 327 | - */ | |
| 328 | - public function create_document_for_preview( int $template_id ) { | |
| 329 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 330 | - return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'You do not have permission to create preview documents.', 'elementor' ) ); | |
| 331 | - } | |
| 332 | - | |
| 333 | - $cloud_library_app = $this->get_app(); | |
| 334 | - | |
| 335 | - $template = $cloud_library_app->get_resource( [ 'id' => $template_id ] ); | |
| 336 | - | |
| 337 | - if ( is_wp_error( $template ) ) { | |
| 338 | - $error_message = $template->get_error_message(); | |
| 339 | - throw new \Exception( esc_html( $error_message ), Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 340 | - } | |
| 341 | - | |
| 342 | - $decoded_content = json_decode( $template['content'], true ); | |
| 343 | - | |
| 344 | - return $this->save_document_for_preview( [ | |
| 345 | - 'content' => $decoded_content['content'], | |
| 346 | - 'page_settings' => $decoded_content['page_settings'], | |
| 347 | - ] ); | |
| 348 | - } | |
| 349 | - | |
| 350 | - protected function save_document_for_preview( $template_content ) { | |
| 351 | - $template_data = [ | |
| 352 | - 'title' => esc_html__( '(no title)', 'elementor' ), | |
| 353 | - 'page_settings' => $template_content['page_settings'] ?? [], | |
| 354 | - 'status' => 'draft', | |
| 355 | - 'type' => 'container', | |
| 356 | - ]; | |
| 357 | - | |
| 358 | - $document = Plugin::$instance->documents->create( | |
| 359 | - Cloud_Template_Preview::TYPE, | |
| 360 | - [ | |
| 361 | - 'post_title' => $template_data['title'], | |
| 362 | - 'post_status' => $template_data['status'], | |
| 363 | - ] | |
| 364 | - ); | |
| 365 | - | |
| 366 | - if ( is_wp_error( $document ) ) { | |
| 367 | - wp_die(); | |
| 368 | - } | |
| 369 | - | |
| 370 | - $template_data['content'] = $this->replace_elements_ids( $template_content['content'] ); | |
| 371 | - | |
| 372 | - $document->save( [ | |
| 373 | - 'elements' => $template_data['content'], | |
| 374 | - 'settings' => $template_data['page_settings'], | |
| 375 | - ] ); | |
| 376 | - | |
| 377 | - do_action( 'elementor/template-library/after_save_template', $document->get_main_id(), $template_data ); | |
| 378 | - do_action( 'elementor/template-library/after_update_template', $document->get_main_id(), $template_data ); | |
| 379 | - | |
| 380 | - return $document; | |
| 381 | - } | |
| 382 | - | |
| 383 | - public function bulk_delete_items( array $template_ids ) { | |
| 384 | - return $this->get_app()->bulk_delete_resources( $template_ids ); | |
| 385 | - } | |
| 386 | - | |
| 387 | - | |
| 388 | - public function bulk_undo_delete_items( array $template_ids ) { | |
| 389 | - return $this->get_app()->bulk_undo_delete_resources( $template_ids ); | |
| 390 | - } | |
| 391 | - | |
| 392 | - public function save_bulk_items( array $data = [] ) { | |
| 393 | - $items = []; | |
| 394 | - | |
| 395 | - foreach ( $data as $template_data ) { | |
| 396 | - $items[] = $this->format_resource_item_for_create( $template_data ); | |
| 397 | - } | |
| 398 | - | |
| 399 | - return $this->get_app()->post_bulk_resources( $items ); | |
| 400 | - } | |
| 401 | - | |
| 402 | - public function get_bulk_items( array $args = [] ) { | |
| 403 | - return $this->get_app()->get_bulk_resources_with_content( $args ); | |
| 404 | - } | |
| 405 | - | |
| 406 | - public function get_quota() { | |
| 407 | - return $this->get_app()->get_quota(); | |
| 408 | - } | |
| 409 | - | |
| 410 | - public function import_template( $name, $path, $import_mode = 'match_site' ) { | |
| 411 | - if ( empty( $path ) ) { | |
| 412 | - return new \WP_Error( 'file_error', 'Please upload a file to import' ); | |
| 413 | - } | |
| 414 | - | |
| 415 | - Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); | |
| 416 | - | |
| 417 | - $items = []; | |
| 418 | - | |
| 419 | - $quota = $this->get_quota(); | |
| 420 | - | |
| 421 | - if ( is_wp_error( $quota ) ) { | |
| 422 | - return $quota; | |
| 423 | - } | |
| 424 | - | |
| 425 | - if ( $quota['currentUsage'] >= $quota['threshold'] ) { | |
| 426 | - return new \WP_Error( 'quota_error', 'The upload failed because you’ve saved the maximum templates already.' ); | |
| 427 | - } | |
| 428 | - | |
| 429 | - if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) { | |
| 430 | - $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] ); | |
| 431 | - | |
| 432 | - if ( is_wp_error( $extracted_files ) ) { | |
| 433 | - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] ); | |
| 434 | - | |
| 435 | - return $extracted_files; | |
| 436 | - } | |
| 437 | - | |
| 438 | - $items_to_save = []; | |
| 439 | - | |
| 440 | - foreach ( $extracted_files['files'] as $file_path ) { | |
| 441 | - // Skip macOS metadata files and folders | |
| 442 | - if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) { | |
| 443 | - continue; | |
| 444 | - } | |
| 445 | - | |
| 446 | - $prepared = $this->prepare_import_template_data( $file_path, $import_mode ); | |
| 447 | - | |
| 448 | - if ( is_wp_error( $prepared ) ) { | |
| 449 | - // Skip failed templates | |
| 450 | - continue; | |
| 451 | - } | |
| 452 | - | |
| 453 | - $items_to_save[] = $this->format_resource_item_for_create( $prepared ); | |
| 454 | - } | |
| 455 | - | |
| 456 | - $is_quota_valid = $this->validate_quota( $items_to_save ); | |
| 457 | - | |
| 458 | - if ( is_wp_error( $is_quota_valid ) ) { | |
| 459 | - return $is_quota_valid; | |
| 460 | - } | |
| 461 | - | |
| 462 | - if ( ! $is_quota_valid ) { | |
| 463 | - return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' ); | |
| 464 | - } | |
| 465 | - | |
| 466 | - $items = $this->get_app()->post_bulk_resources( $items_to_save ); | |
| 467 | - | |
| 468 | - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] ); | |
| 469 | - } else { | |
| 470 | - $prepared = $this->prepare_import_template_data( $path, $import_mode ); | |
| 471 | - | |
| 472 | - if ( is_wp_error( $prepared ) ) { | |
| 473 | - return $prepared; | |
| 474 | - } | |
| 475 | - | |
| 476 | - $is_quota_valid = $this->validate_quota( [ $prepared ] ); | |
| 477 | - | |
| 478 | - if ( is_wp_error( $is_quota_valid ) ) { | |
| 479 | - return $is_quota_valid; | |
| 480 | - } | |
| 481 | - | |
| 482 | - if ( ! $is_quota_valid ) { | |
| 483 | - return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' ); | |
| 484 | - } | |
| 485 | - | |
| 486 | - $item = $this->get_app()->post_resource( $this->format_resource_item_for_create( $prepared ) ); | |
| 487 | - | |
| 488 | - if ( is_wp_error( $item ) ) { | |
| 489 | - return $item; | |
| 490 | - } | |
| 491 | - | |
| 492 | - $items[] = $item; | |
| 493 | - } | |
| 494 | - | |
| 495 | - return $items; | |
| 496 | - } | |
| 497 | - | |
| 498 | - public function validate_quota( $items ) { | |
| 499 | - $quota = $this->get_quota(); | |
| 500 | - | |
| 501 | - if ( is_wp_error( $quota ) ) { | |
| 502 | - return $quota; | |
| 503 | - } | |
| 504 | - | |
| 505 | - return $quota['currentUsage'] + count( $items ) <= $quota['threshold']; | |
| 506 | - } | |
| 507 | - | |
| 508 | - public function format_args_for_bulk_action( $args ) { | |
| 509 | - $templates = $this->get_bulk_items( $args ); | |
| 510 | - $bulk_args = []; | |
| 511 | - | |
| 512 | - foreach ( $templates as $template ) { | |
| 513 | - $content = json_decode( $template['content'], true ); | |
| 514 | - | |
| 515 | - $bulk_args[] = array_merge( | |
| 516 | - $args, | |
| 517 | - [ | |
| 518 | - 'title' => $template['title'], | |
| 519 | - 'type' => $template['type'], | |
| 520 | - 'content' => $content['content'], | |
| 521 | - 'page_settings' => $content['page_settings'], | |
| 522 | - ] | |
| 523 | - ); | |
| 524 | - } | |
| 525 | - | |
| 526 | - return $bulk_args; | |
| 527 | - } | |
| 528 | - | |
| 529 | - public function format_args_for_single_action( $args ) { | |
| 530 | - $data = $this->get_item( $args['from_template_id'] ); | |
| 531 | - | |
| 532 | - if ( is_wp_error( $data ) || empty( $data['content'] ) ) { | |
| 533 | - return new \WP_Error( 'template_error', 'Unable to format template args.' ); | |
| 534 | - } | |
| 535 | - | |
| 536 | - $decoded_data = json_decode( $data['content'], true ); | |
| 537 | - $args['content'] = $decoded_data['content']; | |
| 538 | - $args['page_settings'] = $decoded_data['page_settings']; | |
| 539 | - | |
| 540 | - return $args; | |
| 541 | 255 | } |
| 542 | 256 | } |