PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.9
Elementor Website Builder – more than just a page builder v4.0.9
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.0.9, at modules/interactions/module.php

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