PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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 / modules / interactions / module.php

module.php in Elementor Website Builder – more than just a page builder 4.3.0-beta1, at modules/interactions/module.php

183 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Interactions;
4
5 use Elementor\Core\Base\Module as BaseModule;
6 use Elementor\Core\Base\Document;
7 use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule;
8 use Elementor\Modules\Interactions\Cache\Interactions_Postmeta;
9 use Elementor\Plugin;
10 use Elementor\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 class Module extends BaseModule {
17 const EXPERIMENT_NAME = AtomicWidgetsModule::EXPERIMENT_NAME;
18 const MODULE_NAME = 'e-interactions';
19
20 const HANDLE_MOTION_JS = 'motion-js';
21 const HANDLE_SHARED_UTILS = 'elementor-interactions-shared-utils';
22 const HANDLE_FRONTEND = 'elementor-interactions';
23 const HANDLE_EDITOR = 'elementor-editor-interactions';
24 const JS_CONFIG_OBJECT = 'ElementorInteractionsConfig';
25 const SCRIPT_ID_INTERACTIONS_DATA = 'elementor-interactions-data';
26
27 public function get_name() {
28 return self::MODULE_NAME;
29 }
30
31 private $preset_animations;
32
33 private function get_presets() {
34 if ( ! $this->preset_animations ) {
35 $this->preset_animations = new Presets();
36 }
37
38 return $this->preset_animations;
39 }
40
41 private $frontend_handler;
42
43 private function get_frontend_handler() {
44 if ( ! $this->frontend_handler ) {
45 $this->frontend_handler = new Interactions_Frontend_Handler( fn () => $this->get_config() );
46 }
47
48 return $this->frontend_handler;
49 }
50
51 public function is_experiment_active() {
52 return Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME );
53 }
54
55 public function __construct() {
56 parent::__construct();
57
58 if ( ! $this->is_experiment_active() ) {
59 return;
60 }
61
62 $this->register_hooks();
63 }
64
65 private function register_hooks() {
66 add_action( 'elementor/frontend/after_register_scripts', fn () => $this->register_frontend_scripts() );
67 add_action( 'elementor/preview/enqueue_scripts', fn () => $this->enqueue_preview_scripts() );
68
69 add_action( 'elementor/editor/before_enqueue_scripts', fn () => $this->enqueue_editor_scripts() );
70 add_action( 'elementor/editor/after_enqueue_scripts', fn () => $this->enqueue_editor_scripts() );
71
72 add_filter( 'elementor/document/save/data', [ $this, 'handle_interactions' ], 10, 2 );
73 add_action( 'elementor/document/after_save', [ $this, 'handle_interactions_cache' ], 10, 2 );
74
75 // Collect interactions from documents before they render (header, footer, post content)
76 add_filter( 'elementor/frontend/builder_content_data', [
77 $this->get_frontend_handler(),
78 'collect_document_interactions',
79 ], 10, 2 );
80
81 // Output centralized interaction data in footer
82 add_action( 'wp_footer', [ $this->get_frontend_handler(), 'print_interactions_data' ], 1 );
83 }
84
85 /**
86 * Sanitize and validate data before saving the document.
87 *
88 * @throws \Exception When validation fails.
89 * @return array
90 */
91 public function handle_interactions( $data, $document ) {
92 $validation = new Validation();
93 $document_after_sanitization = $validation->sanitize( $data );
94
95 $validation->validate();
96
97 $parser = new Parser( $document->get_main_id() );
98 return $parser->assign_interaction_ids( $document_after_sanitization );
99 }
100
101 public function handle_interactions_cache( Document $document, $data ) {
102 $postmeta = new Interactions_Postmeta();
103 $postmeta->process_content( $document->get_main_id(), $data );
104 }
105
106 public function get_config() {
107 return [
108 'constants' => $this->get_presets()->defaults(),
109 'breakpoints' => $this->get_active_breakpoints(),
110 ];
111 }
112
113 private function get_active_breakpoints() {
114 $breakpoints_config = Plugin::$instance->breakpoints->get_breakpoints_config();
115 $active_breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints();
116
117 $breakpoints = [];
118
119 foreach ( array_keys( $active_breakpoints ) as $breakpoint_label ) {
120 $breakpoints[ $breakpoint_label ] = $breakpoints_config[ $breakpoint_label ];
121 }
122
123 return $breakpoints;
124 }
125
126 private function register_frontend_scripts() {
127 $suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min';
128
129 wp_register_script(
130 self::HANDLE_MOTION_JS,
131 ELEMENTOR_ASSETS_URL . 'lib/motion/motion' . $suffix . '.js',
132 [],
133 '11.13.5',
134 true
135 );
136
137 wp_register_script(
138 self::HANDLE_SHARED_UTILS,
139 $this->get_js_assets_url( 'interactions-shared-utils' ),
140 [ self::HANDLE_MOTION_JS ],
141 '1.0.0',
142 true
143 );
144
145 wp_register_script(
146 self::HANDLE_FRONTEND,
147 $this->get_js_assets_url( 'interactions' ),
148 [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ],
149 '1.0.0',
150 true
151 );
152
153 wp_register_script(
154 self::HANDLE_EDITOR,
155 $this->get_js_assets_url( 'editor-interactions' ),
156 [ self::HANDLE_MOTION_JS, self::HANDLE_SHARED_UTILS ],
157 '1.0.0',
158 true
159 );
160 }
161
162 public function enqueue_editor_scripts() {
163 wp_add_inline_script(
164 'elementor-common',
165 'window.' . self::JS_CONFIG_OBJECT . ' = ' . wp_json_encode( $this->get_config() ) . ';',
166 'before'
167 );
168 }
169
170 public function enqueue_preview_scripts() {
171 wp_enqueue_script( self::HANDLE_SHARED_UTILS );
172 // Ensure motion-js and editor-interactions handler are available in preview iframe
173 wp_enqueue_script( self::HANDLE_MOTION_JS );
174 wp_enqueue_script( self::HANDLE_EDITOR );
175
176 wp_localize_script(
177 self::HANDLE_EDITOR,
178 self::JS_CONFIG_OBJECT,
179 $this->get_config()
180 );
181 }
182 }
183