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

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