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

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