PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev2
Elementor Website Builder – more than just a page builder v4.0.0-dev2
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 / module.php

module.php in Elementor Website Builder – more than just a page builder 4.0.0-dev2, at core/base/module.php

359 lines 7.4 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\Plugin;
5 use Elementor\Utils;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor module.
13 *
14 * An abstract class that provides the needed properties and methods to
15 * manage and handle modules in inheriting classes.
16 *
17 * @since 1.7.0
18 * @abstract
19 */
20 abstract class Module extends Base_Object {
21
22 /**
23 * Module class reflection.
24 *
25 * Holds the information about a class.
26 *
27 * @since 1.7.0
28 * @access private
29 *
30 * @var \ReflectionClass
31 */
32 private $reflection;
33
34 /**
35 * Module components.
36 *
37 * Holds the module components.
38 *
39 * @since 1.7.0
40 * @access private
41 *
42 * @var array
43 */
44 private $components = [];
45
46 /**
47 * Module instance.
48 *
49 * Holds the module instance.
50 *
51 * @since 1.7.0
52 * @access protected
53 *
54 * @var Module
55 */
56 protected static $_instances = [];
57
58 /**
59 * Get module name.
60 *
61 * Retrieve the module name.
62 *
63 * @since 1.7.0
64 * @access public
65 * @abstract
66 *
67 * @return string Module name.
68 */
69 abstract public function get_name();
70
71 /**
72 * Instance.
73 *
74 * Ensures only one instance of the module class is loaded or can be loaded.
75 *
76 * @since 1.7.0
77 * @access public
78 * @static
79 *
80 * @return $this An instance of the class.
81 */
82 public static function instance() {
83 $class_name = static::class_name();
84
85 if ( empty( static::$_instances[ $class_name ] ) ) {
86 static::$_instances[ $class_name ] = new static();
87 }
88
89 return static::$_instances[ $class_name ];
90 }
91
92 /**
93 * @since 2.0.0
94 * @access public
95 * @static
96 */
97 public static function is_active() {
98 return true;
99 }
100
101 /**
102 * Class name.
103 *
104 * Retrieve the name of the class.
105 *
106 * @since 1.7.0
107 * @access public
108 * @static
109 */
110 public static function class_name() {
111 return get_called_class();
112 }
113
114 public static function get_experimental_data() {
115 return [];
116 }
117
118 /**
119 * Clone.
120 *
121 * Disable class cloning and throw an error on object clone.
122 *
123 * The whole idea of the singleton design pattern is that there is a single
124 * object. Therefore, we don't want the object to be cloned.
125 *
126 * @since 1.7.0
127 * @access public
128 */
129 public function __clone() {
130 _doing_it_wrong(
131 __FUNCTION__,
132 sprintf( 'Cloning instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
133 '1.0.0'
134 );
135 }
136
137 /**
138 * Wakeup.
139 *
140 * Disable unserializing of the class.
141 *
142 * @since 1.7.0
143 * @access public
144 */
145 public function __wakeup() {
146 _doing_it_wrong(
147 __FUNCTION__,
148 sprintf( 'Unserializing instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149 '1.0.0'
150 );
151 }
152
153 /**
154 * @since 2.0.0
155 * @access public
156 */
157 public function get_reflection() {
158 if ( null === $this->reflection ) {
159 $this->reflection = new \ReflectionClass( $this );
160 }
161
162 return $this->reflection;
163 }
164
165 /**
166 * Add module component.
167 *
168 * Add new component to the current module.
169 *
170 * @since 1.7.0
171 * @access public
172 *
173 * @param string $id Component ID.
174 * @param mixed $instance An instance of the component.
175 */
176 public function add_component( $id, $instance ) {
177 $this->components[ $id ] = $instance;
178 }
179
180 /**
181 * @since 2.3.0
182 * @access public
183 * @return Module[]
184 */
185 public function get_components() {
186 return $this->components;
187 }
188
189 /**
190 * Get module component.
191 *
192 * Retrieve the module component.
193 *
194 * @since 1.7.0
195 * @access public
196 *
197 * @param string $id Component ID.
198 *
199 * @return mixed An instance of the component, or `false` if the component
200 * doesn't exist.
201 */
202 public function get_component( $id ) {
203 if ( isset( $this->components[ $id ] ) ) {
204 return $this->components[ $id ];
205 }
206
207 return false;
208 }
209
210 /**
211 * Get assets url.
212 *
213 * @since 2.3.0
214 * @access protected
215 *
216 * @param string $file_name
217 * @param string $file_extension
218 * @param string $relative_url Optional. Default is null.
219 * @param string $add_min_suffix Optional. Default is 'default'.
220 *
221 * @return string
222 */
223 final protected function get_assets_url( $file_name, $file_extension, $relative_url = null, $add_min_suffix = 'default' ) {
224 static $is_test_mode = null;
225
226 if ( null === $is_test_mode ) {
227 $is_test_mode = Utils::is_script_debug() || Utils::is_elementor_tests();
228 }
229
230 if ( ! $relative_url ) {
231 $relative_url = $this->get_assets_relative_url() . $file_extension . '/';
232 }
233
234 $url = $this->get_assets_base_url() . $relative_url . $file_name;
235
236 if ( 'default' === $add_min_suffix ) {
237 $add_min_suffix = ! $is_test_mode;
238 }
239
240 if ( $add_min_suffix ) {
241 $url .= '.min';
242 }
243
244 return $url . '.' . $file_extension;
245 }
246
247 /**
248 * Get js assets url
249 *
250 * @since 2.3.0
251 * @access protected
252 *
253 * @param string $file_name
254 * @param string $relative_url Optional. Default is null.
255 * @param string $add_min_suffix Optional. Default is 'default'.
256 *
257 * @return string
258 */
259 final protected function get_js_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default' ) {
260 return $this->get_assets_url( $file_name, 'js', $relative_url, $add_min_suffix );
261 }
262
263 /**
264 * Get css assets url
265 *
266 * @since 2.3.0
267 * @access protected
268 *
269 * @param string $file_name
270 * @param string $relative_url Optional. Default is null.
271 * @param string $add_min_suffix Optional. Default is 'default'.
272 * @param bool $add_direction_suffix Optional. Default is `false`.
273 *
274 * @return string
275 */
276 final protected function get_css_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default', $add_direction_suffix = false ) {
277 static $direction_suffix = null;
278
279 if ( ! $direction_suffix ) {
280 $direction_suffix = is_rtl() ? '-rtl' : '';
281 }
282
283 if ( $add_direction_suffix ) {
284 $file_name .= $direction_suffix;
285 }
286
287 return $this->get_assets_url( $file_name, 'css', $relative_url, $add_min_suffix );
288 }
289
290 /**
291 * Get Frontend File URL
292 *
293 * Returns the URL for the CSS file to be loaded in the front end. If requested via the second parameter, a custom
294 * file is generated based on a passed template file name. Otherwise, the URL for the default CSS file is returned.
295 *
296 * @since 3.24.0
297 *
298 * @access public
299 *
300 * @param string $file_name
301 * @param boolean $has_custom_breakpoints
302 *
303 * @return string frontend file URL
304 */
305 public function get_frontend_file_url( $file_name, $has_custom_breakpoints ) {
306 return Plugin::$instance->frontend->get_frontend_file_url( $file_name, $has_custom_breakpoints );
307 }
308
309 /**
310 * Get assets base url
311 *
312 * @since 2.6.0
313 * @access protected
314 *
315 * @return string
316 */
317 protected function get_assets_base_url() {
318 return ELEMENTOR_URL;
319 }
320
321 /**
322 * Get assets relative url
323 *
324 * @since 2.3.0
325 * @access protected
326 *
327 * @return string
328 */
329 protected function get_assets_relative_url() {
330 return 'assets/';
331 }
332
333 /**
334 * Get the module's associated widgets.
335 *
336 * @return string[]
337 */
338 protected function get_widgets() {
339 return [];
340 }
341
342 /**
343 * Initialize the module related widgets.
344 */
345 public function init_widgets() {
346 $widget_manager = Plugin::instance()->widgets_manager;
347
348 foreach ( $this->get_widgets() as $widget ) {
349 $class_name = $this->get_reflection()->getNamespaceName() . '\Widgets\\' . $widget;
350
351 $widget_manager->register( new $class_name() );
352 }
353 }
354
355 public function __construct() {
356 add_action( 'elementor/widgets/register', [ $this, 'init_widgets' ] );
357 }
358 }
359