PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.0-dev1
Elementor Website Builder – more than just a page builder v3.6.0-dev1
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 / trunk / core / base / document.php

document.php in Elementor Website Builder – more than just a page builder 3.6.0-dev1, at trunk/core/base/document.php

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