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

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