PluginProbe
Elementor Website Builder – more than just a page builder / 3.33.0-dev1
Elementor Website Builder – more than just a page builder v3.33.0-dev1
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.33.0-dev1, at core/base/document.php

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