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

915 lines 27.6 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 'default' => self::STATE_ACTIVE,
325 'generator_tag' => true,
326 ] );
327
328 $this->add_feature( [
329 'name' => 'e_optimized_assets_loading',
330 'title' => esc_html__( 'Improved Asset Loading', 'elementor' ),
331 'tag' => esc_html__( 'Performance', 'elementor' ),
332 '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' )
333 . ' <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">'
334 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
335 'release_status' => self::RELEASE_STATUS_STABLE,
336 'default' => self::STATE_ACTIVE,
337 'generator_tag' => true,
338 ] );
339
340 $this->add_feature( [
341 'name' => 'e_optimized_css_loading',
342 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
343 'tag' => esc_html__( 'Performance', 'elementor' ),
344 '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' )
345 . ' <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">'
346 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
347 'release_status' => self::RELEASE_STATUS_STABLE,
348 'default' => self::STATE_ACTIVE,
349 'generator_tag' => true,
350 ] );
351
352 $this->add_feature( [
353 'name' => 'e_font_icon_svg',
354 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
355 'tag' => esc_html__( 'Performance', 'elementor' ),
356 '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' )
357 . ' <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">'
358 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
359 'release_status' => self::RELEASE_STATUS_BETA,
360 'generator_tag' => true,
361 ] );
362
363 $this->add_feature( [
364 'name' => 'additional_custom_breakpoints',
365 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
366 'tag' => esc_html__( 'Performance', 'elementor' ),
367 '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' )
368 . ' <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">'
369 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
370 'release_status' => self::RELEASE_STATUS_STABLE,
371 'default' => self::STATE_ACTIVE,
372 'generator_tag' => true,
373 ] );
374
375 $this->add_feature( [
376 'name' => 'admin_menu_rearrangement',
377 'mutable' => false,
378 ] );
379
380 $this->add_feature( [
381 'name' => 'container',
382 'title' => esc_html__( 'Flexbox Container', 'elementor' ),
383 'description' => sprintf( esc_html__(
384 'Create advanced layouts and responsive designs with the new %1$sFlexbox Container element%2$s.
385 This experiment replaces the current section/column structure, but you\'ll still keep your existing
386 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.',
387 'elementor'
388 ), '<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>'),
389 'release_status' => self::RELEASE_STATUS_RC,
390 'default' => self::STATE_INACTIVE,
391 'new_site' => [
392 'default_active' => true,
393 'minimum_installation_version' => '3.16.0',
394 ],
395 'messages' => [
396 'on_deactivate' => esc_html__(
397 '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',
398 'elementor'
399 ),
400 ],
401 ] );
402
403 $this->add_feature( [
404 'name' => 'e_swiper_latest',
405 'title' => esc_html__( 'Upgrade Swiper Library', 'elementor' ),
406 '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' ),
407 'release_status' => self::RELEASE_STATUS_STABLE,
408 'new_site' => [
409 'default_active' => true,
410 'minimum_installation_version' => '3.11.0',
411 ],
412 'default' => self::STATE_INACTIVE,
413 ] );
414
415 $this->add_feature( [
416 'name' => 'container_grid',
417 'title' => esc_html__( 'Grid Container', 'elementor' ),
418 'tag' => esc_html__( 'Feature', 'elementor' ),
419 /* translators: %1$s Link open tag, %2$s: Link close tag. */
420 'description' => sprintf( esc_html__(
421 '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',
422 'elementor'
423 ), '<a target="_blank" href="https://go.elementor.com/wp-dash-grid-container/">', '</a>'),
424
425 'release_status' => self::RELEASE_STATUS_BETA,
426 'default' => self::STATE_INACTIVE,
427 'hidden' => false,
428 'dependencies' => [
429 'container',
430 ],
431 ] );
432 }
433
434 /**
435 * Init States
436 *
437 * @since 3.1.0
438 * @access private
439 */
440 private function init_states() {
441 $this->states = [
442 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
443 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
444 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
445 ];
446 }
447
448 /**
449 * Init Statuses
450 *
451 * @since 3.1.0
452 * @access private
453 */
454 private function init_release_statuses() {
455 $this->release_statuses = [
456 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
457 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
458 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
459 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
460 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
461 ];
462 }
463
464 /**
465 * Init Features
466 *
467 * @since 3.1.0
468 * @access private
469 */
470 private function init_features() {
471 $this->features = [];
472
473 $this->add_default_features();
474
475 do_action( 'elementor/experiments/default-features-registered', $this );
476 }
477
478 /**
479 * Register Settings Fields
480 *
481 * @param Settings $settings
482 *
483 * @since 3.1.0
484 * @access private
485 *
486 */
487 private function register_settings_fields( Settings $settings ) {
488 $features = $this->get_features();
489
490 $fields = [];
491
492 foreach ( $features as $feature_name => $feature ) {
493 $is_hidden = $feature[ static::TYPE_HIDDEN ];
494 $is_mutable = $feature['mutable'];
495 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
496
497 if ( $should_hide_experiment ) {
498 unset( $features[ $feature_name ] );
499
500 continue;
501 }
502
503 $feature_key = 'experiment-' . $feature_name;
504
505 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
506
507 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
508
509 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
510
511 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
512 $this->render_feature_settings_field( $feature );
513 };
514 }
515
516 foreach ( [ 'stable', 'ongoing' ] as $section ) {
517 if ( ! isset( $fields[ $section ] ) ) {
518 $fields[ $section ]['no_features'] = [
519 'label' => esc_html__( 'No available experiments', 'elementor' ),
520 'field_args' => [
521 'type' => 'raw_html',
522 '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' ),
523 ],
524 ];
525 }
526
527 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
528 $fields[ $section ] += $settings->get_usage_fields();
529 }
530 }
531
532 $settings->add_tab(
533 'experiments', [
534 'label' => esc_html__( 'Features', 'elementor' ),
535 'sections' => [
536 'ongoing_experiments' => [
537 'callback' => function() {
538 $this->render_settings_intro();
539 },
540 'fields' => $fields['ongoing'],
541 ],
542 'stable_experiments' => [
543 'callback' => function() {
544 $this->render_stable_section_title();
545 },
546 'fields' => $fields['stable'],
547 ],
548 ],
549 ]
550 );
551 }
552
553 private function render_stable_section_title() {
554 ?>
555 <hr>
556 <h2>
557 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
558 </h2>
559 <?php
560 }
561
562 /**
563 * Render Settings Intro
564 *
565 * @since 3.1.0
566 * @access private
567 */
568 private function render_settings_intro() {
569 ?>
570 <h2>
571 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
572 </h2>
573 <p class="e-experiment__description">
574 <?php
575 printf(
576 /* translators: %1$s Link open tag, %2$s: Link close tag. */
577 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' ),
578 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
579 '</a>'
580 );
581 ?>
582 </p>
583 <p class="e-experiment__description">
584 <?php
585 printf(
586 /* translators: %1$s Link open tag, %2$s: Link close tag. */
587 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' ),
588 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
589 '</a>'
590 );
591 ?>
592 </p>
593
594 <?php if ( $this->get_features() ) { ?>
595 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
596 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
597 <button type="button" class="button e-experiment__button" value="default"><?php echo esc_html__( 'Back to default', 'elementor' ); ?></button>
598 <?php } ?>
599 <hr>
600 <h2 class="e-experiment__table-title">
601 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
602 </h2>
603 <?php
604 }
605
606 /**
607 * Render Feature Settings Field
608 *
609 * @since 3.1.0
610 * @access private
611 *
612 * @param array $feature
613 */
614 private function render_feature_settings_field( array $feature ) {
615 $control_id = 'e-experiment-' . $feature['name'];
616 $control_name = $this->get_feature_option_key( $feature['name'] );
617
618 $status = sprintf(
619 esc_html__( 'Status: %s', 'elementor' ),
620 $this->release_statuses[ $feature['release_status'] ]
621 );
622
623 ?>
624 <div class="e-experiment__content">
625 <select class="e-experiment__select"
626 id="<?php echo esc_attr( $control_id ); ?>"
627 name="<?php echo esc_attr( $control_name ); ?>"
628 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
629 >
630 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
631 <option value="<?php echo esc_attr( $state_key ); ?>"
632 <?php selected( $state_key, $feature['state'] ); ?>
633 >
634 <?php echo esc_html( $state_title ); ?>
635 </option>
636 <?php } ?>
637 </select>
638
639 <p class="description">
640 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
641 </p>
642
643 <?php $this->render_feature_dependency( $feature ); ?>
644
645 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
646 <div class="e-experiment__status">
647 <?php echo esc_html( $status ); ?>
648 </div>
649 <?php } ?>
650 </div>
651 <?php
652 }
653
654 private function render_feature_dependency( $feature ) {
655 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
656 ->map( function ( $dependency ) {
657 return $dependency->get_title();
658 } )
659 ->implode( ', ' );
660
661 if ( empty( $dependencies ) ) {
662 return;
663 }
664
665 ?>
666 <div class="e-experiment__dependency">
667 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
668 <span><?php echo esc_html( $dependencies ); ?></span>
669 </div>
670 <?php
671 }
672
673 private function has_non_existing_dependency( $feature ) {
674 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
675 ->find( function ( $dependency ) {
676 return $dependency instanceof Non_Existing_Dependency;
677 } );
678
679 return ! ! $non_existing_dep;
680 }
681
682 /**
683 * Get Feature Settings Label HTML
684 *
685 * @since 3.1.0
686 * @access private
687 *
688 * @param array $feature
689 *
690 * @return string
691 */
692 private function get_feature_settings_label_html( array $feature ) {
693 ob_start();
694
695 $is_feature_active = $this->is_feature_active( $feature['name'] );
696
697 $indicator_classes = 'e-experiment__title__indicator';
698
699 if ( $is_feature_active ) {
700 $indicator_classes .= ' e-experiment__title__indicator--active';
701 }
702
703 $indicator_tooltip = $this->get_feature_state_label( $feature );
704
705 ?>
706 <div class="e-experiment__title">
707 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
708 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
709 <?php foreach ( $feature['tags'] as $tag ) { ?>
710 <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>
711 <?php } ?>
712 </div>
713 <?php
714
715 return ob_get_clean();
716 }
717
718 /**
719 * Get Feature State Label
720 *
721 * @param array $feature
722 *
723 * @return string
724 */
725 public function get_feature_state_label( array $feature ) {
726 $is_feature_active = $this->is_feature_active( $feature['name'] );
727
728 if ( self::STATE_DEFAULT === $feature['state'] ) {
729 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
730 esc_html__( 'Inactive by default', 'elementor' );
731 } else {
732 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
733 esc_html__( 'Inactive', 'elementor' );
734 }
735
736 return $label;
737 }
738
739 /**
740 * Get Feature Settings Label HTML
741 *
742 * @since 3.1.0
743 * @access private
744 *
745 * @param string $feature_name
746 *
747 * @return int
748 */
749 private function get_saved_feature_state( $feature_name ) {
750 return get_option( $this->get_feature_option_key( $feature_name ) );
751 }
752
753 /**
754 * Get Feature Actual State
755 *
756 * @since 3.1.0
757 * @access private
758 *
759 * @param array $feature
760 *
761 * @return string
762 */
763 private function get_feature_actual_state( array $feature ) {
764 if ( ! empty( $feature['state'] ) && self::STATE_DEFAULT !== $feature['state'] ) {
765 return $feature['state'];
766 }
767
768 return $feature['default'];
769 }
770
771 /**
772 * On Feature State Change
773 *
774 * @since 3.1.0
775 * @access private
776 *
777 * @param array $old_feature_data
778 * @param string $new_state
779 *
780 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
781 */
782 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
783 $new_feature_data = $this->get_features( $old_feature_data['name'] );
784 $this->validate_dependency( $new_feature_data, $new_state );
785 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
786 if ( $old_state === $new_state ) {
787 return;
788 }
789
790 Plugin::$instance->files_manager->clear_cache();
791 if ( $new_feature_data['on_state_change'] ) {
792 $new_feature_data['on_state_change']( $old_state, $new_state );
793 }
794
795 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
796 }
797
798 /**
799 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
800 */
801 private function validate_dependency( array $feature, $new_state ) {
802 $rollback = function ( $feature_option_key, $state ) {
803 remove_all_actions( 'add_option_' . $feature_option_key );
804 remove_all_actions( 'update_option_' . $feature_option_key );
805
806 update_option( $feature_option_key, $state );
807 };
808
809 if ( self::STATE_DEFAULT === $new_state ) {
810 $new_state = $this->get_feature_actual_state( $feature );
811 }
812
813 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
814
815 if ( self::STATE_ACTIVE === $new_state ) {
816 if ( empty( $feature['dependencies'] ) ) {
817 return;
818 }
819
820 // Validate if the current feature dependency is available.
821 foreach ( $feature['dependencies'] as $dependency ) {
822 $dependency_feature = $this->get_features( $dependency->get_name() );
823
824 if ( ! $dependency_feature ) {
825 $rollback( $feature_option_key, self::STATE_INACTIVE );
826
827 throw new Exceptions\Dependency_Exception(
828 sprintf(
829 'The feature `%s` has a dependency `%s` that is not available.',
830 $feature['name'],
831 $dependency->get_name()
832 )
833 );
834 }
835
836 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
837
838 // If dependency is not active.
839 if ( self::STATE_INACTIVE === $dependency_state ) {
840 $rollback( $feature_option_key, self::STATE_INACTIVE );
841
842 throw new Exceptions\Dependency_Exception(
843 sprintf(
844 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
845 $feature['name'],
846 $dependency_feature['name']
847 )
848 );
849 }
850 }
851 } elseif ( self::STATE_INACTIVE === $new_state ) {
852 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
853 foreach ( $this->get_features() as $current_feature ) {
854 if ( empty( $current_feature['dependencies'] ) ) {
855 continue;
856 }
857
858 $current_feature_state = $this->get_feature_actual_state( $current_feature );
859
860 foreach ( $current_feature['dependencies'] as $dependency ) {
861 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
862 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
863 }
864 }
865 }
866 }
867 }
868
869 private function should_show_hidden() {
870 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
871 }
872
873 private function create_dependency_class( $dependency_name, $dependency_args ) {
874 if ( class_exists( $dependency_name ) ) {
875 return $dependency_name::instance();
876 }
877
878 if ( ! empty( $dependency_args ) ) {
879 return new Wrap_Core_Dependency( $dependency_args );
880 }
881
882 return new Non_Existing_Dependency( $dependency_name );
883 }
884
885 public function __construct() {
886 $this->init_states();
887
888 $this->init_release_statuses();
889
890 $this->init_features();
891
892 add_action( 'admin_init', function () {
893 System_Info::add_report(
894 'experiments', [
895 'file_name' => __DIR__ . '/experiments-reporter.php',
896 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
897 ]
898 );
899 }, 79 /* Before log */ );
900
901 if ( is_admin() ) {
902 $page_id = Settings::PAGE_ID;
903
904 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
905 $this->register_settings_fields( $settings );
906 }, 11 );
907 }
908
909 // Register CLI commands.
910 if ( Utils::is_wp_cli() ) {
911 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
912 }
913 }
914 }
915