PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.0-beta1
Elementor Website Builder – more than just a page builder v3.5.0-beta1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / experiments / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.5.0-beta1, at core/experiments/manager.php

658 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_ALPHA,
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_ALPHA,
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__( 'Font-Awesome Inline', 'elementor' ),
261 'description' => esc_html__( 'The "Font-Awesome Inline" will render the Font-Awesome icons as inline SVG without loading the Font-Awesome library and its related CSS files and fonts.', '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” section at the bottom of the panel.', 'elementor' ),
313 'release_status' => self::RELEASE_STATUS_STABLE,
314 'new_site' => [
315 'default_active' => true,
316 'minimum_installation_version' => '3.5.0',
317 ],
318 ] );
319 }
320
321 /**
322 * Init States
323 *
324 * @since 3.1.0
325 * @access private
326 */
327 private function init_states() {
328 $this->states = [
329 self::STATE_DEFAULT => esc_html__( 'Default', 'elementor' ),
330 self::STATE_ACTIVE => esc_html__( 'Active', 'elementor' ),
331 self::STATE_INACTIVE => esc_html__( 'Inactive', 'elementor' ),
332 ];
333 }
334
335 /**
336 * Init Statuses
337 *
338 * @since 3.1.0
339 * @access private
340 */
341 private function init_release_statuses() {
342 $this->release_statuses = [
343 self::RELEASE_STATUS_DEV => esc_html__( 'Development', 'elementor' ),
344 self::RELEASE_STATUS_ALPHA => esc_html__( 'Alpha', 'elementor' ),
345 self::RELEASE_STATUS_BETA => esc_html__( 'Beta', 'elementor' ),
346 self::RELEASE_STATUS_RC => esc_html__( 'Release Candidate', 'elementor' ),
347 self::RELEASE_STATUS_STABLE => esc_html__( 'Stable', 'elementor' ),
348 ];
349 }
350
351 /**
352 * Init Features
353 *
354 * @since 3.1.0
355 * @access private
356 */
357 private function init_features() {
358 $this->features = [];
359
360 $this->add_default_features();
361
362 do_action( 'elementor/experiments/default-features-registered', $this );
363 }
364
365 /**
366 * Register Settings Fields
367 *
368 * @param Settings $settings
369 *
370 * @since 3.1.0
371 * @access private
372 *
373 */
374 private function register_settings_fields( Settings $settings ) {
375 $features = $this->get_features();
376
377 $fields = [];
378
379 foreach ( $features as $feature_name => $feature ) {
380 if ( ! $feature['mutable'] ) {
381 unset( $features[ $feature_name ] );
382
383 continue;
384 }
385
386 $feature_key = 'experiment-' . $feature_name;
387
388 $section = 'stable' === $feature['release_status'] ? 'stable' : 'ongoing';
389
390 $fields[ $section ][ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
391
392 $fields[ $section ][ $feature_key ]['field_args'] = $feature;
393
394 $fields[ $section ][ $feature_key ]['render'] = function( $feature ) {
395 $this->render_feature_settings_field( $feature );
396 };
397 }
398
399 foreach ( [ 'stable', 'ongoing' ] as $section ) {
400 if ( ! isset( $fields[ $section ] ) ) {
401 $fields[ $section ]['no_features'] = [
402 'label' => esc_html__( 'No available experiments', 'elementor' ),
403 'field_args' => [
404 'type' => 'raw_html',
405 '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' ),
406 ],
407 ];
408 }
409
410 if ( ! Tracker::is_allow_track() && 'stable' === $section ) {
411 $fields[ $section ] += $settings->get_usage_fields();
412 }
413 }
414
415 $settings->add_tab(
416 'experiments', [
417 'label' => esc_html__( 'Experiments', 'elementor' ),
418 'sections' => [
419 'ongoing_experiments' => [
420 'callback' => function() {
421 $this->render_settings_intro();
422 },
423 'fields' => $fields['ongoing'],
424 ],
425 'stable_experiments' => [
426 'callback' => function() {
427 $this->render_stable_section_title();
428 },
429 'fields' => $fields['stable'],
430 ],
431 ],
432 ]
433 );
434 }
435
436 private function render_stable_section_title() {
437 ?>
438 <hr>
439 <h2>
440 <?php echo esc_html__( 'Stable Features', 'elementor' ); ?>
441 </h2>
442 <?php
443 }
444
445 /**
446 * Render Settings Intro
447 *
448 * @since 3.1.0
449 * @access private
450 */
451 private function render_settings_intro() {
452 ?>
453 <h2>
454 <?php echo esc_html__( 'Elementor Experiments', 'elementor' ); ?>
455 </h2>
456 <p class="e-experiment__description">
457 <?php
458 printf(
459 /* translators: %1$s Link open tag, %2$s: Link close tag. */
460 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' ),
461 '<a href="https://go.elementor.com/wp-dash-experiments/" target="_blank">',
462 '</a>'
463 );
464 ?>
465 </p>
466 <p class="e-experiment__description">
467 <?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' ); ?>
468 </p>
469 <p class="e-experiment__description">
470 <?php
471 printf(
472 /* translators: %1$s Link open tag, %2$s: Link close tag. */
473 esc_html__( 'Your feedback is important - %1$shelp us%2$s improve these features by sharing your thoughts and inputs.', 'elementor' ),
474 '<a href="https://go.elementor.com/wp-dash-experiments-report-an-issue/" target="_blank">',
475 '</a>'
476 );
477 ?>
478 </p>
479 <?php if ( $this->get_features() ) { ?>
480 <button type="button" class="button e-experiment__button" value="active">Activate All Experiments</button>
481 <button type="button" class="button e-experiment__button" value="inactive">Deactivate All Experiments</button>
482 <?php } ?>
483 <hr>
484 <h2 class="e-experiment__table-title">
485 <?php echo esc_html__( 'Ongoing Experiments', 'elementor' ); ?>
486 </h2>
487 <?php
488 }
489
490 /**
491 * Render Feature Settings Field
492 *
493 * @since 3.1.0
494 * @access private
495 *
496 * @param array $feature
497 */
498 private function render_feature_settings_field( array $feature ) {
499 ?>
500 <div class="e-experiment__content">
501 <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'] ); ?>">
502 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
503 <option value="<?php echo $state_key; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
504 <?php } ?>
505 </select>
506 <p class="description"><?php echo $feature['description']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
507 <?php if ( 'stable' !== $feature['release_status'] ) { ?>
508 <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>
509 <?php } ?>
510 </div>
511 <?php
512 }
513
514 /**
515 * Get Feature Settings Label HTML
516 *
517 * @since 3.1.0
518 * @access private
519 *
520 * @param array $feature
521 *
522 * @return string
523 */
524 private function get_feature_settings_label_html( array $feature ) {
525 ob_start();
526
527 $is_feature_active = $this->is_feature_active( $feature['name'] );
528
529 $indicator_classes = 'e-experiment__title__indicator';
530
531 if ( $is_feature_active ) {
532 $indicator_classes .= ' e-experiment__title__indicator--active';
533 }
534
535 $indicator_tooltip = $this->get_feature_state_label( $feature );
536
537 ?>
538 <div class="e-experiment__title">
539 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"></div>
540 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo $feature['title']; ?></label>
541 </div>
542 <?php
543
544 return ob_get_clean();
545 }
546
547 /**
548 * Get Feature State Label
549 *
550 * @param array $feature
551 *
552 * @return string
553 */
554 public function get_feature_state_label( array $feature ) {
555 $is_feature_active = $this->is_feature_active( $feature['name'] );
556
557 if ( self::STATE_DEFAULT === $feature['state'] ) {
558 $label = $is_feature_active ? esc_html__( 'Active by default', 'elementor' ) :
559 esc_html__( 'Inactive by default', 'elementor' );
560 } else {
561 $label = self::STATE_ACTIVE === $feature['state'] ? esc_html__( 'Active', 'elementor' ) :
562 esc_html__( 'Inactive', 'elementor' );
563 }
564
565 return $label;
566 }
567
568 /**
569 * Get Feature Settings Label HTML
570 *
571 * @since 3.1.0
572 * @access private
573 *
574 * @param string $feature_name
575 *
576 * @return int
577 */
578 private function get_saved_feature_state( $feature_name ) {
579 return get_option( $this->get_feature_option_key( $feature_name ) );
580 }
581
582 /**
583 * Get Feature Actual State
584 *
585 * @since 3.1.0
586 * @access private
587 *
588 * @param array $feature
589 *
590 * @return string
591 */
592 private function get_feature_actual_state( array $feature ) {
593 if ( get_option( Safe_Mode::OPTION_ENABLED, '' ) ) {
594 return self::STATE_INACTIVE;
595 }
596
597 if ( self::STATE_DEFAULT !== $feature['state'] ) {
598 return $feature['state'];
599 }
600
601 return $feature['default'];
602 }
603
604 /**
605 * On Feature State Change
606 *
607 * @since 3.1.0
608 * @access private
609 *
610 * @param array $old_feature_data
611 * @param string $new_state
612 */
613 private function on_feature_state_change( array $old_feature_data, $new_state ) {
614 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
615
616 $new_feature_data = $this->get_features( $old_feature_data['name'] );
617
618 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
619
620 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
621
622 if ( $actual_old_state === $actual_new_state ) {
623 return;
624 }
625
626 Plugin::$instance->files_manager->clear_cache();
627
628 if ( $new_feature_data['on_state_change'] ) {
629 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
630 }
631 }
632
633 public function __construct() {
634 $this->init_states();
635
636 $this->init_release_statuses();
637
638 $this->init_features();
639
640 add_action( 'admin_init', function () {
641 System_Info::add_report(
642 'experiments', [
643 'file_name' => __DIR__ . '/experiments-reporter.php',
644 'class_name' => __NAMESPACE__ . '\Experiments_Reporter',
645 ]
646 );
647 }, 79 /* Before log */ );
648
649 if ( is_admin() ) {
650 $page_id = Settings::PAGE_ID;
651
652 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
653 $this->register_settings_fields( $settings );
654 }, 11 );
655 }
656 }
657 }
658