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

1,388 lines 31.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Base;
3
4 use Elementor\Core\Files\CSS\Post as Post_CSS;
5 use Elementor\Core\Utils\Exceptions;
6 use Elementor\Plugin;
7 use Elementor\Controls_Manager;
8 use Elementor\Controls_Stack;
9 use Elementor\User;
10 use Elementor\Core\Settings\Manager as SettingsManager;
11 use Elementor\Utils;
12 use Elementor\Widget_Base;
13 use Elementor\Core\Settings\Page\Manager as PageManager;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly
17 }
18
19 /**
20 * Elementor document.
21 *
22 * An abstract class that provides the needed properties and methods to
23 * manage and handle documents in inheriting classes.
24 *
25 * @since 2.0.0
26 * @abstract
27 */
28 abstract class Document extends Controls_Stack {
29
30 /**
31 * Document type meta key.
32 */
33 const TYPE_META_KEY = '_elementor_template_type';
34 const PAGE_META_KEY = '_elementor_page_settings';
35
36 const BUILT_WITH_ELEMENTOR_META_KEY = '_elementor_edit_mode';
37
38 /**
39 * Document publish status.
40 */
41 const STATUS_PUBLISH = 'publish';
42
43 /**
44 * Document draft status.
45 */
46 const STATUS_DRAFT = 'draft';
47
48 /**
49 * Document private status.
50 */
51 const STATUS_PRIVATE = 'private';
52
53 /**
54 * Document autosave status.
55 */
56 const STATUS_AUTOSAVE = 'autosave';
57
58 /**
59 * Document pending status.
60 */
61 const STATUS_PENDING = 'pending';
62
63
64 private $main_id;
65
66 /**
67 * @var bool
68 */
69 private $is_saving = false;
70
71 private static $properties = [];
72
73 /**
74 * Document post data.
75 *
76 * Holds the document post data.
77 *
78 * @since 2.0.0
79 * @access protected
80 *
81 * @var \WP_Post WordPress post data.
82 */
83 protected $post;
84
85 /**
86 * @since 2.1.0
87 * @access protected
88 * @static
89 */
90 protected static function get_editor_panel_categories() {
91 return Plugin::$instance->elements_manager->get_categories();
92 }
93
94 /**
95 * Get properties.
96 *
97 * Retrieve the document properties.
98 *
99 * @since 2.0.0
100 * @access public
101 * @static
102 *
103 * @return array Document properties.
104 */
105 public static function get_properties() {
106 return [
107 'has_elements' => true,
108 'is_editable' => true,
109 'edit_capability' => '',
110 'show_in_finder' => true,
111 'show_on_admin_bar' => true,
112 'support_kit' => false,
113 ];
114 }
115
116 /**
117 * @since 2.1.0
118 * @access public
119 * @static
120 */
121 public static function get_editor_panel_config() {
122 $default_route = 'panel/elements/categories';
123
124 if ( ! Plugin::instance()->role_manager->user_can( 'design' ) ) {
125 $default_route = 'panel/page-settings/settings';
126 }
127
128 return [
129 'title' => static::get_title(), // JS Container title.
130 'widgets_settings' => [],
131 'elements_categories' => static::get_editor_panel_categories(),
132 'default_route' => $default_route,
133 'has_elements' => static::get_property( 'has_elements' ),
134 'support_kit' => static::get_property( 'support_kit' ),
135 'messages' => [
136 /* translators: %s: the document title. */
137 'publish_notification' => sprintf( __( 'Hurray! Your %s is live.', 'elementor' ), static::get_title() ),
138 ],
139 ];
140 }
141
142 /**
143 * Get element title.
144 *
145 * Retrieve the element title.
146 *
147 * @since 2.0.0
148 * @access public
149 * @static
150 *
151 * @return string Element title.
152 */
153 public static function get_title() {
154 return __( 'Document', 'elementor' );
155 }
156
157 /**
158 * Get property.
159 *
160 * Retrieve the document property.
161 *
162 * @since 2.0.0
163 * @access public
164 * @static
165 *
166 * @param string $key The property key.
167 *
168 * @return mixed The property value.
169 */
170 public static function get_property( $key ) {
171 $id = static::get_class_full_name();
172
173 if ( ! isset( self::$properties[ $id ] ) ) {
174 self::$properties[ $id ] = static::get_properties();
175 }
176
177 return self::get_items( self::$properties[ $id ], $key );
178 }
179
180 /**
181 * @since 2.0.0
182 * @access public
183 * @static
184 */
185 public static function get_class_full_name() {
186 return get_called_class();
187 }
188
189 /**
190 * @since 2.0.0
191 * @access public
192 */
193 public function get_unique_name() {
194 return $this->get_name() . '-' . $this->post->ID;
195 }
196
197 /**
198 * @since 2.3.0
199 * @access public
200 */
201 public function get_post_type_title() {
202 $post_type_object = get_post_type_object( $this->post->post_type );
203
204 return $post_type_object->labels->singular_name;
205 }
206
207 /**
208 * @since 2.0.12
209 * @deprecated 2.4.0 Use `Document::get_remote_library_config()` instead
210 * @access public
211 */
212 public function get_remote_library_type() {
213 _deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_remote_library_config()' );
214 }
215
216 /**
217 * @since 2.0.0
218 * @access public
219 */
220 public function get_main_id() {
221 if ( ! $this->main_id ) {
222 $post_id = $this->post->ID;
223
224 $parent_post_id = wp_is_post_revision( $post_id );
225
226 if ( $parent_post_id ) {
227 $post_id = $parent_post_id;
228 }
229
230 $this->main_id = $post_id;
231 }
232
233 return $this->main_id;
234 }
235
236 /**
237 * @since 2.0.0
238 * @access public
239 *
240 * @param $data
241 *
242 * @throws \Exception If the widget was not found.
243 *
244 * @return string
245 */
246 public function render_element( $data ) {
247 // Start buffering
248 ob_start();
249
250 /** @var Widget_Base $widget */
251 $widget = Plugin::$instance->elements_manager->create_element_instance( $data );
252
253 if ( ! $widget ) {
254 throw new \Exception( 'Widget not found.' );
255 }
256
257 $widget->render_content();
258
259 $render_html = ob_get_clean();
260
261 return $render_html;
262 }
263
264 /**
265 * @since 2.0.0
266 * @access public
267 */
268 public function get_main_post() {
269 return get_post( $this->get_main_id() );
270 }
271
272 /**
273 * @since 2.0.6
274 * @deprecated 2.4.0 Use `Document::get_container_attributes()` instead
275 * @access public
276 */
277 public function get_container_classes() {
278 _deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_container_attributes()' );
279
280 return '';
281 }
282
283 public function get_container_attributes() {
284 $id = $this->get_main_id();
285
286 $attributes = [
287 'data-elementor-type' => $this->get_name(),
288 'data-elementor-id' => $id,
289 'class' => 'elementor elementor-' . $id,
290 ];
291
292 $version_meta = $this->get_main_meta( '_elementor_version' );
293
294 if ( version_compare( $version_meta, '2.5.0', '<' ) ) {
295 $attributes['class'] .= ' elementor-bc-flex-widget';
296 }
297
298 if ( Plugin::$instance->preview->is_preview() ) {
299 $attributes['data-elementor-title'] = static::get_title();
300 } else {
301 $attributes['data-elementor-settings'] = wp_json_encode( $this->get_frontend_settings() );
302 }
303
304 return $attributes;
305 }
306
307 /**
308 * @since 2.0.0
309 * @access public
310 */
311 public function get_wp_preview_url() {
312 $main_post_id = $this->get_main_id();
313 $document = $this;
314
315 // Ajax request from editor.
316 if ( ! empty( $_POST['initial_document_id'] ) ) {
317 $document = Plugin::$instance->documents->get( $_POST['initial_document_id'] );
318 }
319
320 $url = get_preview_post_link(
321 $document->get_main_id(),
322 [
323 'preview_id' => $main_post_id,
324 'preview_nonce' => wp_create_nonce( 'post_preview_' . $main_post_id ),
325 ]
326 );
327
328 /**
329 * Document "WordPress preview" URL.
330 *
331 * Filters the WordPress preview URL.
332 *
333 * @since 2.0.0
334 *
335 * @param string $url WordPress preview URL.
336 * @param Document $this The document instance.
337 */
338 $url = apply_filters( 'elementor/document/urls/wp_preview', $url, $this );
339
340 return $url;
341 }
342
343 /**
344 * @since 2.0.0
345 * @access public
346 */
347 public function get_exit_to_dashboard_url() {
348 $url = get_edit_post_link( $this->get_main_id(), 'raw' );
349
350 /**
351 * Document "exit to dashboard" URL.
352 *
353 * Filters the "Exit To Dashboard" URL.
354 *
355 * @since 2.0.0
356 *
357 * @param string $url The exit URL
358 * @param Document $this The document instance.
359 */
360 $url = apply_filters( 'elementor/document/urls/exit_to_dashboard', $url, $this );
361
362 return $url;
363 }
364
365 /**
366 * Get auto-saved post revision.
367 *
368 * Retrieve the auto-saved post revision that is newer than current post.
369 *
370 * @since 2.0.0
371 * @access public
372 *
373 *
374 * @return bool|Document
375 */
376
377 public function get_newer_autosave() {
378 $autosave = $this->get_autosave();
379
380 // Detect if there exists an autosave newer than the post.
381 if ( $autosave && mysql2date( 'U', $autosave->get_post()->post_modified_gmt, false ) > mysql2date( 'U', $this->post->post_modified_gmt, false ) ) {
382 return $autosave;
383 }
384
385 return false;
386 }
387
388 /**
389 * @since 2.0.0
390 * @access public
391 */
392 public function is_autosave() {
393 return wp_is_post_autosave( $this->post->ID );
394 }
395
396 /**
397 * Check if the current document is a 'revision'
398 *
399 * @return bool
400 */
401 public function is_revision() {
402 return 'revision' === $this->post->post_type;
403 }
404
405 /**
406 * Checks if the current document status is 'trash'.
407 *
408 * @return bool
409 */
410 public function is_trash() {
411 return 'trash' === $this->post->post_status;
412 }
413
414 /**
415 * @since 2.0.0
416 * @access public
417 *
418 * @param int $user_id
419 * @param bool $create
420 *
421 * @return bool|Document
422 */
423 public function get_autosave( $user_id = 0, $create = false ) {
424 if ( ! $user_id ) {
425 $user_id = get_current_user_id();
426 }
427
428 $autosave_id = $this->get_autosave_id( $user_id );
429
430 if ( $autosave_id ) {
431 $document = Plugin::$instance->documents->get( $autosave_id );
432 } elseif ( $create ) {
433 $autosave_id = wp_create_post_autosave( [
434 'post_ID' => $this->post->ID,
435 'post_type' => $this->post->post_type,
436 'post_title' => $this->post->post_title,
437 'post_excerpt' => $this->post->post_excerpt,
438 // Hack to cause $autosave_is_different=true in `wp_create_post_autosave`.
439 'post_content' => '<!-- Created With Elementor -->',
440 'post_modified' => current_time( 'mysql' ),
441 ] );
442
443 Plugin::$instance->db->copy_elementor_meta( $this->post->ID, $autosave_id );
444
445 $document = Plugin::$instance->documents->get( $autosave_id );
446 $document->save_template_type();
447 } else {
448 $document = false;
449 }
450
451 return $document;
452 }
453
454 /**
455 * Add/Remove edit link in dashboard.
456 *
457 * Add or remove an edit link to the post/page action links on the post/pages list table.
458 *
459 * Fired by `post_row_actions` and `page_row_actions` filters.
460 *
461 * @access public
462 *
463 * @param array $actions An array of row action links.
464 *
465 * @return array An updated array of row action links.
466 */
467 public function filter_admin_row_actions( $actions ) {
468 if ( $this->is_built_with_elementor() && $this->is_editable_by_current_user() ) {
469 $actions['edit_with_elementor'] = sprintf(
470 '<a href="%1$s">%2$s</a>',
471 $this->get_edit_url(),
472 __( 'Edit with Elementor', 'elementor' )
473 );
474 }
475
476 return $actions;
477 }
478
479 /**
480 * @since 2.0.0
481 * @access public
482 */
483 public function is_editable_by_current_user() {
484 $edit_capability = static::get_property( 'edit_capability' );
485 if ( $edit_capability && ! current_user_can( $edit_capability ) ) {
486 return false;
487 }
488
489 return self::get_property( 'is_editable' ) && User::is_current_user_can_edit( $this->get_main_id() );
490 }
491
492 /**
493 * @since 2.9.0
494 * @access protected
495 */
496 protected function get_initial_config() {
497 // 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.
498
499 $locked_user = Plugin::$instance->editor->get_locked_user( $this->get_main_id() );
500
501 if ( $locked_user ) {
502 $locked_user = $locked_user->display_name;
503 }
504
505 $post_type_object = get_post_type_object( $this->get_main_post()->post_type );
506
507 $settings = SettingsManager::get_settings_managers_config();
508
509 $config = [
510 'id' => $this->get_main_id(),
511 'type' => $this->get_name(),
512 'version' => $this->get_main_meta( '_elementor_version' ),
513 'settings' => $settings['page'],
514 'remoteLibrary' => $this->get_remote_library_config(),
515 'last_edited' => $this->get_last_edited(),
516 'panel' => static::get_editor_panel_config(),
517 'container' => 'body',
518 'post_type_title' => $this->get_post_type_title(),
519 'user' => [
520 'can_publish' => current_user_can( $post_type_object->cap->publish_posts ),
521
522 // Deprecated config since 2.9.0.
523 'locked' => $locked_user,
524 ],
525 'urls' => [
526 'exit_to_dashboard' => $this->get_exit_to_dashboard_url(),
527 'preview' => $this->get_preview_url(),
528 'wp_preview' => $this->get_wp_preview_url(),
529 'permalink' => $this->get_permalink(),
530 'have_a_look' => $this->get_have_a_look_url(),
531 ],
532 ];
533
534 if ( static::get_property( 'has_elements' ) ) {
535 $config['elements'] = $this->get_elements_raw_data( null, true );
536 $config['widgets'] = Plugin::$instance->widgets_manager->get_widget_types_config();
537 }
538
539 $additional_config = apply_filters( 'elementor/document/config', [], $this->get_main_id() );
540
541 if ( ! empty( $additional_config ) ) {
542 $config = array_replace_recursive( $config, $additional_config );
543 }
544
545 return $config;
546 }
547
548 /**
549 * @since 3.1.0
550 * @access protected
551 */
552 protected function register_controls() {
553 $this->register_document_controls();
554 /**
555 * Register document controls.
556 *
557 * Fires after Elementor registers the document controls.
558 *
559 * @since 2.0.0
560 *
561 * @param Document $this The document instance.
562 */
563 do_action( 'elementor/documents/register_controls', $this );
564 }
565
566 /**
567 * @since 2.0.0
568 * @access public
569 *
570 * @param $data
571 *
572 * @return bool
573 */
574 public function save( $data ) {
575 $this->add_handle_revisions_changed_filter();
576
577 if ( ! $this->is_editable_by_current_user() ) {
578 return false;
579 }
580
581 $this->set_is_saving( true );
582
583 /**
584 * Before document save.
585 *
586 * Fires when document save starts on Elementor.
587 *
588 * @since 2.5.12
589 *
590 * @param \Elementor\Core\Base\Document $this The current document.
591 * @param $data.
592 */
593 do_action( 'elementor/document/before_save', $this, $data );
594
595 if ( ! current_user_can( 'unfiltered_html' ) ) {
596 $data = wp_kses_post_deep( $data );
597 }
598
599 if ( ! empty( $data['settings'] ) ) {
600 if ( isset( $data['settings']['post_status'] ) && self::STATUS_AUTOSAVE === $data['settings']['post_status'] ) {
601 if ( ! defined( 'DOING_AUTOSAVE' ) ) {
602 define( 'DOING_AUTOSAVE', true );
603 }
604 }
605
606 $this->save_settings( $data['settings'] );
607
608 // Refresh post after save settings.
609 $this->post = get_post( $this->post->ID );
610 }
611
612 // Don't check is_empty, because an empty array should be saved.
613 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
614 $this->save_elements( $data['elements'] );
615 }
616
617 $this->save_template_type();
618
619 $this->save_version();
620
621 // Remove Post CSS
622 $post_css = Post_CSS::create( $this->post->ID );
623
624 $post_css->delete();
625
626 /**
627 * After document save.
628 *
629 * Fires when document save is complete.
630 *
631 * @since 2.5.12
632 *
633 * @param \Elementor\Core\Base\Document $this The current document.
634 * @param $data.
635 */
636 do_action( 'elementor/document/after_save', $this, $data );
637
638 $this->set_is_saving( false );
639
640 $this->remove_handle_revisions_changed_filter();
641
642 return true;
643 }
644
645 /**
646 * @param array $new_settings
647 *
648 * @return static
649 */
650 public function update_settings( array $new_settings ) {
651 $document_settings = $this->get_meta( PageManager::META_KEY );
652
653 if ( ! $document_settings ) {
654 $document_settings = [];
655 }
656
657 $this->save_settings(
658 array_replace_recursive( $document_settings, $new_settings )
659 );
660
661 return $this;
662 }
663
664 /**
665 * Is built with Elementor.
666 *
667 * Check whether the post was built with Elementor.
668 *
669 * @since 2.0.0
670 * @access public
671 *
672 * @return bool Whether the post was built with Elementor.
673 */
674 public function is_built_with_elementor() {
675 return ! ! $this->get_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
676 }
677
678 /**
679 * Mark the post as "built with elementor" or not.
680 *
681 * @param bool $is_built_with_elementor
682 *
683 * @return $this
684 */
685 public function set_is_built_with_elementor( $is_built_with_elementor ) {
686 if ( $is_built_with_elementor ) {
687 // Use the string `builder` and not a boolean for rollback compatibility
688 $this->update_meta( self::BUILT_WITH_ELEMENTOR_META_KEY, 'builder' );
689 } else {
690 $this->delete_meta( self::BUILT_WITH_ELEMENTOR_META_KEY );
691 }
692
693 return $this;
694 }
695
696 /**
697 * @since 2.0.0
698 * @access public
699 * @static
700 *
701 * @return mixed
702 */
703 public function get_edit_url() {
704 $url = add_query_arg(
705 [
706 'post' => $this->get_main_id(),
707 'action' => 'elementor',
708 ],
709 admin_url( 'post.php' )
710 );
711
712 /**
713 * Document edit url.
714 *
715 * Filters the document edit url.
716 *
717 * @since 2.0.0
718 *
719 * @param string $url The edit url.
720 * @param Document $this The document instance.
721 */
722 $url = apply_filters( 'elementor/document/urls/edit', $url, $this );
723
724 return $url;
725 }
726
727 /**
728 * @since 2.0.0
729 * @access public
730 */
731 public function get_preview_url() {
732 /**
733 * Use a static var - to avoid change the `ver` parameter on every call.
734 */
735 static $url;
736
737 if ( empty( $url ) ) {
738
739 add_filter( 'pre_option_permalink_structure', '__return_empty_string' );
740
741 $url = set_url_scheme( add_query_arg( [
742 'elementor-preview' => $this->get_main_id(),
743 'ver' => time(),
744 ], $this->get_permalink() ) );
745
746 remove_filter( 'pre_option_permalink_structure', '__return_empty_string' );
747
748 /**
749 * Document preview URL.
750 *
751 * Filters the document preview URL.
752 *
753 * @since 2.0.0
754 *
755 * @param string $url The preview URL.
756 * @param Document $this The document instance.
757 */
758 $url = apply_filters( 'elementor/document/urls/preview', $url, $this );
759 }
760
761 return $url;
762 }
763
764 /**
765 * @since 2.0.0
766 * @access public
767 *
768 * @param string $key
769 *
770 * @return array
771 */
772 public function get_json_meta( $key ) {
773 $meta = get_post_meta( $this->post->ID, $key, true );
774
775 if ( is_string( $meta ) && ! empty( $meta ) ) {
776 $meta = json_decode( $meta, true );
777 }
778
779 if ( empty( $meta ) ) {
780 $meta = [];
781 }
782
783 return $meta;
784 }
785
786 /**
787 * @since 2.0.0
788 * @access public
789 *
790 * @param null $data
791 * @param bool $with_html_content
792 *
793 * @return array
794 */
795 public function get_elements_raw_data( $data = null, $with_html_content = false ) {
796 if ( ! static::get_property( 'has_elements' ) ) {
797 return [];
798 }
799
800 if ( is_null( $data ) ) {
801 $data = $this->get_elements_data();
802 }
803
804 // Change the current documents, so widgets can use `documents->get_current` and other post data
805 Plugin::$instance->documents->switch_to_document( $this );
806
807 $editor_data = [];
808
809 foreach ( $data as $element_data ) {
810 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
811
812 if ( ! $element ) {
813 continue;
814 }
815
816 $editor_data[] = $element->get_raw_data( $with_html_content );
817 } // End foreach().
818
819 Plugin::$instance->documents->restore_document();
820
821 return $editor_data;
822 }
823
824 /**
825 * @since 2.0.0
826 * @access public
827 *
828 * @param string $status
829 *
830 * @return array
831 */
832 public function get_elements_data( $status = self::STATUS_PUBLISH ) {
833 $elements = $this->get_json_meta( '_elementor_data' );
834
835 if ( self::STATUS_DRAFT === $status ) {
836 $autosave = $this->get_newer_autosave();
837
838 if ( is_object( $autosave ) ) {
839 $autosave_elements = Plugin::$instance->documents
840 ->get( $autosave->get_post()->ID )
841 ->get_json_meta( '_elementor_data' );
842 }
843 }
844
845 if ( Plugin::$instance->editor->is_edit_mode() ) {
846 if ( empty( $elements ) && empty( $autosave_elements ) ) {
847 // Convert to Elementor.
848 $elements = $this->convert_to_elementor();
849 if ( $this->is_autosave() ) {
850 Plugin::$instance->db->copy_elementor_meta( $this->post->post_parent, $this->post->ID );
851 }
852 }
853 }
854
855 if ( ! empty( $autosave_elements ) ) {
856 $elements = $autosave_elements;
857 }
858
859 return $elements;
860 }
861
862 /**
863 * Get document setting from DB.
864 *
865 * @return array
866 */
867 public function get_db_document_settings() {
868 return $this->get_meta( static::PAGE_META_KEY );
869 }
870
871 /**
872 * @since 2.3.0
873 * @access public
874 */
875 public function convert_to_elementor() {
876 $this->save( [] );
877
878 if ( empty( $this->post->post_content ) ) {
879 return [];
880 }
881
882 // Check if it's only a shortcode.
883 preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER );
884 if ( ! empty( $matches ) ) {
885 foreach ( $matches as $shortcode ) {
886 if ( trim( $this->post->post_content ) === $shortcode[0] ) {
887 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' );
888 $settings = [
889 'shortcode' => $this->post->post_content,
890 ];
891 break;
892 }
893 }
894 }
895
896 if ( empty( $widget_type ) ) {
897 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' );
898 $settings = [
899 'editor' => $this->post->post_content,
900 ];
901 }
902
903 // TODO: Better coding to start template for editor
904 return [
905 [
906 'id' => Utils::generate_random_string(),
907 'elType' => 'section',
908 'elements' => [
909 [
910 'id' => Utils::generate_random_string(),
911 'elType' => 'column',
912 'elements' => [
913 [
914 'id' => Utils::generate_random_string(),
915 'elType' => $widget_type::get_type(),
916 'widgetType' => $widget_type->get_name(),
917 'settings' => $settings,
918 ],
919 ],
920 ],
921 ],
922 ],
923 ];
924 }
925
926 /**
927 * @since 2.1.3
928 * @access public
929 */
930 public function print_elements_with_wrapper( $elements_data = null ) {
931 if ( ! $elements_data ) {
932 $elements_data = $this->get_elements_data();
933 }
934
935 $is_dom_optimization_active = Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' );
936 ?>
937 <div <?php echo Utils::render_html_attributes( $this->get_container_attributes() ); ?>>
938 <?php if ( ! $is_dom_optimization_active ) { ?>
939 <div class="elementor-inner">
940 <?php } ?>
941 <div class="elementor-section-wrap">
942 <?php $this->print_elements( $elements_data ); ?>
943 </div>
944 <?php if ( ! $is_dom_optimization_active ) { ?>
945 </div>
946 <?php } ?>
947 </div>
948 <?php
949 }
950
951 /**
952 * @since 2.0.0
953 * @access public
954 */
955 public function get_css_wrapper_selector() {
956 return '';
957 }
958
959 /**
960 * @since 2.0.0
961 * @access public
962 */
963 public function get_panel_page_settings() {
964 return [
965 /* translators: %s: Document title */
966 'title' => sprintf( __( '%s Settings', 'elementor' ), static::get_title() ),
967 ];
968 }
969
970 /**
971 * @since 2.0.0
972 * @access public
973 */
974 public function get_post() {
975 return $this->post;
976 }
977
978 /**
979 * @since 2.0.0
980 * @access public
981 */
982 public function get_permalink() {
983 return get_permalink( $this->get_main_id() );
984 }
985
986 /**
987 * @since 2.0.8
988 * @access public
989 */
990 public function get_content( $with_css = false ) {
991 return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css );
992 }
993
994 /**
995 * @since 2.0.0
996 * @access public
997 */
998 public function delete() {
999 if ( 'revision' === $this->post->post_type ) {
1000 $deleted = wp_delete_post_revision( $this->post );
1001 } else {
1002 $deleted = wp_delete_post( $this->post->ID );
1003 }
1004
1005 return $deleted && ! is_wp_error( $deleted );
1006 }
1007
1008 /**
1009 * Save editor elements.
1010 *
1011 * Save data from the editor to the database.
1012 *
1013 * @since 2.0.0
1014 * @access protected
1015 *
1016 * @param array $elements
1017 */
1018 protected function save_elements( $elements ) {
1019 $editor_data = $this->get_elements_raw_data( $elements );
1020
1021 // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1022 $json_value = wp_slash( wp_json_encode( $editor_data ) );
1023
1024 // Don't use `update_post_meta` that can't handle `revision` post type
1025 $is_meta_updated = update_metadata( 'post', $this->post->ID, '_elementor_data', $json_value );
1026
1027 /**
1028 * Before saving data.
1029 *
1030 * Fires before Elementor saves data to the database.
1031 *
1032 * @since 1.0.0
1033 *
1034 * @param string $status Post status.
1035 * @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure.
1036 */
1037 do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated );
1038
1039 Plugin::$instance->db->save_plain_text( $this->post->ID );
1040
1041 /**
1042 * After saving data.
1043 *
1044 * Fires after Elementor saves data to the database.
1045 *
1046 * @since 1.0.0
1047 *
1048 * @param int $post_id The ID of the post.
1049 * @param array $editor_data Sanitize posted data.
1050 */
1051 do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data );
1052 }
1053
1054 /**
1055 * @since 2.0.0
1056 * @access public
1057 *
1058 * @param int $user_id Optional. User ID. Default value is `0`.
1059 *
1060 * @return bool|int
1061 */
1062 public function get_autosave_id( $user_id = 0 ) {
1063 if ( ! $user_id ) {
1064 $user_id = get_current_user_id();
1065 }
1066
1067 $autosave = Utils::get_post_autosave( $this->post->ID, $user_id );
1068 if ( $autosave ) {
1069 return $autosave->ID;
1070 }
1071
1072 return false;
1073 }
1074
1075 public function save_version() {
1076 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
1077 // Save per revision.
1078 $this->update_meta( '_elementor_version', ELEMENTOR_VERSION );
1079
1080 /**
1081 * Document version save.
1082 *
1083 * Fires when document version is saved on Elementor.
1084 * Will not fire during Elementor Upgrade.
1085 *
1086 * @since 2.5.12
1087 *
1088 * @param \Elementor\Core\Base\Document $this The current document.
1089 *
1090 */
1091 do_action( 'elementor/document/save_version', $this );
1092 }
1093 }
1094
1095 /**
1096 * @since 2.3.0
1097 * @access public
1098 */
1099 public function save_template_type() {
1100 return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() );
1101 }
1102
1103 /**
1104 * @since 2.3.0
1105 * @access public
1106 */
1107 public function get_template_type() {
1108 return $this->get_main_meta( self::TYPE_META_KEY );
1109 }
1110
1111 /**
1112 * @since 2.0.0
1113 * @access public
1114 *
1115 * @param string $key Meta data key.
1116 *
1117 * @return mixed
1118 */
1119 public function get_main_meta( $key ) {
1120 return get_post_meta( $this->get_main_id(), $key, true );
1121 }
1122
1123 /**
1124 * @since 2.0.4
1125 * @access public
1126 *
1127 * @param string $key Meta data key.
1128 * @param string $value Meta data value.
1129 *
1130 * @return bool|int
1131 */
1132 public function update_main_meta( $key, $value ) {
1133 return update_post_meta( $this->get_main_id(), $key, $value );
1134 }
1135
1136 /**
1137 * @since 2.0.4
1138 * @access public
1139 *
1140 * @param string $key Meta data key.
1141 * @param string $value Optional. Meta data value. Default is an empty string.
1142 *
1143 * @return bool
1144 */
1145 public function delete_main_meta( $key, $value = '' ) {
1146 return delete_post_meta( $this->get_main_id(), $key, $value );
1147 }
1148
1149 /**
1150 * @since 2.0.0
1151 * @access public
1152 *
1153 * @param string $key Meta data key.
1154 *
1155 * @return mixed
1156 */
1157 public function get_meta( $key ) {
1158 return get_post_meta( $this->post->ID, $key, true );
1159 }
1160
1161 /**
1162 * @since 2.0.0
1163 * @access public
1164 *
1165 * @param string $key Meta data key.
1166 * @param mixed $value Meta data value.
1167 *
1168 * @return bool|int
1169 */
1170 public function update_meta( $key, $value ) {
1171 // Use `update_metadata` in order to work also with revisions.
1172 return update_metadata( 'post', $this->post->ID, $key, $value );
1173 }
1174
1175 /**
1176 * @since 2.0.3
1177 * @access public
1178 *
1179 * @param string $key Meta data key.
1180 * @param string $value Meta data value.
1181 *
1182 * @return bool
1183 */
1184 public function delete_meta( $key, $value = '' ) {
1185 // Use `delete_metadata` in order to work also with revisions.
1186 return delete_metadata( 'post', $this->post->ID, $key, $value );
1187 }
1188
1189 /**
1190 * @since 2.0.0
1191 * @access public
1192 */
1193 public function get_last_edited() {
1194 $post = $this->post;
1195 $autosave_post = $this->get_autosave();
1196
1197 if ( $autosave_post ) {
1198 $post = $autosave_post->get_post();
1199 }
1200
1201 $date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) );
1202 $display_name = get_the_author_meta( 'display_name', $post->post_author );
1203
1204 if ( $autosave_post || 'revision' === $post->post_type ) {
1205 /* translators: 1: Saving date, 2: Author display name */
1206 $last_edited = sprintf( __( 'Draft saved on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1207 } else {
1208 /* translators: 1: Editing date, 2: Author display name */
1209 $last_edited = sprintf( __( 'Last edited on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1210 }
1211
1212 return $last_edited;
1213 }
1214
1215
1216 /**
1217 * @return bool
1218 */
1219 public function is_saving() {
1220 return $this->is_saving;
1221 }
1222
1223 /**
1224 * @param $is_saving
1225 *
1226 * @return $this
1227 */
1228 public function set_is_saving( $is_saving ) {
1229 $this->is_saving = $is_saving;
1230
1231 return $this;
1232 }
1233
1234 /**
1235 * @since 2.0.0
1236 * @access public
1237 *
1238 * @param array $data
1239 *
1240 * @throws \Exception If the post does not exist.
1241 */
1242 public function __construct( array $data = [] ) {
1243 if ( $data ) {
1244 if ( empty( $data['post_id'] ) ) {
1245 $this->post = new \WP_Post( (object) [] );
1246 } else {
1247 $this->post = get_post( $data['post_id'] );
1248
1249 if ( ! $this->post ) {
1250 throw new \Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ), Exceptions::NOT_FOUND );
1251 }
1252 }
1253
1254 // Each Control_Stack is based on a unique ID.
1255 $data['id'] = $data['post_id'];
1256
1257 if ( ! isset( $data['settings'] ) ) {
1258 $data['settings'] = [];
1259 }
1260
1261 $saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true );
1262 if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) {
1263 $data['settings'] += $saved_settings;
1264 }
1265 }
1266
1267 parent::__construct( $data );
1268 }
1269
1270 protected function get_remote_library_config() {
1271 $config = [
1272 'type' => 'block',
1273 'default_route' => 'templates/blocks',
1274 'category' => $this->get_name(),
1275 'autoImportSettings' => false,
1276 ];
1277
1278 return $config;
1279 }
1280
1281 /**
1282 * @since 2.0.4
1283 * @access protected
1284 *
1285 * @param $settings
1286 */
1287 protected function save_settings( $settings ) {
1288 $page_settings_manager = SettingsManager::get_settings_managers( 'page' );
1289 $page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID );
1290 $page_settings_manager->save_settings( $settings, $this->post->ID );
1291 }
1292
1293 /**
1294 * @since 2.1.3
1295 * @access protected
1296 */
1297 protected function print_elements( $elements_data ) {
1298 foreach ( $elements_data as $element_data ) {
1299 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1300
1301 if ( ! $element ) {
1302 continue;
1303 }
1304
1305 $element->print_element();
1306 }
1307 }
1308
1309 protected function register_document_controls() {
1310 $this->start_controls_section(
1311 'document_settings',
1312 [
1313 'label' => __( 'General Settings', 'elementor' ),
1314 'tab' => Controls_Manager::TAB_SETTINGS,
1315 ]
1316 );
1317
1318 $this->add_control(
1319 'post_title',
1320 [
1321 'label' => __( 'Title', 'elementor' ),
1322 'type' => Controls_Manager::TEXT,
1323 'default' => $this->post->post_title,
1324 'label_block' => true,
1325 'separator' => 'none',
1326 ]
1327 );
1328
1329 $post_type_object = get_post_type_object( $this->post->post_type );
1330
1331 $can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts );
1332 $is_published = self::STATUS_PUBLISH === $this->post->post_status || self::STATUS_PRIVATE === $this->post->post_status;
1333
1334 if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) {
1335
1336 $statuses = $this->get_post_statuses();
1337 if ( 'future' === $this->get_main_post()->post_status ) {
1338 $statuses['future'] = __( 'Future', 'elementor' );
1339 }
1340
1341 $this->add_control(
1342 'post_status',
1343 [
1344 'label' => __( 'Status', 'elementor' ),
1345 'type' => Controls_Manager::SELECT,
1346 'default' => $this->get_main_post()->post_status,
1347 'options' => $statuses,
1348 ]
1349 );
1350 }
1351
1352 $this->end_controls_section();
1353 }
1354
1355 protected function get_post_statuses() {
1356 return get_post_statuses();
1357 }
1358
1359 protected function get_have_a_look_url() {
1360 return $this->get_permalink();
1361 }
1362
1363 public function handle_revisions_changed( $post_has_changed, $last_revision, $post ) {
1364 // In case default, didn't determine the changes.
1365 if ( ! $post_has_changed ) {
1366 $last_revision_id = $last_revision->ID;
1367 $last_revision_document = Plugin::instance()->documents->get( $last_revision_id );
1368 $post_document = Plugin::instance()->documents->get( $post->ID );
1369
1370 $last_revision_settings = $last_revision_document->get_settings();
1371 $post_settings = $post_document->get_settings();
1372
1373 // TODO: Its better to add crc32 signature for each revision and then only compare one part of the checksum.
1374 $post_has_changed = $last_revision_settings !== $post_settings;
1375 }
1376
1377 return $post_has_changed;
1378 }
1379
1380 private function add_handle_revisions_changed_filter() {
1381 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ], 10, 3 );
1382 }
1383
1384 private function remove_handle_revisions_changed_filter() {
1385 remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] );
1386 }
1387 }
1388