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

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