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

843 lines 25.8 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\Core\Utils\Collection;
7 use Elementor\Modules\System_Info\Module as System_Info;
8 use Elementor\Plugin;
9 use Elementor\Settings;
10 use Elementor\Tracker;
11 use Elementor\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 class Manager extends Base_Object {
18
19 const RELEASE_STATUS_DEV = 'dev';
20
21 const RELEASE_STATUS_ALPHA = 'alpha';
22
23 const RELEASE_STATUS_BETA = 'beta';
24
25 const RELEASE_STATUS_RC = 'rc';
26
27 const RELEASE_STATUS_STABLE = 'stable';
28
29 const STATE_DEFAULT = 'default';
30
31 const STATE_ACTIVE = 'active';
32
33 const STATE_INACTIVE = 'inactive';
34
35 const TYPE_HIDDEN = 'hidden';
36
37 private $states;
38
39 private $release_statuses;
40
41 private $features;
42
43 /**
44 * Add Feature
45 *
46 * @since 3.1.0
47 * @access public
48 *
49 * @param array $options {
50 * @type string $name
51 * @type string $title
52 * @type string $tag
53 * @type string $description
54 * @type string $release_status
55 * @type string $default
56 * @type callable $on_state_change
57 * }
58 *
59 * @return array|null
60 * @throws \Exception
61 */
62 public function add_feature( array $options ) {
63 if ( isset( $this->features[ $options['name'] ] ) ) {
64 return null;
65 }
66
67 $default_experimental_data = [
68 'tag' => '',
69 'description' => '',
70 'release_status' => self::RELEASE_STATUS_ALPHA,
71 'default' => self::STATE_INACTIVE,
72 'mutable' => true,
73 static::TYPE_HIDDEN => false,
74 'new_site' => [
75 'default_active' => false,
76 'always_active' => false,
77 'minimum_installation_version' => null,
78 ],
79 'on_state_change' => null,
80 'generator_tag' => false,
81 ];
82
83 $allowed_options = [ 'name', 'title', 'tag', 'description', 'release_status', 'default', 'mutable', static::TYPE_HIDDEN, 'new_site', 'on_state_change', 'dependencies', 'generator_tag' ];
84
85 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
86
87 $new_site = $experimental_data['new_site'];
88
89 if ( $new_site['default_active'] || $new_site['always_active'] ) {
90 $is_new_installation = Upgrade_Manager::install_compare( $new_site['minimum_installation_version'], '>=' );
91
92 if ( $is_new_installation ) {
93 if ( $new_site['always_active'] ) {
94 $experimental_data['state'] = self::STATE_ACTIVE;
95
96 $experimental_data['mutable'] = false;
97 } elseif ( $new_site['default_active'] ) {
98 $experimental_data['default'] = self::STATE_ACTIVE;
99 }
100 }
101 }
102
103 if ( $experimental_data['mutable'] ) {
104 $experimental_data['state'] = $this->get_saved_feature_state( $options['name'] );
105 }
106
107 if ( empty( $experimental_data['state'] ) ) {
108 $experimental_data['state'] = self::STATE_DEFAULT;
109 }
110
111 if ( ! empty( $experimental_data['dependencies'] ) ) {
112 foreach ( $experimental_data['dependencies'] as $key => $dependency ) {
113 $feature = $this->get_features( $dependency );
114
115 if ( ! empty( $feature[ static::TYPE_HIDDEN ] ) ) {
116 throw new Exceptions\Dependency_Exception( 'Depending on a hidden experiment is not allowed.' );
117 }
118
119 $experimental_data['dependencies'][ $key ] = $this->create_dependency_class( $dependency, $feature );
120 }
121 }
122
123 $this->features[ $options['name'] ] = $experimental_data;
124
125 if ( $experimental_data['mutable'] && is_admin() ) {
126 $feature_option_key = $this->get_feature_option_key( $options['name'] );
127
128 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data, $feature_option_key ) {
129 try {
130 $this->on_feature_state_change( $experimental_data, $new_state, $old_state );
131 } catch ( Exceptions\Dependency_Exception $e ) {
132 $message = sprintf(
133 '<p>%s</p><p><a href="#" onclick="location.href=\'%s\'">%s</a></p>',
134 esc_html( $e->getMessage() ),
135 site_url( 'wp-admin/admin.php?page=elementor#tab-experiments' ),
136 esc_html__( 'Back', 'elementor' )
137 );
138
139 wp_die( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
140 }
141 };
142
143 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
144 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
145 }
146
147 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
148
149 return $experimental_data;
150 }
151
152 /**
153 * Remove Feature
154 *
155 * @since 3.1.0
156 * @access public
157 *
158 * @param string $feature_name
159 */
160 public function remove_feature( $feature_name ) {
161 unset( $this->features[ $feature_name ] );
162 }
163
164 /**
165 * Get Features
166 *
167 * @since 3.1.0
168 * @access public
169 *
170 * @param string $feature_name Optional. Default is null
171 *
172 * @return array|null
173 */
174 public function get_features( $feature_name = null ) {
175 return self::get_items( $this->features, $feature_name );
176 }
177
178 /**
179 * Get Active Features
180 *
181 * @since 3.1.0
182 * @access public
183 *
184 * @return array
185 */
186 public function get_active_features() {
187 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
188 }
189
190 /**
191 * Is Feature Active
192 *
193 * @since 3.1.0
194 * @access public
195 *
196 * @param string $feature_name
197 *
198 * @return bool
199 */
200 public function is_feature_active( $feature_name ) {
201 $feature = $this->get_features( $feature_name );
202
203 if ( ! $feature ) {
204 return false;
205 }
206
207 return self::STATE_ACTIVE === $this->get_feature_actual_state( $feature );
208 }
209
210 /**
211 * Set Feature Default State
212 *
213 * @since 3.1.0
214 * @access public
215 *
216 * @param string $feature_name
217 * @param string $default_state
218 */
219 public function set_feature_default_state( $feature_name, $default_state ) {
220 $feature = $this->get_features( $feature_name );
221
222 if ( ! $feature ) {
223 return;
224 }
225
226 $this->features[ $feature_name ]['default'] = $default_state;
227 }
228
229 /**
230 * Get Feature Option Key
231 *
232 * @since 3.1.0
233 * @access public
234 *
235 * @param string $feature_name
236 *
237 * @return string
238 */
239 public function get_feature_option_key( $feature_name ) {
240 return 'elementor_experiment-' . $feature_name;
241 }
242
243 private function add_default_features() {
244 $this->add_feature( [
245 'name' => 'e_dom_optimization',
246 'title' => esc_html__( 'Optimized DOM Output', 'elementor' ),
247 'tag' => esc_html__( 'Performance', 'elementor' ),
248 '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' )
249 . ' <a href="https://go.elementor.com/wp-dash-legacy-optimized-dom/" target="_blank">'
250 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
251 'release_status' => self::RELEASE_STATUS_STABLE,
252 'new_site' => [
253 'default_active' => true,
254 'minimum_installation_version' => '3.1.0-beta',
255 ],
256 'generator_tag' => true,
257 ] );
258
259 $this->add_feature( [
260 'name' => 'e_optimized_assets_loading',
261 'title' => esc_html__( 'Improved Asset Loading', 'elementor' ),
262 'tag' => esc_html__( 'Performance', 'elementor' ),
263 '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' )
264 . ' <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">'
265 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
266 'release_status' => self::RELEASE_STATUS_STABLE,
267 'new_site' => [
268 'default_active' => true,
269 'minimum_installation_version' => '3.2.0-beta',
270 ],
271 'generator_tag' => true,
272 ] );
273
274 $this->add_feature( [
275 'name' => 'e_optimized_css_loading',
276 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
277 'tag' => esc_html__( 'Performance', 'elementor' ),
278 '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' )
279 . ' <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">'
280 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
281 'release_status' => self::RELEASE_STATUS_STABLE,
282 'new_site' => [
283 'default_active' => true,
284 'minimum_installation_version' => '3.3.0-beta',
285 ],
286 'generator_tag' => true,
287 ] );
288
289 $this->add_feature( [
290 'name' => 'e_font_icon_svg',
291 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
292 'tag' => esc_html__( 'Performance', 'elementor' ),
293 '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' )
294 . ' <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">'
295 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
296 'release_status' => self::RELEASE_STATUS_BETA,
297 'generator_tag' => true,
298 ] );
299
300 $this->add_feature( [
301 'name' => 'a11y_improvements',
302 'title' => esc_html__( 'Accessibility Improvements', 'elementor' ),
303 'tag' => esc_html__( 'Performance', 'elementor' ),
304 'description' => esc_html__( 'An array of accessibility enhancements in Elementor pages.', 'elementor' )
305 . '<br><strong>' . esc_html__( 'Please note!', 'elementor' ) . '</strong> ' . esc_html__( 'These enhancements may include some markup changes to existing elementor widgets', 'elementor' )
306 . ' <a href="https://go.elementor.com/wp-dash-a11y-improvements/" target="_blank">'
307 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
308 'release_status' => self::RELEASE_STATUS_STABLE,
309 'new_site' => [
310 'default_active' => true,
311 'minimum_installation_version' => '3.1.0-beta',
312 ],
313 'generator_tag' => true,
314 ] );
315
316 $this->add_feature( [
317 'name' => 'additional_custom_breakpoints',
318 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
319 'tag' => esc_html__( 'Performance', 'elementor' ),
320 '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' )
321 . ' <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">'
322 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
323 'release_status' => self::RELEASE_STATUS_STABLE,
324 'new_site' => [
325 'default_active' => true,
326 'minimum_installation_version' => '3.4.0-beta',
327 ],
328 'generator_tag' => true,
329 ] );
330
331 $this->add_feature( [
332 'name' => 'admin_menu_rearrangement',
333 'mutable' => false,
334 ] );
335
336 $this->add_feature( [
337 'name' => 'container',
338 'title' => esc_html__( 'Flexbox Container', 'elementor' ),
339 'tag' => esc_html__( 'Feature', 'elementor' ),
340 'description' => sprintf( esc_html__(
341 'Create advanced layouts and responsive designs with the new %1$sFlexbox Container element%2$s.
342 This experiment replaces the current section/column structure, but you\'ll still keep your existing
343 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.',
344 'elementor'
345 ), '<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>'),
346 'release_status' => self::RELEASE_STATUS_RC,
347 'default' => self::STATE_INACTIVE,
348 ] );
349
350 $this->add_feature( [
351 'name' => 'e_swiper_latest',
352 'title' => esc_html__( 'Upgrade Swiper Library', 'elementor' ),
353 'description' => esc_html__( 'Prepare your website for future improvements to carousel features by upgrading the Swiper library integrated into your site from v5.36 to v8.45. This experiment includes markup changes so it might require updating custom code and cause compatibility issues with third party plugins.', 'elementor' ),
354 'release_status' => self::RELEASE_STATUS_BETA,
355 'new_site' => [
356 'default_active' => true,
357 'minimum_installation_version' => '3.11.0',
358 ],
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 $is_hidden = $feature[ static::TYPE_HIDDEN ];
423 $is_mutable = $feature['mutable'];
424 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
425
426 if ( $should_hide_experiment ) {
427 unset( $features[ $feature_name ] );
428
429 continue;
430 }
431
432 $feature_key = 'experiment-' . $feature_name;
433
434 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
435
436 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
437
438 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
439
440 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
441 $this->render_feature_settings_field( $feature );
442 };
443 }
444
445 foreach ( [ 'stable', 'ongoing' ] as $section ) {
446 if ( ! isset( $fields[ $section ] ) ) {
447 $fields[ $section ]['no_features'] = [
448 'label' => esc_html__( 'No available experiments', 'elementor' ),
449 'field_args' => [
450 'type' => 'raw_html',
451 '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' ),
452 ],
453 ];
454 }
455
456 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
457 $fields[ $section ] += $settings->get_usage_fields();
458 }
459 }
460
461 $settings->add_tab(
462 'experiments', [
463 'label' => esc_html__( 'Features', 'elementor' ),
464 'sections' => [
465 'ongoing_experiments' => [
466 'callback' => function() {
467 $this->render_settings_intro();
468 },
469 'fields' => $fields['ongoing'],
470 ],
471 'stable_experiments' => [
472 'callback' => function() {
473 $this->render_stable_section_title();
474 },
475 'fields' => $fields['stable'],
476 ],
477 ],
478 ]
479 );
480 }
481
482 private function render_stable_section_title() {
483 ?>
484 <hr>
485 <h2>
486 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
487 </h2>
488 <?php
489 }
490
491 /**
492 * Render Settings Intro
493 *
494 * @since 3.1.0
495 * @access private
496 */
497 private function render_settings_intro() {
498 ?>
499 <h2>
500 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
501 </h2>
502 <p class="e-experiment__description">
503 <?php
504 printf(
505 /* translators: %1$s Link open tag, %2$s: Link close tag. */
506 esc_html__( 'Personalize your Elementor experience by controlling which features and experiments are active on your site. Help make Elementor better by %1$ssharing your experience and feedback with us%2$s.', 'elementor' ),
507 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
508 '</a>'
509 );
510 ?>
511 </p>
512 <p class="e-experiment__description">
513 <?php
514 printf(
515 /* translators: %1$s Link open tag, %2$s: Link close tag. */
516 esc_html__( 'To use an experiment or feature on your site, simply click on the dropdown next to it and switch to Active. You can always deactivate them at any time. %1$sLearn More.%2$s', 'elementor' ),
517 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
518 '</a>'
519 );
520 ?>
521 </p>
522
523 <?php if ( $this->get_features() ) { ?>
524 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
525 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
526 <?php } ?>
527 <hr>
528 <h2 class="e-experiment__table-title">
529 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
530 </h2>
531 <?php
532 }
533
534 /**
535 * Render Feature Settings Field
536 *
537 * @since 3.1.0
538 * @access private
539 *
540 * @param array $feature
541 */
542 private function render_feature_settings_field( array $feature ) {
543 $control_id = 'e-experiment-' . $feature['name'];
544 $control_name = $this->get_feature_option_key( $feature['name'] );
545
546 $status = sprintf(
547 esc_html__( 'Status: %s', 'elementor' ),
548 $this->release_statuses[ $feature['release_status'] ]
549 );
550
551 ?>
552 <div class="e-experiment__content">
553 <select class="e-experiment__select"
554 id="<?php echo esc_attr( $control_id ); ?>"
555 name="<?php echo esc_attr( $control_name ); ?>"
556 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
557 >
558 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
559 <option value="<?php echo esc_attr( $state_key ); ?>"
560 <?php selected( $state_key, $feature['state'] ); ?>
561 >
562 <?php echo esc_html( $state_title ); ?>
563 </option>
564 <?php } ?>
565 </select>
566
567 <p class="description">
568 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
569 </p>
570
571 <?php $this->render_feature_dependency( $feature ); ?>
572
573 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
574 <div class="e-experiment__status">
575 <?php echo esc_html( $status ); ?>
576 </div>
577 <?php } ?>
578 </div>
579 <?php
580 }
581
582 private function render_feature_dependency( $feature ) {
583 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
584 ->map( function ( $dependency ) {
585 return $dependency->get_title();
586 } )
587 ->implode( ', ' );
588
589 if ( empty( $dependencies ) ) {
590 return;
591 }
592
593 ?>
594 <div class="e-experiment__dependency">
595 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
596 <span><?php echo esc_html( $dependencies ); ?></span>
597 </div>
598 <?php
599 }
600
601 private function has_non_existing_dependency( $feature ) {
602 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
603 ->find( function ( $dependency ) {
604 return $dependency instanceof Non_Existing_Dependency;
605 } );
606
607 return ! ! $non_existing_dep;
608 }
609
610 /**
611 * Get Feature Settings Label HTML
612 *
613 * @since 3.1.0
614 * @access private
615 *
616 * @param array $feature
617 *
618 * @return string
619 */
620 private function get_feature_settings_label_html( array $feature ) {
621 ob_start();
622
623 $is_feature_active = $this->is_feature_active( $feature['name'] );
624
625 $indicator_classes = 'e-experiment__title__indicator';
626
627 if ( $is_feature_active ) {
628 $indicator_classes .= ' e-experiment__title__indicator--active';
629 }
630
631 $indicator_tooltip = $this->get_feature_state_label( $feature );
632
633 ?>
634 <div class="e-experiment__title">
635 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
636 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
637 <?php if ( ! empty( $feature['tag'] ) ) : ?>
638 <span class="e-experiment__title__tag"><?php echo $feature['tag']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
639 <?php endif; ?>
640 </div>
641 <?php
642
643 return ob_get_clean();
644 }
645
646 /**
647 * Get Feature State Label
648 *
649 * @param array $feature
650 *
651 * @return string
652 */
653 public function get_feature_state_label( array $feature ) {
654 $is_feature_active = $this->is_feature_active( $feature['name'] );
655
656 if ( self::STATE_DEFAULT === $feature['state'] ) {
657 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
658 esc_html__( 'Inactive by default', 'elementor' );
659 } else {
660 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
661 esc_html__( 'Inactive', 'elementor' );
662 }
663
664 return $label;
665 }
666
667 /**
668 * Get Feature Settings Label HTML
669 *
670 * @since 3.1.0
671 * @access private
672 *
673 * @param string $feature_name
674 *
675 * @return int
676 */
677 private function get_saved_feature_state( $feature_name ) {
678 return get_option( $this->get_feature_option_key( $feature_name ) );
679 }
680
681 /**
682 * Get Feature Actual State
683 *
684 * @since 3.1.0
685 * @access private
686 *
687 * @param array $feature
688 *
689 * @return string
690 */
691 private function get_feature_actual_state( array $feature ) {
692 if ( self::STATE_DEFAULT !== $feature['state'] ) {
693 return $feature['state'];
694 }
695
696 return $feature['default'];
697 }
698
699 /**
700 * On Feature State Change
701 *
702 * @since 3.1.0
703 * @access private
704 *
705 * @param array $old_feature_data
706 * @param string $new_state
707 *
708 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
709 */
710 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
711 $new_feature_data = $this->get_features( $old_feature_data['name'] );
712 $this->validate_dependency( $new_feature_data, $new_state );
713 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
714 if ( $old_state === $new_state ) {
715 return;
716 }
717
718 Plugin::$instance->files_manager->clear_cache();
719 if ( $new_feature_data['on_state_change'] ) {
720 $new_feature_data['on_state_change']( $old_state, $new_state );
721 }
722
723 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
724 }
725
726 /**
727 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
728 */
729 private function validate_dependency( array $feature, $new_state ) {
730 $rollback = function ( $feature_option_key, $state ) {
731 remove_all_actions( 'add_option_' . $feature_option_key );
732 remove_all_actions( 'update_option_' . $feature_option_key );
733
734 update_option( $feature_option_key, $state );
735 };
736
737 if ( self::STATE_DEFAULT === $new_state ) {
738 $new_state = $this->get_feature_actual_state( $feature );
739 }
740
741 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
742
743 if ( self::STATE_ACTIVE === $new_state ) {
744 if ( empty( $feature['dependencies'] ) ) {
745 return;
746 }
747
748 // Validate if the current feature dependency is available.
749 foreach ( $feature['dependencies'] as $dependency ) {
750 $dependency_feature = $this->get_features( $dependency->get_name() );
751
752 if ( ! $dependency_feature ) {
753 $rollback( $feature_option_key, self::STATE_INACTIVE );
754
755 throw new Exceptions\Dependency_Exception(
756 sprintf(
757 'The feature `%s` has a dependency `%s` that is not available.',
758 $feature['name'],
759 $dependency->get_name()
760 )
761 );
762 }
763
764 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
765
766 // If dependency is not active.
767 if ( self::STATE_INACTIVE === $dependency_state ) {
768 $rollback( $feature_option_key, self::STATE_INACTIVE );
769
770 throw new Exceptions\Dependency_Exception(
771 sprintf(
772 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
773 $feature['name'],
774 $dependency_feature['name']
775 )
776 );
777 }
778 }
779 } elseif ( self::STATE_INACTIVE === $new_state ) {
780 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
781 foreach ( $this->get_features() as $current_feature ) {
782 if ( empty( $current_feature['dependencies'] ) ) {
783 continue;
784 }
785
786 $current_feature_state = $this->get_feature_actual_state( $current_feature );
787
788 foreach ( $current_feature['dependencies'] as $dependency ) {
789 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
790 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
791 }
792 }
793 }
794 }
795 }
796
797 private function should_show_hidden() {
798 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
799 }
800
801 private function create_dependency_class( $dependency_name, $dependency_args ) {
802 if ( class_exists( $dependency_name ) ) {
803 return $dependency_name::instance();
804 }
805
806 if ( ! empty( $dependency_args ) ) {
807 return new Wrap_Core_Dependency( $dependency_args );
808 }
809
810 return new Non_Existing_Dependency( $dependency_name );
811 }
812
813 public function __construct() {
814 $this->init_states();
815
816 $this->init_release_statuses();
817
818 $this->init_features();
819
820 add_action( 'admin_init', function () {
821 System_Info::add_report(
822 'experiments', [
823 'file_name' => __DIR__ . '/experiments-reporter.php',
824 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
825 ]
826 );
827 }, 79 /* Before log */ );
828
829 if ( is_admin() ) {
830 $page_id = Settings::PAGE_ID;
831
832 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
833 $this->register_settings_fields( $settings );
834 }, 11 );
835 }
836
837 // Register CLI commands.
838 if ( Utils::is_wp_cli() ) {
839 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
840 }
841 }
842 }
843