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

1,019 lines 29.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 const OPTION_PREFIX = 'elementor_experiment-';
38
39 private $states;
40
41 private $release_statuses;
42
43 private $features;
44
45 /**
46 * Add Feature
47 *
48 * @since 3.1.0
49 * @access public
50 *
51 * @param array $options {
52 * @type string $name
53 * @type string $title
54 * @type string $tag
55 * @type array $tags
56 * @type string $description
57 * @type string $release_status
58 * @type string $default
59 * @type callable $on_state_change
60 * }
61 *
62 * @return array|null
63 * @throws \Exception
64 */
65 public function add_feature( array $options ) {
66 if ( isset( $this->features[ $options['name'] ] ) ) {
67 return null;
68 }
69
70 $default_experimental_data = [
71 'tag' => '', // Deprecated, use 'tags' instead.
72 'tags' => [],
73 'description' => '',
74 'release_status' => self::RELEASE_STATUS_ALPHA,
75 'default' => self::STATE_INACTIVE,
76 'mutable' => true,
77 static::TYPE_HIDDEN => false,
78 'new_site' => [
79 'always_active' => false,
80 'default_active' => false,
81 'default_inactive' => false,
82 'minimum_installation_version' => null,
83 ],
84 'on_state_change' => null,
85 'generator_tag' => false,
86 ];
87
88 $allowed_options = [ 'name', 'title', 'tag', 'tags', 'description', 'release_status', 'default', 'mutable', static::TYPE_HIDDEN, 'new_site', 'on_state_change', 'dependencies', 'generator_tag', 'messages' ];
89
90 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
91
92 $experimental_data = $this->unify_feature_tags( $experimental_data );
93
94 $new_site = $experimental_data['new_site'];
95
96 if ( $new_site['default_active'] || $new_site['always_active'] || $new_site['default_inactive'] ) {
97 $is_new_installation = $this->install_compare( $new_site['minimum_installation_version'] );
98
99 if ( $is_new_installation ) {
100 if ( $new_site['always_active'] ) {
101 $experimental_data['state'] = self::STATE_ACTIVE;
102
103 $experimental_data['mutable'] = false;
104 } elseif ( $new_site['default_active'] ) {
105 $experimental_data['default'] = self::STATE_ACTIVE;
106 } elseif ( $new_site['default_inactive'] ) {
107 $experimental_data['default'] = self::STATE_INACTIVE;
108 }
109 }
110 }
111
112 if ( $experimental_data['mutable'] ) {
113 $experimental_data['state'] = $this->get_saved_feature_state( $options['name'] );
114 }
115
116 if ( empty( $experimental_data['state'] ) ) {
117 $experimental_data['state'] = self::STATE_DEFAULT;
118 }
119
120 if ( ! empty( $experimental_data['dependencies'] ) ) {
121 foreach ( $experimental_data['dependencies'] as $key => $dependency ) {
122 $feature = $this->get_features( $dependency );
123
124 if ( ! empty( $feature[ static::TYPE_HIDDEN ] ) ) {
125 throw new Exceptions\Dependency_Exception( 'Depending on a hidden experiment is not allowed.' );
126 }
127
128 $experimental_data['dependencies'][ $key ] = $this->create_dependency_class( $dependency, $feature );
129 }
130 }
131
132 $this->features[ $options['name'] ] = $experimental_data;
133
134 if ( $experimental_data['mutable'] && is_admin() ) {
135 $feature_option_key = $this->get_feature_option_key( $options['name'] );
136
137 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data, $feature_option_key ) {
138 try {
139 $this->on_feature_state_change( $experimental_data, $new_state, $old_state );
140 } catch ( Exceptions\Dependency_Exception $e ) {
141 $message = sprintf(
142 '<p>%s</p><p><a href="#" onclick="location.href=\'%s\'">%s</a></p>',
143 esc_html( $e->getMessage() ),
144 Settings::get_settings_tab_url( 'experiments' ),
145 esc_html__( 'Back', 'elementor' )
146 );
147
148 wp_die( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149 }
150 };
151
152 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
153 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
154 }
155
156 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
157
158 return $experimental_data;
159 }
160
161 private function install_compare( $version ) {
162 $installs_history = Upgrade_Manager::get_installs_history();
163
164 if ( empty( $installs_history ) ) {
165 return false;
166 }
167
168 $cleaned_version = preg_replace( '/-(beta|cloud|dev)\d*$/', '', key( $installs_history ) );
169
170 return version_compare(
171 $cleaned_version,
172 $version,
173 '>='
174 );
175 }
176
177 /**
178 * Combine 'tag' and 'tags' into one property.
179 *
180 * @param array $experimental_data
181 *
182 * @return array
183 */
184 private function unify_feature_tags( array $experimental_data ) : array {
185 foreach ( [ 'tag', 'tags' ] as $key ) {
186 if ( empty( $experimental_data[ $key ] ) ) {
187 continue;
188 }
189
190 $experimental_data[ $key ] = $this->format_feature_tags( $experimental_data[ $key ] );
191 }
192
193 if ( is_array( $experimental_data['tag'] ) ) {
194 $experimental_data['tags'] = array_merge( $experimental_data['tag'], $experimental_data['tags'] );
195 }
196
197 return $experimental_data;
198 }
199
200 /**
201 * Format feature tags into the right format.
202 *
203 * @param string|array[
204 * [
205 * 'type' => string,
206 * 'label' => string
207 * ]
208 * ] $tag
209 *
210 * @return array
211 */
212 private function format_feature_tags( $tags ) : array {
213 if ( ! is_string( $tags ) && ! is_array( $tags ) ) {
214 return [];
215 }
216
217 $default_tag = [
218 'type' => 'default',
219 'label' => '',
220 ];
221
222 $allowed_tag_properties = [ 'type', 'label' ];
223
224 // If $tags is string, explode by commas and convert to array.
225 if ( is_string( $tags ) ) {
226 $tags = array_filter( explode( ',', $tags ) );
227
228 foreach ( $tags as $i => $tag ) {
229 $tags[ $i ] = [ 'label' => trim( $tag ) ];
230 }
231 }
232
233 foreach ( $tags as $i => $tag ) {
234 if ( empty( $tag['label'] ) ) {
235 unset( $tags[ $i ] );
236 continue;
237 }
238
239 $tags[ $i ] = $this->merge_properties( $default_tag, $tag, $allowed_tag_properties );
240 }
241
242 return $tags;
243 }
244
245 /**
246 * Remove Feature
247 *
248 * @since 3.1.0
249 * @access public
250 *
251 * @param string $feature_name
252 */
253 public function remove_feature( $feature_name ) {
254 unset( $this->features[ $feature_name ] );
255 }
256
257 /**
258 * Get Features
259 *
260 * @since 3.1.0
261 * @access public
262 *
263 * @param string $feature_name Optional. Default is null
264 *
265 * @return array|null
266 */
267 public function get_features( $feature_name = null ) {
268 return self::get_items( $this->features, $feature_name );
269 }
270
271 /**
272 * Get Active Features
273 *
274 * @since 3.1.0
275 * @access public
276 *
277 * @return array
278 */
279 public function get_active_features() {
280 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
281 }
282
283 /**
284 * Is Feature Active
285 *
286 * @since 3.1.0
287 * @access public
288 *
289 * @param string $feature_name
290 *
291 * @return bool
292 */
293 public function is_feature_active( $feature_name, $check_dependencies = false ) {
294 $feature = $this->get_features( $feature_name );
295
296 if ( ! $feature || self::STATE_ACTIVE !== $this->get_feature_actual_state( $feature ) ) {
297 return false;
298 }
299
300 if ( $check_dependencies && isset( $feature['dependencies'] ) && is_array( $feature['dependencies'] ) ) {
301 foreach ( $feature['dependencies'] as $dependency ) {
302 $dependent_feature = $this->get_features( $dependency->get_name() );
303 $feature_state = self::STATE_ACTIVE === $this->get_feature_actual_state( $dependent_feature );
304
305 if ( ! $feature_state ) {
306 return false;
307 }
308 }
309 }
310
311 return true;
312 }
313
314 /**
315 * Set Feature Default State
316 *
317 * @since 3.1.0
318 * @access public
319 *
320 * @param string $feature_name
321 * @param string $default_state
322 */
323 public function set_feature_default_state( $feature_name, $default_state ) {
324 $feature = $this->get_features( $feature_name );
325
326 if ( ! $feature ) {
327 return;
328 }
329
330 $this->features[ $feature_name ]['default'] = $default_state;
331 }
332
333 /**
334 * Get Feature Option Key
335 *
336 * @since 3.1.0
337 * @access public
338 *
339 * @param string $feature_name
340 *
341 * @return string
342 */
343 public function get_feature_option_key( $feature_name ) {
344 return static::OPTION_PREFIX . $feature_name;
345 }
346
347 private function add_default_features() {
348 $this->add_feature( [
349 'name' => 'e_font_icon_svg',
350 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
351 'tag' => esc_html__( 'Performance', 'elementor' ),
352 'description' => sprintf(
353 '%1$s <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">%2$s</a>',
354 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' ),
355 esc_html__( 'Learn more', 'elementor' )
356 ),
357 'release_status' => self::RELEASE_STATUS_STABLE,
358 'new_site' => [
359 'default_active' => true,
360 'minimum_installation_version' => '3.17.0',
361 ],
362 'generator_tag' => true,
363 ] );
364
365 $this->add_feature( [
366 'name' => 'additional_custom_breakpoints',
367 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
368 'description' => sprintf(
369 '%1$s <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">%2$s</a>',
370 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' ),
371 esc_html__( 'Learn more', 'elementor' )
372 ),
373 'release_status' => self::RELEASE_STATUS_STABLE,
374 'default' => self::STATE_ACTIVE,
375 'generator_tag' => true,
376 ] );
377
378 $this->add_feature( [
379 'name' => 'container',
380 'title' => esc_html__( 'Container', 'elementor' ),
381 'description' => sprintf(
382 esc_html__( 'Create advanced layouts and responsive designs with %1$sFlexbox%2$s and %3$sGrid%4$s container elements. Give it a try using the %5$sContainer playground%6$s.', 'elementor' ),
383 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container/">',
384 '</a>',
385 '<a target="_blank" href="https://go.elementor.com/wp-dash-grid-container/">',
386 '</a>',
387 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container-playground/">',
388 '</a>'
389 ),
390 'release_status' => self::RELEASE_STATUS_STABLE,
391 'default' => self::STATE_INACTIVE,
392 'new_site' => [
393 'default_active' => true,
394 'minimum_installation_version' => '3.16.0',
395 ],
396 'messages' => [
397 'on_deactivate' => sprintf(
398 '%1$s <a target="_blank" href="https://go.elementor.com/wp-dash-deactivate-container/">%2$s</a>',
399 esc_html__( 'Container-based content will be hidden from your site and may not be recoverable in all cases.', 'elementor' ),
400 esc_html__( 'Learn more', 'elementor' ),
401 ),
402 ],
403 ] );
404
405 // TODO: Remove this experiment in v3.28 [ED-15983].
406 $this->add_feature( [
407 'name' => 'e_swiper_latest',
408 'title' => esc_html__( 'Upgrade Swiper Library', 'elementor' ),
409 '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' ),
410 'default' => self::STATE_ACTIVE,
411 static::TYPE_HIDDEN => true,
412 'mutable' => false,
413 ] );
414
415 $this->add_feature( [
416 'name' => 'e_nested_atomic_repeaters',
417 'title' => esc_html__( 'Nested Elements Performance', 'elementor' ),
418 'tag' => esc_html__( 'Performance', 'elementor' ),
419 'description' => esc_html__( 'Improve the performance of the Nested widgets.', 'elementor' ),
420 static::TYPE_HIDDEN => true,
421 'release_status' => self::RELEASE_STATUS_DEV,
422 'default' => self::STATE_ACTIVE,
423 ] );
424
425 $this->add_feature( [
426 'name' => 'e_optimized_markup',
427 'title' => esc_html__( 'Optimized Markup', 'elementor' ),
428 'tag' => esc_html__( 'Performance', 'elementor' ),
429 'description' => esc_html__( 'Reduce the DOM size by eliminating HTML tags in various elements and widgets. This experiment includes markup changes so it might require updating custom CSS/JS code and cause compatibility issues with third party plugins.', 'elementor' ),
430 'release_status' => self::RELEASE_STATUS_ALPHA,
431 'default' => self::STATE_INACTIVE,
432 ] );
433
434 $this->add_feature( [
435 'name' => 'e_onboarding',
436 'title' => esc_html__( 'Plugin Onboarding', 'elementor' ),
437 'description' => esc_html__( 'New plugin onboarding.', 'elementor' ),
438 static::TYPE_HIDDEN => true,
439 'release_status' => self::RELEASE_STATUS_ALPHA,
440 'default' => self::STATE_ACTIVE,
441 ] );
442
443 // TODO: Possibly remove experiment in v3.27.0 [ED-15717].
444 // Check this reference in Pro: 'sticky_anchor_link_offset'.
445 $this->add_feature( [
446 'name' => 'e_css_smooth_scroll',
447 'title' => esc_html__( 'CSS Smooth Scroll', 'elementor' ),
448 'tag' => esc_html__( 'Performance', 'elementor' ),
449 'description' => esc_html__( 'Use CSS Smooth Scroll to improve the user experience on your site. This experiment replaces the default JavaScript-based smooth scroll with a CSS-based solution.', 'elementor' ),
450 'release_status' => self::RELEASE_STATUS_DEV,
451 static::TYPE_HIDDEN => true,
452 'default' => self::STATE_ACTIVE,
453 'mutable' => false,
454 ] );
455
456 $this->add_feature( [
457 'name' => 'e_head_loading_styles',
458 'title' => esc_html__( 'Head Loading Styles', 'elementor' ),
459 'description' => esc_html__( 'Head Loading Styles', 'elementor' ),
460 'hidden' => true,
461 'default' => self::STATE_INACTIVE,
462 'release_status' => self::RELEASE_STATUS_ALPHA,
463 ] );
464 }
465
466 /**
467 * Init States
468 *
469 * @since 3.1.0
470 * @access private
471 */
472 private function init_states() {
473 $this->states = [
474 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
475 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
476 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
477 ];
478 }
479
480 /**
481 * Init Statuses
482 *
483 * @since 3.1.0
484 * @access private
485 */
486 private function init_release_statuses() {
487 $this->release_statuses = [
488 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
489 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
490 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
491 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
492 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
493 ];
494 }
495
496 /**
497 * Init Features
498 *
499 * @since 3.1.0
500 * @access private
501 */
502 private function init_features() {
503 $this->features = [];
504
505 $this->add_default_features();
506
507 do_action( 'elementor/experiments/default-features-registered', $this );
508 }
509
510 /**
511 * Register Settings Fields
512 *
513 * @param Settings $settings
514 *
515 * @since 3.1.0
516 * @access private
517 *
518 */
519 private function register_settings_fields( Settings $settings ) {
520 $features = $this->get_features();
521
522 $fields = [];
523
524 foreach ( $features as $feature_name => $feature ) {
525 $is_hidden = $feature[ static::TYPE_HIDDEN ];
526 $is_mutable = $feature['mutable'];
527 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
528
529 if ( $should_hide_experiment ) {
530 unset( $features[ $feature_name ] );
531
532 continue;
533 }
534
535 $feature_key = 'experiment-' . $feature_name;
536
537 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
538
539 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
540
541 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
542
543 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
544 $this->render_feature_settings_field( $feature );
545 };
546 }
547
548 foreach ( [ 'stable', 'ongoing' ] as $section ) {
549 if ( ! isset( $fields[ $section ] ) ) {
550 $fields[ $section ]['no_features'] = [
551 'label' => esc_html__( 'No available experiments', 'elementor' ),
552 'field_args' => [
553 'type' => 'raw_html',
554 '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' ),
555 ],
556 ];
557 }
558
559 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
560 $fields[ $section ] += $settings->get_usage_fields();
561 }
562 }
563
564 $settings->add_tab(
565 'experiments', [
566 'label' => esc_html__( 'Features', 'elementor' ),
567 'sections' => [
568 'ongoing_experiments' => [
569 'callback' => function() {
570 $this->render_settings_intro();
571 },
572 'fields' => $fields['ongoing'],
573 ],
574 'stable_experiments' => [
575 'callback' => function() {
576 $this->render_stable_section_title();
577 },
578 'fields' => $fields['stable'],
579 ],
580 ],
581 ]
582 );
583 }
584
585 private function render_stable_section_title() {
586 ?>
587 <hr>
588 <h2>
589 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
590 </h2>
591 <?php
592 }
593
594 /**
595 * Render Settings Intro
596 *
597 * @since 3.1.0
598 * @access private
599 */
600 private function render_settings_intro() {
601 ?>
602 <h2>
603 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
604 </h2>
605 <p class="e-experiment__description">
606 <?php
607 echo sprintf(
608 /* translators: %1$s Link open tag, %2$s: Link close tag. */
609 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' ),
610 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
611 '</a>'
612 );
613 ?>
614 </p>
615 <p class="e-experiment__description">
616 <?php
617 echo sprintf(
618 '%1$s <a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">%2$s</a>',
619 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.', 'elementor' ),
620 esc_html__( 'Learn more', 'elementor' ),
621 );
622 ?>
623 </p>
624
625 <?php if ( $this->get_features() ) { ?>
626 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
627 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
628 <button type="button" class="button e-experiment__button" value="default"><?php echo esc_html__( 'Back to default', 'elementor' ); ?></button>
629 <?php } ?>
630 <hr>
631 <h2 class="e-experiment__table-title">
632 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
633 </h2>
634 <?php
635 }
636
637 /**
638 * Render Feature Settings Field
639 *
640 * @since 3.1.0
641 * @access private
642 *
643 * @param array $feature
644 */
645 private function render_feature_settings_field( array $feature ) {
646 $control_id = 'e-experiment-' . $feature['name'];
647 $control_name = $this->get_feature_option_key( $feature['name'] );
648
649 $status = sprintf(
650 /* translators: %s Release status. */
651 esc_html__( 'Status: %s', 'elementor' ),
652 $this->release_statuses[ $feature['release_status'] ]
653 );
654
655 ?>
656 <div class="e-experiment__content">
657 <select class="e-experiment__select"
658 id="<?php echo esc_attr( $control_id ); ?>"
659 name="<?php echo esc_attr( $control_name ); ?>"
660 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
661 >
662 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
663 <option value="<?php echo esc_attr( $state_key ); ?>"
664 <?php selected( $state_key, $feature['state'] ); ?>
665 >
666 <?php echo esc_html( $state_title ); ?>
667 </option>
668 <?php } ?>
669 </select>
670
671 <p class="description">
672 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
673 </p>
674
675 <?php $this->render_feature_dependency( $feature ); ?>
676
677 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
678 <div class="e-experiment__status">
679 <?php echo esc_html( $status ); ?>
680 </div>
681 <?php } ?>
682 </div>
683 <?php
684 }
685
686 private function render_feature_dependency( $feature ) {
687 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
688 ->map( function ( $dependency ) {
689 return $dependency->get_title();
690 } )
691 ->implode( ', ' );
692
693 if ( empty( $dependencies ) ) {
694 return;
695 }
696
697 ?>
698 <div class="e-experiment__dependency">
699 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
700 <span><?php echo esc_html( $dependencies ); ?></span>
701 </div>
702 <?php
703 }
704
705 private function has_non_existing_dependency( $feature ) {
706 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
707 ->find( function ( $dependency ) {
708 return $dependency instanceof Non_Existing_Dependency;
709 } );
710
711 return ! ! $non_existing_dep;
712 }
713
714 /**
715 * Get Feature Settings Label HTML
716 *
717 * @since 3.1.0
718 * @access private
719 *
720 * @param array $feature
721 *
722 * @return string
723 */
724 private function get_feature_settings_label_html( array $feature ) {
725 ob_start();
726
727 $is_feature_active = $this->is_feature_active( $feature['name'] );
728
729 $indicator_classes = 'e-experiment__title__indicator';
730
731 if ( $is_feature_active ) {
732 $indicator_classes .= ' e-experiment__title__indicator--active';
733 }
734
735 $indicator_tooltip = $this->get_feature_state_label( $feature );
736
737 ?>
738 <div class="e-experiment__title">
739 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
740 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
741 <?php foreach ( $feature['tags'] as $tag ) { ?>
742 <span class="e-experiment__title__tag e-experiment__title__tag__<?php echo $tag['type']; ?>"><?php echo $tag['label']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
743 <?php } ?>
744 </div>
745 <?php
746
747 return ob_get_clean();
748 }
749
750 /**
751 * Get Feature State Label
752 *
753 * @param array $feature
754 *
755 * @return string
756 */
757 public function get_feature_state_label( array $feature ) {
758 $is_feature_active = $this->is_feature_active( $feature['name'] );
759
760 if ( self::STATE_DEFAULT === $feature['state'] ) {
761 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
762 esc_html__( 'Inactive by default', 'elementor' );
763 } else {
764 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
765 esc_html__( 'Inactive', 'elementor' );
766 }
767
768 return $label;
769 }
770
771 /**
772 * Get Feature Settings Label HTML
773 *
774 * @since 3.1.0
775 * @access private
776 *
777 * @param string $feature_name
778 *
779 * @return int
780 */
781 private function get_saved_feature_state( $feature_name ) {
782 return get_option( $this->get_feature_option_key( $feature_name ) );
783 }
784
785 /**
786 * Get Feature Actual State
787 *
788 * @since 3.1.0
789 * @access private
790 *
791 * @param array $feature
792 *
793 * @return string
794 */
795 private function get_feature_actual_state( array $feature ) {
796 if ( ! empty( $feature['state'] ) && self::STATE_DEFAULT !== $feature['state'] ) {
797 return $feature['state'];
798 }
799
800 return $feature['default'];
801 }
802
803 /**
804 * On Feature State Change
805 *
806 * @since 3.1.0
807 * @access private
808 *
809 * @param array $old_feature_data
810 * @param string $new_state
811 *
812 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
813 */
814 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
815 $new_feature_data = $this->get_features( $old_feature_data['name'] );
816 $this->validate_dependency( $new_feature_data, $new_state );
817 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
818 if ( $old_state === $new_state ) {
819 return;
820 }
821
822 Plugin::$instance->files_manager->clear_cache();
823 if ( $new_feature_data['on_state_change'] ) {
824 $new_feature_data['on_state_change']( $old_state, $new_state );
825 }
826
827 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
828 }
829
830 /**
831 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
832 */
833 private function validate_dependency( array $feature, $new_state ) {
834 $rollback = function ( $feature_option_key, $state ) {
835 remove_all_actions( 'add_option_' . $feature_option_key );
836 remove_all_actions( 'update_option_' . $feature_option_key );
837
838 update_option( $feature_option_key, $state );
839 };
840
841 if ( self::STATE_DEFAULT === $new_state ) {
842 $new_state = $this->get_feature_actual_state( $feature );
843 }
844
845 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
846
847 if ( self::STATE_ACTIVE === $new_state ) {
848 if ( empty( $feature['dependencies'] ) ) {
849 return;
850 }
851
852 // Validate if the current feature dependency is available.
853 foreach ( $feature['dependencies'] as $dependency ) {
854 $dependency_feature = $this->get_features( $dependency->get_name() );
855
856 if ( ! $dependency_feature ) {
857 $rollback( $feature_option_key, self::STATE_INACTIVE );
858
859 throw new Exceptions\Dependency_Exception(
860 sprintf(
861 'The feature `%s` has a dependency `%s` that is not available.',
862 $feature['name'],
863 $dependency->get_name()
864 )
865 );
866 }
867
868 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
869
870 // If dependency is not active.
871 if ( self::STATE_INACTIVE === $dependency_state ) {
872 $rollback( $feature_option_key, self::STATE_INACTIVE );
873
874 throw new Exceptions\Dependency_Exception(
875 sprintf(
876 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
877 $feature['name'],
878 $dependency_feature['name']
879 )
880 );
881 }
882 }
883 } elseif ( self::STATE_INACTIVE === $new_state ) {
884 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
885 foreach ( $this->get_features() as $current_feature ) {
886 if ( empty( $current_feature['dependencies'] ) ) {
887 continue;
888 }
889
890 $current_feature_state = $this->get_feature_actual_state( $current_feature );
891
892 foreach ( $current_feature['dependencies'] as $dependency ) {
893 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
894 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
895 }
896 }
897 }
898 }
899 }
900
901 private function should_show_hidden() {
902 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
903 }
904
905 private function create_dependency_class( $dependency_name, $dependency_args ) {
906 if ( class_exists( $dependency_name ) ) {
907 return $dependency_name::instance();
908 }
909
910 if ( ! empty( $dependency_args ) ) {
911 return new Wrap_Core_Dependency( $dependency_args );
912 }
913
914 return new Non_Existing_Dependency( $dependency_name );
915 }
916
917 /**
918 * The experiments page is a WordPress options page, which means all the experiments are registered via WordPress' register_settings(),
919 * and their states are being sent in the POST request when saving.
920 * The options are being updated in a chronological order based on the POST data.
921 * This behavior interferes with the experiments dependency mechanism because the data that's being sent can be in any order,
922 * while the dependencies mechanism expects it to be in a specific order (dependencies should be activated before their dependents can).
923 * In order to solve this issue, we sort the experiments in the POST data based on their dependencies tree.
924 *
925 * @param $allowed_options
926 *
927 * @return mixed
928 */
929 private function sort_allowed_options_by_dependencies( $allowed_options ) {
930 if ( ! isset( $allowed_options['elementor'] ) ) {
931 return $allowed_options;
932 }
933
934 $sorted = Collection::make();
935 $visited = Collection::make();
936
937 $sort = function ( $item ) use ( &$sort, $sorted, $visited ) {
938 if ( $visited->contains( $item ) ) {
939 return;
940 }
941
942 $visited->push( $item );
943
944 $feature = $this->get_features( $item );
945
946 if ( ! $feature ) {
947 return;
948 }
949
950 foreach ( $feature['dependencies'] ?? [] as $dep ) {
951 $name = is_string( $dep ) ? $dep : $dep->get_name();
952
953 $sort( $name );
954 }
955
956 $sorted->push( $item );
957 };
958
959 foreach ( $allowed_options['elementor'] as $option ) {
960 $is_experiment_option = strpos( $option, static::OPTION_PREFIX ) === 0;
961
962 if ( ! $is_experiment_option ) {
963 continue;
964 }
965
966 $sort(
967 str_replace( static::OPTION_PREFIX, '', $option )
968 );
969 }
970
971 $allowed_options['elementor'] = Collection::make( $allowed_options['elementor'] )
972 ->filter( function ( $option ) {
973 return 0 !== strpos( $option, static::OPTION_PREFIX );
974 } )
975 ->merge(
976 $sorted->map( function ( $item ) {
977 return static::OPTION_PREFIX . $item;
978 } )
979 )
980 ->values();
981
982 return $allowed_options;
983 }
984
985 public function __construct() {
986 $this->init_states();
987
988 $this->init_release_statuses();
989
990 $this->init_features();
991
992 add_action( 'admin_init', function () {
993 System_Info::add_report(
994 'experiments', [
995 'file_name' => __DIR__ . '/experiments-reporter.php',
996 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
997 ]
998 );
999 }, 79 /* Before log */ );
1000
1001 if ( is_admin() ) {
1002 $page_id = Settings::PAGE_ID;
1003
1004 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
1005 $this->register_settings_fields( $settings );
1006 }, 11 );
1007
1008 add_filter( 'allowed_options', function ( $allowed_options ) {
1009 return $this->sort_allowed_options_by_dependencies( $allowed_options );
1010 }, 11 );
1011 }
1012
1013 // Register CLI commands.
1014 if ( Utils::is_wp_cli() ) {
1015 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
1016 }
1017 }
1018 }
1019