PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev2
Elementor Website Builder – more than just a page builder v3.35.0-dev2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / base / document.php

document.php in Elementor Website Builder – more than just a page builder 3.35.0-dev2, at core/base/document.php

2,109 lines 52.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Base;
3
4 use Elementor\Core\Base\Elements_Iteration_Actions\Assets as Assets_Iteration_Action;
5 use Elementor\Core\Base\Elements_Iteration_Actions\Base as Elements_Iteration_Action;
6 use Elementor\Core\Behaviors\Interfaces\Lock_Behavior;
7 use Elementor\Core\Files\CSS\Post as Post_CSS;
8 use Elementor\Core\Settings\Page\Model as Page_Model;
9 use Elementor\Core\Utils\Collection;
10 use Elementor\Core\Utils\Exceptions;
11 use Elementor\Includes\Elements\Container;
12 use Elementor\Plugin;
13 use Elementor\Controls_Manager;
14 use Elementor\Controls_Stack;
15 use Elementor\TemplateLibrary\Source_Local;
16 use Elementor\User;
17 use Elementor\Core\Settings\Manager as SettingsManager;
18 use Elementor\Utils;
19 use Elementor\Widget_Base;
20 use Elementor\Core\Settings\Page\Manager as PageManager;
21 use ElementorPro\Modules\Library\Widgets\Template;
22 use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager;
23
24 if ( ! defined( 'ABSPATH' ) ) {
25 exit; // Exit if accessed directly.
26 }
27
28 /**
29 * Elementor document.
30 *
31 * An abstract class that provides the needed properties and methods to
32 * manage and handle documents in inheriting classes.
33 *
34 * @since 2.0.0
35 * @abstract
36 */
37 abstract class Document extends Controls_Stack {
38
39 /**
40 * Document type meta key.
41 */
42 const TYPE_META_KEY = '_elementor_template_type';
43 const PAGE_META_KEY = '_elementor_page_settings';
44 const ELEMENTOR_DATA_META_KEY = '_elementor_data';
45
46 const BUILT_WITH_ELEMENTOR_META_KEY = '_elementor_edit_mode';
47
48 const CACHE_META_KEY = '_elementor_element_cache';
49
50 /**
51 * Document publish status.
52 */
53 const STATUS_PUBLISH = 'publish';
54
55 /**
56 * Document draft status.
57 */
58 const STATUS_DRAFT = 'draft';
59
60 /**
61 * Document private status.
62 */
63 const STATUS_PRIVATE = 'private';
64
65 /**
66 * Document autosave status.
67 */
68 const STATUS_AUTOSAVE = 'autosave';
69
70 /**
71 * Document pending status.
72 */
73 const STATUS_PENDING = 'pending';
74
75 /**
76 * @var int
77 */
78 private $main_id;
79
80 /**
81 * @var bool
82 */
83 private $is_saving = false;
84
85 private static $properties = [];
86
87 /**
88 * @var Elements_Iteration_Action[]
89 */
90 private $elements_iteration_actions = [];
91
92 /**
93 * Document post data.
94 *
95 * Holds the document post data.
96 *
97 * @since 2.0.0
98 * @access protected
99 *
100 * @var \WP_Post WordPress post data.
101 */
102 protected $post;
103
104 /**
105 * @param array $internal_elements
106 *
107 * @return array[]
108 */
109 private function get_container_elements_data( array $internal_elements ): array {
110 return [
111 [
112 'id' => Utils::generate_random_string(),
113 'elType' => 'container',
114 'elements' => $internal_elements,
115 ],
116 ];
117 }
118
119 /**
120 * @param array $internal_elements
121 *
122 * @return array[]
123 */
124 private function get_sections_elements_data( array $internal_elements ): array {
125 return [
126 [
127 'id' => Utils::generate_random_string(),
128 'elType' => 'section',
129 'elements' => [
130 [
131 'id' => Utils::generate_random_string(),
132 'elType' => 'column',
133 'elements' => $internal_elements,
134 ],
135 ],
136 ],
137 ];
138 }
139
140 /**
141 * @since 2.1.0
142 * @access protected
143 * @static
144 */
145 protected static function get_editor_panel_categories() {
146 return Plugin::$instance->elements_manager->get_categories();
147 }
148
149 /**
150 * Get properties.
151 *
152 * Retrieve the document properties.
153 *
154 * @since 2.0.0
155 * @access public
156 * @static
157 *
158 * @return array Document properties.
159 */
160 public static function get_properties() {
161 return [
162 'has_elements' => true,
163 'is_editable' => true,
164 'edit_capability' => '',
165 'show_in_finder' => true,
166 'show_on_admin_bar' => true,
167 'support_kit' => false,
168 'show_navigator' => true,
169 'allow_adding_widgets' => true,
170 'support_page_layout' => true,
171 'show_copy_and_share' => false,
172 'library_close_title' => esc_html__( 'Close', 'elementor' ),
173 'publish_button_title' => esc_html__( 'Publish', 'elementor' ),
174 'allow_closing_remote_library' => true,
175 ];
176 }
177
178 /**
179 * @since 2.1.0
180 * @access public
181 * @static
182 */
183 public static function get_editor_panel_config() {
184 $default_route = 'panel/elements/categories';
185
186 if ( ! Plugin::instance()->role_manager->user_can( 'design' ) ) {
187 $default_route = 'panel/page-settings/settings';
188 }
189
190 return [
191 'title' => static::get_title(), // JS Container title.
192 'widgets_settings' => [],
193 'elements_categories' => self::get_filtered_editor_panel_categories(),
194 'default_route' => $default_route,
195 'has_elements' => static::get_property( 'has_elements' ),
196 'support_kit' => static::get_property( 'support_kit' ),
197 'messages' => [
198 'publish_notification' => sprintf(
199 /* translators: %s: Document title. */
200 esc_html__( 'Hurray! Your %s is live.', 'elementor' ),
201 static::get_title()
202 ),
203 ],
204 'show_navigator' => static::get_property( 'show_navigator' ),
205 'allow_adding_widgets' => static::get_property( 'allow_adding_widgets' ),
206 'show_copy_and_share' => static::get_property( 'show_copy_and_share' ),
207 'library_close_title' => static::get_property( 'library_close_title' ),
208 'publish_button_title' => static::get_property( 'publish_button_title' ),
209 'allow_closing_remote_library' => static::get_property( 'allow_closing_remote_library' ),
210 ];
211 }
212
213 public static function get_filtered_editor_panel_categories(): array {
214 $categories = static::get_editor_panel_categories();
215 $has_pro = Utils::has_pro();
216
217 foreach ( $categories as $index => $category ) {
218 if ( isset( $category['promotion'] ) ) {
219 $categories = self::get_panel_category_item( $category['promotion'], $index, $categories, $has_pro );
220 }
221 }
222
223 return $categories;
224 }
225
226 /**
227 * @param $promotion
228 * @param $index
229 * @param array $categories
230 *
231 * @return array
232 */
233 private static function get_panel_category_item( $promotion, $index, array $categories, bool $has_pro ): array {
234 if ( ! $has_pro ) {
235 $categories[ $index ]['promotion'] = Filtered_Promotions_Manager::get_filtered_promotion_data(
236 $promotion,
237 'elementor/panel/' . $index . '/custom_promotion',
238 'url'
239 );
240 } else {
241 unset( $categories[ $index ]['promotion'] );
242 }
243
244 return $categories;
245 }
246
247 /**
248 * Get element title.
249 *
250 * Retrieve the element title.
251 *
252 * @since 2.0.0
253 * @access public
254 * @static
255 *
256 * @return string Element title.
257 */
258 public static function get_title() {
259 return esc_html__( 'Document', 'elementor' );
260 }
261
262 public static function get_plural_title() {
263 return static::get_title();
264 }
265
266 public static function get_add_new_title() {
267 return sprintf(
268 /* translators: %s: Document title. */
269 esc_html__( 'Add New %s', 'elementor' ),
270 static::get_title()
271 );
272 }
273
274 /**
275 * Get property.
276 *
277 * Retrieve the document property.
278 *
279 * @since 2.0.0
280 * @access public
281 * @static
282 *
283 * @param string $key The property key.
284 *
285 * @return mixed The property value.
286 */
287 public static function get_property( $key ) {
288 $id = static::get_class_full_name();
289
290 if ( ! isset( self::$properties[ $id ] ) ) {
291 self::$properties[ $id ] = static::get_properties();
292 }
293
294 return self::get_items( self::$properties[ $id ], $key );
295 }
296
297 /**
298 * @since 2.0.0
299 * @access public
300 * @static
301 */
302 public static function get_class_full_name() {
303 return get_called_class();
304 }
305
306 public static function get_create_url() {
307 $properties = static::get_properties();
308
309 // BC Support - Each document should define it own CPT this code is for BC support.
310 $cpt = Source_Local::CPT;
311
312 if ( isset( $properties['cpt'][0] ) ) {
313 $cpt = $properties['cpt'][0];
314 }
315
316 return Plugin::$instance->documents->get_create_new_post_url( $cpt, static::get_type() );
317 }
318
319 public function get_name() {
320 return static::get_type();
321 }
322
323 /**
324 * @since 2.0.0
325 * @access public
326 */
327 public function get_unique_name() {
328 return static::get_type() . '-' . $this->post->ID;
329 }
330
331 /**
332 * @since 2.3.0
333 * @access public
334 */
335 public function get_post_type_title() {
336 $post_type_object = get_post_type_object( $this->post->post_type );
337
338 return $post_type_object->labels->singular_name;
339 }
340
341 /**
342 * @since 2.0.0
343 * @access public
344 */
345 public function get_main_id() {
346 if ( ! $this->main_id ) {
347 $post_id = $this->post->ID;
348
349 $parent_post_id = wp_is_post_revision( $post_id );
350
351 if ( $parent_post_id ) {
352 $post_id = $parent_post_id;
353 }
354
355 $this->main_id = $post_id;
356 }
357
358 return $this->main_id;
359 }
360
361 /**
362 * @return null|Lock_Behavior
363 */
364 public static function get_lock_behavior_v2() {
365 return null;
366 }
367
368 /**
369 * @since 2.0.0
370 * @access public
371 *
372 * @param $data
373 *
374 * @throws \Exception If the widget was not found.
375 *
376 * @return string
377 */
378 public function render_element( $data ) {
379 // Start buffering
380 ob_start();
381
382 /** @var Widget_Base $widget */
383 $widget = Plugin::$instance->elements_manager->create_element_instance( $data );
384
385 if ( ! $widget ) {
386 throw new \Exception( 'Widget not found.' );
387 }
388
389 $widget->render_content();
390
391 $render_html = ob_get_clean();
392
393 return $render_html;
394 }
395
396 /**
397 * @since 2.0.0
398 * @access public
399 */
400 public function get_main_post() {
401 return get_post( $this->get_main_id() );
402 }
403
404 public function get_container_attributes() {
405 $id = $this->get_main_id();
406
407 $attributes = [
408 'data-elementor-type' => $this->get_name(),
409 'data-elementor-id' => $id,
410 'class' => 'elementor elementor-' . $id,
411 ];
412
413 $version_meta = $this->get_main_meta( '_elementor_version' );
414
415 if ( version_compare( $version_meta, '2.5.0', '<' ) ) {
416 $attributes['class'] .= ' elementor-bc-flex-widget';
417 }
418
419 if ( Plugin::$instance->preview->is_preview() ) {
420 $attributes['data-elementor-title'] = static::get_title();
421 } else {
422 $elementor_settings = $this->get_frontend_settings();
423 if ( ! empty( $elementor_settings ) ) {
424 $attributes['data-elementor-settings'] = wp_json_encode( $elementor_settings );
425 }
426 }
427
428 // apply this filter to allow the attributes to be modified by different sources
429 return apply_filters( 'elementor/document/wrapper_attributes', $attributes, $this );
430 }
431
432 /**
433 * @since 2.0.0
434 * @access public
435 */
436 public function get_wp_preview_url() {
437 $main_post_id = $this->get_main_id();
438 $document = $this;
439
440 // Ajax request from editor.
441 $initial_document_id = Utils::get_super_global_value( $_POST, 'initial_document_id' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
442
443 if ( ! empty( $initial_document_id ) ) {
444 $document = Plugin::$instance->documents->get( $initial_document_id ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
445 }
446
447 $url = get_preview_post_link(
448 $document->get_main_id(),
449 [
450 'preview_id' => $main_post_id,
451 'preview_nonce' => wp_create_nonce( 'post_preview_' . $main_post_id ),
452 ]
453 );
454
455 /**
456 * Document "WordPress preview" URL.
457 *
458 * Filters the WordPress preview URL.
459 *
460 * @since 2.0.0
461 *
462 * @param string $url WordPress preview URL.
463 * @param Document $this The document instance.
464 */
465 $url = apply_filters( 'elementor/document/urls/wp_preview', $url, $this );
466
467 return $url;
468 }
469
470 /**
471 * @since 2.0.0
472 * @access public
473 */
474 public function get_exit_to_dashboard_url() {
475 $url = get_edit_post_link( $this->get_main_id(), 'raw' );
476
477 /**
478 * Document "exit to dashboard" URL.
479 *
480 * Filters the "Exit To Dashboard" URL.
481 *
482 * @since 2.0.0
483 *
484 * @param string $url The exit URL
485 * @param Document $this The document instance.
486 */
487 $url = apply_filters( 'elementor/document/urls/exit_to_dashboard', $url, $this );
488
489 return $url;
490 }
491
492 /**
493 * Get All Post Type URL
494 *
495 * Get url of the page which display all the posts of the current active document's post type.
496 *
497 * @since 3.7.0
498 *
499 * @return string $url
500 */
501 public function get_all_post_type_url() {
502 $post_type = get_post_type( $this->get_main_id() );
503
504 $url = get_admin_url() . 'edit.php';
505
506 if ( 'post' !== $post_type ) {
507 $url .= '?post_type=' . $post_type;
508 }
509
510 /**
511 * Document "display all post type" URL.
512 *
513 * @since 3.7.0
514 *
515 * @param string $url The URL.
516 * @param Document $this The document instance.
517 */
518 $url = apply_filters( 'elementor/document/urls/all_post_type', $url, $this );
519
520 return $url;
521 }
522
523 /**
524 * Get Main WP dashboard URL.
525 *
526 * @since 3.7.0
527 *
528 * @return string $url
529 */
530 protected function get_main_dashboard_url() {
531 $url = get_dashboard_url();
532
533 /**
534 * Document "Main Dashboard" URL.
535 *
536 * @since 3.7.0
537 *
538 * @param string $url The URL.
539 * @param Document $this The document instance.
540 */
541 $url = apply_filters( 'elementor/document/urls/main_dashboard', $url, $this );
542
543 return $url;
544 }
545
546 /**
547 * Get auto-saved post revision.
548 *
549 * Retrieve the auto-saved post revision that is newer than current post.
550 *
551 * @since 2.0.0
552 * @access public
553 *
554 * @return bool|Document
555 */
556 public function get_newer_autosave() {
557 $autosave = $this->get_autosave();
558
559 // Detect if there exists an autosave newer than the post.
560 if ( $autosave && mysql2date( 'U', $autosave->get_post()->post_modified_gmt, false ) > mysql2date( 'U', $this->post->post_modified_gmt, false ) ) {
561 return $autosave;
562 }
563
564 return false;
565 }
566
567 /**
568 * @since 2.0.0
569 * @access public
570 */
571 public function is_autosave() {
572 return wp_is_post_autosave( $this->post->ID );
573 }
574
575 /**
576 * Check if the current document is a 'revision'
577 *
578 * @return bool
579 */
580 public function is_revision() {
581 return 'revision' === $this->post->post_type;
582 }
583
584 /**
585 * Checks if the current document status is 'trash'.
586 *
587 * @return bool
588 */
589 public function is_trash() {
590 return 'trash' === $this->post->post_status;
591 }
592
593 /**
594 * @since 2.0.0
595 * @access public
596 *
597 * @param int $user_id
598 * @param bool $create
599 *
600 * @return bool|Document
601 */
602 public function get_autosave( $user_id = 0, $create = false ) {
603 if ( ! $user_id ) {
604 $user_id = get_current_user_id();
605 }
606
607 $autosave_id = $this->get_autosave_id( $user_id );
608
609 if ( $autosave_id ) {
610 $document = Plugin::$instance->documents->get( $autosave_id );
611 } elseif ( $create ) {
612 $autosave_id = wp_create_post_autosave( [
613 'post_ID' => $this->post->ID,
614 'post_type' => $this->post->post_type,
615 'post_title' => $this->post->post_title,
616 'post_excerpt' => $this->post->post_excerpt,
617 // Hack to cause $autosave_is_different=true in `wp_create_post_autosave`.
618 'post_content' => '<!-- Created With Elementor -->',
619 'post_modified' => current_time( 'mysql' ),
620 ] );
621
622 Plugin::$instance->db->copy_elementor_meta( $this->post->ID, $autosave_id );
623
624 $document = Plugin::$instance->documents->get( $autosave_id );
625 $document->save_template_type();
626 } else {
627 $document = false;
628 }
629
630 return $document;
631 }
632
633 /**
634 * Add/Remove edit link in dashboard.
635 *
636 * Add or remove an edit link to the post/page action links on the post/pages list table.
637 *
638 * Fired by `post_row_actions` and `page_row_actions` filters.
639 *
640 * @access public
641 *
642 * @param array $actions An array of row action links.
643 *
644 * @return array An updated array of row action links.
645 */
646 public function filter_admin_row_actions( $actions ) {
647 if ( $this->is_built_with_elementor() && $this->is_editable_by_current_user() ) {
648 $actions['edit_with_elementor'] = sprintf(
649 '<a href="%1$s">%2$s</a>',
650 $this->get_edit_url(),
651 __( 'Edit with Elementor', 'elementor' )
652 );
653 }
654
655 return $actions;
656 }
657
658 /**
659 * @since 2.0.0
660 * @access public
661 */
662 public function is_editable_by_current_user() {
663 $edit_capability = static::get_property( 'edit_capability' );
664 if ( $edit_capability && ! current_user_can( $edit_capability ) ) {
665 return false;
666 }
667
668 return self::get_property( 'is_editable' ) && User::is_current_user_can_edit( $this->get_main_id() );
669 }
670
671 /**
672 * @since 2.9.0
673 * @access protected
674 */
675 protected function get_initial_config() {
676 // Get document data *after* the scripts hook - so plugins can run compatibility before get data, but *before* enqueue the editor script - so elements can enqueue their own scripts that depended in editor script.
677
678 $locked_user = Plugin::$instance->editor->get_locked_user( $this->get_main_id() );
679
680 if ( $locked_user ) {
681 $locked_user = $locked_user->display_name;
682 }
683
684 $post = $this->get_main_post();
685
686 $post_type_object = get_post_type_object( $post->post_type );
687
688 $settings = SettingsManager::get_settings_managers_config();
689
690 $config = [
691 'id' => $this->get_main_id(),
692 'type' => $this->get_name(),
693 'version' => $this->get_main_meta( '_elementor_version' ),
694 'settings' => $settings['page'],
695 'remoteLibrary' => $this->get_remote_library_config(),
696 'last_edited' => $this->get_last_edited(),
697 'panel' => static::get_editor_panel_config(),
698 'container' => 'body',
699 'post_type_title' => $this->get_post_type_title(),
700 'user' => [
701 'can_publish' => current_user_can( $post_type_object->cap->publish_posts ),
702
703 // Deprecated config since 2.9.0.
704 'locked' => $locked_user,
705 ],
706 'urls' => [
707 'exit_to_dashboard' => $this->get_exit_to_dashboard_url(), // WP post type edit page
708 'all_post_type' => $this->get_all_post_type_url(),
709 'preview' => $this->get_preview_url(),
710 'wp_preview' => $this->get_wp_preview_url(),
711 'permalink' => $this->get_permalink(),
712 'have_a_look' => $this->get_have_a_look_url(),
713 'main_dashboard' => $this->get_main_dashboard_url(),
714 ],
715 ];
716
717 $post_status_object = get_post_status_object( $post->post_status );
718
719 if ( $post_status_object ) {
720 $config['status'] = [
721 'value' => $post_status_object->name,
722 'label' => $post_status_object->label,
723 ];
724 }
725
726 do_action( 'elementor/document/before_get_config', $this );
727
728 if ( static::get_property( 'has_elements' ) ) {
729 $elements_config = Collection::make( Plugin::$instance->elements_manager->get_element_types() )
730 ->filter( fn( $element ) => ( ! empty( $element->get_config()['include_in_widgets_config'] ) ) )
731 ->map( fn( $element ) => $element->get_config() )
732 ->all();
733
734 $config['elements'] = $this->get_elements_raw_data( null, true );
735 // `get_elements_raw_data` has to be called before `get_widget_types_config`, because it affects it.
736 $config['widgets'] = array_merge( $elements_config, Plugin::$instance->widgets_manager->get_widget_types_config() );
737 }
738
739 $additional_config = [];
740
741 /**
742 * Additional document configuration.
743 *
744 * Filters the document configuration by adding additional configuration.
745 * External developers can use this hook to add custom configuration in
746 * addition to Elementor's initial configuration.
747 *
748 * Use the $post_id to add custom configuration for different pages.
749 *
750 * @param array $additional_config The additional document configuration.
751 * @param int $post_id The post ID of the document.
752 */
753 $additional_config = apply_filters( 'elementor/document/config', $additional_config, $this->get_main_id() );
754
755 if ( ! empty( $additional_config ) ) {
756 $config = array_replace_recursive( $config, $additional_config );
757 }
758
759 return $config;
760 }
761
762 /**
763 * @since 3.1.0
764 * @access protected
765 */
766 protected function register_controls() {
767 $this->register_document_controls();
768
769 /**
770 * Register document controls.
771 *
772 * Fires after Elementor registers the document controls.
773 *
774 * External developers can use this hook to add new controls to the document.
775 *
776 * @since 2.0.0
777 *
778 * @param Document $this The document instance.
779 */
780 do_action( 'elementor/documents/register_controls', $this );
781 }
782
783 /**
784 * @since 2.0.0
785 * @access public
786 *
787 * @param $data
788 *
789 * @return bool
790 */
791 public function save( $data ) {
792 /**
793 * Set locale to "C" to avoid issues with comma as decimal separator.
794 *
795 * @see https://github.com/elementor/elementor/issues/10992
796 */
797 $original_lc = setlocale( LC_NUMERIC, 0 );
798 setlocale( LC_NUMERIC, 'C' );
799
800 /**
801 * Document save data.
802 *
803 * Filter the document data before saving process starts.
804 *
805 * External developers can use this hook to change the data before
806 * saving it to the database.
807 *
808 * @since 3.3.0
809 *
810 * @param array $data The document data.
811 * @param \Elementor\Core\Base\Document $this The document instance.
812 */
813 $data = apply_filters( 'elementor/document/save/data', $data, $this );
814
815 $this->add_handle_revisions_changed_filter();
816
817 if ( ! $this->is_editable_by_current_user() ) {
818 return false;
819 }
820
821 $this->set_is_saving( true );
822
823 /**
824 * Before document save.
825 *
826 * Fires when document save starts on Elementor.
827 *
828 * @since 2.5.12
829 *
830 * @param \Elementor\Core\Base\Document $this The current document.
831 * @param $data.
832 */
833 do_action( 'elementor/document/before_save', $this, $data );
834
835 if ( ! current_user_can( 'unfiltered_html' ) ) {
836 $data = map_deep( $data, function ( $value ) {
837 return is_bool( $value ) || is_null( $value ) ? $value : wp_kses_post( $value );
838 } );
839 }
840
841 if ( ! empty( $data['settings'] ) ) {
842 if ( isset( $data['settings']['post_status'] ) && self::STATUS_AUTOSAVE === $data['settings']['post_status'] ) {
843 if ( ! defined( 'DOING_AUTOSAVE' ) ) {
844 define( 'DOING_AUTOSAVE', true );
845 }
846 }
847
848 $this->save_settings( $data['settings'] );
849
850 $this->refresh_post();
851 }
852
853 // Don't check is_empty, because an empty array should be saved.
854 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
855 $this->save_elements( $data['elements'] );
856 }
857
858 $this->save_template_type();
859
860 $this->save_version();
861
862 // Remove Post CSS
863 $post_css = Post_CSS::create( $this->post->ID );
864
865 $post_css->delete();
866
867 // Remove Document Cache
868 $this->delete_cache();
869
870 /**
871 * After document save.
872 *
873 * Fires when document save is complete.
874 *
875 * @since 2.5.12
876 *
877 * @param \Elementor\Core\Base\Document $this The current document.
878 * @param $data.
879 */
880 do_action( 'elementor/document/after_save', $this, $data );
881
882 $this->set_is_saving( false );
883
884 $this->remove_handle_revisions_changed_filter();
885
886 setlocale( LC_NUMERIC, $original_lc );
887
888 return true;
889 }
890
891 public function refresh_post() {
892 $this->post = get_post( $this->post->ID );
893 }
894
895 /**
896 * @param array $new_settings
897 *
898 * @return static
899 */
900 public function update_settings( array $new_settings ) {
901 $document_settings = $this->get_meta( PageManager::META_KEY );
902
903 if ( ! $document_settings ) {
904 $document_settings = [];
905 }
906
907 $this->save_settings(
908 array_replace_recursive( $document_settings, $new_settings )
909 );
910
911 return $this;
912 }
913
914 /**
915 * Is built with Elementor.
916 *
917 * Check whether the post was built with Elementor.
918 *
919 * @since 2.0.0
920 * @access public
921 *
922 * @return bool Whether the post was built with Elementor.
923 */
924 public function is_built_with_elementor() {
925 return (bool) $this->get_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
926 }
927
928 /**
929 * Mark the post as "built with elementor" or not.
930 *
931 * @param bool $is_built_with_elementor
932 *
933 * @return $this
934 */
935 public function set_is_built_with_elementor( $is_built_with_elementor ) {
936 if ( $is_built_with_elementor ) {
937 // Use the string `builder` and not a boolean for rollback compatibility
938 $this->update_meta( self::BUILT_WITH_ELEMENTOR_META_KEY, 'builder' );
939 } else {
940 $this->delete_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
941 }
942
943 return $this;
944 }
945
946 /**
947 * @since 2.0.0
948 * @access public
949 * @static
950 *
951 * @return mixed
952 */
953 public function get_edit_url() {
954 $url = add_query_arg(
955 [
956 'post' => $this->get_main_id(),
957 'action' => 'elementor',
958 ],
959 admin_url( 'post.php' )
960 );
961
962 /**
963 * Document edit url.
964 *
965 * Filters the document edit url.
966 *
967 * @since 2.0.0
968 *
969 * @param string $url The edit url.
970 * @param Document $this The document instance.
971 */
972 $url = apply_filters( 'elementor/document/urls/edit', $url, $this );
973
974 return $url;
975 }
976
977 /**
978 * @since 2.0.0
979 * @access public
980 */
981 public function get_preview_url() {
982 /**
983 * Use a static var - to avoid change the `ver` parameter on every call.
984 */
985 static $url;
986
987 if ( empty( $url ) ) {
988
989 add_filter( 'pre_option_permalink_structure', '__return_empty_string' );
990
991 $url = set_url_scheme( add_query_arg( [
992 'elementor-preview' => $this->get_main_id(),
993 'ver' => time(),
994 ], $this->get_permalink() ) );
995
996 remove_filter( 'pre_option_permalink_structure', '__return_empty_string' );
997
998 /**
999 * Document preview URL.
1000 *
1001 * Filters the document preview URL.
1002 *
1003 * @since 2.0.0
1004 *
1005 * @param string $url The preview URL.
1006 * @param Document $this The document instance.
1007 */
1008 $url = apply_filters( 'elementor/document/urls/preview', $url, $this );
1009 }
1010
1011 return $url;
1012 }
1013
1014 /**
1015 * @since 2.0.0
1016 * @access public
1017 *
1018 * @param string $key
1019 *
1020 * @return array
1021 */
1022 public function get_json_meta( $key ) {
1023 $meta = get_post_meta( $this->post->ID, $key, true );
1024
1025 if ( is_string( $meta ) && ! empty( $meta ) ) {
1026 $meta = json_decode( $meta, true );
1027 }
1028
1029 if ( empty( $meta ) ) {
1030 $meta = [];
1031 }
1032
1033 return $meta;
1034 }
1035
1036 public function update_json_meta( $key, $value ) {
1037 return $this->update_meta(
1038 $key,
1039 // `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1040 wp_slash( wp_json_encode( $value ) )
1041 );
1042 }
1043
1044 /**
1045 * @since 2.0.0
1046 * @access public
1047 *
1048 * @param null $data
1049 * @param bool $with_html_content
1050 *
1051 * @return array
1052 *
1053 * @throws \Exception If elements retrieval fails or data processing errors occur.
1054 */
1055 public function get_elements_raw_data( $data = null, $with_html_content = false ) {
1056 if ( ! static::get_property( 'has_elements' ) ) {
1057 return [];
1058 }
1059
1060 if ( is_null( $data ) ) {
1061 $data = $this->get_elements_data();
1062 }
1063
1064 /**
1065 * Filters document elements data after loading.
1066 *
1067 * Allows modification of elements data when loading (not saving).
1068 * Useful for migrations, transformations, or data enrichment.
1069 *
1070 * @since 3.35.0
1071 *
1072 * @param array $data The elements data array.
1073 * @param \Elementor\Core\Base\Document $document The document instance.
1074 */
1075 if ( ! $this->is_saving ) {
1076 $data = apply_filters( 'elementor/document/load/data', $data, $this );
1077 }
1078
1079 // Change the current documents, so widgets can use `documents->get_current` and other post data
1080 Plugin::$instance->documents->switch_to_document( $this );
1081
1082 $editor_data = [];
1083
1084 foreach ( $data as $element_data ) {
1085 if ( ! is_array( $element_data ) ) {
1086 throw new \Exception( 'Invalid data: ' . wp_json_encode( [
1087 'data' => $data,
1088 'element' => $element_data,
1089 ] ) );
1090 }
1091
1092 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1093
1094 if ( ! $element ) {
1095 continue;
1096 }
1097
1098 if ( $this->is_saving ) {
1099 $element_data = $element->get_data_for_save();
1100 } else {
1101 $element_data = $element->get_raw_data( $with_html_content );
1102 }
1103
1104 $editor_data[] = $element_data;
1105 }
1106
1107 Plugin::$instance->documents->restore_document();
1108
1109 return $editor_data;
1110 }
1111
1112 /**
1113 * @since 2.0.0
1114 * @access public
1115 *
1116 * @param string $status
1117 *
1118 * @return array
1119 */
1120 public function get_elements_data( $status = self::STATUS_PUBLISH ) {
1121 $elements = $this->get_json_meta( self::ELEMENTOR_DATA_META_KEY );
1122
1123 if ( self::STATUS_DRAFT === $status ) {
1124 $autosave = $this->get_newer_autosave();
1125
1126 if ( is_object( $autosave ) ) {
1127 $autosave_elements = Plugin::$instance->documents
1128 ->get( $autosave->get_post()->ID )
1129 ->get_json_meta( self::ELEMENTOR_DATA_META_KEY );
1130 }
1131 }
1132
1133 if ( Plugin::$instance->editor->is_edit_mode() ) {
1134 if ( empty( $elements ) && empty( $autosave_elements ) ) {
1135 // Convert to Elementor.
1136 $elements = $this->convert_to_elementor();
1137 if ( $this->is_autosave() ) {
1138 Plugin::$instance->db->copy_elementor_meta( $this->post->post_parent, $this->post->ID );
1139 }
1140 }
1141 }
1142
1143 if ( ! empty( $autosave_elements ) ) {
1144 $elements = $autosave_elements;
1145 }
1146
1147 return $elements;
1148 }
1149
1150 /**
1151 * Get document setting from DB.
1152 *
1153 * @return array
1154 */
1155 public function get_db_document_settings() {
1156 return $this->get_meta( static::PAGE_META_KEY );
1157 }
1158
1159 /**
1160 * @since 2.3.0
1161 * @access public
1162 */
1163 public function convert_to_elementor() {
1164 $this->save( [] );
1165
1166 if ( empty( $this->post->post_content ) ) {
1167 return [];
1168 }
1169
1170 // Check if it's only a shortcode.
1171 preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER );
1172 if ( ! empty( $matches ) ) {
1173 foreach ( $matches as $shortcode ) {
1174 if ( trim( $this->post->post_content ) === $shortcode[0] ) {
1175 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' );
1176 $settings = [
1177 'shortcode' => $this->post->post_content,
1178 ];
1179 break;
1180 }
1181 }
1182 }
1183
1184 if ( empty( $widget_type ) ) {
1185 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' );
1186 $settings = [
1187 'editor' => $this->post->post_content,
1188 ];
1189 }
1190
1191 // TODO: Better coding to start template for editor
1192 $converted_blocks = [
1193 [
1194 'id' => Utils::generate_random_string(),
1195 'elType' => $widget_type::get_type(),
1196 'widgetType' => $widget_type->get_name(),
1197 'settings' => $settings,
1198 ],
1199 ];
1200
1201 return Plugin::$instance->experiments->is_feature_active( 'container' )
1202 ? $this->get_container_elements_data( $converted_blocks )
1203 : $this->get_sections_elements_data( $converted_blocks );
1204 }
1205
1206 /**
1207 * @since 2.1.3
1208 * @access public
1209 */
1210 public function print_elements_with_wrapper( $elements_data = null ) {
1211 if ( ! $elements_data ) {
1212 $elements_data = $this->get_elements_data();
1213 }
1214
1215 ?>
1216 <div <?php Utils::print_html_attributes( $this->get_container_attributes() ); ?>>
1217 <?php $this->print_elements( $elements_data ); ?>
1218 </div>
1219 <?php
1220 }
1221
1222 /**
1223 * @since 2.0.0
1224 * @access public
1225 */
1226 public function get_css_wrapper_selector() {
1227 return '';
1228 }
1229
1230 /**
1231 * @since 2.0.0
1232 * @access public
1233 */
1234 public function get_panel_page_settings() {
1235 return [
1236 'title' => sprintf(
1237 /* translators: %s: Document title. */
1238 esc_html__( '%s Settings', 'elementor' ),
1239 static::get_title()
1240 ),
1241 ];
1242 }
1243
1244 /**
1245 * @since 2.0.0
1246 * @access public
1247 */
1248 public function get_post() {
1249 return $this->post;
1250 }
1251
1252 /**
1253 * @since 2.0.0
1254 * @access public
1255 */
1256 public function get_permalink() {
1257 return get_permalink( $this->get_main_id() );
1258 }
1259
1260 /**
1261 * @since 2.0.8
1262 * @access public
1263 */
1264 public function get_content( $with_css = false ) {
1265 return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css );
1266 }
1267
1268 /**
1269 * @since 2.0.0
1270 * @access public
1271 */
1272 public function delete() {
1273 if ( 'revision' === $this->post->post_type ) {
1274 $deleted = wp_delete_post_revision( $this->post );
1275 } else {
1276 $deleted = wp_delete_post( $this->post->ID );
1277 }
1278
1279 return $deleted && ! is_wp_error( $deleted );
1280 }
1281
1282 public function force_delete() {
1283 $deleted = wp_delete_post( $this->post->ID, true );
1284
1285 return $deleted && ! is_wp_error( $deleted );
1286 }
1287
1288 /**
1289 * On import update dynamic content (e.g. post and term IDs).
1290 *
1291 * @since 3.8.0
1292 *
1293 * @param array $config The config of the passed element.
1294 * @param array $data The data that requires updating/replacement when imported.
1295 * @param array|null $controls The available controls.
1296 *
1297 * @return array Element data.
1298 */
1299 public static function on_import_update_dynamic_content( array $config, array $data, $controls = null ): array {
1300 foreach ( $config as &$element_config ) {
1301 $element_instance = Plugin::$instance->elements_manager->create_element_instance( $element_config );
1302
1303 if ( is_null( $element_instance ) ) {
1304 continue;
1305 }
1306
1307 if ( $element_instance->has_own_method( 'on_import_replace_dynamic_content' ) ) {
1308 // TODO: Remove this check in the future.
1309 $element_config = $element_instance::on_import_replace_dynamic_content( $element_config, $data['post_ids'] );
1310 } else {
1311 $element_config = $element_instance::on_import_update_dynamic_content( $element_config, $data, $element_instance->get_controls() );
1312 }
1313
1314 $element_config['elements'] = static::on_import_update_dynamic_content( $element_config['elements'], $data );
1315 }
1316
1317 return $config;
1318 }
1319
1320 /**
1321 * Update dynamic settings in the document for import.
1322 *
1323 * @param array $settings The settings of the document.
1324 * @param array $config Import config to update the settings.
1325 *
1326 * @return array
1327 */
1328 public function on_import_update_settings( array $settings, array $config ): array {
1329 $controls = $this->get_controls();
1330 $controls_manager = Plugin::$instance->controls_manager;
1331
1332 foreach ( $settings as $key => $value ) {
1333
1334 if ( ! isset( $controls[ $key ] ) ) {
1335 continue;
1336 }
1337
1338 $control = $controls[ $key ];
1339 $control_instance = $controls_manager->get_control( $control['type'] );
1340
1341 if ( ! $control_instance ) {
1342 continue;
1343 }
1344
1345 $settings[ $key ] = $control_instance->on_import_update_settings( $value, $control, $config );
1346 }
1347
1348 return $settings;
1349 }
1350
1351 /**
1352 * Save editor elements.
1353 *
1354 * Save data from the editor to the database.
1355 *
1356 * @since 2.0.0
1357 * @access protected
1358 *
1359 * @param array $elements
1360 */
1361 protected function save_elements( $elements ) {
1362 $editor_data = $this->get_elements_raw_data( $elements );
1363
1364 // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1365 $json_value = wp_slash( wp_json_encode( $editor_data ) );
1366
1367 // Don't use `update_post_meta` that can't handle `revision` post type
1368 $is_meta_updated = update_metadata( 'post', $this->post->ID, self::ELEMENTOR_DATA_META_KEY, $json_value );
1369
1370 /**
1371 * Before saving data.
1372 *
1373 * Fires before Elementor saves data to the database.
1374 *
1375 * @since 1.0.0
1376 *
1377 * @param string $status Post status.
1378 * @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure.
1379 */
1380 do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated );
1381
1382 Plugin::$instance->db->save_plain_text( $this->post->ID );
1383
1384 $elements_iteration_actions = $this->get_elements_iteration_actions();
1385
1386 if ( $elements_iteration_actions ) {
1387 $this->iterate_elements( $elements, $elements_iteration_actions, 'save' );
1388 }
1389
1390 /**
1391 * After saving data.
1392 *
1393 * Fires after Elementor saves data to the database.
1394 *
1395 * @since 1.0.0
1396 *
1397 * @param int $post_id The ID of the post.
1398 * @param array $editor_data Sanitize posted data.
1399 */
1400 do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data );
1401 }
1402
1403 /**
1404 * @since 2.0.0
1405 * @access public
1406 *
1407 * @param int $user_id Optional. User ID. Default value is `0`.
1408 *
1409 * @return bool|int
1410 */
1411 public function get_autosave_id( $user_id = 0 ) {
1412 if ( ! $user_id ) {
1413 $user_id = get_current_user_id();
1414 }
1415
1416 $autosave = Utils::get_post_autosave( $this->post->ID, $user_id );
1417 if ( $autosave ) {
1418 return $autosave->ID;
1419 }
1420
1421 return false;
1422 }
1423
1424 public function save_version() {
1425 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
1426 // Save per revision.
1427 $this->update_meta( '_elementor_version', ELEMENTOR_VERSION );
1428
1429 /**
1430 * Document version save.
1431 *
1432 * Fires when document version is saved on Elementor.
1433 * Will not fire during Elementor Upgrade.
1434 *
1435 * @since 2.5.12
1436 *
1437 * @param \Elementor\Core\Base\Document $this The current document.
1438 */
1439 do_action( 'elementor/document/save_version', $this );
1440 }
1441 }
1442
1443 /**
1444 * @since 2.3.0
1445 * @access public
1446 */
1447 public function save_template_type() {
1448 return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() );
1449 }
1450
1451 /**
1452 * @since 2.3.0
1453 * @access public
1454 */
1455 public function get_template_type() {
1456 return $this->get_main_meta( self::TYPE_META_KEY );
1457 }
1458
1459 /**
1460 * @since 2.0.0
1461 * @access public
1462 *
1463 * @param string $key Meta data key.
1464 *
1465 * @return mixed
1466 */
1467 public function get_main_meta( $key ) {
1468 return get_post_meta( $this->get_main_id(), $key, true );
1469 }
1470
1471 /**
1472 * @since 2.0.4
1473 * @access public
1474 *
1475 * @param string $key Meta data key.
1476 * @param mixed $value Meta data value.
1477 *
1478 * @return bool|int
1479 */
1480 public function update_main_meta( $key, $value ) {
1481 return update_post_meta( $this->get_main_id(), $key, $value );
1482 }
1483
1484 /**
1485 * @since 2.0.4
1486 * @access public
1487 *
1488 * @param string $key Meta data key.
1489 * @param string $value Optional. Meta data value. Default is an empty string.
1490 *
1491 * @return bool
1492 */
1493 public function delete_main_meta( $key, $value = '' ) {
1494 return delete_post_meta( $this->get_main_id(), $key, $value );
1495 }
1496
1497 /**
1498 * @since 2.0.0
1499 * @access public
1500 *
1501 * @param string $key Meta data key.
1502 *
1503 * @return mixed
1504 */
1505 public function get_meta( $key ) {
1506 return get_post_meta( $this->post->ID, $key, true );
1507 }
1508
1509 /**
1510 * @since 2.0.0
1511 * @access public
1512 *
1513 * @param string $key Meta data key.
1514 * @param mixed $value Meta data value.
1515 *
1516 * @return bool|int
1517 */
1518 public function update_meta( $key, $value ) {
1519 // Use `update_metadata` in order to work also with revisions.
1520 return update_metadata( 'post', $this->post->ID, $key, $value );
1521 }
1522
1523 /**
1524 * @since 2.0.3
1525 * @access public
1526 *
1527 * @param string $key Meta data key.
1528 * @param string $value Meta data value.
1529 *
1530 * @return bool
1531 */
1532 public function delete_meta( $key, $value = '' ) {
1533 // Use `delete_metadata` in order to work also with revisions.
1534 return delete_metadata( 'post', $this->post->ID, $key, $value );
1535 }
1536
1537 /**
1538 * @since 2.0.0
1539 * @access public
1540 */
1541 public function get_last_edited() {
1542 $post = $this->post;
1543 $autosave_post = $this->get_autosave();
1544
1545 if ( $autosave_post ) {
1546 $post = $autosave_post->get_post();
1547 }
1548
1549 $date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) );
1550 $display_name = get_the_author_meta( 'display_name', $post->post_author );
1551
1552 if ( $autosave_post || 'revision' === $post->post_type ) {
1553 $last_edited = sprintf(
1554 /* translators: 1: Saving date, 2: Author display name. */
1555 esc_html__( 'Draft saved on %1$s by %2$s', 'elementor' ),
1556 '<time>' . $date . '</time>',
1557 $display_name
1558 );
1559 } else {
1560 $last_edited = sprintf(
1561 /* translators: 1: Editing date, 2: Author display name. */
1562 esc_html__( 'Last edited on %1$s by %2$s', 'elementor' ),
1563 '<time>' . $date . '</time>',
1564 $display_name
1565 );
1566 }
1567
1568 return $last_edited;
1569 }
1570
1571
1572 /**
1573 * @return bool
1574 */
1575 public function is_saving() {
1576 return $this->is_saving;
1577 }
1578
1579 /**
1580 * @param $is_saving
1581 *
1582 * @return $this
1583 */
1584 public function set_is_saving( $is_saving ) {
1585 $this->is_saving = $is_saving;
1586
1587 return $this;
1588 }
1589
1590 /**
1591 * @since 2.0.0
1592 * @access public
1593 *
1594 * @param array $data
1595 *
1596 * @throws \Exception If the post does not exist.
1597 */
1598 public function __construct( array $data = [] ) {
1599 if ( $data ) {
1600 if ( empty( $data['post_id'] ) ) {
1601 $this->post = new \WP_Post( (object) [] );
1602 } else {
1603 $this->post = get_post( $data['post_id'] );
1604
1605 if ( ! $this->post ) {
1606 throw new \Exception( sprintf( 'Post ID #%s does not exist.', esc_html( $data['post_id'] ) ), Exceptions::NOT_FOUND ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
1607 }
1608 }
1609
1610 // Each Control_Stack is based on a unique ID.
1611 $data['id'] = $data['post_id'];
1612
1613 if ( ! isset( $data['settings'] ) ) {
1614 $data['settings'] = [];
1615 }
1616
1617 $saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true );
1618 if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) {
1619 $data['settings'] += $saved_settings;
1620 }
1621 }
1622
1623 parent::__construct( $data );
1624 }
1625
1626 /**
1627 * Get Export Data
1628 *
1629 * Filters a document's data on export
1630 *
1631 * @since 3.2.0
1632 * @access public
1633 *
1634 * @return array The data to export
1635 */
1636 public function get_export_data() {
1637 $content = Plugin::$instance->db->iterate_data( $this->get_elements_data(), function( $element_data ) {
1638 $element_data['id'] = Utils::generate_random_string();
1639
1640 $element_data = apply_filters( 'elementor/document/element/replace_id', $element_data );
1641
1642 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1643
1644 // If the widget/element does not exist, like a plugin that creates a widget but deactivated.
1645 if ( ! $element ) {
1646 return null;
1647 }
1648
1649 return $this->process_element_import_export( $element, 'on_export' );
1650 } );
1651
1652 return [
1653 'content' => $content,
1654 'settings' => $this->get_data( 'settings' ),
1655 'metadata' => $this->get_export_metadata(),
1656 ];
1657 }
1658
1659 public function get_export_summary() {
1660 return [
1661 'title' => $this->post->post_title,
1662 'doc_type' => $this->get_name(),
1663 'thumbnail' => get_the_post_thumbnail_url( $this->post ),
1664 ];
1665 }
1666
1667 /**
1668 * Get Import Data
1669 *
1670 * Filters a document's data on import
1671 *
1672 * @since 3.2.0
1673 * @access public
1674 *
1675 * @return array The data to import
1676 */
1677 public function get_import_data( array $data ) {
1678 $data['content'] = Plugin::$instance->db->iterate_data( $data['content'], function( $element_data ) {
1679 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1680
1681 // If the widget/element isn't exist, like a plugin that creates a widget but deactivated
1682 if ( ! $element ) {
1683 return null;
1684 }
1685
1686 return $this->process_element_import_export( $element, 'on_import' );
1687 } );
1688
1689 if ( ! empty( $data['settings'] ) ) {
1690 $template_model = new Page_Model( [
1691 'id' => 0,
1692 'settings' => $data['settings'],
1693 ] );
1694
1695 $page_data = $this->process_element_import_export( $template_model, 'on_import' );
1696
1697 $data['settings'] = $page_data['settings'];
1698 }
1699
1700 return $data;
1701 }
1702
1703 /**
1704 * Import
1705 *
1706 * Allows to import an external data to a document
1707 *
1708 * @since 3.2.0
1709 * @access public
1710 *
1711 * @param array $data
1712 */
1713 public function import( array $data ) {
1714 $data = $this->get_import_data( $data );
1715
1716 $this->save( [
1717 'elements' => $data['content'],
1718 'settings' => $data['settings'],
1719 ] );
1720
1721 if ( $data['import_settings']['thumbnail'] ) {
1722 $attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( [ 'url' => $data['import_settings']['thumbnail'] ] );
1723
1724 set_post_thumbnail( $this->get_main_post(), $attachment['id'] );
1725 }
1726
1727 if ( ! empty( $data['metadata'] ) ) {
1728 foreach ( $data['metadata'] as $key => $value ) {
1729 $this->update_meta( $key, $value );
1730 }
1731 }
1732 }
1733
1734 public function process_element_import_export( Controls_Stack $element, $method, $element_data = null ) {
1735 if ( null === $element_data ) {
1736 $element_data = $element->get_data();
1737 }
1738
1739 if ( method_exists( $element, $method ) ) {
1740 // TODO: Use the internal element data without parameters.
1741 $element_data = $element->{$method}( $element_data );
1742 }
1743
1744 foreach ( $element->get_controls() as $control ) {
1745 $control_class = Plugin::$instance->controls_manager->get_control( $control['type'] );
1746
1747 // If the control isn't exist, like a plugin that creates the control but deactivated.
1748 if ( ! $control_class ) {
1749 return $element_data;
1750 }
1751
1752 // Do not add default value to the final settings, if there is no value at the
1753 // data before the methods `on_import` or `on_export` called.
1754 $has_value = isset( $element_data['settings'][ $control['name'] ] );
1755
1756 if ( $has_value && method_exists( $control_class, $method ) ) {
1757 $element_data['settings'][ $control['name'] ] = $control_class->{$method}(
1758 $element_data['settings'][ $control['name'] ],
1759 $control
1760 );
1761 }
1762
1763 // On Export, check if the control has an argument 'export' => false.
1764 if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) {
1765 unset( $element_data['settings'][ $control['name'] ] );
1766 }
1767 }
1768
1769 return $element_data;
1770 }
1771
1772 protected function get_export_metadata() {
1773 $metadata = get_post_meta( $this->get_main_id() );
1774
1775 foreach ( $metadata as $meta_key => $meta_value ) {
1776 if ( is_protected_meta( $meta_key, 'post' ) ) {
1777 unset( $metadata[ $meta_key ] );
1778
1779 continue;
1780 }
1781
1782 $metadata[ $meta_key ] = $meta_value[0];
1783 }
1784
1785 return $metadata;
1786 }
1787
1788 protected function get_remote_library_config() {
1789 $config = [
1790 'type' => 'block',
1791 'default_route' => 'templates/blocks',
1792 'category' => $this->get_name(),
1793 'autoImportSettings' => false,
1794 ];
1795
1796 return $config;
1797 }
1798
1799 /**
1800 * @since 2.0.4
1801 * @access protected
1802 *
1803 * @param $settings
1804 */
1805 protected function save_settings( $settings ) {
1806 $page_settings_manager = SettingsManager::get_settings_managers( 'page' );
1807 $page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID );
1808 $page_settings_manager->save_settings( $settings, $this->post->ID );
1809 }
1810
1811 /**
1812 * @since 2.1.3
1813 * @access public
1814 */
1815 public function print_elements( $elements_data ) {
1816 $is_element_cache_active = 'disable' !== get_option( 'elementor_element_cache_ttl', '' );
1817 if ( ! $is_element_cache_active ) {
1818 ob_start();
1819
1820 $this->do_print_elements( $elements_data );
1821
1822 $content = ob_get_clean();
1823
1824 if ( has_blocks( $content ) ) {
1825 $content = do_blocks( $content );
1826 }
1827
1828 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1829
1830 return;
1831 }
1832
1833 $cached_data = $this->get_document_cache();
1834
1835 if ( false === $cached_data ) {
1836 add_filter( 'elementor/element/should_render_shortcode', '__return_true' );
1837
1838 $scripts_to_queue = [];
1839 $styles_to_queue = [];
1840
1841 global $wp_scripts, $wp_styles;
1842
1843 $should_store_scripts = $wp_scripts instanceof \WP_Scripts && $wp_styles instanceof \WP_Styles;
1844 if ( $should_store_scripts ) {
1845 $scripts_ignored = $wp_scripts->queue;
1846 $styles_ignored = $wp_styles->queue;
1847 }
1848
1849 ob_start();
1850
1851 $this->do_print_elements( $elements_data );
1852
1853 if ( $should_store_scripts ) {
1854 $scripts_to_queue = array_values( array_diff( $wp_scripts->queue, $scripts_ignored ) );
1855 $styles_to_queue = array_values( array_diff( $wp_styles->queue, $styles_ignored ) );
1856 }
1857
1858 $cached_data = [
1859 'content' => ob_get_clean(),
1860 'scripts' => $scripts_to_queue,
1861 'styles' => $styles_to_queue,
1862 ];
1863
1864 if ( $this->should_store_cache_elements() ) {
1865 $this->set_document_cache( $cached_data );
1866 }
1867
1868 remove_filter( 'elementor/element/should_render_shortcode', '__return_true' );
1869 } else {
1870 if ( ! empty( $cached_data['scripts'] ) ) {
1871 foreach ( $cached_data['scripts'] as $script_handle ) {
1872 wp_enqueue_script( $script_handle );
1873 }
1874 }
1875
1876 if ( ! empty( $cached_data['styles'] ) ) {
1877 foreach ( $cached_data['styles'] as $style_handle ) {
1878 wp_enqueue_style( $style_handle );
1879 }
1880 }
1881 }
1882
1883 if ( ! empty( $cached_data['content'] ) ) {
1884 $content = do_shortcode( $cached_data['content'] );
1885
1886 if ( has_blocks( $content ) ) {
1887 $content = do_blocks( $content );
1888 }
1889
1890 echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1891 }
1892 }
1893
1894 protected function do_print_elements( $elements_data ) {
1895 $this->update_runtime_elements( $elements_data );
1896
1897 foreach ( $elements_data as $element_data ) {
1898 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1899
1900 if ( ! $element ) {
1901 continue;
1902 }
1903
1904 $element->print_element();
1905 }
1906 }
1907
1908 public function update_runtime_elements( $elements_data = null ) {
1909 if ( null === $elements_data ) {
1910 $elements_data = $this->get_elements_data();
1911 }
1912
1913 // Collect all data updaters that should be updated on runtime.
1914 $runtime_elements_iteration_actions = $this->get_runtime_elements_iteration_actions();
1915
1916 if ( $runtime_elements_iteration_actions ) {
1917 $this->iterate_elements( $elements_data, $runtime_elements_iteration_actions, 'render' );
1918 }
1919 }
1920
1921 public function set_document_cache( $value ) {
1922 $expiration_hours = get_option( 'elementor_element_cache_ttl', '' );
1923
1924 if ( empty( $expiration_hours ) || ! is_numeric( $expiration_hours ) ) {
1925 $expiration_hours = '24';
1926 }
1927
1928 $expiration_hours = absint( $expiration_hours );
1929
1930 $expiration = '+' . $expiration_hours . ' hours';
1931
1932 $data = [
1933 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ),
1934 'value' => $value,
1935 ];
1936
1937 $this->update_json_meta( static::CACHE_META_KEY, $data );
1938 }
1939
1940 private function get_document_cache() {
1941 $cache = $this->get_json_meta( static::CACHE_META_KEY );
1942
1943 if ( empty( $cache['timeout'] ) ) {
1944 return false;
1945 }
1946
1947 if ( current_time( 'timestamp' ) > $cache['timeout'] ) {
1948 return false;
1949 }
1950
1951 if ( ! is_array( $cache['value'] ) ) {
1952 return false;
1953 }
1954
1955 return $cache['value'];
1956 }
1957
1958 protected function delete_cache() {
1959 $this->delete_meta( static::CACHE_META_KEY );
1960 }
1961
1962 private function should_store_cache_elements() {
1963 static $should_store_cache_elements = null;
1964
1965 if ( null === $should_store_cache_elements ) {
1966 $should_store_cache_elements = (
1967 ! is_admin()
1968 && ! Plugin::$instance->preview->is_preview_mode()
1969 );
1970 }
1971
1972 return $should_store_cache_elements;
1973 }
1974
1975 protected function register_document_controls() {
1976 $this->start_controls_section(
1977 'document_settings',
1978 [
1979 'label' => esc_html__( 'General Settings', 'elementor' ),
1980 'tab' => Controls_Manager::TAB_SETTINGS,
1981 ]
1982 );
1983
1984 $this->add_control(
1985 'post_title',
1986 [
1987 'label' => esc_html__( 'Title', 'elementor' ),
1988 'type' => Controls_Manager::TEXT,
1989 'default' => $this->post->post_title,
1990 'label_block' => true,
1991 ]
1992 );
1993
1994 $post_type_object = get_post_type_object( $this->post->post_type );
1995
1996 $can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts );
1997 $is_published = self::STATUS_PUBLISH === $this->post->post_status || self::STATUS_PRIVATE === $this->post->post_status;
1998
1999 if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) {
2000
2001 $statuses = $this->get_post_statuses();
2002 if ( 'future' === $this->get_main_post()->post_status ) {
2003 $statuses['future'] = esc_html__( 'Future', 'elementor' );
2004 }
2005
2006 $this->add_control(
2007 'post_status',
2008 [
2009 'label' => esc_html__( 'Status', 'elementor' ),
2010 'type' => Controls_Manager::SELECT,
2011 'default' => $this->get_main_post()->post_status,
2012 'options' => $statuses,
2013 ]
2014 );
2015 }
2016
2017 $this->end_controls_section();
2018 }
2019
2020 protected function get_post_statuses() {
2021 return get_post_statuses();
2022 }
2023
2024 protected function get_have_a_look_url() {
2025 return $this->get_permalink();
2026 }
2027
2028 public function handle_revisions_changed( $post_has_changed, $last_revision, $post ) {
2029 // In case default, didn't determine the changes.
2030 if ( ! $post_has_changed ) {
2031 $last_revision_id = $last_revision->ID;
2032 $last_revision_document = Plugin::instance()->documents->get( $last_revision_id );
2033 $post_document = Plugin::instance()->documents->get( $post->ID );
2034
2035 $last_revision_settings = $last_revision_document->get_settings();
2036 $post_settings = $post_document->get_settings();
2037
2038 // TODO: Its better to add crc32 signature for each revision and then only compare one part of the checksum.
2039 $post_has_changed = $last_revision_settings !== $post_settings;
2040 }
2041
2042 return $post_has_changed;
2043 }
2044
2045 private function add_handle_revisions_changed_filter() {
2046 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ], 10, 3 );
2047 }
2048
2049 private function remove_handle_revisions_changed_filter() {
2050 remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] );
2051 }
2052
2053 private function get_runtime_elements_iteration_actions() {
2054 $runtime_elements_iteration_actions = [];
2055
2056 $elements_iteration_actions = $this->get_elements_iteration_actions();
2057
2058 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2059 if ( $elements_iteration_action->is_action_needed() ) {
2060 $runtime_elements_iteration_actions[] = $elements_iteration_action;
2061 }
2062 }
2063
2064 return $runtime_elements_iteration_actions;
2065 }
2066
2067 private function iterate_elements( $elements, $elements_iteration_actions, $mode ) {
2068 $unique_page_elements = [];
2069
2070 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2071 $elements_iteration_action->set_mode( $mode );
2072 }
2073
2074 Plugin::$instance->db->iterate_data( $elements, function( array $element_data ) use ( &$unique_page_elements, $elements_iteration_actions ) {
2075 $element_type = 'widget' === $element_data['elType'] ? $element_data['widgetType'] : $element_data['elType'];
2076
2077 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
2078
2079 if ( $element ) {
2080 if ( ! in_array( $element_type, $unique_page_elements, true ) ) {
2081 $unique_page_elements[] = $element_type;
2082
2083 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2084 $elements_iteration_action->unique_element_action( $element );
2085 }
2086 }
2087
2088 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2089 $elements_iteration_action->element_action( $element );
2090 }
2091 }
2092
2093 return $element_data;
2094 } );
2095
2096 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2097 $elements_iteration_action->after_elements_iteration();
2098 }
2099 }
2100
2101 private function get_elements_iteration_actions() {
2102 if ( ! $this->elements_iteration_actions ) {
2103 $this->elements_iteration_actions[] = new Assets_Iteration_Action( $this );
2104 }
2105
2106 return $this->elements_iteration_actions;
2107 }
2108 }
2109