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

2,066 lines 50.3 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 *
553 * @return bool|Document
554 */
555
556 public function get_newer_autosave() {
557 $autosave = $this->get_autosave();
558
559 // Detect if there exists an autosave newer than the post.
560 if ( $autosave && mysql2date( 'U', $autosave->get_post()->post_modified_gmt, false ) > mysql2date( 'U', $this->post->post_modified_gmt, false ) ) {
561 return $autosave;
562 }
563
564 return false;
565 }
566
567 /**
568 * @since 2.0.0
569 * @access public
570 */
571 public function is_autosave() {
572 return wp_is_post_autosave( $this->post->ID );
573 }
574
575 /**
576 * Check if the current document is a 'revision'
577 *
578 * @return bool
579 */
580 public function is_revision() {
581 return 'revision' === $this->post->post_type;
582 }
583
584 /**
585 * Checks if the current document status is 'trash'.
586 *
587 * @return bool
588 */
589 public function is_trash() {
590 return 'trash' === $this->post->post_status;
591 }
592
593 /**
594 * @since 2.0.0
595 * @access public
596 *
597 * @param int $user_id
598 * @param bool $create
599 *
600 * @return bool|Document
601 */
602 public function get_autosave( $user_id = 0, $create = false ) {
603 if ( ! $user_id ) {
604 $user_id = get_current_user_id();
605 }
606
607 $autosave_id = $this->get_autosave_id( $user_id );
608
609 if ( $autosave_id ) {
610 $document = Plugin::$instance->documents->get( $autosave_id );
611 } elseif ( $create ) {
612 $autosave_id = wp_create_post_autosave( [
613 'post_ID' => $this->post->ID,
614 'post_type' => $this->post->post_type,
615 'post_title' => $this->post->post_title,
616 'post_excerpt' => $this->post->post_excerpt,
617 // Hack to cause $autosave_is_different=true in `wp_create_post_autosave`.
618 'post_content' => '<!-- Created With Elementor -->',
619 'post_modified' => current_time( 'mysql' ),
620 ] );
621
622 Plugin::$instance->db->copy_elementor_meta( $this->post->ID, $autosave_id );
623
624 $document = Plugin::$instance->documents->get( $autosave_id );
625 $document->save_template_type();
626 } else {
627 $document = false;
628 }
629
630 return $document;
631 }
632
633 /**
634 * Add/Remove edit link in dashboard.
635 *
636 * Add or remove an edit link to the post/page action links on the post/pages list table.
637 *
638 * Fired by `post_row_actions` and `page_row_actions` filters.
639 *
640 * @access public
641 *
642 * @param array $actions An array of row action links.
643 *
644 * @return array An updated array of row action links.
645 */
646 public function filter_admin_row_actions( $actions ) {
647 if ( $this->is_built_with_elementor() && $this->is_editable_by_current_user() ) {
648 $actions['edit_with_elementor'] = sprintf(
649 '<a href="%1$s">%2$s</a>',
650 $this->get_edit_url(),
651 __( 'Edit with Elementor', 'elementor' )
652 );
653 }
654
655 return $actions;
656 }
657
658 /**
659 * @since 2.0.0
660 * @access public
661 */
662 public function is_editable_by_current_user() {
663 $edit_capability = static::get_property( 'edit_capability' );
664 if ( $edit_capability && ! current_user_can( $edit_capability ) ) {
665 return false;
666 }
667
668 return self::get_property( 'is_editable' ) && User::is_current_user_can_edit( $this->get_main_id() );
669 }
670
671 /**
672 * @since 2.9.0
673 * @access protected
674 */
675 protected function get_initial_config() {
676 // Get document data *after* the scripts hook - so plugins can run compatibility before get data, but *before* enqueue the editor script - so elements can enqueue their own scripts that depended in editor script.
677
678 $locked_user = Plugin::$instance->editor->get_locked_user( $this->get_main_id() );
679
680 if ( $locked_user ) {
681 $locked_user = $locked_user->display_name;
682 }
683
684 $post = $this->get_main_post();
685
686 $post_type_object = get_post_type_object( $post->post_type );
687
688 $settings = SettingsManager::get_settings_managers_config();
689
690 $config = [
691 'id' => $this->get_main_id(),
692 'type' => $this->get_name(),
693 'version' => $this->get_main_meta( '_elementor_version' ),
694 'settings' => $settings['page'],
695 'remoteLibrary' => $this->get_remote_library_config(),
696 'last_edited' => $this->get_last_edited(),
697 'panel' => static::get_editor_panel_config(),
698 'container' => 'body',
699 'post_type_title' => $this->get_post_type_title(),
700 'user' => [
701 'can_publish' => current_user_can( $post_type_object->cap->publish_posts ),
702
703 // Deprecated config since 2.9.0.
704 'locked' => $locked_user,
705 ],
706 'urls' => [
707 'exit_to_dashboard' => $this->get_exit_to_dashboard_url(), // WP post type edit page
708 'all_post_type' => $this->get_all_post_type_url(),
709 'preview' => $this->get_preview_url(),
710 'wp_preview' => $this->get_wp_preview_url(),
711 'permalink' => $this->get_permalink(),
712 'have_a_look' => $this->get_have_a_look_url(),
713 'main_dashboard' => $this->get_main_dashboard_url(),
714 ],
715 ];
716
717 $post_status_object = get_post_status_object( $post->post_status );
718
719 if ( $post_status_object ) {
720 $config['status'] = [
721 'value' => $post_status_object->name,
722 'label' => $post_status_object->label,
723 ];
724 }
725
726 do_action( 'elementor/document/before_get_config', $this );
727
728 if ( static::get_property( 'has_elements' ) ) {
729 $container_config = [];
730
731 if ( Plugin::$instance->experiments->is_feature_active( 'container' ) ) {
732 $container_config = [
733 'container' => Plugin::$instance->elements_manager->get_element_types( 'container' )->get_config(),
734 ];
735 }
736
737 $config['elements'] = $this->get_elements_raw_data( null, true );
738 $config['widgets'] = $container_config + Plugin::$instance->widgets_manager->get_widget_types_config();
739 }
740
741 $additional_config = [];
742
743 /**
744 * Additional document configuration.
745 *
746 * Filters the document configuration by adding additional configuration.
747 * External developers can use this hook to add custom configuration in
748 * addition to Elementor's initial configuration.
749 *
750 * Use the $post_id to add custom configuration for different pages.
751 *
752 * @param array $additional_config The additional document configuration.
753 * @param int $post_id The post ID of the document.
754 */
755 $additional_config = apply_filters( 'elementor/document/config', $additional_config, $this->get_main_id() );
756
757 if ( ! empty( $additional_config ) ) {
758 $config = array_replace_recursive( $config, $additional_config );
759 }
760
761 return $config;
762 }
763
764 /**
765 * @since 3.1.0
766 * @access protected
767 */
768 protected function register_controls() {
769 $this->register_document_controls();
770
771 /**
772 * Register document controls.
773 *
774 * Fires after Elementor registers the document controls.
775 *
776 * External developers can use this hook to add new controls to the document.
777 *
778 * @since 2.0.0
779 *
780 * @param Document $this The document instance.
781 */
782 do_action( 'elementor/documents/register_controls', $this );
783 }
784
785 /**
786 * @since 2.0.0
787 * @access public
788 *
789 * @param $data
790 *
791 * @return bool
792 */
793 public function save( $data ) {
794 /**
795 * Set locale to "C" to avoid issues with comma as decimal separator.
796 *
797 * @see https://github.com/elementor/elementor/issues/10992
798 */
799 $original_lc = setlocale( LC_NUMERIC, 0 );
800 setlocale( LC_NUMERIC, 'C' );
801
802 /**
803 * Document save data.
804 *
805 * Filter the document data before saving process starts.
806 *
807 * External developers can use this hook to change the data before
808 * saving it to the database.
809 *
810 * @since 3.3.0
811 *
812 * @param array $data The document data.
813 * @param \Elementor\Core\Base\Document $this The document instance.
814 */
815 $data = apply_filters( 'elementor/document/save/data', $data, $this );
816
817 $this->add_handle_revisions_changed_filter();
818
819 if ( ! $this->is_editable_by_current_user() ) {
820 return false;
821 }
822
823 $this->set_is_saving( true );
824
825 /**
826 * Before document save.
827 *
828 * Fires when document save starts on Elementor.
829 *
830 * @since 2.5.12
831 *
832 * @param \Elementor\Core\Base\Document $this The current document.
833 * @param $data.
834 */
835 do_action( 'elementor/document/before_save', $this, $data );
836
837 if ( ! current_user_can( 'unfiltered_html' ) ) {
838 $data = wp_kses_post_deep( $data );
839 }
840
841 if ( ! empty( $data['settings'] ) ) {
842 if ( isset( $data['settings']['post_status'] ) && self::STATUS_AUTOSAVE === $data['settings']['post_status'] ) {
843 if ( ! defined( 'DOING_AUTOSAVE' ) ) {
844 define( 'DOING_AUTOSAVE', true );
845 }
846 }
847
848 $this->save_settings( $data['settings'] );
849
850 $this->refresh_post();
851 }
852
853 // Don't check is_empty, because an empty array should be saved.
854 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
855 $this->save_elements( $data['elements'] );
856 }
857
858 $this->save_template_type();
859
860 $this->save_version();
861
862 // Remove Post CSS
863 $post_css = Post_CSS::create( $this->post->ID );
864
865 $post_css->delete();
866
867 // Remove Document Cache
868 $this->delete_cache();
869
870 /**
871 * After document save.
872 *
873 * Fires when document save is complete.
874 *
875 * @since 2.5.12
876 *
877 * @param \Elementor\Core\Base\Document $this The current document.
878 * @param $data.
879 */
880 do_action( 'elementor/document/after_save', $this, $data );
881
882 $this->set_is_saving( false );
883
884 $this->remove_handle_revisions_changed_filter();
885
886 setlocale( LC_NUMERIC, $original_lc );
887
888 return true;
889 }
890
891 public function refresh_post() {
892 $this->post = get_post( $this->post->ID );
893 }
894
895 /**
896 * @param array $new_settings
897 *
898 * @return static
899 */
900 public function update_settings( array $new_settings ) {
901 $document_settings = $this->get_meta( PageManager::META_KEY );
902
903 if ( ! $document_settings ) {
904 $document_settings = [];
905 }
906
907 $this->save_settings(
908 array_replace_recursive( $document_settings, $new_settings )
909 );
910
911 return $this;
912 }
913
914 /**
915 * Is built with Elementor.
916 *
917 * Check whether the post was built with Elementor.
918 *
919 * @since 2.0.0
920 * @access public
921 *
922 * @return bool Whether the post was built with Elementor.
923 */
924 public function is_built_with_elementor() {
925 return ! ! $this->get_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
926 }
927
928 /**
929 * Mark the post as "built with elementor" or not.
930 *
931 * @param bool $is_built_with_elementor
932 *
933 * @return $this
934 */
935 public function set_is_built_with_elementor( $is_built_with_elementor ) {
936 if ( $is_built_with_elementor ) {
937 // Use the string `builder` and not a boolean for rollback compatibility
938 $this->update_meta( self::BUILT_WITH_ELEMENTOR_META_KEY, 'builder' );
939 } else {
940 $this->delete_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
941 }
942
943 return $this;
944 }
945
946 /**
947 * @since 2.0.0
948 * @access public
949 * @static
950 *
951 * @return mixed
952 */
953 public function get_edit_url() {
954 $url = add_query_arg(
955 [
956 'post' => $this->get_main_id(),
957 'action' => 'elementor',
958 ],
959 admin_url( 'post.php' )
960 );
961
962 /**
963 * Document edit url.
964 *
965 * Filters the document edit url.
966 *
967 * @since 2.0.0
968 *
969 * @param string $url The edit url.
970 * @param Document $this The document instance.
971 */
972 $url = apply_filters( 'elementor/document/urls/edit', $url, $this );
973
974 return $url;
975 }
976
977 /**
978 * @since 2.0.0
979 * @access public
980 */
981 public function get_preview_url() {
982 /**
983 * Use a static var - to avoid change the `ver` parameter on every call.
984 */
985 static $url;
986
987 if ( empty( $url ) ) {
988
989 add_filter( 'pre_option_permalink_structure', '__return_empty_string' );
990
991 $url = set_url_scheme( add_query_arg( [
992 'elementor-preview' => $this->get_main_id(),
993 'ver' => time(),
994 ], $this->get_permalink() ) );
995
996 remove_filter( 'pre_option_permalink_structure', '__return_empty_string' );
997
998 /**
999 * Document preview URL.
1000 *
1001 * Filters the document preview URL.
1002 *
1003 * @since 2.0.0
1004 *
1005 * @param string $url The preview URL.
1006 * @param Document $this The document instance.
1007 */
1008 $url = apply_filters( 'elementor/document/urls/preview', $url, $this );
1009 }
1010
1011 return $url;
1012 }
1013
1014 /**
1015 * @since 2.0.0
1016 * @access public
1017 *
1018 * @param string $key
1019 *
1020 * @return array
1021 */
1022 public function get_json_meta( $key ) {
1023 $meta = get_post_meta( $this->post->ID, $key, true );
1024
1025 if ( is_string( $meta ) && ! empty( $meta ) ) {
1026 $meta = json_decode( $meta, true );
1027 }
1028
1029 if ( empty( $meta ) ) {
1030 $meta = [];
1031 }
1032
1033 return $meta;
1034 }
1035
1036 public function update_json_meta( $key, $value ) {
1037 $this->update_meta(
1038 $key,
1039 // `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1040 wp_slash( wp_json_encode( $value ) )
1041 );
1042 }
1043
1044 /**
1045 * @since 2.0.0
1046 * @access public
1047 *
1048 * @param null $data
1049 * @param bool $with_html_content
1050 *
1051 * @return array
1052 */
1053 public function get_elements_raw_data( $data = null, $with_html_content = false ) {
1054 if ( ! static::get_property( 'has_elements' ) ) {
1055 return [];
1056 }
1057
1058 if ( is_null( $data ) ) {
1059 $data = $this->get_elements_data();
1060 }
1061
1062 // Change the current documents, so widgets can use `documents->get_current` and other post data
1063 Plugin::$instance->documents->switch_to_document( $this );
1064
1065 $editor_data = [];
1066
1067 foreach ( $data as $element_data ) {
1068 if ( ! is_array( $element_data ) ) {
1069 throw new \Exception( 'Invalid data: ' . wp_json_encode( [
1070 'data' => $data,
1071 'element' => $element_data,
1072 ] ) );
1073 }
1074
1075 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1076
1077 if ( ! $element ) {
1078 continue;
1079 }
1080
1081 if ( $this->is_saving ) {
1082 $element_data = $element->get_data_for_save();
1083 } else {
1084 $element_data = $element->get_raw_data( $with_html_content );
1085 }
1086
1087 $editor_data[] = $element_data;
1088 } // End foreach().
1089
1090 Plugin::$instance->documents->restore_document();
1091
1092 return $editor_data;
1093 }
1094
1095 /**
1096 * @since 2.0.0
1097 * @access public
1098 *
1099 * @param string $status
1100 *
1101 * @return array
1102 */
1103 public function get_elements_data( $status = self::STATUS_PUBLISH ) {
1104 $elements = $this->get_json_meta( '_elementor_data' );
1105
1106 if ( self::STATUS_DRAFT === $status ) {
1107 $autosave = $this->get_newer_autosave();
1108
1109 if ( is_object( $autosave ) ) {
1110 $autosave_elements = Plugin::$instance->documents
1111 ->get( $autosave->get_post()->ID )
1112 ->get_json_meta( '_elementor_data' );
1113 }
1114 }
1115
1116 if ( Plugin::$instance->editor->is_edit_mode() ) {
1117 if ( empty( $elements ) && empty( $autosave_elements ) ) {
1118 // Convert to Elementor.
1119 $elements = $this->convert_to_elementor();
1120 if ( $this->is_autosave() ) {
1121 Plugin::$instance->db->copy_elementor_meta( $this->post->post_parent, $this->post->ID );
1122 }
1123 }
1124 }
1125
1126 if ( ! empty( $autosave_elements ) ) {
1127 $elements = $autosave_elements;
1128 }
1129
1130 return $elements;
1131 }
1132
1133 /**
1134 * Get document setting from DB.
1135 *
1136 * @return array
1137 */
1138 public function get_db_document_settings() {
1139 return $this->get_meta( static::PAGE_META_KEY );
1140 }
1141
1142 /**
1143 * @since 2.3.0
1144 * @access public
1145 */
1146 public function convert_to_elementor() {
1147 $this->save( [] );
1148
1149 if ( empty( $this->post->post_content ) ) {
1150 return [];
1151 }
1152
1153 // Check if it's only a shortcode.
1154 preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER );
1155 if ( ! empty( $matches ) ) {
1156 foreach ( $matches as $shortcode ) {
1157 if ( trim( $this->post->post_content ) === $shortcode[0] ) {
1158 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' );
1159 $settings = [
1160 'shortcode' => $this->post->post_content,
1161 ];
1162 break;
1163 }
1164 }
1165 }
1166
1167 if ( empty( $widget_type ) ) {
1168 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' );
1169 $settings = [
1170 'editor' => $this->post->post_content,
1171 ];
1172 }
1173
1174 // TODO: Better coding to start template for editor
1175 $converted_blocks = [
1176 [
1177 'id' => Utils::generate_random_string(),
1178 'elType' => $widget_type::get_type(),
1179 'widgetType' => $widget_type->get_name(),
1180 'settings' => $settings,
1181 ],
1182 ];
1183
1184 return Plugin::$instance->experiments->is_feature_active( 'container' )
1185 ? $this->get_container_elements_data( $converted_blocks )
1186 : $this->get_sections_elements_data( $converted_blocks );
1187 }
1188
1189 /**
1190 * @since 2.1.3
1191 * @access public
1192 */
1193 public function print_elements_with_wrapper( $elements_data = null ) {
1194 if ( ! $elements_data ) {
1195 $elements_data = $this->get_elements_data();
1196 }
1197
1198 ?>
1199 <div <?php Utils::print_html_attributes( $this->get_container_attributes() ); ?>>
1200 <?php $this->print_elements( $elements_data ); ?>
1201 </div>
1202 <?php
1203 }
1204
1205 /**
1206 * @since 2.0.0
1207 * @access public
1208 */
1209 public function get_css_wrapper_selector() {
1210 return '';
1211 }
1212
1213 /**
1214 * @since 2.0.0
1215 * @access public
1216 */
1217 public function get_panel_page_settings() {
1218 return [
1219 'title' => sprintf(
1220 /* translators: %s: Document title. */
1221 esc_html__( '%s Settings', 'elementor' ),
1222 static::get_title()
1223 ),
1224 ];
1225 }
1226
1227 /**
1228 * @since 2.0.0
1229 * @access public
1230 */
1231 public function get_post() {
1232 return $this->post;
1233 }
1234
1235 /**
1236 * @since 2.0.0
1237 * @access public
1238 */
1239 public function get_permalink() {
1240 return get_permalink( $this->get_main_id() );
1241 }
1242
1243 /**
1244 * @since 2.0.8
1245 * @access public
1246 */
1247 public function get_content( $with_css = false ) {
1248 return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css );
1249 }
1250
1251 /**
1252 * @since 2.0.0
1253 * @access public
1254 */
1255 public function delete() {
1256 if ( 'revision' === $this->post->post_type ) {
1257 $deleted = wp_delete_post_revision( $this->post );
1258 } else {
1259 $deleted = wp_delete_post( $this->post->ID );
1260 }
1261
1262 return $deleted && ! is_wp_error( $deleted );
1263 }
1264
1265 public function force_delete() {
1266 $deleted = wp_delete_post( $this->post->ID, true );
1267
1268 return $deleted && ! is_wp_error( $deleted );
1269 }
1270
1271 /**
1272 * On import update dynamic content (e.g. post and term IDs).
1273 *
1274 * @since 3.8.0
1275 *
1276 * @param array $config The config of the passed element.
1277 * @param array $data The data that requires updating/replacement when imported.
1278 * @param array|null $controls The available controls.
1279 *
1280 * @return array Element data.
1281 */
1282 public static function on_import_update_dynamic_content( array $config, array $data, $controls = null ) : array {
1283 foreach ( $config as &$element_config ) {
1284 $element_instance = Plugin::$instance->elements_manager->create_element_instance( $element_config );
1285
1286 if ( is_null( $element_instance ) ) {
1287 continue;
1288 }
1289
1290 if ( $element_instance->has_own_method( 'on_import_replace_dynamic_content' ) ) {
1291 // TODO: Remove this check in the future.
1292 $element_config = $element_instance::on_import_replace_dynamic_content( $element_config, $data['post_ids'] );
1293 } else {
1294 $element_config = $element_instance::on_import_update_dynamic_content( $element_config, $data, $element_instance->get_controls() );
1295 }
1296
1297 $element_config['elements'] = static::on_import_update_dynamic_content( $element_config['elements'], $data );
1298 }
1299
1300 return $config;
1301 }
1302
1303 /**
1304 * Update dynamic settings in the document for import.
1305 *
1306 * @param array $settings The settings of the document.
1307 * @param array $config Import config to update the settings.
1308 *
1309 * @return array
1310 */
1311 public function on_import_update_settings( array $settings, array $config ): array {
1312 $controls = $this->get_controls();
1313 $controls_manager = Plugin::$instance->controls_manager;
1314
1315 foreach ( $settings as $key => $value ) {
1316
1317 if ( ! isset( $controls[ $key ] ) ) {
1318 continue;
1319 }
1320
1321 $control = $controls[ $key ];
1322 $control_instance = $controls_manager->get_control( $control['type'] );
1323
1324 if ( ! $control_instance ) {
1325 continue;
1326 }
1327
1328 $settings[ $key ] = $control_instance->on_import_update_settings( $value, $control, $config );
1329 }
1330
1331 return $settings;
1332 }
1333
1334 /**
1335 * Save editor elements.
1336 *
1337 * Save data from the editor to the database.
1338 *
1339 * @since 2.0.0
1340 * @access protected
1341 *
1342 * @param array $elements
1343 */
1344 protected function save_elements( $elements ) {
1345 $editor_data = $this->get_elements_raw_data( $elements );
1346
1347 // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1348 $json_value = wp_slash( wp_json_encode( $editor_data ) );
1349
1350 // Don't use `update_post_meta` that can't handle `revision` post type
1351 $is_meta_updated = update_metadata( 'post', $this->post->ID, '_elementor_data', $json_value );
1352
1353 /**
1354 * Before saving data.
1355 *
1356 * Fires before Elementor saves data to the database.
1357 *
1358 * @since 1.0.0
1359 *
1360 * @param string $status Post status.
1361 * @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure.
1362 */
1363 do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated );
1364
1365 Plugin::$instance->db->save_plain_text( $this->post->ID );
1366
1367 $elements_iteration_actions = $this->get_elements_iteration_actions();
1368
1369 if ( $elements_iteration_actions ) {
1370 $this->iterate_elements( $elements, $elements_iteration_actions, 'save' );
1371 }
1372
1373 /**
1374 * After saving data.
1375 *
1376 * Fires after Elementor saves data to the database.
1377 *
1378 * @since 1.0.0
1379 *
1380 * @param int $post_id The ID of the post.
1381 * @param array $editor_data Sanitize posted data.
1382 */
1383 do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data );
1384 }
1385
1386 /**
1387 * @since 2.0.0
1388 * @access public
1389 *
1390 * @param int $user_id Optional. User ID. Default value is `0`.
1391 *
1392 * @return bool|int
1393 */
1394 public function get_autosave_id( $user_id = 0 ) {
1395 if ( ! $user_id ) {
1396 $user_id = get_current_user_id();
1397 }
1398
1399 $autosave = Utils::get_post_autosave( $this->post->ID, $user_id );
1400 if ( $autosave ) {
1401 return $autosave->ID;
1402 }
1403
1404 return false;
1405 }
1406
1407 public function save_version() {
1408 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
1409 // Save per revision.
1410 $this->update_meta( '_elementor_version', ELEMENTOR_VERSION );
1411
1412 /**
1413 * Document version save.
1414 *
1415 * Fires when document version is saved on Elementor.
1416 * Will not fire during Elementor Upgrade.
1417 *
1418 * @since 2.5.12
1419 *
1420 * @param \Elementor\Core\Base\Document $this The current document.
1421 *
1422 */
1423 do_action( 'elementor/document/save_version', $this );
1424 }
1425 }
1426
1427 /**
1428 * @since 2.3.0
1429 * @access public
1430 */
1431 public function save_template_type() {
1432 return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() );
1433 }
1434
1435 /**
1436 * @since 2.3.0
1437 * @access public
1438 */
1439 public function get_template_type() {
1440 return $this->get_main_meta( self::TYPE_META_KEY );
1441 }
1442
1443 /**
1444 * @since 2.0.0
1445 * @access public
1446 *
1447 * @param string $key Meta data key.
1448 *
1449 * @return mixed
1450 */
1451 public function get_main_meta( $key ) {
1452 return get_post_meta( $this->get_main_id(), $key, true );
1453 }
1454
1455 /**
1456 * @since 2.0.4
1457 * @access public
1458 *
1459 * @param string $key Meta data key.
1460 * @param mixed $value Meta data value.
1461 *
1462 * @return bool|int
1463 */
1464 public function update_main_meta( $key, $value ) {
1465 return update_post_meta( $this->get_main_id(), $key, $value );
1466 }
1467
1468 /**
1469 * @since 2.0.4
1470 * @access public
1471 *
1472 * @param string $key Meta data key.
1473 * @param string $value Optional. Meta data value. Default is an empty string.
1474 *
1475 * @return bool
1476 */
1477 public function delete_main_meta( $key, $value = '' ) {
1478 return delete_post_meta( $this->get_main_id(), $key, $value );
1479 }
1480
1481 /**
1482 * @since 2.0.0
1483 * @access public
1484 *
1485 * @param string $key Meta data key.
1486 *
1487 * @return mixed
1488 */
1489 public function get_meta( $key ) {
1490 return get_post_meta( $this->post->ID, $key, true );
1491 }
1492
1493 /**
1494 * @since 2.0.0
1495 * @access public
1496 *
1497 * @param string $key Meta data key.
1498 * @param mixed $value Meta data value.
1499 *
1500 * @return bool|int
1501 */
1502 public function update_meta( $key, $value ) {
1503 // Use `update_metadata` in order to work also with revisions.
1504 return update_metadata( 'post', $this->post->ID, $key, $value );
1505 }
1506
1507 /**
1508 * @since 2.0.3
1509 * @access public
1510 *
1511 * @param string $key Meta data key.
1512 * @param string $value Meta data value.
1513 *
1514 * @return bool
1515 */
1516 public function delete_meta( $key, $value = '' ) {
1517 // Use `delete_metadata` in order to work also with revisions.
1518 return delete_metadata( 'post', $this->post->ID, $key, $value );
1519 }
1520
1521 /**
1522 * @since 2.0.0
1523 * @access public
1524 */
1525 public function get_last_edited() {
1526 $post = $this->post;
1527 $autosave_post = $this->get_autosave();
1528
1529 if ( $autosave_post ) {
1530 $post = $autosave_post->get_post();
1531 }
1532
1533 $date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) );
1534 $display_name = get_the_author_meta( 'display_name', $post->post_author );
1535
1536 if ( $autosave_post || 'revision' === $post->post_type ) {
1537 $last_edited = sprintf(
1538 /* translators: 1: Saving date, 2: Author display name. */
1539 esc_html__( 'Draft saved on %1$s by %2$s', 'elementor' ),
1540 '<time>' . $date . '</time>',
1541 $display_name
1542 );
1543 } else {
1544 $last_edited = sprintf(
1545 /* translators: 1: Editing date, 2: Author display name. */
1546 esc_html__( 'Last edited on %1$s by %2$s', 'elementor' ),
1547 '<time>' . $date . '</time>',
1548 $display_name
1549 );
1550 }
1551
1552 return $last_edited;
1553 }
1554
1555
1556 /**
1557 * @return bool
1558 */
1559 public function is_saving() {
1560 return $this->is_saving;
1561 }
1562
1563 /**
1564 * @param $is_saving
1565 *
1566 * @return $this
1567 */
1568 public function set_is_saving( $is_saving ) {
1569 $this->is_saving = $is_saving;
1570
1571 return $this;
1572 }
1573
1574 /**
1575 * @since 2.0.0
1576 * @access public
1577 *
1578 * @param array $data
1579 *
1580 * @throws \Exception If the post does not exist.
1581 */
1582 public function __construct( array $data = [] ) {
1583 if ( $data ) {
1584 if ( empty( $data['post_id'] ) ) {
1585 $this->post = new \WP_Post( (object) [] );
1586 } else {
1587 $this->post = get_post( $data['post_id'] );
1588
1589 if ( ! $this->post ) {
1590 throw new \Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ), Exceptions::NOT_FOUND );
1591 }
1592 }
1593
1594 // Each Control_Stack is based on a unique ID.
1595 $data['id'] = $data['post_id'];
1596
1597 if ( ! isset( $data['settings'] ) ) {
1598 $data['settings'] = [];
1599 }
1600
1601 $saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true );
1602 if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) {
1603 $data['settings'] += $saved_settings;
1604 }
1605 }
1606
1607 parent::__construct( $data );
1608 }
1609
1610 /*
1611 * Get Export Data
1612 *
1613 * Filters a document's data on export
1614 *
1615 * @since 3.2.0
1616 * @access public
1617 *
1618 * @return array The data to export
1619 */
1620 public function get_export_data() {
1621 $content = Plugin::$instance->db->iterate_data( $this->get_elements_data(), function( $element_data ) {
1622 $element_data['id'] = Utils::generate_random_string();
1623
1624 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1625
1626 // If the widget/element does not exist, like a plugin that creates a widget but deactivated.
1627 if ( ! $element ) {
1628 return null;
1629 }
1630
1631 return $this->process_element_import_export( $element, 'on_export' );
1632 } );
1633
1634 return [
1635 'content' => $content,
1636 'settings' => $this->get_data( 'settings' ),
1637 'metadata' => $this->get_export_metadata(),
1638 ];
1639 }
1640
1641 public function get_export_summary() {
1642 return [
1643 'title' => $this->post->post_title,
1644 'doc_type' => $this->get_name(),
1645 'thumbnail' => get_the_post_thumbnail_url( $this->post ),
1646 ];
1647 }
1648
1649 /*
1650 * Get Import Data
1651 *
1652 * Filters a document's data on import
1653 *
1654 * @since 3.2.0
1655 * @access public
1656 *
1657 * @return array The data to import
1658 */
1659 public function get_import_data( array $data ) {
1660 $data['content'] = Plugin::$instance->db->iterate_data( $data['content'], function( $element_data ) {
1661 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1662
1663 // If the widget/element isn't 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_import' );
1669 } );
1670
1671 if ( ! empty( $data['settings'] ) ) {
1672 $template_model = new Page_Model( [
1673 'id' => 0,
1674 'settings' => $data['settings'],
1675 ] );
1676
1677 $page_data = $this->process_element_import_export( $template_model, 'on_import' );
1678
1679 $data['settings'] = $page_data['settings'];
1680 }
1681
1682 return $data;
1683 }
1684
1685 /**
1686 * Import
1687 *
1688 * Allows to import an external data to a document
1689 *
1690 * @since 3.2.0
1691 * @access public
1692 *
1693 * @param array $data
1694 */
1695 public function import( array $data ) {
1696 $data = $this->get_import_data( $data );
1697
1698 $this->save( [
1699 'elements' => $data['content'],
1700 'settings' => $data['settings'],
1701 ] );
1702
1703 if ( $data['import_settings']['thumbnail'] ) {
1704 $attachment = Plugin::$instance->templates_manager->get_import_images_instance()->import( [ 'url' => $data['import_settings']['thumbnail'] ] );
1705
1706 set_post_thumbnail( $this->get_main_post(), $attachment['id'] );
1707 }
1708
1709 if ( ! empty( $data['metadata'] ) ) {
1710 foreach ( $data['metadata'] as $key => $value ) {
1711 $this->update_meta( $key, $value );
1712 }
1713 }
1714 }
1715
1716 public function process_element_import_export( Controls_Stack $element, $method, $element_data = null ) {
1717 if ( null === $element_data ) {
1718 $element_data = $element->get_data();
1719 }
1720
1721 if ( method_exists( $element, $method ) ) {
1722 // TODO: Use the internal element data without parameters.
1723 $element_data = $element->{$method}( $element_data );
1724 }
1725
1726 foreach ( $element->get_controls() as $control ) {
1727 $control_class = Plugin::$instance->controls_manager->get_control( $control['type'] );
1728
1729 // If the control isn't exist, like a plugin that creates the control but deactivated.
1730 if ( ! $control_class ) {
1731 return $element_data;
1732 }
1733
1734 // Do not add default value to the final settings, if there is no value at the
1735 // data before the methods `on_import` or `on_export` called.
1736 $has_value = isset( $element_data['settings'][ $control['name'] ] );
1737
1738 if ( $has_value && method_exists( $control_class, $method ) ) {
1739 $element_data['settings'][ $control['name'] ] = $control_class->{$method}(
1740 $element_data['settings'][ $control['name'] ],
1741 $control
1742 );
1743 }
1744
1745 // On Export, check if the control has an argument 'export' => false.
1746 if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) {
1747 unset( $element_data['settings'][ $control['name'] ] );
1748 }
1749 }
1750
1751 return $element_data;
1752 }
1753
1754 protected function get_export_metadata() {
1755 $metadata = get_post_meta( $this->get_main_id() );
1756
1757 foreach ( $metadata as $meta_key => $meta_value ) {
1758 if ( is_protected_meta( $meta_key, 'post' ) ) {
1759 unset( $metadata[ $meta_key ] );
1760
1761 continue;
1762 }
1763
1764 $metadata[ $meta_key ] = $meta_value[0];
1765 }
1766
1767 return $metadata;
1768 }
1769
1770 protected function get_remote_library_config() {
1771 $config = [
1772 'type' => 'block',
1773 'default_route' => 'templates/blocks',
1774 'category' => $this->get_name(),
1775 'autoImportSettings' => false,
1776 ];
1777
1778 return $config;
1779 }
1780
1781 /**
1782 * @since 2.0.4
1783 * @access protected
1784 *
1785 * @param $settings
1786 */
1787 protected function save_settings( $settings ) {
1788 $page_settings_manager = SettingsManager::get_settings_managers( 'page' );
1789 $page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID );
1790 $page_settings_manager->save_settings( $settings, $this->post->ID );
1791 }
1792
1793 /**
1794 * @since 2.1.3
1795 * @access protected
1796 */
1797 protected function print_elements( $elements_data ) {
1798 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_element_cache' ) ) {
1799 $this->do_print_elements( $elements_data );
1800
1801 return;
1802 }
1803
1804 $cached_data = $this->get_document_cache();
1805
1806 if ( false === $cached_data ) {
1807 add_filter( 'elementor/element/should_render_shortcode', '__return_true' );
1808
1809 $scripts_to_queue = [];
1810 $styles_to_queue = [];
1811
1812 global $wp_scripts, $wp_styles;
1813
1814 $should_store_scripts = $wp_scripts instanceof \WP_Scripts && $wp_styles instanceof \WP_Styles;
1815 if ( $should_store_scripts ) {
1816 $scripts_ignored = $wp_scripts->queue;
1817 $styles_ignored = $wp_styles->queue;
1818 }
1819
1820 ob_start();
1821
1822 $this->do_print_elements( $elements_data );
1823
1824 if ( $should_store_scripts ) {
1825 $scripts_to_queue = array_values( array_diff( $wp_scripts->queue, $scripts_ignored ) );
1826 $styles_to_queue = array_values( array_diff( $wp_styles->queue, $styles_ignored ) );
1827 }
1828
1829 $cached_data = [
1830 'content' => ob_get_clean(),
1831 'scripts' => $scripts_to_queue,
1832 'styles' => $styles_to_queue,
1833 ];
1834
1835 if ( $this->should_store_cache_elements() ) {
1836 $this->set_document_cache( $cached_data );
1837 }
1838
1839 remove_filter( 'elementor/element/should_render_shortcode', '__return_true' );
1840 } else {
1841 if ( ! empty( $cached_data['scripts'] ) ) {
1842 foreach ( $cached_data['scripts'] as $script_handle ) {
1843 wp_enqueue_script( $script_handle );
1844 }
1845 }
1846
1847 if ( ! empty( $cached_data['styles'] ) ) {
1848 foreach ( $cached_data['styles'] as $style_handle ) {
1849 wp_enqueue_style( $style_handle );
1850 }
1851 }
1852 }
1853
1854 if ( ! empty( $cached_data['content'] ) ) {
1855 echo do_shortcode( $cached_data['content'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1856 }
1857 }
1858
1859 protected function do_print_elements( $elements_data ) {
1860 // Collect all data updaters that should be updated on runtime.
1861 $runtime_elements_iteration_actions = $this->get_runtime_elements_iteration_actions();
1862
1863 if ( $runtime_elements_iteration_actions ) {
1864 $this->iterate_elements( $elements_data, $runtime_elements_iteration_actions, 'render' );
1865 }
1866
1867 foreach ( $elements_data as $element_data ) {
1868 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1869
1870 if ( ! $element ) {
1871 continue;
1872 }
1873
1874 $element->print_element();
1875 }
1876 }
1877
1878 public function set_document_cache( $value ) {
1879 $expiration_hours = get_option( 'elementor_element_cache_ttl', '' );
1880
1881 if ( empty( $expiration_hours ) || ! is_numeric( $expiration_hours ) ) {
1882 $expiration_hours = '24';
1883 }
1884
1885 $expiration_hours = absint( $expiration_hours );
1886
1887 $expiration = '+' . $expiration_hours . ' hours';
1888
1889 $data = [
1890 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ),
1891 'value' => $value,
1892 ];
1893
1894 $this->update_json_meta( static::CACHE_META_KEY, $data );
1895 }
1896
1897 private function get_document_cache() {
1898 $cache = $this->get_json_meta( static::CACHE_META_KEY );
1899
1900 if ( empty( $cache['timeout'] ) ) {
1901 return false;
1902 }
1903
1904 if ( current_time( 'timestamp' ) > $cache['timeout'] ) {
1905 return false;
1906 }
1907
1908 if ( ! is_array( $cache['value'] ) ) {
1909 return false;
1910 }
1911
1912 return $cache['value'];
1913 }
1914
1915 protected function delete_cache() {
1916 $this->delete_meta( static::CACHE_META_KEY );
1917 }
1918
1919 private function should_store_cache_elements() {
1920 static $should_store_cache_elements = null;
1921
1922 if ( null === $should_store_cache_elements ) {
1923 $should_store_cache_elements = (
1924 ! is_admin()
1925 && ! Plugin::$instance->preview->is_preview_mode()
1926 );
1927 }
1928
1929 return $should_store_cache_elements;
1930 }
1931
1932 protected function register_document_controls() {
1933 $this->start_controls_section(
1934 'document_settings',
1935 [
1936 'label' => esc_html__( 'General Settings', 'elementor' ),
1937 'tab' => Controls_Manager::TAB_SETTINGS,
1938 ]
1939 );
1940
1941 $this->add_control(
1942 'post_title',
1943 [
1944 'label' => esc_html__( 'Title', 'elementor' ),
1945 'type' => Controls_Manager::TEXT,
1946 'default' => $this->post->post_title,
1947 'label_block' => true,
1948 ]
1949 );
1950
1951 $post_type_object = get_post_type_object( $this->post->post_type );
1952
1953 $can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts );
1954 $is_published = self::STATUS_PUBLISH === $this->post->post_status || self::STATUS_PRIVATE === $this->post->post_status;
1955
1956 if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) {
1957
1958 $statuses = $this->get_post_statuses();
1959 if ( 'future' === $this->get_main_post()->post_status ) {
1960 $statuses['future'] = esc_html__( 'Future', 'elementor' );
1961 }
1962
1963 $this->add_control(
1964 'post_status',
1965 [
1966 'label' => esc_html__( 'Status', 'elementor' ),
1967 'type' => Controls_Manager::SELECT,
1968 'default' => $this->get_main_post()->post_status,
1969 'options' => $statuses,
1970 ]
1971 );
1972 }
1973
1974 $this->end_controls_section();
1975 }
1976
1977 protected function get_post_statuses() {
1978 return get_post_statuses();
1979 }
1980
1981 protected function get_have_a_look_url() {
1982 return $this->get_permalink();
1983 }
1984
1985 public function handle_revisions_changed( $post_has_changed, $last_revision, $post ) {
1986 // In case default, didn't determine the changes.
1987 if ( ! $post_has_changed ) {
1988 $last_revision_id = $last_revision->ID;
1989 $last_revision_document = Plugin::instance()->documents->get( $last_revision_id );
1990 $post_document = Plugin::instance()->documents->get( $post->ID );
1991
1992 $last_revision_settings = $last_revision_document->get_settings();
1993 $post_settings = $post_document->get_settings();
1994
1995 // TODO: Its better to add crc32 signature for each revision and then only compare one part of the checksum.
1996 $post_has_changed = $last_revision_settings !== $post_settings;
1997 }
1998
1999 return $post_has_changed;
2000 }
2001
2002 private function add_handle_revisions_changed_filter() {
2003 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ], 10, 3 );
2004 }
2005
2006 private function remove_handle_revisions_changed_filter() {
2007 remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] );
2008 }
2009
2010 private function get_runtime_elements_iteration_actions() {
2011 $runtime_elements_iteration_actions = [];
2012
2013 $elements_iteration_actions = $this->get_elements_iteration_actions();
2014
2015 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2016 if ( $elements_iteration_action->is_action_needed() ) {
2017 $runtime_elements_iteration_actions[] = $elements_iteration_action;
2018 }
2019 }
2020
2021 return $runtime_elements_iteration_actions;
2022 }
2023
2024 private function iterate_elements( $elements, $elements_iteration_actions, $mode ) {
2025 $unique_page_elements = [];
2026
2027 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2028 $elements_iteration_action->set_mode( $mode );
2029 }
2030
2031 Plugin::$instance->db->iterate_data( $elements, function( array $element_data ) use ( &$unique_page_elements, $elements_iteration_actions ) {
2032 $element_type = 'widget' === $element_data['elType'] ? $element_data['widgetType'] : $element_data['elType'];
2033
2034 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
2035
2036 if ( $element ) {
2037 if ( ! in_array( $element_type, $unique_page_elements, true ) ) {
2038 $unique_page_elements[] = $element_type;
2039
2040 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2041 $elements_iteration_action->unique_element_action( $element );
2042 }
2043 }
2044
2045 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2046 $elements_iteration_action->element_action( $element );
2047 }
2048 }
2049
2050 return $element_data;
2051 } );
2052
2053 foreach ( $elements_iteration_actions as $elements_iteration_action ) {
2054 $elements_iteration_action->after_elements_iteration();
2055 }
2056 }
2057
2058 private function get_elements_iteration_actions() {
2059 if ( ! $this->elements_iteration_actions ) {
2060 $this->elements_iteration_actions[] = new Assets_Iteration_Action( $this );
2061 }
2062
2063 return $this->elements_iteration_actions;
2064 }
2065 }
2066