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

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