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

933 lines 28.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Experiments;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
6 use Elementor\Core\Utils\Collection;
7 use Elementor\Modules\System_Info\Module as System_Info;
8 use Elementor\Plugin;
9 use Elementor\Settings;
10 use Elementor\Tracker;
11 use Elementor\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 class Manager extends Base_Object {
18
19 const RELEASE_STATUS_DEV = 'dev';
20
21 const RELEASE_STATUS_ALPHA = 'alpha';
22
23 const RELEASE_STATUS_BETA = 'beta';
24
25 const RELEASE_STATUS_RC = 'rc';
26
27 const RELEASE_STATUS_STABLE = 'stable';
28
29 const STATE_DEFAULT = 'default';
30
31 const STATE_ACTIVE = 'active';
32
33 const STATE_INACTIVE = 'inactive';
34
35 const TYPE_HIDDEN = 'hidden';
36
37 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 /* translators: %1$s Link open tag, %2$s: Link close tag. */
438 'description' => sprintf( esc_html__(
439 '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',
440 'elementor'
441 ), '<a target="_blank" href="https://go.elementor.com/wp-dash-grid-container/">', '</a>'),
442
443 'release_status' => self::RELEASE_STATUS_ALPHA,
444 'default' => self::STATE_INACTIVE,
445 'hidden' => false,
446 'dependencies' => [
447 'container',
448 ],
449 ] );
450 }
451
452 /**
453 * Init States
454 *
455 * @since 3.1.0
456 * @access private
457 */
458 private function init_states() {
459 $this->states = [
460 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
461 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
462 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
463 ];
464 }
465
466 /**
467 * Init Statuses
468 *
469 * @since 3.1.0
470 * @access private
471 */
472 private function init_release_statuses() {
473 $this->release_statuses = [
474 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
475 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
476 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
477 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
478 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
479 ];
480 }
481
482 /**
483 * Init Features
484 *
485 * @since 3.1.0
486 * @access private
487 */
488 private function init_features() {
489 $this->features = [];
490
491 $this->add_default_features();
492
493 do_action( 'elementor/experiments/default-features-registered', $this );
494 }
495
496 /**
497 * Register Settings Fields
498 *
499 * @param Settings $settings
500 *
501 * @since 3.1.0
502 * @access private
503 *
504 */
505 private function register_settings_fields( Settings $settings ) {
506 $features = $this->get_features();
507
508 $fields = [];
509
510 foreach ( $features as $feature_name => $feature ) {
511 $is_hidden = $feature[ static::TYPE_HIDDEN ];
512 $is_mutable = $feature['mutable'];
513 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
514
515 if ( $should_hide_experiment ) {
516 unset( $features[ $feature_name ] );
517
518 continue;
519 }
520
521 $feature_key = 'experiment-' . $feature_name;
522
523 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
524
525 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
526
527 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
528
529 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
530 $this->render_feature_settings_field( $feature );
531 };
532 }
533
534 foreach ( [ 'stable', 'ongoing' ] as $section ) {
535 if ( ! isset( $fields[ $section ] ) ) {
536 $fields[ $section ]['no_features'] = [
537 'label' => esc_html__( 'No available experiments', 'elementor' ),
538 'field_args' => [
539 'type' => 'raw_html',
540 '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' ),
541 ],
542 ];
543 }
544
545 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
546 $fields[ $section ] += $settings->get_usage_fields();
547 }
548 }
549
550 $settings->add_tab(
551 'experiments', [
552 'label' => esc_html__( 'Features', 'elementor' ),
553 'sections' => [
554 'ongoing_experiments' => [
555 'callback' => function() {
556 $this->render_settings_intro();
557 },
558 'fields' => $fields['ongoing'],
559 ],
560 'stable_experiments' => [
561 'callback' => function() {
562 $this->render_stable_section_title();
563 },
564 'fields' => $fields['stable'],
565 ],
566 ],
567 ]
568 );
569 }
570
571 private function render_stable_section_title() {
572 ?>
573 <hr>
574 <h2>
575 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
576 </h2>
577 <?php
578 }
579
580 /**
581 * Render Settings Intro
582 *
583 * @since 3.1.0
584 * @access private
585 */
586 private function render_settings_intro() {
587 ?>
588 <h2>
589 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
590 </h2>
591 <p class="e-experiment__description">
592 <?php
593 printf(
594 /* translators: %1$s Link open tag, %2$s: Link close tag. */
595 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' ),
596 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
597 '</a>'
598 );
599 ?>
600 </p>
601 <p class="e-experiment__description">
602 <?php
603 printf(
604 /* translators: %1$s Link open tag, %2$s: Link close tag. */
605 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' ),
606 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
607 '</a>'
608 );
609 ?>
610 </p>
611
612 <?php if ( $this->get_features() ) { ?>
613 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
614 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
615 <button type="button" class="button e-experiment__button" value="default"><?php echo esc_html__( 'Back to default', 'elementor' ); ?></button>
616 <?php } ?>
617 <hr>
618 <h2 class="e-experiment__table-title">
619 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
620 </h2>
621 <?php
622 }
623
624 /**
625 * Render Feature Settings Field
626 *
627 * @since 3.1.0
628 * @access private
629 *
630 * @param array $feature
631 */
632 private function render_feature_settings_field( array $feature ) {
633 $control_id = 'e-experiment-' . $feature['name'];
634 $control_name = $this->get_feature_option_key( $feature['name'] );
635
636 $status = sprintf(
637 esc_html__( 'Status: %s', 'elementor' ),
638 $this->release_statuses[ $feature['release_status'] ]
639 );
640
641 ?>
642 <div class="e-experiment__content">
643 <select class="e-experiment__select"
644 id="<?php echo esc_attr( $control_id ); ?>"
645 name="<?php echo esc_attr( $control_name ); ?>"
646 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
647 >
648 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
649 <option value="<?php echo esc_attr( $state_key ); ?>"
650 <?php selected( $state_key, $feature['state'] ); ?>
651 >
652 <?php echo esc_html( $state_title ); ?>
653 </option>
654 <?php } ?>
655 </select>
656
657 <p class="description">
658 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
659 </p>
660
661 <?php $this->render_feature_dependency( $feature ); ?>
662
663 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
664 <div class="e-experiment__status">
665 <?php echo esc_html( $status ); ?>
666 </div>
667 <?php } ?>
668 </div>
669 <?php
670 }
671
672 private function render_feature_dependency( $feature ) {
673 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
674 ->map( function ( $dependency ) {
675 return $dependency->get_title();
676 } )
677 ->implode( ', ' );
678
679 if ( empty( $dependencies ) ) {
680 return;
681 }
682
683 ?>
684 <div class="e-experiment__dependency">
685 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
686 <span><?php echo esc_html( $dependencies ); ?></span>
687 </div>
688 <?php
689 }
690
691 private function has_non_existing_dependency( $feature ) {
692 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
693 ->find( function ( $dependency ) {
694 return $dependency instanceof Non_Existing_Dependency;
695 } );
696
697 return ! ! $non_existing_dep;
698 }
699
700 /**
701 * Get Feature Settings Label HTML
702 *
703 * @since 3.1.0
704 * @access private
705 *
706 * @param array $feature
707 *
708 * @return string
709 */
710 private function get_feature_settings_label_html( array $feature ) {
711 ob_start();
712
713 $is_feature_active = $this->is_feature_active( $feature['name'] );
714
715 $indicator_classes = 'e-experiment__title__indicator';
716
717 if ( $is_feature_active ) {
718 $indicator_classes .= ' e-experiment__title__indicator--active';
719 }
720
721 $indicator_tooltip = $this->get_feature_state_label( $feature );
722
723 ?>
724 <div class="e-experiment__title">
725 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
726 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
727 <?php foreach ( $feature['tags'] as $tag ) { ?>
728 <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>
729 <?php } ?>
730 </div>
731 <?php
732
733 return ob_get_clean();
734 }
735
736 /**
737 * Get Feature State Label
738 *
739 * @param array $feature
740 *
741 * @return string
742 */
743 public function get_feature_state_label( array $feature ) {
744 $is_feature_active = $this->is_feature_active( $feature['name'] );
745
746 if ( self::STATE_DEFAULT === $feature['state'] ) {
747 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
748 esc_html__( 'Inactive by default', 'elementor' );
749 } else {
750 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
751 esc_html__( 'Inactive', 'elementor' );
752 }
753
754 return $label;
755 }
756
757 /**
758 * Get Feature Settings Label HTML
759 *
760 * @since 3.1.0
761 * @access private
762 *
763 * @param string $feature_name
764 *
765 * @return int
766 */
767 private function get_saved_feature_state( $feature_name ) {
768 return get_option( $this->get_feature_option_key( $feature_name ) );
769 }
770
771 /**
772 * Get Feature Actual State
773 *
774 * @since 3.1.0
775 * @access private
776 *
777 * @param array $feature
778 *
779 * @return string
780 */
781 private function get_feature_actual_state( array $feature ) {
782 if ( ! empty( $feature['state'] ) && self::STATE_DEFAULT !== $feature['state'] ) {
783 return $feature['state'];
784 }
785
786 return $feature['default'];
787 }
788
789 /**
790 * On Feature State Change
791 *
792 * @since 3.1.0
793 * @access private
794 *
795 * @param array $old_feature_data
796 * @param string $new_state
797 *
798 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
799 */
800 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
801 $new_feature_data = $this->get_features( $old_feature_data['name'] );
802 $this->validate_dependency( $new_feature_data, $new_state );
803 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
804 if ( $old_state === $new_state ) {
805 return;
806 }
807
808 Plugin::$instance->files_manager->clear_cache();
809 if ( $new_feature_data['on_state_change'] ) {
810 $new_feature_data['on_state_change']( $old_state, $new_state );
811 }
812
813 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
814 }
815
816 /**
817 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
818 */
819 private function validate_dependency( array $feature, $new_state ) {
820 $rollback = function ( $feature_option_key, $state ) {
821 remove_all_actions( 'add_option_' . $feature_option_key );
822 remove_all_actions( 'update_option_' . $feature_option_key );
823
824 update_option( $feature_option_key, $state );
825 };
826
827 if ( self::STATE_DEFAULT === $new_state ) {
828 $new_state = $this->get_feature_actual_state( $feature );
829 }
830
831 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
832
833 if ( self::STATE_ACTIVE === $new_state ) {
834 if ( empty( $feature['dependencies'] ) ) {
835 return;
836 }
837
838 // Validate if the current feature dependency is available.
839 foreach ( $feature['dependencies'] as $dependency ) {
840 $dependency_feature = $this->get_features( $dependency->get_name() );
841
842 if ( ! $dependency_feature ) {
843 $rollback( $feature_option_key, self::STATE_INACTIVE );
844
845 throw new Exceptions\Dependency_Exception(
846 sprintf(
847 'The feature `%s` has a dependency `%s` that is not available.',
848 $feature['name'],
849 $dependency->get_name()
850 )
851 );
852 }
853
854 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
855
856 // If dependency is not active.
857 if ( self::STATE_INACTIVE === $dependency_state ) {
858 $rollback( $feature_option_key, self::STATE_INACTIVE );
859
860 throw new Exceptions\Dependency_Exception(
861 sprintf(
862 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
863 $feature['name'],
864 $dependency_feature['name']
865 )
866 );
867 }
868 }
869 } elseif ( self::STATE_INACTIVE === $new_state ) {
870 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
871 foreach ( $this->get_features() as $current_feature ) {
872 if ( empty( $current_feature['dependencies'] ) ) {
873 continue;
874 }
875
876 $current_feature_state = $this->get_feature_actual_state( $current_feature );
877
878 foreach ( $current_feature['dependencies'] as $dependency ) {
879 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
880 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
881 }
882 }
883 }
884 }
885 }
886
887 private function should_show_hidden() {
888 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
889 }
890
891 private function create_dependency_class( $dependency_name, $dependency_args ) {
892 if ( class_exists( $dependency_name ) ) {
893 return $dependency_name::instance();
894 }
895
896 if ( ! empty( $dependency_args ) ) {
897 return new Wrap_Core_Dependency( $dependency_args );
898 }
899
900 return new Non_Existing_Dependency( $dependency_name );
901 }
902
903 public function __construct() {
904 $this->init_states();
905
906 $this->init_release_statuses();
907
908 $this->init_features();
909
910 add_action( 'admin_init', function () {
911 System_Info::add_report(
912 'experiments', [
913 'file_name' => __DIR__ . '/experiments-reporter.php',
914 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
915 ]
916 );
917 }, 79 /* Before log */ );
918
919 if ( is_admin() ) {
920 $page_id = Settings::PAGE_ID;
921
922 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
923 $this->register_settings_fields( $settings );
924 }, 11 );
925 }
926
927 // Register CLI commands.
928 if ( Utils::is_wp_cli() ) {
929 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
930 }
931 }
932 }
933