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

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

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