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

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