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

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