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

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