PluginProbe
Elementor Website Builder – more than just a page builder / 3.3.1
Elementor Website Builder – more than just a page builder v3.3.1
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 / experiments / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.3.1, at core/experiments/manager.php

528 lines 15.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Core\Experiments;
4
5 use Elementor\Core\Base\Base_Object;
6 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
7 use Elementor\Plugin;
8 use Elementor\Settings;
9 use Elementor\Tracker;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15 class Manager extends Base_Object {
16
17 const RELEASE_STATUS_DEV = 'dev';
18
19 const RELEASE_STATUS_ALPHA = 'alpha';
20
21 const RELEASE_STATUS_BETA = 'beta';
22
23 const RELEASE_STATUS_RC = 'rc';
24
25 const RELEASE_STATUS_STABLE = 'stable';
26
27 const STATE_DEFAULT = 'default';
28
29 const STATE_ACTIVE = 'active';
30
31 const STATE_INACTIVE = 'inactive';
32
33 private $states;
34
35 private $release_statuses;
36
37 private $features;
38
39 /**
40 * Add Feature
41 *
42 * @since 3.1.0
43 * @access public
44 *
45 * @param array $options {
46 * @type string $name
47 * @type string $title
48 * @type string $description
49 * @type string $release_status
50 * @type string $default
51 * @type callable $on_state_change
52 * }
53 *
54 * @return array|null
55 */
56 public function add_feature( array $options ) {
57 if ( isset( $this->features[ $options['name'] ] ) ) {
58 return null;
59 }
60
61 $default_experimental_data = [
62 'description' => '',
63 'release_status' => self::RELEASE_STATUS_ALPHA,
64 'default' => self::STATE_INACTIVE,
65 'new_site' => [
66 'default_active' => false,
67 'always_active' => false,
68 'minimum_installation_version' => null,
69 ],
70 'on_state_change' => null,
71 ];
72
73 $allowed_options = [ 'name', 'title', 'description', 'release_status', 'default', 'new_site', 'on_state_change' ];
74
75 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
76
77 $new_site = $experimental_data['new_site'];
78
79 $feature_is_mutable = true;
80
81 if ( $new_site['default_active'] || $new_site['always_active'] ) {
82 $is_new_installation = Upgrade_Manager::install_compare( $new_site['minimum_installation_version'], '>=' );
83
84 if ( $is_new_installation ) {
85 if ( $new_site['always_active'] ) {
86 $experimental_data['state'] = self::STATE_ACTIVE;
87
88 $feature_is_mutable = false;
89 } elseif ( $new_site['default_active'] ) {
90 $experimental_data['default'] = self::STATE_ACTIVE;
91 }
92 }
93 }
94
95 $experimental_data['mutable'] = $feature_is_mutable;
96
97 if ( $feature_is_mutable ) {
98 $state = $this->get_saved_feature_state( $options['name'] );
99
100 if ( ! $state ) {
101 $state = self::STATE_DEFAULT;
102 }
103
104 $experimental_data['state'] = $state;
105 }
106
107 $this->features[ $options['name'] ] = $experimental_data;
108
109 if ( $feature_is_mutable && is_admin() ) {
110 $feature_option_key = $this->get_feature_option_key( $options['name'] );
111
112 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data ) {
113 $this->on_feature_state_change( $experimental_data, $new_state );
114 };
115
116 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
117 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
118 }
119
120 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
121
122 return $experimental_data;
123 }
124
125 /**
126 * Remove Feature
127 *
128 * @since 3.1.0
129 * @access public
130 *
131 * @param string $feature_name
132 */
133 public function remove_feature( $feature_name ) {
134 unset( $this->features[ $feature_name ] );
135 }
136
137 /**
138 * Get Features
139 *
140 * @since 3.1.0
141 * @access public
142 *
143 * @param string $feature_name Optional. Default is null
144 *
145 * @return array|null
146 */
147 public function get_features( $feature_name = null ) {
148 return self::get_items( $this->features, $feature_name );
149 }
150
151 /**
152 * Get Active Features
153 *
154 * @since 3.1.0
155 * @access public
156 *
157 * @return array
158 */
159 public function get_active_features() {
160 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
161 }
162
163 /**
164 * Is Feature Active
165 *
166 * @since 3.1.0
167 * @access public
168 *
169 * @param string $feature_name
170 *
171 * @return bool
172 */
173 public function is_feature_active( $feature_name ) {
174 $feature = $this->get_features( $feature_name );
175
176 if ( ! $feature ) {
177 return false;
178 }
179
180 return self::STATE_ACTIVE === $this->get_feature_actual_state( $feature );
181 }
182
183 /**
184 * Set Feature Default State
185 *
186 * @since 3.1.0
187 * @access public
188 *
189 * @param string $feature_name
190 * @param int $default_state
191 */
192 public function set_feature_default_state( $feature_name, $default_state ) {
193 $feature = $this->get_features( $feature_name );
194
195 if ( ! $feature ) {
196 return;
197 }
198
199 $this->features[ $feature_name ]['default'] = $default_state;
200 }
201
202 /**
203 * Get Feature Option Key
204 *
205 * @since 3.1.0
206 * @access private
207 *
208 * @param string $feature_name
209 *
210 * @return string
211 */
212 private function get_feature_option_key( $feature_name ) {
213 return 'elementor_experiment-' . $feature_name;
214 }
215
216 private function add_default_features() {
217 $this->add_feature( [
218 'name' => 'e_dom_optimization',
219 'title' => __( 'Optimized DOM Output', 'elementor' ),
220 'description' => __( 'Developers, Please Note! This experiment includes some markup changes. If you\'ve used custom code in Elementor, you might have experienced a snippet of code not running. Turning this experiment off allows you to keep prior Elementor markup output settings, and have that lovely code running again.', 'elementor' )
221 . ' <a href="https://go.elementor.com/wp-dash-legacy-optimized-dom" target="_blank">'
222 . __( 'Learn More', 'elementor' ) . '</a>',
223 'release_status' => self::RELEASE_STATUS_BETA,
224 'new_site' => [
225 'default_active' => true,
226 'minimum_installation_version' => '3.1.0-beta',
227 ],
228 ] );
229
230 $this->add_feature( [
231 'name' => 'e_optimized_assets_loading',
232 'title' => __( 'Improved Asset Loading', 'elementor' ),
233 'description' => __( 'Please Note! The "Improved Asset Loading" mode reduces the amount of code that is loaded on the page by default. When activated, parts of the infrastructure code will be loaded dynamically, only when needed. Keep in mind that activating this experiment may cause conflicts with incompatible plugins.', 'elementor' )
234 . ' <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">'
235 . __( 'Learn More', 'elementor' ) . '</a>',
236 'release_status' => self::RELEASE_STATUS_ALPHA,
237 ] );
238
239 $this->add_feature( [
240 'name' => 'e_optimized_css_loading',
241 'title' => __( 'Improved CSS Loading', 'elementor' ),
242 'description' => __( 'Please Note! The “Improved CSS Loading” mode reduces the amount of CSS code that is loaded on the page by default. When activated, the CSS code will be loaded, rather inline or in a dedicated file, only when needed. Activating this experiment may cause conflicts with incompatible plugins.', 'elementor' )
243 . ' <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">'
244 . __( 'Learn More', 'elementor' ) . '</a>',
245 'release_status' => self::RELEASE_STATUS_ALPHA,
246 ] );
247
248 $this->add_feature( [
249 'name' => 'a11y_improvements',
250 'title' => __( 'Accessibility Improvements', 'elementor' ),
251 'description' => __( 'An array of accessibility enhancements in Elementor pages.', 'elementor' )
252 . '<br><strong>' . __( 'Please note!', 'elementor' ) . '</strong> ' . __( 'These enhancements may include some markup changes to existing elementor widgets', 'elementor' )
253 . ' <a href="https://go.elementor.com/wp-dash-a11y-improvements" target="_blank">'
254 . __( 'Learn More', 'elementor' ) . '</a>',
255 'release_status' => self::RELEASE_STATUS_BETA,
256 'new_site' => [
257 'default_active' => true,
258 'minimum_installation_version' => '3.1.0-beta',
259 ],
260 ] );
261
262 $this->add_feature( [
263 'name' => 'e_import_export',
264 'title' => __( 'Import Export Template Kit', 'elementor' ),
265 'description' => __( 'Design sites faster with a template kit that contains some or all components of a complete site, like templates, content & site settings.', 'elementor' )
266 . '<br>'
267 . __( 'You can import a kit and apply it to your site, or export the elements from this site to be used anywhere else.', 'elementor' ),
268 'release_status' => self::RELEASE_STATUS_BETA,
269 'default' => self::STATE_ACTIVE,
270 ] );
271 }
272
273 /**
274 * Init States
275 *
276 * @since 3.1.0
277 * @access private
278 */
279 private function init_states() {
280 $this->states = [
281 self::STATE_DEFAULT => __( 'Default', 'elementor' ),
282 self::STATE_ACTIVE => __( 'Active', 'elementor' ),
283 self::STATE_INACTIVE => __( 'Inactive', 'elementor' ),
284 ];
285 }
286
287 /**
288 * Init Statuses
289 *
290 * @since 3.1.0
291 * @access private
292 */
293 private function init_release_statuses() {
294 $this->release_statuses = [
295 self::RELEASE_STATUS_DEV => __( 'Development', 'elementor' ),
296 self::RELEASE_STATUS_ALPHA => __( 'Alpha', 'elementor' ),
297 self::RELEASE_STATUS_BETA => __( 'Beta', 'elementor' ),
298 self::RELEASE_STATUS_RC => __( 'Release Candidate', 'elementor' ),
299 self::RELEASE_STATUS_STABLE => __( 'Stable', 'elementor' ),
300 ];
301 }
302
303 /**
304 * Init Features
305 *
306 * @since 3.1.0
307 * @access private
308 */
309 private function init_features() {
310 $this->features = [];
311
312 $this->add_default_features();
313
314 do_action( 'elementor/experiments/default-features-registered', $this );
315 }
316
317 /**
318 * Register Settings Fields
319 *
320 * @param Settings $settings
321 *
322 * @since 3.1.0
323 * @access private
324 *
325 */
326 private function register_settings_fields( Settings $settings ) {
327 $features = $this->get_features();
328
329 $fields = [];
330
331 foreach ( $features as $feature_name => $feature ) {
332 if ( ! $feature['mutable'] ) {
333 unset( $features[ $feature_name ] );
334
335 continue;
336 }
337
338 $feature_key = 'experiment-' . $feature_name;
339
340 $fields[ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
341
342 $fields[ $feature_key ]['field_args'] = $feature;
343
344 $fields[ $feature_key ]['render'] = function( $feature ) {
345 $this->render_feature_settings_field( $feature );
346 };
347 }
348
349 if ( ! $features ) {
350 $fields['no_features'] = [
351 'label' => __( 'No available experiments', 'elementor' ),
352 'field_args' => [
353 'type' => 'raw_html',
354 'html' => __( 'The current version of Elementor doesn\'t have any experimental features . if you\'re feeling curious make sure to come back in future versions.', 'elementor' ),
355 ],
356 ];
357 }
358
359 if ( ! Tracker::is_allow_track() ) {
360 $fields += $settings->get_usage_fields();
361 }
362
363 $settings->add_tab(
364 'experiments', [
365 'label' => __( 'Experiments', 'elementor' ),
366 'sections' => [
367 'experiments' => [
368 'callback' => function() {
369 $this->render_settings_intro();
370 },
371 'fields' => $fields,
372 ],
373 ],
374 ]
375 );
376 }
377
378 /**
379 * Render Settings Intro
380 *
381 * @since 3.1.0
382 * @access private
383 */
384 private function render_settings_intro() {
385 ?>
386 <h2><?php echo __( 'Experiments', 'elementor' ); ?></h2>
387 <p><?php echo sprintf( __( 'Access new and experimental features from Elementor before they\'re officially released. As these features are still in development, they are likely to change, evolve or even be removed altogether. <a href="%s" target="_blank">Learn More.</a>', 'elementor' ), 'https://go.elementor.com/wp-dash-experiments/' ); ?></p>
388 <p><?php echo __( 'To use an experiment on your site, simply click on the dropdown next to it and switch to Active. You can always deactivate them at any time.', 'elementor' ); ?></p>
389 <p><?php echo sprintf( __( 'Your feedback is important - <a href="%s" target="_blank">help us</a> improve these features by sharing your thoughts and inputs.', 'elementor' ), 'https://go.elementor.com/wp-dash-experiments-report-an-issue/' ); ?></p>
390 <?php
391 }
392
393 /**
394 * Render Feature Settings Field
395 *
396 * @since 3.1.0
397 * @access private
398 *
399 * @param array $feature
400 */
401 private function render_feature_settings_field( array $feature ) {
402 ?>
403 <div class="e-experiment__content">
404 <select id="e-experiment-<?php echo $feature['name']; ?>" class="e-experiment__select" name="<?php echo $this->get_feature_option_key( $feature['name'] ); ?>">
405 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
406 <option value="<?php echo $state_key; ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
407 <?php } ?>
408 </select>
409 <p class="description"><?php echo $feature['description']; ?></p>
410 <div class="e-experiment__status"><?php echo sprintf( __( 'Status: %s', 'elementor' ), $this->release_statuses[ $feature['release_status'] ] ); ?></div>
411 </div>
412 <?php
413 }
414
415 /**
416 * Get Feature Settings Label HTML
417 *
418 * @since 3.1.0
419 * @access private
420 *
421 * @param array $feature
422 *
423 * @return string
424 */
425 private function get_feature_settings_label_html( array $feature ) {
426 ob_start();
427
428 $is_feature_active = $this->is_feature_active( $feature['name'] );
429
430 $indicator_classes = 'e-experiment__title__indicator';
431
432 if ( $is_feature_active ) {
433 $indicator_classes .= ' e-experiment__title__indicator--active';
434 }
435
436 if ( self::STATE_DEFAULT === $feature['state'] ) {
437 $indicator_tooltip = $is_feature_active ? __( 'Active by default', 'elementor' ) : __( 'Inactive by default', 'elementor' );
438 } else {
439 $indicator_tooltip = self::STATE_ACTIVE === $feature['state'] ? __( 'Active', 'elementor' ) : __( 'Inactive', 'elementor' );
440 }
441 ?>
442 <div class="e-experiment__title">
443 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; ?>"></div>
444 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; ?>"><?php echo $feature['title']; ?></label>
445 </div>
446 <?php
447
448 return ob_get_clean();
449 }
450
451 /**
452 * Get Feature Settings Label HTML
453 *
454 * @since 3.1.0
455 * @access private
456 *
457 * @param string $feature_name
458 *
459 * @return int
460 */
461 private function get_saved_feature_state( $feature_name ) {
462 return get_option( $this->get_feature_option_key( $feature_name ) );
463 }
464
465 /**
466 * Get Feature Actual State
467 *
468 * @since 3.1.0
469 * @access private
470 *
471 * @param array $feature
472 *
473 * @return string
474 */
475 private function get_feature_actual_state( array $feature ) {
476 if ( self::STATE_DEFAULT !== $feature['state'] ) {
477 return $feature['state'];
478 }
479
480 return $feature['default'];
481 }
482
483 /**
484 * On Feature State Change
485 *
486 * @since 3.1.0
487 * @access private
488 *
489 * @param array $old_feature_data
490 * @param string $new_state
491 */
492 private function on_feature_state_change( array $old_feature_data, $new_state ) {
493 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
494
495 $new_feature_data = $this->get_features( $old_feature_data['name'] );
496
497 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
498
499 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
500
501 if ( $actual_old_state === $actual_new_state ) {
502 return;
503 }
504
505 Plugin::$instance->files_manager->clear_cache();
506
507 if ( $new_feature_data['on_state_change'] ) {
508 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
509 }
510 }
511
512 public function __construct() {
513 $this->init_states();
514
515 $this->init_release_statuses();
516
517 $this->init_features();
518
519 if ( is_admin() ) {
520 $page_id = Settings::PAGE_ID;
521
522 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
523 $this->register_settings_fields( $settings );
524 }, 11 );
525 }
526 }
527 }
528