PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.2
Elementor Website Builder – more than just a page builder v3.1.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / base / document.php

document.php in Elementor Website Builder – more than just a page builder 3.1.2, at core/base/document.php

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