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

1,041 lines 31.0 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_optimized_css_loading',
350 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
351 'tag' => esc_html__( 'Performance', 'elementor' ),
352 'description' => sprintf(
353 '%1$s <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">%2$s</a>',
354 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' ),
355 esc_html__( 'Learn more', 'elementor' )
356 ),
357 'release_status' => self::RELEASE_STATUS_STABLE,
358 'default' => self::STATE_INACTIVE,
359 static::TYPE_HIDDEN => true,
360 'mutable' => false,
361 'generator_tag' => true,
362 ] );
363
364 $this->add_feature( [
365 'name' => 'e_font_icon_svg',
366 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
367 'tag' => esc_html__( 'Performance', 'elementor' ),
368 'description' => sprintf(
369 '%1$s <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">%2$s</a>',
370 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' ),
371 esc_html__( 'Learn more', 'elementor' )
372 ),
373 'release_status' => self::RELEASE_STATUS_STABLE,
374 'new_site' => [
375 'default_active' => true,
376 'minimum_installation_version' => '3.17.0',
377 ],
378 'generator_tag' => true,
379 ] );
380
381 $this->add_feature( [
382 'name' => 'additional_custom_breakpoints',
383 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
384 'description' => sprintf(
385 '%1$s <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">%2$s</a>',
386 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' ),
387 esc_html__( 'Learn more', 'elementor' )
388 ),
389 'release_status' => self::RELEASE_STATUS_STABLE,
390 'default' => self::STATE_ACTIVE,
391 'generator_tag' => true,
392 ] );
393
394 $this->add_feature( [
395 'name' => 'container',
396 'title' => esc_html__( 'Container', 'elementor' ),
397 'description' => sprintf(
398 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' ),
399 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container/">',
400 '</a>',
401 '<a target="_blank" href="https://go.elementor.com/wp-dash-grid-container/">',
402 '</a>',
403 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container-playground/">',
404 '</a>'
405 ),
406 'release_status' => self::RELEASE_STATUS_STABLE,
407 'default' => self::STATE_INACTIVE,
408 'new_site' => [
409 'default_active' => true,
410 'minimum_installation_version' => '3.16.0',
411 ],
412 'messages' => [
413 'on_deactivate' => sprintf(
414 '%1$s <a target="_blank" href="https://go.elementor.com/wp-dash-deactivate-container/">%2$s</a>',
415 esc_html__( 'Container-based content will be hidden from your site and may not be recoverable in all cases.', 'elementor' ),
416 esc_html__( 'Learn more', 'elementor' ),
417 ),
418 ],
419 ] );
420
421 $this->add_feature( [
422 'name' => 'e_swiper_latest',
423 'title' => esc_html__( 'Upgrade Swiper Library', 'elementor' ),
424 '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' ),
425 'release_status' => self::RELEASE_STATUS_STABLE,
426 'default' => self::STATE_ACTIVE,
427 ] );
428
429 $this->add_feature( [
430 'name' => 'e_nested_atomic_repeaters',
431 'title' => esc_html__( 'Nested Elements Performance', 'elementor' ),
432 'tag' => esc_html__( 'Performance', 'elementor' ),
433 'description' => esc_html__( 'Improve the performance of the Nested widgets.', 'elementor' ),
434 static::TYPE_HIDDEN => true,
435 'release_status' => self::RELEASE_STATUS_DEV,
436 'default' => self::STATE_ACTIVE,
437 ] );
438
439 $this->add_feature( [
440 'name' => 'e_optimized_control_loading',
441 'title' => esc_html__( 'Optimized Control Loading', 'elementor' ),
442 'tag' => esc_html__( 'Performance', 'elementor' ),
443 'description' => esc_html__( 'Use this experiment to improve control loading. This experiment improves site performance by loading controls only when needed.', 'elementor' ),
444 'release_status' => self::RELEASE_STATUS_STABLE,
445 'default' => self::STATE_ACTIVE,
446 'generator_tag' => true,
447 ] );
448
449 $this->add_feature( [
450 'name' => 'e_optimized_markup',
451 'title' => esc_html__( 'Optimized Markup', 'elementor' ),
452 'tag' => esc_html__( 'Performance', 'elementor' ),
453 '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' ),
454 'release_status' => self::RELEASE_STATUS_ALPHA,
455 'default' => self::STATE_INACTIVE,
456 ] );
457
458 $this->add_feature( [
459 'name' => 'e_swiper_css_conditional_loading',
460 'title' => esc_html__( 'Conditionally load Swiper CSS files', 'elementor' ),
461 static::TYPE_HIDDEN => true,
462 'default' => self::STATE_INACTIVE,
463 ] );
464
465 $this->add_feature( [
466 'name' => 'e_onboarding',
467 'title' => esc_html__( 'Plugin Onboarding', 'elementor' ),
468 'description' => esc_html__( 'New plugin onboarding.', 'elementor' ),
469 static::TYPE_HIDDEN => true,
470 'release_status' => self::RELEASE_STATUS_ALPHA,
471 'default' => self::STATE_ACTIVE,
472 ] );
473
474 // TODO: Possibly remove experiment in v3.27.0 [ED-15717].
475 // Check this reference in Pro: 'sticky_anchor_link_offset'.
476 $this->add_feature( [
477 'name' => 'e_css_smooth_scroll',
478 'title' => esc_html__( 'CSS Smooth Scroll', 'elementor' ),
479 'tag' => esc_html__( 'Performance', 'elementor' ),
480 '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' ),
481 'release_status' => self::RELEASE_STATUS_DEV,
482 static::TYPE_HIDDEN => true,
483 'default' => self::STATE_ACTIVE,
484 'mutable' => false,
485 ] );
486 }
487
488 /**
489 * Init States
490 *
491 * @since 3.1.0
492 * @access private
493 */
494 private function init_states() {
495 $this->states = [
496 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
497 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
498 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
499 ];
500 }
501
502 /**
503 * Init Statuses
504 *
505 * @since 3.1.0
506 * @access private
507 */
508 private function init_release_statuses() {
509 $this->release_statuses = [
510 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
511 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
512 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
513 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
514 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
515 ];
516 }
517
518 /**
519 * Init Features
520 *
521 * @since 3.1.0
522 * @access private
523 */
524 private function init_features() {
525 $this->features = [];
526
527 $this->add_default_features();
528
529 do_action( 'elementor/experiments/default-features-registered', $this );
530 }
531
532 /**
533 * Register Settings Fields
534 *
535 * @param Settings $settings
536 *
537 * @since 3.1.0
538 * @access private
539 *
540 */
541 private function register_settings_fields( Settings $settings ) {
542 $features = $this->get_features();
543
544 $fields = [];
545
546 foreach ( $features as $feature_name => $feature ) {
547 $is_hidden = $feature[ static::TYPE_HIDDEN ];
548 $is_mutable = $feature['mutable'];
549 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
550
551 if ( $should_hide_experiment ) {
552 unset( $features[ $feature_name ] );
553
554 continue;
555 }
556
557 $feature_key = 'experiment-' . $feature_name;
558
559 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
560
561 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
562
563 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
564
565 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
566 $this->render_feature_settings_field( $feature );
567 };
568 }
569
570 foreach ( [ 'stable', 'ongoing' ] as $section ) {
571 if ( ! isset( $fields[ $section ] ) ) {
572 $fields[ $section ]['no_features'] = [
573 'label' => esc_html__( 'No available experiments', 'elementor' ),
574 'field_args' => [
575 'type' => 'raw_html',
576 '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' ),
577 ],
578 ];
579 }
580
581 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
582 $fields[ $section ] += $settings->get_usage_fields();
583 }
584 }
585
586 $settings->add_tab(
587 'experiments', [
588 'label' => esc_html__( 'Features', 'elementor' ),
589 'sections' => [
590 'ongoing_experiments' => [
591 'callback' => function() {
592 $this->render_settings_intro();
593 },
594 'fields' => $fields['ongoing'],
595 ],
596 'stable_experiments' => [
597 'callback' => function() {
598 $this->render_stable_section_title();
599 },
600 'fields' => $fields['stable'],
601 ],
602 ],
603 ]
604 );
605 }
606
607 private function render_stable_section_title() {
608 ?>
609 <hr>
610 <h2>
611 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
612 </h2>
613 <?php
614 }
615
616 /**
617 * Render Settings Intro
618 *
619 * @since 3.1.0
620 * @access private
621 */
622 private function render_settings_intro() {
623 ?>
624 <h2>
625 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
626 </h2>
627 <p class="e-experiment__description">
628 <?php
629 echo sprintf(
630 /* translators: %1$s Link open tag, %2$s: Link close tag. */
631 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' ),
632 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
633 '</a>'
634 );
635 ?>
636 </p>
637 <p class="e-experiment__description">
638 <?php
639 echo sprintf(
640 '%1$s <a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">%2$s</a>',
641 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' ),
642 esc_html__( 'Learn more', 'elementor' ),
643 );
644 ?>
645 </p>
646
647 <?php if ( $this->get_features() ) { ?>
648 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
649 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
650 <button type="button" class="button e-experiment__button" value="default"><?php echo esc_html__( 'Back to default', 'elementor' ); ?></button>
651 <?php } ?>
652 <hr>
653 <h2 class="e-experiment__table-title">
654 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
655 </h2>
656 <?php
657 }
658
659 /**
660 * Render Feature Settings Field
661 *
662 * @since 3.1.0
663 * @access private
664 *
665 * @param array $feature
666 */
667 private function render_feature_settings_field( array $feature ) {
668 $control_id = 'e-experiment-' . $feature['name'];
669 $control_name = $this->get_feature_option_key( $feature['name'] );
670
671 $status = sprintf(
672 /* translators: %s Release status. */
673 esc_html__( 'Status: %s', 'elementor' ),
674 $this->release_statuses[ $feature['release_status'] ]
675 );
676
677 ?>
678 <div class="e-experiment__content">
679 <select class="e-experiment__select"
680 id="<?php echo esc_attr( $control_id ); ?>"
681 name="<?php echo esc_attr( $control_name ); ?>"
682 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
683 >
684 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
685 <option value="<?php echo esc_attr( $state_key ); ?>"
686 <?php selected( $state_key, $feature['state'] ); ?>
687 >
688 <?php echo esc_html( $state_title ); ?>
689 </option>
690 <?php } ?>
691 </select>
692
693 <p class="description">
694 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
695 </p>
696
697 <?php $this->render_feature_dependency( $feature ); ?>
698
699 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
700 <div class="e-experiment__status">
701 <?php echo esc_html( $status ); ?>
702 </div>
703 <?php } ?>
704 </div>
705 <?php
706 }
707
708 private function render_feature_dependency( $feature ) {
709 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
710 ->map( function ( $dependency ) {
711 return $dependency->get_title();
712 } )
713 ->implode( ', ' );
714
715 if ( empty( $dependencies ) ) {
716 return;
717 }
718
719 ?>
720 <div class="e-experiment__dependency">
721 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
722 <span><?php echo esc_html( $dependencies ); ?></span>
723 </div>
724 <?php
725 }
726
727 private function has_non_existing_dependency( $feature ) {
728 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
729 ->find( function ( $dependency ) {
730 return $dependency instanceof Non_Existing_Dependency;
731 } );
732
733 return ! ! $non_existing_dep;
734 }
735
736 /**
737 * Get Feature Settings Label HTML
738 *
739 * @since 3.1.0
740 * @access private
741 *
742 * @param array $feature
743 *
744 * @return string
745 */
746 private function get_feature_settings_label_html( array $feature ) {
747 ob_start();
748
749 $is_feature_active = $this->is_feature_active( $feature['name'] );
750
751 $indicator_classes = 'e-experiment__title__indicator';
752
753 if ( $is_feature_active ) {
754 $indicator_classes .= ' e-experiment__title__indicator--active';
755 }
756
757 $indicator_tooltip = $this->get_feature_state_label( $feature );
758
759 ?>
760 <div class="e-experiment__title">
761 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
762 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
763 <?php foreach ( $feature['tags'] as $tag ) { ?>
764 <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>
765 <?php } ?>
766 </div>
767 <?php
768
769 return ob_get_clean();
770 }
771
772 /**
773 * Get Feature State Label
774 *
775 * @param array $feature
776 *
777 * @return string
778 */
779 public function get_feature_state_label( array $feature ) {
780 $is_feature_active = $this->is_feature_active( $feature['name'] );
781
782 if ( self::STATE_DEFAULT === $feature['state'] ) {
783 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
784 esc_html__( 'Inactive by default', 'elementor' );
785 } else {
786 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
787 esc_html__( 'Inactive', 'elementor' );
788 }
789
790 return $label;
791 }
792
793 /**
794 * Get Feature Settings Label HTML
795 *
796 * @since 3.1.0
797 * @access private
798 *
799 * @param string $feature_name
800 *
801 * @return int
802 */
803 private function get_saved_feature_state( $feature_name ) {
804 return get_option( $this->get_feature_option_key( $feature_name ) );
805 }
806
807 /**
808 * Get Feature Actual State
809 *
810 * @since 3.1.0
811 * @access private
812 *
813 * @param array $feature
814 *
815 * @return string
816 */
817 private function get_feature_actual_state( array $feature ) {
818 if ( ! empty( $feature['state'] ) && self::STATE_DEFAULT !== $feature['state'] ) {
819 return $feature['state'];
820 }
821
822 return $feature['default'];
823 }
824
825 /**
826 * On Feature State Change
827 *
828 * @since 3.1.0
829 * @access private
830 *
831 * @param array $old_feature_data
832 * @param string $new_state
833 *
834 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
835 */
836 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
837 $new_feature_data = $this->get_features( $old_feature_data['name'] );
838 $this->validate_dependency( $new_feature_data, $new_state );
839 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
840 if ( $old_state === $new_state ) {
841 return;
842 }
843
844 Plugin::$instance->files_manager->clear_cache();
845 if ( $new_feature_data['on_state_change'] ) {
846 $new_feature_data['on_state_change']( $old_state, $new_state );
847 }
848
849 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
850 }
851
852 /**
853 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
854 */
855 private function validate_dependency( array $feature, $new_state ) {
856 $rollback = function ( $feature_option_key, $state ) {
857 remove_all_actions( 'add_option_' . $feature_option_key );
858 remove_all_actions( 'update_option_' . $feature_option_key );
859
860 update_option( $feature_option_key, $state );
861 };
862
863 if ( self::STATE_DEFAULT === $new_state ) {
864 $new_state = $this->get_feature_actual_state( $feature );
865 }
866
867 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
868
869 if ( self::STATE_ACTIVE === $new_state ) {
870 if ( empty( $feature['dependencies'] ) ) {
871 return;
872 }
873
874 // Validate if the current feature dependency is available.
875 foreach ( $feature['dependencies'] as $dependency ) {
876 $dependency_feature = $this->get_features( $dependency->get_name() );
877
878 if ( ! $dependency_feature ) {
879 $rollback( $feature_option_key, self::STATE_INACTIVE );
880
881 throw new Exceptions\Dependency_Exception(
882 sprintf(
883 'The feature `%s` has a dependency `%s` that is not available.',
884 $feature['name'],
885 $dependency->get_name()
886 )
887 );
888 }
889
890 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
891
892 // If dependency is not active.
893 if ( self::STATE_INACTIVE === $dependency_state ) {
894 $rollback( $feature_option_key, self::STATE_INACTIVE );
895
896 throw new Exceptions\Dependency_Exception(
897 sprintf(
898 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
899 $feature['name'],
900 $dependency_feature['name']
901 )
902 );
903 }
904 }
905 } elseif ( self::STATE_INACTIVE === $new_state ) {
906 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
907 foreach ( $this->get_features() as $current_feature ) {
908 if ( empty( $current_feature['dependencies'] ) ) {
909 continue;
910 }
911
912 $current_feature_state = $this->get_feature_actual_state( $current_feature );
913
914 foreach ( $current_feature['dependencies'] as $dependency ) {
915 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
916 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
917 }
918 }
919 }
920 }
921 }
922
923 private function should_show_hidden() {
924 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
925 }
926
927 private function create_dependency_class( $dependency_name, $dependency_args ) {
928 if ( class_exists( $dependency_name ) ) {
929 return $dependency_name::instance();
930 }
931
932 if ( ! empty( $dependency_args ) ) {
933 return new Wrap_Core_Dependency( $dependency_args );
934 }
935
936 return new Non_Existing_Dependency( $dependency_name );
937 }
938
939 /**
940 * The experiments page is a WordPress options page, which means all the experiments are registered via WordPress' register_settings(),
941 * and their states are being sent in the POST request when saving.
942 * The options are being updated in a chronological order based on the POST data.
943 * This behavior interferes with the experiments dependency mechanism because the data that's being sent can be in any order,
944 * while the dependencies mechanism expects it to be in a specific order (dependencies should be activated before their dependents can).
945 * In order to solve this issue, we sort the experiments in the POST data based on their dependencies tree.
946 *
947 * @param $allowed_options
948 *
949 * @return mixed
950 */
951 private function sort_allowed_options_by_dependencies( $allowed_options ) {
952 if ( ! isset( $allowed_options['elementor'] ) ) {
953 return $allowed_options;
954 }
955
956 $sorted = Collection::make();
957 $visited = Collection::make();
958
959 $sort = function ( $item ) use ( &$sort, $sorted, $visited ) {
960 if ( $visited->contains( $item ) ) {
961 return;
962 }
963
964 $visited->push( $item );
965
966 $feature = $this->get_features( $item );
967
968 if ( ! $feature ) {
969 return;
970 }
971
972 foreach ( $feature['dependencies'] ?? [] as $dep ) {
973 $name = is_string( $dep ) ? $dep : $dep->get_name();
974
975 $sort( $name );
976 }
977
978 $sorted->push( $item );
979 };
980
981 foreach ( $allowed_options['elementor'] as $option ) {
982 $is_experiment_option = strpos( $option, static::OPTION_PREFIX ) === 0;
983
984 if ( ! $is_experiment_option ) {
985 continue;
986 }
987
988 $sort(
989 str_replace( static::OPTION_PREFIX, '', $option )
990 );
991 }
992
993 $allowed_options['elementor'] = Collection::make( $allowed_options['elementor'] )
994 ->filter( function ( $option ) {
995 return 0 !== strpos( $option, static::OPTION_PREFIX );
996 } )
997 ->merge(
998 $sorted->map( function ( $item ) {
999 return static::OPTION_PREFIX . $item;
1000 } )
1001 )
1002 ->values();
1003
1004 return $allowed_options;
1005 }
1006
1007 public function __construct() {
1008 $this->init_states();
1009
1010 $this->init_release_statuses();
1011
1012 $this->init_features();
1013
1014 add_action( 'admin_init', function () {
1015 System_Info::add_report(
1016 'experiments', [
1017 'file_name' => __DIR__ . '/experiments-reporter.php',
1018 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
1019 ]
1020 );
1021 }, 79 /* Before log */ );
1022
1023 if ( is_admin() ) {
1024 $page_id = Settings::PAGE_ID;
1025
1026 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
1027 $this->register_settings_fields( $settings );
1028 }, 11 );
1029
1030 add_filter( 'allowed_options', function ( $allowed_options ) {
1031 return $this->sort_allowed_options_by_dependencies( $allowed_options );
1032 }, 11 );
1033 }
1034
1035 // Register CLI commands.
1036 if ( Utils::is_wp_cli() ) {
1037 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
1038 }
1039 }
1040 }
1041