PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.1
Elementor Website Builder – more than just a page builder v4.2.1
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 4.2.1, at core/base/document.php

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