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

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