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

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