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

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