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

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

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