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

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