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

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