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

1,379 lines 31.5 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 * @since 2.3.0
864 * @access public
865 */
866 public function convert_to_elementor() {
867 $this->save( [] );
868
869 if ( empty( $this->post->post_content ) ) {
870 return [];
871 }
872
873 // Check if it's only a shortcode.
874 preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER );
875 if ( ! empty( $matches ) ) {
876 foreach ( $matches as $shortcode ) {
877 if ( trim( $this->post->post_content ) === $shortcode[0] ) {
878 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' );
879 $settings = [
880 'shortcode' => $this->post->post_content,
881 ];
882 break;
883 }
884 }
885 }
886
887 if ( empty( $widget_type ) ) {
888 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' );
889 $settings = [
890 'editor' => $this->post->post_content,
891 ];
892 }
893
894 // TODO: Better coding to start template for editor
895 return [
896 [
897 'id' => Utils::generate_random_string(),
898 'elType' => 'section',
899 'elements' => [
900 [
901 'id' => Utils::generate_random_string(),
902 'elType' => 'column',
903 'elements' => [
904 [
905 'id' => Utils::generate_random_string(),
906 'elType' => $widget_type::get_type(),
907 'widgetType' => $widget_type->get_name(),
908 'settings' => $settings,
909 ],
910 ],
911 ],
912 ],
913 ],
914 ];
915 }
916
917 /**
918 * @since 2.1.3
919 * @access public
920 */
921 public function print_elements_with_wrapper( $elements_data = null ) {
922 if ( ! $elements_data ) {
923 $elements_data = $this->get_elements_data();
924 }
925
926 $is_dom_optimization_active = Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' );
927 ?>
928 <div <?php echo Utils::render_html_attributes( $this->get_container_attributes() ); ?>>
929 <?php if ( ! $is_dom_optimization_active ) { ?>
930 <div class="elementor-inner">
931 <?php } ?>
932 <div class="elementor-section-wrap">
933 <?php $this->print_elements( $elements_data ); ?>
934 </div>
935 <?php if ( ! $is_dom_optimization_active ) { ?>
936 </div>
937 <?php } ?>
938 </div>
939 <?php
940 }
941
942 /**
943 * @since 2.0.0
944 * @access public
945 */
946 public function get_css_wrapper_selector() {
947 return '';
948 }
949
950 /**
951 * @since 2.0.0
952 * @access public
953 */
954 public function get_panel_page_settings() {
955 return [
956 /* translators: %s: Document title */
957 'title' => sprintf( __( '%s Settings', 'elementor' ), static::get_title() ),
958 ];
959 }
960
961 /**
962 * @since 2.0.0
963 * @access public
964 */
965 public function get_post() {
966 return $this->post;
967 }
968
969 /**
970 * @since 2.0.0
971 * @access public
972 */
973 public function get_permalink() {
974 return get_permalink( $this->get_main_id() );
975 }
976
977 /**
978 * @since 2.0.8
979 * @access public
980 */
981 public function get_content( $with_css = false ) {
982 return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css );
983 }
984
985 /**
986 * @since 2.0.0
987 * @access public
988 */
989 public function delete() {
990 if ( 'revision' === $this->post->post_type ) {
991 $deleted = wp_delete_post_revision( $this->post );
992 } else {
993 $deleted = wp_delete_post( $this->post->ID );
994 }
995
996 return $deleted && ! is_wp_error( $deleted );
997 }
998
999 /**
1000 * Save editor elements.
1001 *
1002 * Save data from the editor to the database.
1003 *
1004 * @since 2.0.0
1005 * @access protected
1006 *
1007 * @param array $elements
1008 */
1009 protected function save_elements( $elements ) {
1010 $editor_data = $this->get_elements_raw_data( $elements );
1011
1012 // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta`
1013 $json_value = wp_slash( wp_json_encode( $editor_data ) );
1014
1015 // Don't use `update_post_meta` that can't handle `revision` post type
1016 $is_meta_updated = update_metadata( 'post', $this->post->ID, '_elementor_data', $json_value );
1017
1018 /**
1019 * Before saving data.
1020 *
1021 * Fires before Elementor saves data to the database.
1022 *
1023 * @since 1.0.0
1024 *
1025 * @param string $status Post status.
1026 * @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure.
1027 */
1028 do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated );
1029
1030 Plugin::$instance->db->save_plain_text( $this->post->ID );
1031
1032 /**
1033 * After saving data.
1034 *
1035 * Fires after Elementor saves data to the database.
1036 *
1037 * @since 1.0.0
1038 *
1039 * @param int $post_id The ID of the post.
1040 * @param array $editor_data Sanitize posted data.
1041 */
1042 do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data );
1043 }
1044
1045 /**
1046 * @since 2.0.0
1047 * @access public
1048 *
1049 * @param int $user_id Optional. User ID. Default value is `0`.
1050 *
1051 * @return bool|int
1052 */
1053 public function get_autosave_id( $user_id = 0 ) {
1054 if ( ! $user_id ) {
1055 $user_id = get_current_user_id();
1056 }
1057
1058 $autosave = Utils::get_post_autosave( $this->post->ID, $user_id );
1059 if ( $autosave ) {
1060 return $autosave->ID;
1061 }
1062
1063 return false;
1064 }
1065
1066 public function save_version() {
1067 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
1068 // Save per revision.
1069 $this->update_meta( '_elementor_version', ELEMENTOR_VERSION );
1070
1071 /**
1072 * Document version save.
1073 *
1074 * Fires when document version is saved on Elementor.
1075 * Will not fire during Elementor Upgrade.
1076 *
1077 * @since 2.5.12
1078 *
1079 * @param \Elementor\Core\Base\Document $this The current document.
1080 *
1081 */
1082 do_action( 'elementor/document/save_version', $this );
1083 }
1084 }
1085
1086 /**
1087 * @since 2.3.0
1088 * @access public
1089 */
1090 public function save_template_type() {
1091 return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() );
1092 }
1093
1094 /**
1095 * @since 2.3.0
1096 * @access public
1097 */
1098 public function get_template_type() {
1099 return $this->get_main_meta( self::TYPE_META_KEY );
1100 }
1101
1102 /**
1103 * @since 2.0.0
1104 * @access public
1105 *
1106 * @param string $key Meta data key.
1107 *
1108 * @return mixed
1109 */
1110 public function get_main_meta( $key ) {
1111 return get_post_meta( $this->get_main_id(), $key, true );
1112 }
1113
1114 /**
1115 * @since 2.0.4
1116 * @access public
1117 *
1118 * @param string $key Meta data key.
1119 * @param string $value Meta data value.
1120 *
1121 * @return bool|int
1122 */
1123 public function update_main_meta( $key, $value ) {
1124 return update_post_meta( $this->get_main_id(), $key, $value );
1125 }
1126
1127 /**
1128 * @since 2.0.4
1129 * @access public
1130 *
1131 * @param string $key Meta data key.
1132 * @param string $value Optional. Meta data value. Default is an empty string.
1133 *
1134 * @return bool
1135 */
1136 public function delete_main_meta( $key, $value = '' ) {
1137 return delete_post_meta( $this->get_main_id(), $key, $value );
1138 }
1139
1140 /**
1141 * @since 2.0.0
1142 * @access public
1143 *
1144 * @param string $key Meta data key.
1145 *
1146 * @return mixed
1147 */
1148 public function get_meta( $key ) {
1149 return get_post_meta( $this->post->ID, $key, true );
1150 }
1151
1152 /**
1153 * @since 2.0.0
1154 * @access public
1155 *
1156 * @param string $key Meta data key.
1157 * @param mixed $value Meta data value.
1158 *
1159 * @return bool|int
1160 */
1161 public function update_meta( $key, $value ) {
1162 // Use `update_metadata` in order to work also with revisions.
1163 return update_metadata( 'post', $this->post->ID, $key, $value );
1164 }
1165
1166 /**
1167 * @since 2.0.3
1168 * @access public
1169 *
1170 * @param string $key Meta data key.
1171 * @param string $value Meta data value.
1172 *
1173 * @return bool
1174 */
1175 public function delete_meta( $key, $value = '' ) {
1176 // Use `delete_metadata` in order to work also with revisions.
1177 return delete_metadata( 'post', $this->post->ID, $key, $value );
1178 }
1179
1180 /**
1181 * @since 2.0.0
1182 * @access public
1183 */
1184 public function get_last_edited() {
1185 $post = $this->post;
1186 $autosave_post = $this->get_autosave();
1187
1188 if ( $autosave_post ) {
1189 $post = $autosave_post->get_post();
1190 }
1191
1192 $date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) );
1193 $display_name = get_the_author_meta( 'display_name', $post->post_author );
1194
1195 if ( $autosave_post || 'revision' === $post->post_type ) {
1196 /* translators: 1: Saving date, 2: Author display name */
1197 $last_edited = sprintf( __( 'Draft saved on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1198 } else {
1199 /* translators: 1: Editing date, 2: Author display name */
1200 $last_edited = sprintf( __( 'Last edited on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1201 }
1202
1203 return $last_edited;
1204 }
1205
1206
1207 /**
1208 * @return bool
1209 */
1210 public function is_saving() {
1211 return $this->is_saving;
1212 }
1213
1214 /**
1215 * @param $is_saving
1216 *
1217 * @return $this
1218 */
1219 public function set_is_saving( $is_saving ) {
1220 $this->is_saving = $is_saving;
1221
1222 return $this;
1223 }
1224
1225 /**
1226 * @since 2.0.0
1227 * @access public
1228 *
1229 * @param array $data
1230 *
1231 * @throws \Exception If the post does not exist.
1232 */
1233 public function __construct( array $data = [] ) {
1234 if ( $data ) {
1235 if ( empty( $data['post_id'] ) ) {
1236 $this->post = new \WP_Post( (object) [] );
1237 } else {
1238 $this->post = get_post( $data['post_id'] );
1239
1240 if ( ! $this->post ) {
1241 throw new \Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ), Exceptions::NOT_FOUND );
1242 }
1243 }
1244
1245 // Each Control_Stack is based on a unique ID.
1246 $data['id'] = $data['post_id'];
1247
1248 if ( ! isset( $data['settings'] ) ) {
1249 $data['settings'] = [];
1250 }
1251
1252 $saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true );
1253 if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) {
1254 $data['settings'] += $saved_settings;
1255 }
1256 }
1257
1258 parent::__construct( $data );
1259 }
1260
1261 protected function get_remote_library_config() {
1262 $config = [
1263 'type' => 'block',
1264 'default_route' => 'templates/blocks',
1265 'category' => $this->get_name(),
1266 'autoImportSettings' => false,
1267 ];
1268
1269 return $config;
1270 }
1271
1272 /**
1273 * @since 2.0.4
1274 * @access protected
1275 *
1276 * @param $settings
1277 */
1278 protected function save_settings( $settings ) {
1279 $page_settings_manager = SettingsManager::get_settings_managers( 'page' );
1280 $page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID );
1281 $page_settings_manager->save_settings( $settings, $this->post->ID );
1282 }
1283
1284 /**
1285 * @since 2.1.3
1286 * @access protected
1287 */
1288 protected function print_elements( $elements_data ) {
1289 foreach ( $elements_data as $element_data ) {
1290 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1291
1292 if ( ! $element ) {
1293 continue;
1294 }
1295
1296 $element->print_element();
1297 }
1298 }
1299
1300 protected function register_document_controls() {
1301 $this->start_controls_section(
1302 'document_settings',
1303 [
1304 'label' => __( 'General Settings', 'elementor' ),
1305 'tab' => Controls_Manager::TAB_SETTINGS,
1306 ]
1307 );
1308
1309 $this->add_control(
1310 'post_title',
1311 [
1312 'label' => __( 'Title', 'elementor' ),
1313 'type' => Controls_Manager::TEXT,
1314 'default' => $this->post->post_title,
1315 'label_block' => true,
1316 'separator' => 'none',
1317 ]
1318 );
1319
1320 $post_type_object = get_post_type_object( $this->post->post_type );
1321
1322 $can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts );
1323 $is_published = self::STATUS_PUBLISH === $this->post->post_status || self::STATUS_PRIVATE === $this->post->post_status;
1324
1325 if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) {
1326
1327 $statuses = $this->get_post_statuses();
1328 if ( 'future' === $this->get_main_post()->post_status ) {
1329 $statuses['future'] = __( 'Future', 'elementor' );
1330 }
1331
1332 $this->add_control(
1333 'post_status',
1334 [
1335 'label' => __( 'Status', 'elementor' ),
1336 'type' => Controls_Manager::SELECT,
1337 'default' => $this->get_main_post()->post_status,
1338 'options' => $statuses,
1339 ]
1340 );
1341 }
1342
1343 $this->end_controls_section();
1344 }
1345
1346 protected function get_post_statuses() {
1347 return get_post_statuses();
1348 }
1349
1350 protected function get_have_a_look_url() {
1351 return $this->get_permalink();
1352 }
1353
1354 public function handle_revisions_changed( $post_has_changed, $last_revision, $post ) {
1355 // In case default, didn't determine the changes.
1356 if ( ! $post_has_changed ) {
1357 $last_revision_id = $last_revision->ID;
1358 $last_revision_document = Plugin::instance()->documents->get( $last_revision_id );
1359 $post_document = Plugin::instance()->documents->get( $post->ID );
1360
1361 $last_revision_settings = $last_revision_document->get_settings();
1362 $post_settings = $post_document->get_settings();
1363
1364 // TODO: Its better to add crc32 signature for each revision and then only compare one part of the checksum.
1365 $post_has_changed = $last_revision_settings !== $post_settings;
1366 }
1367
1368 return $post_has_changed;
1369 }
1370
1371 private function add_handle_revisions_changed_filter() {
1372 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ], 10, 3 );
1373 }
1374
1375 private function remove_handle_revisions_changed_filter() {
1376 remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] );
1377 }
1378 }
1379