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

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