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

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