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

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