PluginProbe
Elementor Website Builder – more than just a page builder / 3.7.4
Elementor Website Builder – more than just a page builder v3.7.4
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.7.4, at core/experiments/manager.php

801 lines 25.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Experiments;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
6 use Elementor\Modules\System_Info\Module as System_Info;
7 use Elementor\Plugin;
8 use Elementor\Settings;
9 use Elementor\Tracker;
10 use Elementor\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 class Manager extends Base_Object {
17
18 const RELEASE_STATUS_DEV = 'dev';
19
20 const RELEASE_STATUS_ALPHA = 'alpha';
21
22 const RELEASE_STATUS_BETA = 'beta';
23
24 const RELEASE_STATUS_RC = 'rc';
25
26 const RELEASE_STATUS_STABLE = 'stable';
27
28 const STATE_DEFAULT = 'default';
29
30 const STATE_ACTIVE = 'active';
31
32 const STATE_INACTIVE = 'inactive';
33
34 private $states;
35
36 private $release_statuses;
37
38 private $features;
39
40 /**
41 * Add Feature
42 *
43 * @since 3.1.0
44 * @access public
45 *
46 * @param array $options {
47 * @type string $name
48 * @type string $title
49 * @type string $tag
50 * @type string $description
51 * @type string $release_status
52 * @type string $default
53 * @type callable $on_state_change
54 * }
55 *
56 * @return array|null
57 * @throws \Exception
58 */
59 public function add_feature( array $options ) {
60 if ( isset( $this->features[ $options['name'] ] ) ) {
61 return null;
62 }
63
64 $default_experimental_data = [
65 'tag' => '',
66 'description' => '',
67 'release_status' => self::RELEASE_STATUS_ALPHA,
68 'default' => self::STATE_INACTIVE,
69 'mutable' => true,
70 'new_site' => [
71 'default_active' => false,
72 'always_active' => false,
73 'minimum_installation_version' => null,
74 ],
75 'on_state_change' => null,
76 ];
77
78 $allowed_options = [ 'name', 'title', 'tag', 'description', 'release_status', 'default', 'mutable', 'new_site', 'on_state_change', 'dependencies' ];
79
80 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
81
82 $new_site = $experimental_data['new_site'];
83
84 if ( $new_site['default_active'] || $new_site['always_active'] ) {
85 $is_new_installation = Upgrade_Manager::install_compare( $new_site['minimum_installation_version'], '>=' );
86
87 if ( $is_new_installation ) {
88 if ( $new_site['always_active'] ) {
89 $experimental_data['state'] = self::STATE_ACTIVE;
90
91 $experimental_data['mutable'] = false;
92 } elseif ( $new_site['default_active'] ) {
93 $experimental_data['default'] = self::STATE_ACTIVE;
94 }
95 }
96 }
97
98 if ( $experimental_data['mutable'] ) {
99 $experimental_data['state'] = $this->get_saved_feature_state( $options['name'] );
100 }
101
102 if ( empty( $experimental_data['state'] ) ) {
103 $experimental_data['state'] = self::STATE_DEFAULT;
104 }
105
106 if ( ! empty( $experimental_data['dependencies'] ) ) {
107 foreach ( $experimental_data['dependencies'] as $key => $dependency ) {
108 $feature = $this->get_features( $dependency );
109 if ( ! class_exists( $dependency ) && ! empty( $feature ) ) {
110 $experimental_data['dependencies'][ $key ] = new Wrap_Core_Dependency( $feature );
111 } else {
112 $experimental_data['dependencies'][ $key ] = $dependency::instance();
113 }
114 }
115 }
116
117 $this->features[ $options['name'] ] = $experimental_data;
118
119 if ( $experimental_data['mutable'] && is_admin() ) {
120 $feature_option_key = $this->get_feature_option_key( $options['name'] );
121
122 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data, $feature_option_key ) {
123 try {
124 $this->on_feature_state_change( $experimental_data, $new_state, $feature_option_key );
125 } catch ( Exceptions\Dependency_Exception $e ) {
126 $message = sprintf(
127 '<p>%s</p><p><a href="#" onclick="location.href=\'%s\'">%s</a></p>',
128 esc_html( $e->getMessage() ),
129 site_url( 'wp-admin/admin.php?page=elementor#tab-experiments' ),
130 esc_html__( 'Back', 'elementor' )
131 );
132
133 wp_die( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 }
135 };
136
137 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
138 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
139 }
140
141 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
142
143 return $experimental_data;
144 }
145
146 /**
147 * Remove Feature
148 *
149 * @since 3.1.0
150 * @access public
151 *
152 * @param string $feature_name
153 */
154 public function remove_feature( $feature_name ) {
155 unset( $this->features[ $feature_name ] );
156 }
157
158 /**
159 * Get Features
160 *
161 * @since 3.1.0
162 * @access public
163 *
164 * @param string $feature_name Optional. Default is null
165 *
166 * @return array|null
167 */
168 public function get_features( $feature_name = null ) {
169 return self::get_items( $this->features, $feature_name );
170 }
171
172 /**
173 * Get Active Features
174 *
175 * @since 3.1.0
176 * @access public
177 *
178 * @return array
179 */
180 public function get_active_features() {
181 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
182 }
183
184 /**
185 * Is Feature Active
186 *
187 * @since 3.1.0
188 * @access public
189 *
190 * @param string $feature_name
191 *
192 * @return bool
193 */
194 public function is_feature_active( $feature_name ) {
195 $feature = $this->get_features( $feature_name );
196
197 if ( ! $feature ) {
198 return false;
199 }
200
201 return self::STATE_ACTIVE === $this->get_feature_actual_state( $feature );
202 }
203
204 /**
205 * Set Feature Default State
206 *
207 * @since 3.1.0
208 * @access public
209 *
210 * @param string $feature_name
211 * @param string $default_state
212 */
213 public function set_feature_default_state( $feature_name, $default_state ) {
214 $feature = $this->get_features( $feature_name );
215
216 if ( ! $feature ) {
217 return;
218 }
219
220 $this->features[ $feature_name ]['default'] = $default_state;
221 }
222
223 /**
224 * Get Feature Option Key
225 *
226 * @since 3.1.0
227 * @access public
228 *
229 * @param string $feature_name
230 *
231 * @return string
232 */
233 public function get_feature_option_key( $feature_name ) {
234 return 'elementor_experiment-' . $feature_name;
235 }
236
237 private function add_default_features() {
238 $this->add_feature( [
239 'name' => 'e_dom_optimization',
240 'title' => esc_html__( 'Optimized DOM Output', 'elementor' ),
241 'tag' => esc_html__( 'Performance', 'elementor' ),
242 'description' => esc_html__( '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' )
243 . ' <a href="https://go.elementor.com/wp-dash-legacy-optimized-dom/" target="_blank">'
244 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
245 'release_status' => self::RELEASE_STATUS_STABLE,
246 'new_site' => [
247 'default_active' => true,
248 'minimum_installation_version' => '3.1.0-beta',
249 ],
250 ] );
251
252 $this->add_feature( [
253 'name' => 'e_optimized_assets_loading',
254 'title' => esc_html__( 'Improved Asset Loading', 'elementor' ),
255 'tag' => esc_html__( 'Performance', 'elementor' ),
256 'description' => esc_html__( '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' )
257 . ' <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">'
258 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
259 'release_status' => self::RELEASE_STATUS_STABLE,
260 'new_site' => [
261 'default_active' => true,
262 'minimum_installation_version' => '3.2.0-beta',
263 ],
264 ] );
265
266 $this->add_feature( [
267 'name' => 'e_optimized_css_loading',
268 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
269 'tag' => esc_html__( 'Performance', 'elementor' ),
270 'description' => esc_html__( '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' )
271 . ' <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">'
272 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
273 'release_status' => self::RELEASE_STATUS_RC,
274 'new_site' => [
275 'default_active' => true,
276 'minimum_installation_version' => '3.3.0-beta',
277 ],
278 ] );
279
280 $this->add_feature( [
281 'name' => 'e_font_icon_svg',
282 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
283 'tag' => esc_html__( 'Performance', 'elementor' ),
284 'description' => esc_html__( 'The “Inline Font Icons” will render the icons as inline SVG without loading the Font-Awesome and the eicons libraries and its related CSS files and fonts.', 'elementor' )
285 . ' <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">'
286 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
287 'release_status' => self::RELEASE_STATUS_ALPHA,
288 ] );
289
290 $this->add_feature( [
291 'name' => 'a11y_improvements',
292 'title' => esc_html__( 'Accessibility Improvements', 'elementor' ),
293 'tag' => esc_html__( 'Performance', 'elementor' ),
294 'description' => esc_html__( 'An array of accessibility enhancements in Elementor pages.', 'elementor' )
295 . '<br><strong>' . esc_html__( 'Please note!', 'elementor' ) . '</strong> ' . esc_html__( 'These enhancements may include some markup changes to existing elementor widgets', 'elementor' )
296 . ' <a href="https://go.elementor.com/wp-dash-a11y-improvements/" target="_blank">'
297 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
298 'release_status' => self::RELEASE_STATUS_STABLE,
299 'new_site' => [
300 'default_active' => true,
301 'minimum_installation_version' => '3.1.0-beta',
302 ],
303 ] );
304
305 $this->add_feature( [
306 'name' => 'additional_custom_breakpoints',
307 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
308 'tag' => esc_html__( 'Performance', 'elementor' ),
309 'description' => esc_html__( 'Get pixel-perfect design for every screen size. You can now add up to 6 customizable breakpoints beyond the default desktop setting: mobile, mobile extra, tablet, tablet extra, laptop, and widescreen.', 'elementor' )
310 . ' <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">'
311 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
312 'release_status' => self::RELEASE_STATUS_STABLE,
313 'new_site' => [
314 'default_active' => true,
315 'minimum_installation_version' => '3.4.0-beta',
316 ],
317 ] );
318
319 $this->add_feature( [
320 'name' => 'e_import_export',
321 'title' => esc_html__( 'Import Export Template Kit', 'elementor' ),
322 'tag' => esc_html__( 'Feature', 'elementor' ),
323 'description' => esc_html__( 'Design sites faster with a template kit that contains some or all components of a complete site, like templates, content & site settings.', 'elementor' )
324 . '<br>'
325 . esc_html__( 'You can import a kit and apply it to your site, or export the elements from this site to be used anywhere else.', 'elementor' ),
326 'release_status' => self::RELEASE_STATUS_STABLE,
327 'default' => self::STATE_ACTIVE,
328 'new_site' => [
329 'default_active' => true,
330 'minimum_installation_version' => '3.2.0-beta',
331 ],
332 ] );
333
334 $this->add_feature( [
335 'name' => 'e_hidden_wordpress_widgets',
336 'title' => esc_html__( 'Hide native WordPress widgets from search results', 'elementor' ),
337 'tag' => esc_html__( 'Improvement', 'elementor' ),
338 'description' => esc_html__( 'WordPress widgets will not be shown when searching in the editor panel. Instead, these widgets can be found in the “WordPress” dropdown at the bottom of the panel.', 'elementor' ),
339 'release_status' => self::RELEASE_STATUS_STABLE,
340 'default' => self::STATE_ACTIVE,
341 ] );
342
343 $this->add_feature( [
344 'name' => 'admin_menu_rearrangement',
345 'mutable' => false,
346 ] );
347
348 $this->add_feature( [
349 'name' => 'container',
350 'title' => esc_html__( 'Flexbox Container', 'elementor' ),
351 'tag' => esc_html__( 'Feature', 'elementor' ),
352 'description' => sprintf( esc_html__(
353 'Create advanced layouts and responsive designs with the new %1$sFlexbox Container element%2$s.
354 This experiment replaces the current section/column structure, but you\'ll still keep your existing
355 Sections, Inner Sections and Columns and be able to edit them. Ready to give it a try? Check out the %3$sFlexbox playground%4$s.',
356 'elementor'
357 ), '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container/">', '</a>', '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container-playground/">', '</a>'),
358 'release_status' => self::RELEASE_STATUS_ALPHA,
359 'default' => self::STATE_INACTIVE,
360 ] );
361 }
362
363 /**
364 * Init States
365 *
366 * @since 3.1.0
367 * @access private
368 */
369 private function init_states() {
370 $this->states = [
371 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
372 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
373 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
374 ];
375 }
376
377 /**
378 * Init Statuses
379 *
380 * @since 3.1.0
381 * @access private
382 */
383 private function init_release_statuses() {
384 $this->release_statuses = [
385 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
386 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
387 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
388 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
389 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
390 ];
391 }
392
393 /**
394 * Init Features
395 *
396 * @since 3.1.0
397 * @access private
398 */
399 private function init_features() {
400 $this->features = [];
401
402 $this->add_default_features();
403
404 do_action( 'elementor/experiments/default-features-registered', $this );
405 }
406
407 /**
408 * Register Settings Fields
409 *
410 * @param Settings $settings
411 *
412 * @since 3.1.0
413 * @access private
414 *
415 */
416 private function register_settings_fields( Settings $settings ) {
417 $features = $this->get_features();
418
419 $fields = [];
420
421 foreach ( $features as $feature_name => $feature ) {
422 if ( ! $feature['mutable'] ) {
423 unset( $features[ $feature_name ] );
424
425 continue;
426 }
427
428 $feature_key = 'experiment-' . $feature_name;
429
430 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
431
432 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
433
434 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
435
436 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
437 $this->render_feature_settings_field( $feature );
438 };
439 }
440
441 foreach ( [ 'stable', 'ongoing' ] as $section ) {
442 if ( ! isset( $fields[ $section ] ) ) {
443 $fields[ $section ]['no_features'] = [
444 'label' => esc_html__( 'No available experiments', 'elementor' ),
445 'field_args' => [
446 'type' => 'raw_html',
447 'html' => esc_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' ),
448 ],
449 ];
450 }
451
452 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
453 $fields[ $section ] += $settings->get_usage_fields();
454 }
455 }
456
457 $settings->add_tab(
458 'experiments', [
459 'label' => esc_html__( 'Experiments', 'elementor' ),
460 'sections' => [
461 'ongoing_experiments' => [
462 'callback' => function() {
463 $this->render_settings_intro();
464 },
465 'fields' => $fields['ongoing'],
466 ],
467 'stable_experiments' => [
468 'callback' => function() {
469 $this->render_stable_section_title();
470 },
471 'fields' => $fields['stable'],
472 ],
473 ],
474 ]
475 );
476 }
477
478 private function render_stable_section_title() {
479 ?>
480 <hr>
481 <h2>
482 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
483 </h2>
484 <?php
485 }
486
487 /**
488 * Render Settings Intro
489 *
490 * @since 3.1.0
491 * @access private
492 */
493 private function render_settings_intro() {
494 ?>
495 <h2>
496 <?php echo esc_html__( 'Elementor Experiments', 'elementor' ); ?>
497 </h2>
498 <p class="e-experiment__description">
499 <?php
500 printf(
501 /* translators: %1$s Link open tag, %2$s: Link close tag. */
502 esc_html__( '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. %1$sLearn More.%2$s', 'elementor' ),
503 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
504 '</a>'
505 );
506 ?>
507 </p>
508 <p class="e-experiment__description">
509 <?php echo esc_html__( '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' ); ?>
510 </p>
511 <p class="e-experiment__description">
512 <?php
513 printf(
514 /* translators: %1$s Link open tag, %2$s: Link close tag. */
515 esc_html__( 'Your feedback is important - %1$shelp us%2$s improve these features by sharing your thoughts and inputs.', 'elementor' ),
516 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
517 '</a>'
518 );
519 ?>
520 </p>
521 <?php if ( $this->get_features() ) { ?>
522 <button type="button" class="button e-experiment__button" value="active">Activate All Experiments</button>
523 <button type="button" class="button e-experiment__button" value="inactive">Deactivate All Experiments</button>
524 <?php } ?>
525 <hr>
526 <h2 class="e-experiment__table-title">
527 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
528 </h2>
529 <?php
530 }
531
532 /**
533 * Render Feature Settings Field
534 *
535 * @since 3.1.0
536 * @access private
537 *
538 * @param array $feature
539 */
540 private function render_feature_settings_field( array $feature ) {
541 ?>
542 <div class="e-experiment__content">
543 <select id="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" class="e-experiment__select" name="<?php echo $this->get_feature_option_key( $feature['name'] ); ?>">
544 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
545 <option value="<?php echo $state_key; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
546 <?php } ?>
547 </select>
548 <p class="description"><?php echo $feature['description']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
549 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
550 <div class="e-experiment__status"><?php echo sprintf( esc_html__( 'Status: %s', 'elementor' ), $this->release_statuses[ $feature['release_status'] ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
551 <?php } ?>
552 </div>
553 <?php $this->render_feature_dependency( $feature );
554 }
555
556 private function render_feature_dependency( $feature ) {
557 if ( ! empty( $feature['dependencies'] ) ) {
558 ?>
559 <div class="e-experiment__dependency">
560 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Depends on', 'elementor' ); ?>:</strong>
561 <?php foreach ( $feature['dependencies'] as $dependency ) : ?>
562 <span class="e-experiment__dependency__item"><?php Utils::print_unescaped_internal_string( $dependency->get_name() ); ?></span>
563 <?php endforeach; ?>
564 </div>
565 <?php
566 }
567 }
568
569 /**
570 * Get Feature Settings Label HTML
571 *
572 * @since 3.1.0
573 * @access private
574 *
575 * @param array $feature
576 *
577 * @return string
578 */
579 private function get_feature_settings_label_html( array $feature ) {
580 ob_start();
581
582 $is_feature_active = $this->is_feature_active( $feature['name'] );
583
584 $indicator_classes = 'e-experiment__title__indicator';
585
586 if ( $is_feature_active ) {
587 $indicator_classes .= ' e-experiment__title__indicator--active';
588 }
589
590 $indicator_tooltip = $this->get_feature_state_label( $feature );
591
592 ?>
593 <div class="e-experiment__title">
594 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
595 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
596 <?php if ( ! empty( $feature['tag'] ) ) : ?>
597 <span class="e-experiment__title__tag"><?php echo $feature['tag']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
598 <?php endif; ?>
599 </div>
600 <?php
601
602 return ob_get_clean();
603 }
604
605 /**
606 * Get Feature State Label
607 *
608 * @param array $feature
609 *
610 * @return string
611 */
612 public function get_feature_state_label( array $feature ) {
613 $is_feature_active = $this->is_feature_active( $feature['name'] );
614
615 if ( self::STATE_DEFAULT === $feature['state'] ) {
616 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
617 esc_html__( 'Inactive by default', 'elementor' );
618 } else {
619 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
620 esc_html__( 'Inactive', 'elementor' );
621 }
622
623 return $label;
624 }
625
626 /**
627 * Get Feature Settings Label HTML
628 *
629 * @since 3.1.0
630 * @access private
631 *
632 * @param string $feature_name
633 *
634 * @return int
635 */
636 private function get_saved_feature_state( $feature_name ) {
637 return get_option( $this->get_feature_option_key( $feature_name ) );
638 }
639
640 /**
641 * Get Feature Actual State
642 *
643 * @since 3.1.0
644 * @access private
645 *
646 * @param array $feature
647 *
648 * @return string
649 */
650 private function get_feature_actual_state( array $feature ) {
651 if ( self::STATE_DEFAULT !== $feature['state'] ) {
652 return $feature['state'];
653 }
654
655 return $feature['default'];
656 }
657
658 /**
659 * On Feature State Change
660 *
661 * @since 3.1.0
662 * @access private
663 *
664 * @param array $old_feature_data
665 * @param string $new_state
666 *
667 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
668 */
669 private function on_feature_state_change( array $old_feature_data, $new_state, $feature_option_key ) {
670 $new_feature_data = $this->get_features( $old_feature_data['name'] );
671
672 $this->validate_dependency( $new_feature_data, $new_state );
673
674 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
675
676 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
677
678 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
679
680 if ( $actual_old_state === $actual_new_state ) {
681 return;
682 }
683
684 Plugin::$instance->files_manager->clear_cache();
685
686 if ( $new_feature_data['on_state_change'] ) {
687 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
688 }
689 }
690
691 /**
692 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
693 */
694 private function validate_dependency( array $feature, $new_state ) {
695 $rollback = function ( $feature_option_key, $state ) {
696 remove_all_actions( 'add_option_' . $feature_option_key );
697 remove_all_actions( 'update_option_' . $feature_option_key );
698
699 update_option( $feature_option_key, $state );
700 };
701
702 if ( self::STATE_DEFAULT === $new_state ) {
703 $new_state = $this->get_feature_actual_state( $feature );
704 }
705
706 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
707
708 if ( self::STATE_ACTIVE === $new_state ) {
709 if ( empty( $feature['dependencies'] ) ) {
710 return;
711 }
712
713 // Validate if the current feature dependency is available.
714 foreach ( $feature['dependencies'] as $dependency ) {
715 $dependency_feature = $this->get_features( $dependency->get_name() );
716
717 if ( ! $dependency_feature ) {
718 $rollback( $feature_option_key, self::STATE_INACTIVE );
719
720 throw new Exceptions\Dependency_Exception(
721 sprintf( 'The feature `%s` has a dependency `%s` that is not available.',
722 $feature['name'],
723 $dependency->get_name()
724 )
725 );
726 }
727
728 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
729
730 // If dependency is not active.
731 if ( self::STATE_INACTIVE === $dependency_state ) {
732 $rollback( $feature_option_key, self::STATE_INACTIVE );
733
734 /* translators: 1: feature_name_that_change_state, 2: dependency_feature_name. */
735 throw new Exceptions\Dependency_Exception(
736 sprintf(
737 esc_html__( 'To turn on `%1$s`, Experiment: `%2$s` activity is required!', 'elementor' ),
738 $feature['name'],
739 $dependency_feature['name']
740 )
741 );
742 }
743 }
744 } elseif ( self::STATE_INACTIVE === $new_state ) {
745 // Validate if current feature that goes 'inactive' is not a dependency of current active feature.
746 foreach ( $this->get_features() as $current_feature ) {
747 if ( empty( $current_feature['dependencies'] ) ) {
748 continue;
749 }
750
751 $current_feature_state = $this->get_feature_actual_state( $current_feature );
752
753 foreach ( $current_feature['dependencies'] as $dependency ) {
754 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
755 $rollback( $feature_option_key, self::STATE_ACTIVE );
756
757 /* translators: 1: feature_name_that_change_state, 2: dependency_feature_name. */
758 throw new Exceptions\Dependency_Exception(
759 sprintf(
760 esc_html__( 'Cannot turn off `%1$s`, Experiment: `%2$s` is still active!', 'elementor' ),
761 $feature['name'],
762 $current_feature['name']
763 )
764 );
765 }
766 }
767 }
768 }
769 }
770
771 public function __construct() {
772 $this->init_states();
773
774 $this->init_release_statuses();
775
776 $this->init_features();
777
778 add_action( 'admin_init', function () {
779 System_Info::add_report(
780 'experiments', [
781 'file_name' => __DIR__ . '/experiments-reporter.php',
782 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
783 ]
784 );
785 }, 79 /* Before log */ );
786
787 if ( is_admin() ) {
788 $page_id = Settings::PAGE_ID;
789
790 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
791 $this->register_settings_fields( $settings );
792 }, 11 );
793 }
794
795 // Register CLI commands.
796 if ( Utils::is_wp_cli() ) {
797 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
798 }
799 }
800 }
801