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

1,288 lines 29.1 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 if ( ! $this->is_editable_by_current_user() ) {
531 return false;
532 }
533
534 $this->set_is_saving( true );
535
536 /**
537 * Before document save.
538 *
539 * Fires when document save starts on Elementor.
540 *
541 * @since 2.5.12
542 *
543 * @param \Elementor\Core\Base\Document $this The current document.
544 * @param $data.
545 */
546 do_action( 'elementor/document/before_save', $this, $data );
547
548 if ( ! current_user_can( 'unfiltered_html' ) ) {
549 $data = wp_kses_post_deep( $data );
550 }
551
552 if ( ! empty( $data['settings'] ) ) {
553 if ( isset( $data['settings']['post_status'] ) && DB::STATUS_AUTOSAVE === $data['settings']['post_status'] ) {
554 if ( ! defined( 'DOING_AUTOSAVE' ) ) {
555 define( 'DOING_AUTOSAVE', true );
556 }
557 }
558
559 $this->save_settings( $data['settings'] );
560
561 // Refresh post after save settings.
562 $this->post = get_post( $this->post->ID );
563 }
564
565 // Don't check is_empty, because an empty array should be saved.
566 if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) {
567 $this->save_elements( $data['elements'] );
568 }
569
570 $this->save_template_type();
571
572 $this->save_version();
573
574 // Remove Post CSS
575 $post_css = Post_CSS::create( $this->post->ID );
576
577 $post_css->delete();
578
579 /**
580 * After document save.
581 *
582 * Fires when document save is complete.
583 *
584 * @since 2.5.12
585 *
586 * @param \Elementor\Core\Base\Document $this The current document.
587 * @param $data.
588 */
589 do_action( 'elementor/document/after_save', $this, $data );
590
591 $this->set_is_saving( false );
592
593 return true;
594 }
595
596 /**
597 * @param array $new_settings
598 *
599 * @return static
600 */
601 public function update_settings( array $new_settings ) {
602 $document_settings = $this->get_meta( PageManager::META_KEY );
603
604 if ( ! $document_settings ) {
605 $document_settings = [];
606 }
607
608 $this->save_settings(
609 array_replace_recursive( $document_settings, $new_settings )
610 );
611
612 return $this;
613 }
614
615 /**
616 * Is built with Elementor.
617 *
618 * Check whether the post was built with Elementor.
619 *
620 * @since 2.0.0
621 * @access public
622 *
623 * @return bool Whether the post was built with Elementor.
624 */
625 public function is_built_with_elementor() {
626 return ! ! get_post_meta( $this->post->ID, '_elementor_edit_mode', true );
627 }
628
629 /**
630 * @since 2.0.0
631 * @access public
632 * @static
633 *
634 * @return mixed
635 */
636 public function get_edit_url() {
637 $url = add_query_arg(
638 [
639 'post' => $this->get_main_id(),
640 'action' => 'elementor',
641 ],
642 admin_url( 'post.php' )
643 );
644
645 /**
646 * Document edit url.
647 *
648 * Filters the document edit url.
649 *
650 * @since 2.0.0
651 *
652 * @param string $url The edit url.
653 * @param Document $this The document instance.
654 */
655 $url = apply_filters( 'elementor/document/urls/edit', $url, $this );
656
657 return $url;
658 }
659
660 /**
661 * @since 2.0.0
662 * @access public
663 */
664 public function get_preview_url() {
665 /**
666 * Use a static var - to avoid change the `ver` parameter on every call.
667 */
668 static $url;
669
670 if ( empty( $url ) ) {
671
672 add_filter( 'pre_option_permalink_structure', '__return_empty_string' );
673
674 $url = set_url_scheme( add_query_arg( [
675 'elementor-preview' => $this->get_main_id(),
676 'ver' => time(),
677 ], $this->get_permalink() ) );
678
679 remove_filter( 'pre_option_permalink_structure', '__return_empty_string' );
680
681 /**
682 * Document preview URL.
683 *
684 * Filters the document preview URL.
685 *
686 * @since 2.0.0
687 *
688 * @param string $url The preview URL.
689 * @param Document $this The document instance.
690 */
691 $url = apply_filters( 'elementor/document/urls/preview', $url, $this );
692 }
693
694 return $url;
695 }
696
697 /**
698 * @since 2.0.0
699 * @access public
700 *
701 * @param string $key
702 *
703 * @return array
704 */
705 public function get_json_meta( $key ) {
706 $meta = get_post_meta( $this->post->ID, $key, true );
707
708 if ( is_string( $meta ) && ! empty( $meta ) ) {
709 $meta = json_decode( $meta, true );
710 }
711
712 if ( empty( $meta ) ) {
713 $meta = [];
714 }
715
716 return $meta;
717 }
718
719 /**
720 * @since 2.0.0
721 * @access public
722 *
723 * @param null $data
724 * @param bool $with_html_content
725 *
726 * @return array
727 */
728 public function get_elements_raw_data( $data = null, $with_html_content = false ) {
729 if ( ! static::get_property( 'has_elements' ) ) {
730 return [];
731 }
732
733 if ( is_null( $data ) ) {
734 $data = $this->get_elements_data();
735 }
736
737 // Change the current documents, so widgets can use `documents->get_current` and other post data
738 Plugin::$instance->documents->switch_to_document( $this );
739
740 $editor_data = [];
741
742 foreach ( $data as $element_data ) {
743 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
744
745 if ( ! $element ) {
746 continue;
747 }
748
749 $editor_data[] = $element->get_raw_data( $with_html_content );
750 } // End foreach().
751
752 Plugin::$instance->documents->restore_document();
753
754 return $editor_data;
755 }
756
757 /**
758 * @since 2.0.0
759 * @access public
760 *
761 * @param string $status
762 *
763 * @return array
764 */
765 public function get_elements_data( $status = DB::STATUS_PUBLISH ) {
766 $elements = $this->get_json_meta( '_elementor_data' );
767
768 if ( DB::STATUS_DRAFT === $status ) {
769 $autosave = $this->get_newer_autosave();
770
771 if ( is_object( $autosave ) ) {
772 $autosave_elements = Plugin::$instance->documents
773 ->get( $autosave->get_post()->ID )
774 ->get_json_meta( '_elementor_data' );
775 }
776 }
777
778 if ( Plugin::$instance->editor->is_edit_mode() ) {
779 if ( empty( $elements ) && empty( $autosave_elements ) ) {
780 // Convert to Elementor.
781 $elements = $this->convert_to_elementor();
782 if ( $this->is_autosave() ) {
783 Plugin::$instance->db->copy_elementor_meta( $this->post->post_parent, $this->post->ID );
784 }
785 }
786 }
787
788 if ( ! empty( $autosave_elements ) ) {
789 $elements = $autosave_elements;
790 }
791
792 return $elements;
793 }
794
795 /**
796 * @since 2.3.0
797 * @access public
798 */
799 public function convert_to_elementor() {
800 $this->save( [] );
801
802 if ( empty( $this->post->post_content ) ) {
803 return [];
804 }
805
806 // Check if it's only a shortcode.
807 preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER );
808 if ( ! empty( $matches ) ) {
809 foreach ( $matches as $shortcode ) {
810 if ( trim( $this->post->post_content ) === $shortcode[0] ) {
811 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' );
812 $settings = [
813 'shortcode' => $this->post->post_content,
814 ];
815 break;
816 }
817 }
818 }
819
820 if ( empty( $widget_type ) ) {
821 $widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' );
822 $settings = [
823 'editor' => $this->post->post_content,
824 ];
825 }
826
827 // TODO: Better coding to start template for editor
828 return [
829 [
830 'id' => Utils::generate_random_string(),
831 'elType' => 'section',
832 'elements' => [
833 [
834 'id' => Utils::generate_random_string(),
835 'elType' => 'column',
836 'elements' => [
837 [
838 'id' => Utils::generate_random_string(),
839 'elType' => $widget_type::get_type(),
840 'widgetType' => $widget_type->get_name(),
841 'settings' => $settings,
842 ],
843 ],
844 ],
845 ],
846 ],
847 ];
848 }
849
850 /**
851 * @since 2.1.3
852 * @access public
853 */
854 public function print_elements_with_wrapper( $elements_data = null ) {
855 if ( ! $elements_data ) {
856 $elements_data = $this->get_elements_data();
857 }
858
859 $is_legacy_mode_active = Plugin::instance()->get_legacy_mode( 'elementWrappers' );
860
861 ?>
862 <div <?php echo Utils::render_html_attributes( $this->get_container_attributes() ); ?>>
863 <?php if ( $is_legacy_mode_active ) { ?>
864 <div class="elementor-inner">
865 <?php } ?>
866 <div class="elementor-section-wrap">
867 <?php $this->print_elements( $elements_data ); ?>
868 </div>
869 <?php if ( $is_legacy_mode_active ) { ?>
870 </div>
871 <?php } ?>
872 </div>
873 <?php
874 }
875
876 /**
877 * @since 2.0.0
878 * @access public
879 */
880 public function get_css_wrapper_selector() {
881 return '';
882 }
883
884 /**
885 * @since 2.0.0
886 * @access public
887 */
888 public function get_panel_page_settings() {
889 return [
890 /* translators: %s: Document title */
891 'title' => sprintf( __( '%s Settings', 'elementor' ), static::get_title() ),
892 ];
893 }
894
895 /**
896 * @since 2.0.0
897 * @access public
898 */
899 public function get_post() {
900 return $this->post;
901 }
902
903 /**
904 * @since 2.0.0
905 * @access public
906 */
907 public function get_permalink() {
908 return get_permalink( $this->get_main_id() );
909 }
910
911 /**
912 * @since 2.0.8
913 * @access public
914 */
915 public function get_content( $with_css = false ) {
916 return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css );
917 }
918
919 /**
920 * @since 2.0.0
921 * @access public
922 */
923 public function delete() {
924 if ( 'revision' === $this->post->post_type ) {
925 $deleted = wp_delete_post_revision( $this->post );
926 } else {
927 $deleted = wp_delete_post( $this->post->ID );
928 }
929
930 return $deleted && ! is_wp_error( $deleted );
931 }
932
933 /**
934 * Save editor elements.
935 *
936 * Save data from the editor to the database.
937 *
938 * @since 2.0.0
939 * @access protected
940 *
941 * @param array $elements
942 */
943 protected function save_elements( $elements ) {
944 $editor_data = $this->get_elements_raw_data( $elements );
945
946 // We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta`
947 $json_value = wp_slash( wp_json_encode( $editor_data ) );
948
949 // Don't use `update_post_meta` that can't handle `revision` post type
950 $is_meta_updated = update_metadata( 'post', $this->post->ID, '_elementor_data', $json_value );
951
952 /**
953 * Before saving data.
954 *
955 * Fires before Elementor saves data to the database.
956 *
957 * @since 1.0.0
958 *
959 * @param string $status Post status.
960 * @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure.
961 */
962 do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated );
963
964 Plugin::$instance->db->save_plain_text( $this->post->ID );
965
966 /**
967 * After saving data.
968 *
969 * Fires after Elementor saves data to the database.
970 *
971 * @since 1.0.0
972 *
973 * @param int $post_id The ID of the post.
974 * @param array $editor_data Sanitize posted data.
975 */
976 do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data );
977 }
978
979 /**
980 * @since 2.0.0
981 * @access public
982 *
983 * @param int $user_id Optional. User ID. Default value is `0`.
984 *
985 * @return bool|int
986 */
987 public function get_autosave_id( $user_id = 0 ) {
988 if ( ! $user_id ) {
989 $user_id = get_current_user_id();
990 }
991
992 $autosave = Utils::get_post_autosave( $this->post->ID, $user_id );
993 if ( $autosave ) {
994 return $autosave->ID;
995 }
996
997 return false;
998 }
999
1000 public function save_version() {
1001 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
1002 // Save per revision.
1003 $this->update_meta( '_elementor_version', ELEMENTOR_VERSION );
1004
1005 /**
1006 * Document version save.
1007 *
1008 * Fires when document version is saved on Elementor.
1009 * Will not fire during Elementor Upgrade.
1010 *
1011 * @since 2.5.12
1012 *
1013 * @param \Elementor\Core\Base\Document $this The current document.
1014 *
1015 */
1016 do_action( 'elementor/document/save_version', $this );
1017 }
1018 }
1019
1020 /**
1021 * @since 2.3.0
1022 * @access public
1023 */
1024 public function save_template_type() {
1025 return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() );
1026 }
1027
1028 /**
1029 * @since 2.3.0
1030 * @access public
1031 */
1032 public function get_template_type() {
1033 return $this->get_main_meta( self::TYPE_META_KEY );
1034 }
1035
1036 /**
1037 * @since 2.0.0
1038 * @access public
1039 *
1040 * @param string $key Meta data key.
1041 *
1042 * @return mixed
1043 */
1044 public function get_main_meta( $key ) {
1045 return get_post_meta( $this->get_main_id(), $key, true );
1046 }
1047
1048 /**
1049 * @since 2.0.4
1050 * @access public
1051 *
1052 * @param string $key Meta data key.
1053 * @param string $value Meta data value.
1054 *
1055 * @return bool|int
1056 */
1057 public function update_main_meta( $key, $value ) {
1058 return update_post_meta( $this->get_main_id(), $key, $value );
1059 }
1060
1061 /**
1062 * @since 2.0.4
1063 * @access public
1064 *
1065 * @param string $key Meta data key.
1066 * @param string $value Optional. Meta data value. Default is an empty string.
1067 *
1068 * @return bool
1069 */
1070 public function delete_main_meta( $key, $value = '' ) {
1071 return delete_post_meta( $this->get_main_id(), $key, $value );
1072 }
1073
1074 /**
1075 * @since 2.0.0
1076 * @access public
1077 *
1078 * @param string $key Meta data key.
1079 *
1080 * @return mixed
1081 */
1082 public function get_meta( $key ) {
1083 return get_post_meta( $this->post->ID, $key, true );
1084 }
1085
1086 /**
1087 * @since 2.0.0
1088 * @access public
1089 *
1090 * @param string $key Meta data key.
1091 * @param mixed $value Meta data value.
1092 *
1093 * @return bool|int
1094 */
1095 public function update_meta( $key, $value ) {
1096 // Use `update_metadata` in order to work also with revisions.
1097 return update_metadata( 'post', $this->post->ID, $key, $value );
1098 }
1099
1100 /**
1101 * @since 2.0.3
1102 * @access public
1103 *
1104 * @param string $key Meta data key.
1105 * @param string $value Meta data value.
1106 *
1107 * @return bool
1108 */
1109 public function delete_meta( $key, $value = '' ) {
1110 // Use `delete_metadata` in order to work also with revisions.
1111 return delete_metadata( 'post', $this->post->ID, $key, $value );
1112 }
1113
1114 /**
1115 * @since 2.0.0
1116 * @access public
1117 */
1118 public function get_last_edited() {
1119 $post = $this->post;
1120 $autosave_post = $this->get_autosave();
1121
1122 if ( $autosave_post ) {
1123 $post = $autosave_post->get_post();
1124 }
1125
1126 $date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) );
1127 $display_name = get_the_author_meta( 'display_name', $post->post_author );
1128
1129 if ( $autosave_post || 'revision' === $post->post_type ) {
1130 /* translators: 1: Saving date, 2: Author display name */
1131 $last_edited = sprintf( __( 'Draft saved on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1132 } else {
1133 /* translators: 1: Editing date, 2: Author display name */
1134 $last_edited = sprintf( __( 'Last edited on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name );
1135 }
1136
1137 return $last_edited;
1138 }
1139
1140
1141 /**
1142 * @return bool
1143 */
1144 public function is_saving() {
1145 return $this->is_saving;
1146 }
1147
1148 /**
1149 * @param $is_saving
1150 *
1151 * @return $this
1152 */
1153 public function set_is_saving( $is_saving ) {
1154 $this->is_saving = $is_saving;
1155
1156 return $this;
1157 }
1158
1159 /**
1160 * @since 2.0.0
1161 * @access public
1162 *
1163 * @param array $data
1164 *
1165 * @throws \Exception If the post does not exist.
1166 */
1167 public function __construct( array $data = [] ) {
1168 if ( $data ) {
1169 if ( empty( $data['post_id'] ) ) {
1170 $this->post = new \WP_Post( (object) [] );
1171 } else {
1172 $this->post = get_post( $data['post_id'] );
1173
1174 if ( ! $this->post ) {
1175 throw new \Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ), Exceptions::NOT_FOUND );
1176 }
1177 }
1178
1179 // Each Control_Stack is based on a unique ID.
1180 $data['id'] = $data['post_id'];
1181
1182 if ( ! isset( $data['settings'] ) ) {
1183 $data['settings'] = [];
1184 }
1185
1186 $saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true );
1187 if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) {
1188 $data['settings'] += $saved_settings;
1189 }
1190 }
1191
1192 parent::__construct( $data );
1193 }
1194
1195 protected function get_remote_library_config() {
1196 $config = [
1197 'type' => 'block',
1198 'default_route' => 'templates/blocks',
1199 'category' => $this->get_name(),
1200 'autoImportSettings' => false,
1201 ];
1202
1203 return $config;
1204 }
1205
1206 /**
1207 * @since 2.0.4
1208 * @access protected
1209 *
1210 * @param $settings
1211 */
1212 protected function save_settings( $settings ) {
1213 $page_settings_manager = SettingsManager::get_settings_managers( 'page' );
1214 $page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID );
1215 $page_settings_manager->save_settings( $settings, $this->post->ID );
1216 }
1217
1218 /**
1219 * @since 2.1.3
1220 * @access protected
1221 */
1222 protected function print_elements( $elements_data ) {
1223 foreach ( $elements_data as $element_data ) {
1224 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
1225
1226 if ( ! $element ) {
1227 continue;
1228 }
1229
1230 $element->print_element();
1231 }
1232 }
1233
1234 protected function register_document_controls() {
1235 $this->start_controls_section(
1236 'document_settings',
1237 [
1238 'label' => __( 'General Settings', 'elementor' ),
1239 'tab' => Controls_Manager::TAB_SETTINGS,
1240 ]
1241 );
1242
1243 $this->add_control(
1244 'post_title',
1245 [
1246 'label' => __( 'Title', 'elementor' ),
1247 'type' => Controls_Manager::TEXT,
1248 'default' => $this->post->post_title,
1249 'label_block' => true,
1250 'separator' => 'none',
1251 ]
1252 );
1253
1254 $post_type_object = get_post_type_object( $this->post->post_type );
1255
1256 $can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts );
1257 $is_published = DB::STATUS_PUBLISH === $this->post->post_status || DB::STATUS_PRIVATE === $this->post->post_status;
1258
1259 if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) {
1260
1261 $statuses = $this->get_post_statuses();
1262 if ( 'future' === $this->get_main_post()->post_status ) {
1263 $statuses['future'] = __( 'Future', 'elementor' );
1264 }
1265
1266 $this->add_control(
1267 'post_status',
1268 [
1269 'label' => __( 'Status', 'elementor' ),
1270 'type' => Controls_Manager::SELECT,
1271 'default' => $this->get_main_post()->post_status,
1272 'options' => $statuses,
1273 ]
1274 );
1275 }
1276
1277 $this->end_controls_section();
1278 }
1279
1280 protected function get_post_statuses() {
1281 return get_post_statuses();
1282 }
1283
1284 protected function get_have_a_look_url() {
1285 return $this->get_permalink();
1286 }
1287 }
1288