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

1,052 lines 31.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Experiments;
3
4 use Elementor\Core\Base\Base_Object;
5 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
6 use Elementor\Core\Utils\Collection;
7 use Elementor\Modules\System_Info\Module as System_Info;
8 use Elementor\Plugin;
9 use Elementor\Settings;
10 use Elementor\Tracker;
11 use Elementor\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 class Manager extends Base_Object {
18
19 const RELEASE_STATUS_DEV = 'dev';
20
21 const RELEASE_STATUS_ALPHA = 'alpha';
22
23 const RELEASE_STATUS_BETA = 'beta';
24
25 const RELEASE_STATUS_RC = 'rc';
26
27 const RELEASE_STATUS_STABLE = 'stable';
28
29 const STATE_DEFAULT = 'default';
30
31 const STATE_ACTIVE = 'active';
32
33 const STATE_INACTIVE = 'inactive';
34
35 const TYPE_HIDDEN = 'hidden';
36
37 const OPTION_PREFIX = 'elementor_experiment-';
38
39 private $states;
40
41 private $release_statuses;
42
43 private $features;
44
45 /**
46 * Add Feature
47 *
48 * @since 3.1.0
49 * @access public
50 *
51 * @param array $options {
52 * @type string $name
53 * @type string $title
54 * @type string $tag
55 * @type array $tags
56 * @type string $description
57 * @type string $release_status
58 * @type string $default
59 * @type callable $on_state_change
60 * }
61 *
62 * @return array|null
63 * @throws \Exception
64 */
65 public function add_feature( array $options ) {
66 if ( isset( $this->features[ $options['name'] ] ) ) {
67 return null;
68 }
69
70 $default_experimental_data = [
71 'tag' => '', // Deprecated, use 'tags' instead.
72 'tags' => [],
73 'description' => '',
74 'release_status' => self::RELEASE_STATUS_ALPHA,
75 'default' => self::STATE_INACTIVE,
76 'mutable' => true,
77 static::TYPE_HIDDEN => false,
78 'new_site' => [
79 'default_active' => false,
80 'always_active' => false,
81 'minimum_installation_version' => null,
82 ],
83 'on_state_change' => null,
84 'generator_tag' => false,
85 ];
86
87 $allowed_options = [ 'name', 'title', 'tag', 'tags', 'description', 'release_status', 'default', 'mutable', static::TYPE_HIDDEN, 'new_site', 'on_state_change', 'dependencies', 'generator_tag', 'messages' ];
88
89 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
90
91 $experimental_data = $this->unify_feature_tags( $experimental_data );
92
93 $new_site = $experimental_data['new_site'];
94
95 if ( $new_site['default_active'] || $new_site['always_active'] ) {
96 $is_new_installation = $this->install_compare( $new_site['minimum_installation_version'] );
97
98 if ( $is_new_installation ) {
99 if ( $new_site['always_active'] ) {
100 $experimental_data['state'] = self::STATE_ACTIVE;
101
102 $experimental_data['mutable'] = false;
103 } elseif ( $new_site['default_active'] ) {
104 $experimental_data['default'] = self::STATE_ACTIVE;
105 }
106 }
107 }
108
109 if ( $experimental_data['mutable'] ) {
110 $experimental_data['state'] = $this->get_saved_feature_state( $options['name'] );
111 }
112
113 if ( empty( $experimental_data['state'] ) ) {
114 $experimental_data['state'] = self::STATE_DEFAULT;
115 }
116
117 if ( ! empty( $experimental_data['dependencies'] ) ) {
118 foreach ( $experimental_data['dependencies'] as $key => $dependency ) {
119 $feature = $this->get_features( $dependency );
120
121 if ( ! empty( $feature[ static::TYPE_HIDDEN ] ) ) {
122 throw new Exceptions\Dependency_Exception( 'Depending on a hidden experiment is not allowed.' );
123 }
124
125 $experimental_data['dependencies'][ $key ] = $this->create_dependency_class( $dependency, $feature );
126 }
127 }
128
129 $this->features[ $options['name'] ] = $experimental_data;
130
131 if ( $experimental_data['mutable'] && is_admin() ) {
132 $feature_option_key = $this->get_feature_option_key( $options['name'] );
133
134 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data, $feature_option_key ) {
135 try {
136 $this->on_feature_state_change( $experimental_data, $new_state, $old_state );
137 } catch ( Exceptions\Dependency_Exception $e ) {
138 $message = sprintf(
139 '<p>%s</p><p><a href="#" onclick="location.href=\'%s\'">%s</a></p>',
140 esc_html( $e->getMessage() ),
141 site_url( 'wp-admin/admin.php?page=elementor#tab-experiments' ),
142 esc_html__( 'Back', 'elementor' )
143 );
144
145 wp_die( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
146 }
147 };
148
149 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
150 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
151 }
152
153 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
154
155 return $experimental_data;
156 }
157
158 private function install_compare( $version ) {
159 $installs_history = Upgrade_Manager::get_installs_history();
160
161 if ( empty( $installs_history ) ) {
162 return false;
163 }
164
165 $cleaned_version = preg_replace( '/-(beta|cloud|dev)\d*$/', '', key( $installs_history ) );
166
167 return version_compare(
168 $cleaned_version,
169 $version,
170 '>='
171 );
172 }
173
174 /**
175 * Combine 'tag' and 'tags' into one property.
176 *
177 * @param array $experimental_data
178 *
179 * @return array
180 */
181 private function unify_feature_tags( array $experimental_data ) : array {
182 foreach ( [ 'tag', 'tags' ] as $key ) {
183 if ( empty( $experimental_data[ $key ] ) ) {
184 continue;
185 }
186
187 $experimental_data[ $key ] = $this->format_feature_tags( $experimental_data[ $key ] );
188 }
189
190 if ( is_array( $experimental_data['tag'] ) ) {
191 $experimental_data['tags'] = array_merge( $experimental_data['tag'], $experimental_data['tags'] );
192 }
193
194 return $experimental_data;
195 }
196
197 /**
198 * Format feature tags into the right format.
199 *
200 * @param string|array[
201 * [
202 * 'type' => string,
203 * 'label' => string
204 * ]
205 * ] $tag
206 *
207 * @return array
208 */
209 private function format_feature_tags( $tags ) : array {
210 if ( ! is_string( $tags ) && ! is_array( $tags ) ) {
211 return [];
212 }
213
214 $default_tag = [
215 'type' => 'default',
216 'label' => '',
217 ];
218
219 $allowed_tag_properties = [ 'type', 'label' ];
220
221 // If $tags is string, explode by commas and convert to array.
222 if ( is_string( $tags ) ) {
223 $tags = array_filter( explode( ',', $tags ) );
224
225 foreach ( $tags as $i => $tag ) {
226 $tags[ $i ] = [ 'label' => trim( $tag ) ];
227 }
228 }
229
230 foreach ( $tags as $i => $tag ) {
231 if ( empty( $tag['label'] ) ) {
232 unset( $tags[ $i ] );
233 continue;
234 }
235
236 $tags[ $i ] = $this->merge_properties( $default_tag, $tag, $allowed_tag_properties );
237 }
238
239 return $tags;
240 }
241
242 /**
243 * Remove Feature
244 *
245 * @since 3.1.0
246 * @access public
247 *
248 * @param string $feature_name
249 */
250 public function remove_feature( $feature_name ) {
251 unset( $this->features[ $feature_name ] );
252 }
253
254 /**
255 * Get Features
256 *
257 * @since 3.1.0
258 * @access public
259 *
260 * @param string $feature_name Optional. Default is null
261 *
262 * @return array|null
263 */
264 public function get_features( $feature_name = null ) {
265 return self::get_items( $this->features, $feature_name );
266 }
267
268 /**
269 * Get Active Features
270 *
271 * @since 3.1.0
272 * @access public
273 *
274 * @return array
275 */
276 public function get_active_features() {
277 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
278 }
279
280 /**
281 * Is Feature Active
282 *
283 * @since 3.1.0
284 * @access public
285 *
286 * @param string $feature_name
287 *
288 * @return bool
289 */
290 public function is_feature_active( $feature_name ) {
291 $feature = $this->get_features( $feature_name );
292
293 if ( ! $feature ) {
294 return false;
295 }
296
297 return self::STATE_ACTIVE === $this->get_feature_actual_state( $feature );
298 }
299
300 /**
301 * Set Feature Default State
302 *
303 * @since 3.1.0
304 * @access public
305 *
306 * @param string $feature_name
307 * @param string $default_state
308 */
309 public function set_feature_default_state( $feature_name, $default_state ) {
310 $feature = $this->get_features( $feature_name );
311
312 if ( ! $feature ) {
313 return;
314 }
315
316 $this->features[ $feature_name ]['default'] = $default_state;
317 }
318
319 /**
320 * Get Feature Option Key
321 *
322 * @since 3.1.0
323 * @access public
324 *
325 * @param string $feature_name
326 *
327 * @return string
328 */
329 public function get_feature_option_key( $feature_name ) {
330 return static::OPTION_PREFIX . $feature_name;
331 }
332
333 private function add_default_features() {
334 $this->add_feature( [
335 'name' => 'e_optimized_assets_loading',
336 'title' => esc_html__( 'Improved Asset Loading', 'elementor' ),
337 'tag' => esc_html__( 'Performance', 'elementor' ),
338 'description' => sprintf(
339 '%1$s <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">%2$s</a>',
340 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' ),
341 esc_html__( 'Learn more', 'elementor' )
342 ),
343 static::TYPE_HIDDEN => true,
344 'mutable' => false,
345 'release_status' => self::RELEASE_STATUS_STABLE,
346 'default' => self::STATE_ACTIVE,
347 'generator_tag' => true,
348 ] );
349
350 $this->add_feature( [
351 'name' => 'e_optimized_css_loading',
352 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
353 'tag' => esc_html__( 'Performance', 'elementor' ),
354 'description' => sprintf(
355 '%1$s <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">%2$s</a>',
356 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' ),
357 esc_html__( 'Learn more', 'elementor' )
358 ),
359 'release_status' => self::RELEASE_STATUS_STABLE,
360 'new_site' => [
361 'default_active' => true,
362 'minimum_installation_version' => '3.3.0',
363 ],
364 'generator_tag' => true,
365 ] );
366
367 $this->add_feature( [
368 'name' => 'e_font_icon_svg',
369 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
370 'tag' => esc_html__( 'Performance', 'elementor' ),
371 'description' => sprintf(
372 '%1$s <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">%2$s</a>',
373 esc_html__( 'The “Inline Font Icons” will render the icons as inline SVG without loading the Font-Awesome and the eicons libraries and its related CSS files and fonts.', 'elementor' ),
374 esc_html__( 'Learn more', 'elementor' )
375 ),
376 'release_status' => self::RELEASE_STATUS_STABLE,
377 'new_site' => [
378 'default_active' => true,
379 'minimum_installation_version' => '3.17.0',
380 ],
381 'generator_tag' => true,
382 ] );
383
384 $this->add_feature( [
385 'name' => 'additional_custom_breakpoints',
386 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
387 'tag' => esc_html__( 'Performance', 'elementor' ),
388 'description' => sprintf(
389 '%1$s <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">%2$s</a>',
390 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' ),
391 esc_html__( 'Learn more', 'elementor' )
392 ),
393 'release_status' => self::RELEASE_STATUS_STABLE,
394 'default' => self::STATE_ACTIVE,
395 'generator_tag' => true,
396 ] );
397
398 $this->add_feature( [
399 'name' => 'container',
400 'title' => esc_html__( 'Flexbox Container', 'elementor' ),
401 'description' => sprintf(
402 esc_html__(
403 'Create advanced layouts and responsive designs with the new %1$sFlexbox Container element%2$s.
404 This experiment replaces the current section/column structure, but you\'ll still keep your existing
405 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.',
406 'elementor'
407 ),
408 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container/">',
409 '</a>',
410 '<a target="_blank" href="https://go.elementor.com/wp-dash-flex-container-playground/">',
411 '</a>'
412 ),
413 'release_status' => self::RELEASE_STATUS_STABLE,
414 'default' => self::STATE_INACTIVE,
415 'new_site' => [
416 'default_active' => true,
417 'minimum_installation_version' => '3.16.0',
418 ],
419 'messages' => [
420 'on_deactivate' => sprintf(
421 '%1$s <a target="_blank" href="https://go.elementor.com/wp-dash-deactivate-container/">%2$s</a>',
422 esc_html__( 'Container-based content will be hidden from your site and may not be recoverable in all cases.', 'elementor' ),
423 esc_html__( 'Learn more', 'elementor' ),
424 ),
425 ],
426 ] );
427
428 $this->add_feature( [
429 'name' => 'container_grid',
430 'title' => esc_html__( 'Grid Container', 'elementor' ),
431 'tag' => esc_html__( 'Feature', 'elementor' ),
432 'description' => sprintf(
433 '%1$s <a target="_blank" href="https://go.elementor.com/wp-dash-grid-container/">%2$s</a>',
434 esc_html__( 'Create pixel perfect layouts by placing elements in a customizable grid. Activate to add the CSS Grid option to container elements.', 'elementor' ),
435 esc_html__( 'Learn more', 'elementor' ),
436 ),
437 'release_status' => self::RELEASE_STATUS_STABLE,
438 'default' => self::STATE_ACTIVE,
439 'dependencies' => [
440 'container',
441 ],
442 ] );
443
444 $this->add_feature( [
445 'name' => 'e_swiper_latest',
446 'title' => esc_html__( 'Upgrade Swiper Library', 'elementor' ),
447 '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' ),
448 'release_status' => self::RELEASE_STATUS_STABLE,
449 'default' => self::STATE_ACTIVE,
450 ] );
451
452 $this->add_feature( [
453 'name' => 'e_nested_atomic_repeaters',
454 'title' => esc_html__( 'Nested Elements Performance', 'elementor' ),
455 'tag' => esc_html__( 'Performance', 'elementor' ),
456 'description' => esc_html__( 'Improve the performance of the Nested widgets.', 'elementor' ),
457 static::TYPE_HIDDEN => true,
458 'release_status' => self::RELEASE_STATUS_DEV,
459 'default' => self::STATE_INACTIVE,
460 'new_site' => [
461 'default_active' => false,
462 ],
463 ] );
464
465 $this->add_feature( [
466 'name' => 'e_optimized_control_loading',
467 'title' => esc_html__( 'Optimized Control Loading', 'elementor' ),
468 'tag' => esc_html__( 'Performance', 'elementor' ),
469 'description' => esc_html__( 'Use this experiment to improve control loading. This experiment improves site performance by loading controls only when needed.', 'elementor' ),
470 'release_status' => self::RELEASE_STATUS_BETA,
471 'default' => self::STATE_INACTIVE,
472 'new_site' => [
473 'default_active' => true,
474 'minimum_installation_version' => '3.22.0',
475 ],
476 'generator_tag' => true,
477 ] );
478
479 $this->add_feature( [
480 'name' => 'e_optimized_markup',
481 'title' => esc_html__( 'Optimized Markup', 'elementor' ),
482 'tag' => esc_html__( 'Performance', 'elementor' ),
483 'description' => esc_html__( 'Reduce the DOM size by eliminating HTML tags in various elements and widgets. This experiment includes markup changes so it might require updating custom CSS/JS code and cause compatibility issues with third party plugins.', 'elementor' ),
484 static::TYPE_HIDDEN => true,
485 'release_status' => self::RELEASE_STATUS_DEV,
486 'default' => self::STATE_INACTIVE,
487 ] );
488
489 $this->add_feature( [
490 'name' => 'e_onboarding',
491 'title' => esc_html__( 'Plugin Onboarding', 'elementor' ),
492 'description' => esc_html__( 'New plugin onboarding.', 'elementor' ),
493 static::TYPE_HIDDEN => true,
494 'release_status' => self::RELEASE_STATUS_ALPHA,
495 'default' => self::STATE_ACTIVE,
496 ] );
497 }
498
499 /**
500 * Init States
501 *
502 * @since 3.1.0
503 * @access private
504 */
505 private function init_states() {
506 $this->states = [
507 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
508 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
509 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
510 ];
511 }
512
513 /**
514 * Init Statuses
515 *
516 * @since 3.1.0
517 * @access private
518 */
519 private function init_release_statuses() {
520 $this->release_statuses = [
521 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
522 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
523 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
524 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
525 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
526 ];
527 }
528
529 /**
530 * Init Features
531 *
532 * @since 3.1.0
533 * @access private
534 */
535 private function init_features() {
536 $this->features = [];
537
538 $this->add_default_features();
539
540 do_action( 'elementor/experiments/default-features-registered', $this );
541 }
542
543 /**
544 * Register Settings Fields
545 *
546 * @param Settings $settings
547 *
548 * @since 3.1.0
549 * @access private
550 *
551 */
552 private function register_settings_fields( Settings $settings ) {
553 $features = $this->get_features();
554
555 $fields = [];
556
557 foreach ( $features as $feature_name => $feature ) {
558 $is_hidden = $feature[ static::TYPE_HIDDEN ];
559 $is_mutable = $feature['mutable'];
560 $should_hide_experiment = ! $is_mutable || ( $is_hidden && ! $this->should_show_hidden() ) || $this->has_non_existing_dependency( $feature );
561
562 if ( $should_hide_experiment ) {
563 unset( $features[ $feature_name ] );
564
565 continue;
566 }
567
568 $feature_key = 'experiment-' . $feature_name;
569
570 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
571
572 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
573
574 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
575
576 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
577 $this->render_feature_settings_field( $feature );
578 };
579 }
580
581 foreach ( [ 'stable', 'ongoing' ] as $section ) {
582 if ( ! isset( $fields[ $section ] ) ) {
583 $fields[ $section ]['no_features'] = [
584 'label' => esc_html__( 'No available experiments', 'elementor' ),
585 'field_args' => [
586 'type' => 'raw_html',
587 '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' ),
588 ],
589 ];
590 }
591
592 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
593 $fields[ $section ] += $settings->get_usage_fields();
594 }
595 }
596
597 $settings->add_tab(
598 'experiments', [
599 'label' => esc_html__( 'Features', 'elementor' ),
600 'sections' => [
601 'ongoing_experiments' => [
602 'callback' => function() {
603 $this->render_settings_intro();
604 },
605 'fields' => $fields['ongoing'],
606 ],
607 'stable_experiments' => [
608 'callback' => function() {
609 $this->render_stable_section_title();
610 },
611 'fields' => $fields['stable'],
612 ],
613 ],
614 ]
615 );
616 }
617
618 private function render_stable_section_title() {
619 ?>
620 <hr>
621 <h2>
622 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
623 </h2>
624 <?php
625 }
626
627 /**
628 * Render Settings Intro
629 *
630 * @since 3.1.0
631 * @access private
632 */
633 private function render_settings_intro() {
634 ?>
635 <h2>
636 <?php echo esc_html__( 'Experiments and Features', 'elementor' ); ?>
637 </h2>
638 <p class="e-experiment__description">
639 <?php
640 echo sprintf(
641 /* translators: %1$s Link open tag, %2$s: Link close tag. */
642 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' ),
643 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
644 '</a>'
645 );
646 ?>
647 </p>
648 <p class="e-experiment__description">
649 <?php
650 echo sprintf(
651 '%1$s <a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">%2$s</a>',
652 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.', 'elementor' ),
653 esc_html__( 'Learn more', 'elementor' ),
654 );
655 ?>
656 </p>
657
658 <?php if ( $this->get_features() ) { ?>
659 <button type="button" class="button e-experiment__button" value="active"><?php echo esc_html__( 'Activate All', 'elementor' ); ?></button>
660 <button type="button" class="button e-experiment__button" value="inactive"><?php echo esc_html__( 'Deactivate All', 'elementor' ); ?></button>
661 <button type="button" class="button e-experiment__button" value="default"><?php echo esc_html__( 'Back to default', 'elementor' ); ?></button>
662 <?php } ?>
663 <hr>
664 <h2 class="e-experiment__table-title">
665 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
666 </h2>
667 <?php
668 }
669
670 /**
671 * Render Feature Settings Field
672 *
673 * @since 3.1.0
674 * @access private
675 *
676 * @param array $feature
677 */
678 private function render_feature_settings_field( array $feature ) {
679 $control_id = 'e-experiment-' . $feature['name'];
680 $control_name = $this->get_feature_option_key( $feature['name'] );
681
682 $status = sprintf(
683 /* translators: %s Release status. */
684 esc_html__( 'Status: %s', 'elementor' ),
685 $this->release_statuses[ $feature['release_status'] ]
686 );
687
688 ?>
689 <div class="e-experiment__content">
690 <select class="e-experiment__select"
691 id="<?php echo esc_attr( $control_id ); ?>"
692 name="<?php echo esc_attr( $control_name ); ?>"
693 data-experiment-id="<?php echo esc_attr( $feature['name'] ); ?>"
694 >
695 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
696 <option value="<?php echo esc_attr( $state_key ); ?>"
697 <?php selected( $state_key, $feature['state'] ); ?>
698 >
699 <?php echo esc_html( $state_title ); ?>
700 </option>
701 <?php } ?>
702 </select>
703
704 <p class="description">
705 <?php Utils::print_unescaped_internal_string( $feature['description'] ); ?>
706 </p>
707
708 <?php $this->render_feature_dependency( $feature ); ?>
709
710 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
711 <div class="e-experiment__status">
712 <?php echo esc_html( $status ); ?>
713 </div>
714 <?php } ?>
715 </div>
716 <?php
717 }
718
719 private function render_feature_dependency( $feature ) {
720 $dependencies = ( new Collection( $feature['dependencies'] ?? [] ) )
721 ->map( function ( $dependency ) {
722 return $dependency->get_title();
723 } )
724 ->implode( ', ' );
725
726 if ( empty( $dependencies ) ) {
727 return;
728 }
729
730 ?>
731 <div class="e-experiment__dependency">
732 <strong class="e-experiment__dependency__title"><?php echo esc_html__( 'Requires', 'elementor' ); ?>:</strong>
733 <span><?php echo esc_html( $dependencies ); ?></span>
734 </div>
735 <?php
736 }
737
738 private function has_non_existing_dependency( $feature ) {
739 $non_existing_dep = ( new Collection( $feature['dependencies'] ?? [] ) )
740 ->find( function ( $dependency ) {
741 return $dependency instanceof Non_Existing_Dependency;
742 } );
743
744 return ! ! $non_existing_dep;
745 }
746
747 /**
748 * Get Feature Settings Label HTML
749 *
750 * @since 3.1.0
751 * @access private
752 *
753 * @param array $feature
754 *
755 * @return string
756 */
757 private function get_feature_settings_label_html( array $feature ) {
758 ob_start();
759
760 $is_feature_active = $this->is_feature_active( $feature['name'] );
761
762 $indicator_classes = 'e-experiment__title__indicator';
763
764 if ( $is_feature_active ) {
765 $indicator_classes .= ' e-experiment__title__indicator--active';
766 }
767
768 $indicator_tooltip = $this->get_feature_state_label( $feature );
769
770 ?>
771 <div class="e-experiment__title">
772 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
773 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
774 <?php foreach ( $feature['tags'] as $tag ) { ?>
775 <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>
776 <?php } ?>
777 </div>
778 <?php
779
780 return ob_get_clean();
781 }
782
783 /**
784 * Get Feature State Label
785 *
786 * @param array $feature
787 *
788 * @return string
789 */
790 public function get_feature_state_label( array $feature ) {
791 $is_feature_active = $this->is_feature_active( $feature['name'] );
792
793 if ( self::STATE_DEFAULT === $feature['state'] ) {
794 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
795 esc_html__( 'Inactive by default', 'elementor' );
796 } else {
797 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
798 esc_html__( 'Inactive', 'elementor' );
799 }
800
801 return $label;
802 }
803
804 /**
805 * Get Feature Settings Label HTML
806 *
807 * @since 3.1.0
808 * @access private
809 *
810 * @param string $feature_name
811 *
812 * @return int
813 */
814 private function get_saved_feature_state( $feature_name ) {
815 return get_option( $this->get_feature_option_key( $feature_name ) );
816 }
817
818 /**
819 * Get Feature Actual State
820 *
821 * @since 3.1.0
822 * @access private
823 *
824 * @param array $feature
825 *
826 * @return string
827 */
828 private function get_feature_actual_state( array $feature ) {
829 if ( ! empty( $feature['state'] ) && self::STATE_DEFAULT !== $feature['state'] ) {
830 return $feature['state'];
831 }
832
833 return $feature['default'];
834 }
835
836 /**
837 * On Feature State Change
838 *
839 * @since 3.1.0
840 * @access private
841 *
842 * @param array $old_feature_data
843 * @param string $new_state
844 *
845 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
846 */
847 private function on_feature_state_change( array $old_feature_data, $new_state, $old_state ) {
848 $new_feature_data = $this->get_features( $old_feature_data['name'] );
849 $this->validate_dependency( $new_feature_data, $new_state );
850 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
851 if ( $old_state === $new_state ) {
852 return;
853 }
854
855 Plugin::$instance->files_manager->clear_cache();
856 if ( $new_feature_data['on_state_change'] ) {
857 $new_feature_data['on_state_change']( $old_state, $new_state );
858 }
859
860 do_action( 'elementor/experiments/feature-state-change/' . $old_feature_data['name'], $old_state, $new_state );
861 }
862
863 /**
864 * @throws \Elementor\Core\Experiments\Exceptions\Dependency_Exception
865 */
866 private function validate_dependency( array $feature, $new_state ) {
867 $rollback = function ( $feature_option_key, $state ) {
868 remove_all_actions( 'add_option_' . $feature_option_key );
869 remove_all_actions( 'update_option_' . $feature_option_key );
870
871 update_option( $feature_option_key, $state );
872 };
873
874 if ( self::STATE_DEFAULT === $new_state ) {
875 $new_state = $this->get_feature_actual_state( $feature );
876 }
877
878 $feature_option_key = $this->get_feature_option_key( $feature['name'] );
879
880 if ( self::STATE_ACTIVE === $new_state ) {
881 if ( empty( $feature['dependencies'] ) ) {
882 return;
883 }
884
885 // Validate if the current feature dependency is available.
886 foreach ( $feature['dependencies'] as $dependency ) {
887 $dependency_feature = $this->get_features( $dependency->get_name() );
888
889 if ( ! $dependency_feature ) {
890 $rollback( $feature_option_key, self::STATE_INACTIVE );
891
892 throw new Exceptions\Dependency_Exception(
893 sprintf(
894 'The feature `%s` has a dependency `%s` that is not available.',
895 $feature['name'],
896 $dependency->get_name()
897 )
898 );
899 }
900
901 $dependency_state = $this->get_feature_actual_state( $dependency_feature );
902
903 // If dependency is not active.
904 if ( self::STATE_INACTIVE === $dependency_state ) {
905 $rollback( $feature_option_key, self::STATE_INACTIVE );
906
907 throw new Exceptions\Dependency_Exception(
908 sprintf(
909 'To turn on `%1$s`, Experiment: `%2$s` activity is required!',
910 $feature['name'],
911 $dependency_feature['name']
912 )
913 );
914 }
915 }
916 } elseif ( self::STATE_INACTIVE === $new_state ) {
917 // Make sure to deactivate a dependant experiment of the current feature when it's deactivated.
918 foreach ( $this->get_features() as $current_feature ) {
919 if ( empty( $current_feature['dependencies'] ) ) {
920 continue;
921 }
922
923 $current_feature_state = $this->get_feature_actual_state( $current_feature );
924
925 foreach ( $current_feature['dependencies'] as $dependency ) {
926 if ( self::STATE_ACTIVE === $current_feature_state && $feature['name'] === $dependency->get_name() ) {
927 update_option( $this->get_feature_option_key( $current_feature['name'] ), static::STATE_INACTIVE );
928 }
929 }
930 }
931 }
932 }
933
934 private function should_show_hidden() {
935 return defined( 'ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS' ) && ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS;
936 }
937
938 private function create_dependency_class( $dependency_name, $dependency_args ) {
939 if ( class_exists( $dependency_name ) ) {
940 return $dependency_name::instance();
941 }
942
943 if ( ! empty( $dependency_args ) ) {
944 return new Wrap_Core_Dependency( $dependency_args );
945 }
946
947 return new Non_Existing_Dependency( $dependency_name );
948 }
949
950 /**
951 * The experiments page is a WordPress options page, which means all the experiments are registered via WordPress' register_settings(),
952 * and their states are being sent in the POST request when saving.
953 * The options are being updated in a chronological order based on the POST data.
954 * This behavior interferes with the experiments dependency mechanism because the data that's being sent can be in any order,
955 * while the dependencies mechanism expects it to be in a specific order (dependencies should be activated before their dependents can).
956 * In order to solve this issue, we sort the experiments in the POST data based on their dependencies tree.
957 *
958 * @param $allowed_options
959 *
960 * @return mixed
961 */
962 private function sort_allowed_options_by_dependencies( $allowed_options ) {
963 if ( ! isset( $allowed_options['elementor'] ) ) {
964 return $allowed_options;
965 }
966
967 $sorted = Collection::make();
968 $visited = Collection::make();
969
970 $sort = function ( $item ) use ( &$sort, $sorted, $visited ) {
971 if ( $visited->contains( $item ) ) {
972 return;
973 }
974
975 $visited->push( $item );
976
977 $feature = $this->get_features( $item );
978
979 if ( ! $feature ) {
980 return;
981 }
982
983 foreach ( $feature['dependencies'] ?? [] as $dep ) {
984 $name = is_string( $dep ) ? $dep : $dep->get_name();
985
986 $sort( $name );
987 }
988
989 $sorted->push( $item );
990 };
991
992 foreach ( $allowed_options['elementor'] as $option ) {
993 $is_experiment_option = strpos( $option, static::OPTION_PREFIX ) === 0;
994
995 if ( ! $is_experiment_option ) {
996 continue;
997 }
998
999 $sort(
1000 str_replace( static::OPTION_PREFIX, '', $option )
1001 );
1002 }
1003
1004 $allowed_options['elementor'] = Collection::make( $allowed_options['elementor'] )
1005 ->filter( function ( $option ) {
1006 return 0 !== strpos( $option, static::OPTION_PREFIX );
1007 } )
1008 ->merge(
1009 $sorted->map( function ( $item ) {
1010 return static::OPTION_PREFIX . $item;
1011 } )
1012 )
1013 ->values();
1014
1015 return $allowed_options;
1016 }
1017
1018 public function __construct() {
1019 $this->init_states();
1020
1021 $this->init_release_statuses();
1022
1023 $this->init_features();
1024
1025 add_action( 'admin_init', function () {
1026 System_Info::add_report(
1027 'experiments', [
1028 'file_name' => __DIR__ . '/experiments-reporter.php',
1029 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
1030 ]
1031 );
1032 }, 79 /* Before log */ );
1033
1034 if ( is_admin() ) {
1035 $page_id = Settings::PAGE_ID;
1036
1037 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
1038 $this->register_settings_fields( $settings );
1039 }, 11 );
1040
1041 add_filter( 'allowed_options', function ( $allowed_options ) {
1042 return $this->sort_allowed_options_by_dependencies( $allowed_options );
1043 }, 11 );
1044 }
1045
1046 // Register CLI commands.
1047 if ( Utils::is_wp_cli() ) {
1048 \WP_CLI::add_command( 'elementor experiments', WP_CLI::class );
1049 }
1050 }
1051 }
1052