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

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