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

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