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

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