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

655 lines 20.0 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\Experiments\Manager as Experiments_Manager;
6 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
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\Modules\SafeMode\Module as Safe_Mode;
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 private $states;
36
37 private $release_statuses;
38
39 private $features;
40
41 /**
42 * Add Feature
43 *
44 * @since 3.1.0
45 * @access public
46 *
47 * @param array $options {
48 * @type string $name
49 * @type string $title
50 * @type string $description
51 * @type string $release_status
52 * @type string $default
53 * @type callable $on_state_change
54 * }
55 *
56 * @return array|null
57 */
58 public function add_feature( array $options ) {
59 if ( isset( $this->features[ $options['name'] ] ) ) {
60 return null;
61 }
62
63 $default_experimental_data = [
64 'description' => '',
65 'release_status' => self::RELEASE_STATUS_ALPHA,
66 'default' => self::STATE_INACTIVE,
67 'new_site' => [
68 'default_active' => false,
69 'always_active' => false,
70 'minimum_installation_version' => null,
71 ],
72 'on_state_change' => null,
73 ];
74
75 $allowed_options = [ 'name', 'title', 'description', 'release_status', 'default', 'new_site', 'on_state_change' ];
76
77 $experimental_data = $this->merge_properties( $default_experimental_data, $options, $allowed_options );
78
79 $new_site = $experimental_data['new_site'];
80
81 $feature_is_mutable = true;
82
83 if ( $new_site['default_active'] || $new_site['always_active'] ) {
84 $is_new_installation = Upgrade_Manager::install_compare( $new_site['minimum_installation_version'], '>=' );
85
86 if ( $is_new_installation ) {
87 if ( $new_site['always_active'] ) {
88 $experimental_data['state'] = self::STATE_ACTIVE;
89
90 $feature_is_mutable = false;
91 } elseif ( $new_site['default_active'] ) {
92 $experimental_data['default'] = self::STATE_ACTIVE;
93 }
94 }
95 }
96
97 $experimental_data['mutable'] = $feature_is_mutable;
98
99 if ( $feature_is_mutable ) {
100 $state = $this->get_saved_feature_state( $options['name'] );
101
102 if ( ! $state ) {
103 $state = self::STATE_DEFAULT;
104 }
105
106 $experimental_data['state'] = $state;
107 }
108
109 $this->features[ $options['name'] ] = $experimental_data;
110
111 if ( $feature_is_mutable && is_admin() ) {
112 $feature_option_key = $this->get_feature_option_key( $options['name'] );
113
114 $on_state_change_callback = function( $old_state, $new_state ) use ( $experimental_data ) {
115 $this->on_feature_state_change( $experimental_data, $new_state );
116 };
117
118 add_action( 'add_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
119 add_action( 'update_option_' . $feature_option_key, $on_state_change_callback, 10, 2 );
120 }
121
122 do_action( 'elementor/experiments/feature-registered', $this, $experimental_data );
123
124 return $experimental_data;
125 }
126
127 /**
128 * Remove Feature
129 *
130 * @since 3.1.0
131 * @access public
132 *
133 * @param string $feature_name
134 */
135 public function remove_feature( $feature_name ) {
136 unset( $this->features[ $feature_name ] );
137 }
138
139 /**
140 * Get Features
141 *
142 * @since 3.1.0
143 * @access public
144 *
145 * @param string $feature_name Optional. Default is null
146 *
147 * @return array|null
148 */
149 public function get_features( $feature_name = null ) {
150 return self::get_items( $this->features, $feature_name );
151 }
152
153 /**
154 * Get Active Features
155 *
156 * @since 3.1.0
157 * @access public
158 *
159 * @return array
160 */
161 public function get_active_features() {
162 return array_filter( $this->features, [ $this, 'is_feature_active' ], ARRAY_FILTER_USE_KEY );
163 }
164
165 /**
166 * Is Feature Active
167 *
168 * @since 3.1.0
169 * @access public
170 *
171 * @param string $feature_name
172 *
173 * @return bool
174 */
175 public function is_feature_active( $feature_name ) {
176 $feature = $this->get_features( $feature_name );
177
178 if ( ! $feature ) {
179 return false;
180 }
181
182 return self::STATE_ACTIVE === $this->get_feature_actual_state( $feature );
183 }
184
185 /**
186 * Set Feature Default State
187 *
188 * @since 3.1.0
189 * @access public
190 *
191 * @param string $feature_name
192 * @param int $default_state
193 */
194 public function set_feature_default_state( $feature_name, $default_state ) {
195 $feature = $this->get_features( $feature_name );
196
197 if ( ! $feature ) {
198 return;
199 }
200
201 $this->features[ $feature_name ]['default'] = $default_state;
202 }
203
204 /**
205 * Get Feature Option Key
206 *
207 * @since 3.1.0
208 * @access private
209 *
210 * @param string $feature_name
211 *
212 * @return string
213 */
214 private function get_feature_option_key( $feature_name ) {
215 return 'elementor_experiment-' . $feature_name;
216 }
217
218 private function add_default_features() {
219 $this->add_feature( [
220 'name' => 'e_dom_optimization',
221 'title' => esc_html__( 'Optimized DOM Output', 'elementor' ),
222 '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' )
223 . ' <a href="https://go.elementor.com/wp-dash-legacy-optimized-dom" target="_blank">'
224 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
225 'release_status' => self::RELEASE_STATUS_STABLE,
226 'new_site' => [
227 'default_active' => true,
228 'minimum_installation_version' => '3.1.0-beta',
229 ],
230 ] );
231
232 $this->add_feature( [
233 'name' => 'e_optimized_assets_loading',
234 'title' => esc_html__( 'Improved Asset Loading', 'elementor' ),
235 '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' )
236 . ' <a href="https://go.elementor.com/wp-dash-improved-asset-loading/" target="_blank">'
237 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
238 'release_status' => self::RELEASE_STATUS_BETA,
239 'new_site' => [
240 'default_active' => true,
241 'minimum_installation_version' => '3.2.0-beta',
242 ],
243 ] );
244
245 $this->add_feature( [
246 'name' => 'e_optimized_css_loading',
247 'title' => esc_html__( 'Improved CSS Loading', 'elementor' ),
248 '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' )
249 . ' <a href="https://go.elementor.com/wp-dash-improved-css-loading/" target="_blank">'
250 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
251 'release_status' => self::RELEASE_STATUS_BETA,
252 'new_site' => [
253 'default_active' => true,
254 'minimum_installation_version' => '3.3.0-beta',
255 ],
256 ] );
257
258 $this->add_feature( [
259 'name' => 'e_font_icon_svg',
260 'title' => esc_html__( 'Inline Font Icons', 'elementor' ),
261 'description' => esc_html__( 'The “Inline Font Icons” will render the icons as inline SVG without loading the Font-Awsome and the eicons libraries and its related CSS files and fonts. Learn More.', 'elementor' )
262 . ' <a href="https://go.elementor.com/wp-dash-inline-font-awesome/" target="_blank">'
263 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
264 'release_status' => self::RELEASE_STATUS_ALPHA,
265 ] );
266
267 $this->add_feature( [
268 'name' => 'a11y_improvements',
269 'title' => esc_html__( 'Accessibility Improvements', 'elementor' ),
270 'description' => esc_html__( 'An array of accessibility enhancements in Elementor pages.', 'elementor' )
271 . '<br><strong>' . esc_html__( 'Please note!', 'elementor' ) . '</strong> ' . esc_html__( 'These enhancements may include some markup changes to existing elementor widgets', 'elementor' )
272 . ' <a href="https://go.elementor.com/wp-dash-a11y-improvements" target="_blank">'
273 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
274 'release_status' => self::RELEASE_STATUS_STABLE,
275 'new_site' => [
276 'default_active' => true,
277 'minimum_installation_version' => '3.1.0-beta',
278 ],
279 ] );
280
281 $this->add_feature( [
282 'name' => 'e_import_export',
283 'title' => esc_html__( 'Import Export Template Kit', 'elementor' ),
284 'description' => esc_html__( 'Design sites faster with a template kit that contains some or all components of a complete site, like templates, content & site settings.', 'elementor' )
285 . '<br>'
286 . esc_html__( 'You can import a kit and apply it to your site, or export the elements from this site to be used anywhere else.', 'elementor' ),
287 'release_status' => self::RELEASE_STATUS_BETA,
288 'default' => self::STATE_ACTIVE,
289 'new_site' => [
290 'default_active' => true,
291 'minimum_installation_version' => '3.2.0-beta',
292 ],
293 ] );
294
295 $this->add_feature( [
296 'name' => 'additional_custom_breakpoints',
297 'title' => esc_html__( 'Additional Custom Breakpoints', 'elementor' ),
298 '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' )
299 . '<br /><strong>' . esc_html__( 'Please note! Conditioning controls on values of responsive controls is not supported when this mode is active.', 'elementor' ) . '</strong>'
300 . ' <a href="https://go.elementor.com/wp-dash-additional-custom-breakpoints/" target="_blank">'
301 . esc_html__( 'Learn More', 'elementor' ) . '</a>',
302 'release_status' => self::RELEASE_STATUS_BETA,
303 'new_site' => [
304 'default_active' => true,
305 'minimum_installation_version' => '3.4.0-beta',
306 ],
307 ] );
308
309 $this->add_feature( [
310 'name' => 'e_hidden_wordpress_widgets',
311 'title' => esc_html__( 'Hide native WordPress widgets from search results', 'elementor' ),
312 'description' => esc_html__( 'WordPress widgets will not be shown when searching in the editor panel. Instead, these widgets can be found in the “WordPress” dropdown at the bottom of the panel.', 'elementor' ),
313 'release_status' => self::RELEASE_STATUS_BETA,
314 'default' => self::STATE_ACTIVE,
315 ] );
316 }
317
318 /**
319 * Init States
320 *
321 * @since 3.1.0
322 * @access private
323 */
324 private function init_states() {
325 $this->states = [
326 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
327 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
328 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
329 ];
330 }
331
332 /**
333 * Init Statuses
334 *
335 * @since 3.1.0
336 * @access private
337 */
338 private function init_release_statuses() {
339 $this->release_statuses = [
340 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
341 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
342 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
343 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
344 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
345 ];
346 }
347
348 /**
349 * Init Features
350 *
351 * @since 3.1.0
352 * @access private
353 */
354 private function init_features() {
355 $this->features = [];
356
357 $this->add_default_features();
358
359 do_action( 'elementor/experiments/default-features-registered', $this );
360 }
361
362 /**
363 * Register Settings Fields
364 *
365 * @param Settings $settings
366 *
367 * @since 3.1.0
368 * @access private
369 *
370 */
371 private function register_settings_fields( Settings $settings ) {
372 $features = $this->get_features();
373
374 $fields = [];
375
376 foreach ( $features as $feature_name => $feature ) {
377 if ( ! $feature['mutable'] ) {
378 unset( $features[ $feature_name ] );
379
380 continue;
381 }
382
383 $feature_key = 'experiment-' . $feature_name;
384
385 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
386
387 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
388
389 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
390
391 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
392 $this->render_feature_settings_field( $feature );
393 };
394 }
395
396 foreach ( [ 'stable', 'ongoing' ] as $section ) {
397 if ( ! isset( $fields[ $section ] ) ) {
398 $fields[ $section ]['no_features'] = [
399 'label' => esc_html__( 'No available experiments', 'elementor' ),
400 'field_args' => [
401 'type' => 'raw_html',
402 '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' ),
403 ],
404 ];
405 }
406
407 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
408 $fields[ $section ] += $settings->get_usage_fields();
409 }
410 }
411
412 $settings->add_tab(
413 'experiments', [
414 'label' => esc_html__( 'Experiments', 'elementor' ),
415 'sections' => [
416 'ongoing_experiments' => [
417 'callback' => function() {
418 $this->render_settings_intro();
419 },
420 'fields' => $fields['ongoing'],
421 ],
422 'stable_experiments' => [
423 'callback' => function() {
424 $this->render_stable_section_title();
425 },
426 'fields' => $fields['stable'],
427 ],
428 ],
429 ]
430 );
431 }
432
433 private function render_stable_section_title() {
434 ?>
435 <hr>
436 <h2>
437 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
438 </h2>
439 <?php
440 }
441
442 /**
443 * Render Settings Intro
444 *
445 * @since 3.1.0
446 * @access private
447 */
448 private function render_settings_intro() {
449 ?>
450 <h2>
451 <?php echo esc_html__( 'Elementor Experiments', 'elementor' ); ?>
452 </h2>
453 <p class="e-experiment__description">
454 <?php
455 printf(
456 /* translators: %1$s Link open tag, %2$s: Link close tag. */
457 esc_html__( 'Access new and experimental features from Elementor before they\'re officially released. As these features are still in development, they are likely to change, evolve or even be removed altogether. %1$sLearn More.%2$s', 'elementor' ),
458 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
459 '</a>'
460 );
461 ?>
462 </p>
463 <p class="e-experiment__description">
464 <?php echo esc_html__( 'To use an experiment on your site, simply click on the dropdown next to it and switch to Active. You can always deactivate them at any time.', 'elementor' ); ?>
465 </p>
466 <p class="e-experiment__description">
467 <?php
468 printf(
469 /* translators: %1$s Link open tag, %2$s: Link close tag. */
470 esc_html__( 'Your feedback is important - %1$shelp us%2$s improve these features by sharing your thoughts and inputs.', 'elementor' ),
471 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
472 '</a>'
473 );
474 ?>
475 </p>
476 <?php if ( $this->get_features() ) { ?>
477 <button type="button" class="button e-experiment__button" value="active">Activate All Experiments</button>
478 <button type="button" class="button e-experiment__button" value="inactive">Deactivate All Experiments</button>
479 <?php } ?>
480 <hr>
481 <h2 class="e-experiment__table-title">
482 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
483 </h2>
484 <?php
485 }
486
487 /**
488 * Render Feature Settings Field
489 *
490 * @since 3.1.0
491 * @access private
492 *
493 * @param array $feature
494 */
495 private function render_feature_settings_field( array $feature ) {
496 ?>
497 <div class="e-experiment__content">
498 <select id="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" class="e-experiment__select" name="<?php echo $this->get_feature_option_key( $feature['name'] ); ?>">
499 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
500 <option value="<?php echo $state_key; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
501 <?php } ?>
502 </select>
503 <p class="description"><?php echo $feature['description']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
504 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
505 <div class="e-experiment__status"><?php echo sprintf( esc_html__( 'Status: %s', 'elementor' ), $this->release_statuses[ $feature['release_status'] ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
506 <?php } ?>
507 </div>
508 <?php
509 }
510
511 /**
512 * Get Feature Settings Label HTML
513 *
514 * @since 3.1.0
515 * @access private
516 *
517 * @param array $feature
518 *
519 * @return string
520 */
521 private function get_feature_settings_label_html( array $feature ) {
522 ob_start();
523
524 $is_feature_active = $this->is_feature_active( $feature['name'] );
525
526 $indicator_classes = 'e-experiment__title__indicator';
527
528 if ( $is_feature_active ) {
529 $indicator_classes .= ' e-experiment__title__indicator--active';
530 }
531
532 $indicator_tooltip = $this->get_feature_state_label( $feature );
533
534 ?>
535 <div class="e-experiment__title">
536 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
537 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
538 </div>
539 <?php
540
541 return ob_get_clean();
542 }
543
544 /**
545 * Get Feature State Label
546 *
547 * @param array $feature
548 *
549 * @return string
550 */
551 public function get_feature_state_label( array $feature ) {
552 $is_feature_active = $this->is_feature_active( $feature['name'] );
553
554 if ( self::STATE_DEFAULT === $feature['state'] ) {
555 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
556 esc_html__( 'Inactive by default', 'elementor' );
557 } else {
558 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
559 esc_html__( 'Inactive', 'elementor' );
560 }
561
562 return $label;
563 }
564
565 /**
566 * Get Feature Settings Label HTML
567 *
568 * @since 3.1.0
569 * @access private
570 *
571 * @param string $feature_name
572 *
573 * @return int
574 */
575 private function get_saved_feature_state( $feature_name ) {
576 return get_option( $this->get_feature_option_key( $feature_name ) );
577 }
578
579 /**
580 * Get Feature Actual State
581 *
582 * @since 3.1.0
583 * @access private
584 *
585 * @param array $feature
586 *
587 * @return string
588 */
589 private function get_feature_actual_state( array $feature ) {
590 if ( get_option( Safe_Mode::OPTION_ENABLED, '' ) ) {
591 return self::STATE_INACTIVE;
592 }
593
594 if ( self::STATE_DEFAULT !== $feature['state'] ) {
595 return $feature['state'];
596 }
597
598 return $feature['default'];
599 }
600
601 /**
602 * On Feature State Change
603 *
604 * @since 3.1.0
605 * @access private
606 *
607 * @param array $old_feature_data
608 * @param string $new_state
609 */
610 private function on_feature_state_change( array $old_feature_data, $new_state ) {
611 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
612
613 $new_feature_data = $this->get_features( $old_feature_data['name'] );
614
615 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
616
617 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
618
619 if ( $actual_old_state === $actual_new_state ) {
620 return;
621 }
622
623 Plugin::$instance->files_manager->clear_cache();
624
625 if ( $new_feature_data['on_state_change'] ) {
626 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
627 }
628 }
629
630 public function __construct() {
631 $this->init_states();
632
633 $this->init_release_statuses();
634
635 $this->init_features();
636
637 add_action( 'admin_init', function () {
638 System_Info::add_report(
639 'experiments', [
640 'file_name' => __DIR__ . '/experiments-reporter.php',
641 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
642 ]
643 );
644 }, 79 /* Before log */ );
645
646 if ( is_admin() ) {
647 $page_id = Settings::PAGE_ID;
648
649 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
650 $this->register_settings_fields( $settings );
651 }, 11 );
652 }
653 }
654 }
655