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

518 lines 14.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' => __( 'Optimized DOM Output', 'elementor' ),
220 'description' => __( '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 . __( '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' => __( 'Improved Asset Loading', 'elementor' ),
233 'description' => __( '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 . __( 'Learn More', 'elementor' ) . '</a>',
236 'release_status' => self::RELEASE_STATUS_ALPHA,
237 ] );
238
239 $this->add_feature( [
240 'name' => 'a11y_improvements',
241 'title' => __( 'Accessibility Improvements', 'elementor' ),
242 'description' => __( 'An array of accessibility enhancements in Elementor pages.', 'elementor' )
243 . '<br><strong>' . __( 'Please note!', 'elementor' ) . '</strong> ' . __( 'These enhancements may include some markup changes to existing elementor widgets', 'elementor' )
244 . ' <a href="https://go.elementor.com/wp-dash-a11y-improvements" target="_blank">'
245 . __( 'Learn More', 'elementor' ) . '</a>',
246 'release_status' => self::RELEASE_STATUS_BETA,
247 'new_site' => [
248 'default_active' => true,
249 'minimum_installation_version' => '3.1.0-beta',
250 ],
251 ] );
252
253 $this->add_feature( [
254 'name' => 'e_import_export',
255 'title' => __( 'Import Export Template Kit', 'elementor' ),
256 'description' => __( 'Design sites faster with a template kit that contains some or all components of a complete site, like templates, content & site settings.', 'elementor' )
257 . '<br>'
258 . __( 'You can import a kit and apply it to your site, or export the elements from this site to be used anywhere else.', 'elementor' ),
259 'release_status' => self::RELEASE_STATUS_ALPHA,
260 ] );
261 }
262
263 /**
264 * Init States
265 *
266 * @since 3.1.0
267 * @access private
268 */
269 private function init_states() {
270 $this->states = [
271 self::STATE_DEFAULT => __( 'Default', 'elementor' ),
272 self::STATE_ACTIVE => __( 'Active', 'elementor' ),
273 self::STATE_INACTIVE => __( 'Inactive', 'elementor' ),
274 ];
275 }
276
277 /**
278 * Init Statuses
279 *
280 * @since 3.1.0
281 * @access private
282 */
283 private function init_release_statuses() {
284 $this->release_statuses = [
285 self::RELEASE_STATUS_DEV => __( 'Development', 'elementor' ),
286 self::RELEASE_STATUS_ALPHA => __( 'Alpha', 'elementor' ),
287 self::RELEASE_STATUS_BETA => __( 'Beta', 'elementor' ),
288 self::RELEASE_STATUS_RC => __( 'Release Candidate', 'elementor' ),
289 self::RELEASE_STATUS_STABLE => __( 'Stable', 'elementor' ),
290 ];
291 }
292
293 /**
294 * Init Features
295 *
296 * @since 3.1.0
297 * @access private
298 */
299 private function init_features() {
300 $this->features = [];
301
302 $this->add_default_features();
303
304 do_action( 'elementor/experiments/default-features-registered', $this );
305 }
306
307 /**
308 * Register Settings Fields
309 *
310 * @param Settings $settings
311 *
312 * @since 3.1.0
313 * @access private
314 *
315 */
316 private function register_settings_fields( Settings $settings ) {
317 $features = $this->get_features();
318
319 $fields = [];
320
321 foreach ( $features as $feature_name => $feature ) {
322 if ( ! $feature['mutable'] ) {
323 unset( $features[ $feature_name ] );
324
325 continue;
326 }
327
328 $feature_key = 'experiment-' . $feature_name;
329
330 $fields[ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
331
332 $fields[ $feature_key ]['field_args'] = $feature;
333
334 $fields[ $feature_key ]['render'] = function( $feature ) {
335 $this->render_feature_settings_field( $feature );
336 };
337 }
338
339 if ( ! $features ) {
340 $fields['no_features'] = [
341 'label' => __( 'No available experiments', 'elementor' ),
342 'field_args' => [
343 'type' => 'raw_html',
344 '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' ),
345 ],
346 ];
347 }
348
349 if ( ! Tracker::is_allow_track() ) {
350 $fields += $settings->get_usage_fields();
351 }
352
353 $settings->add_tab(
354 'experiments', [
355 'label' => __( 'Experiments', 'elementor' ),
356 'sections' => [
357 'experiments' => [
358 'callback' => function() {
359 $this->render_settings_intro();
360 },
361 'fields' => $fields,
362 ],
363 ],
364 ]
365 );
366 }
367
368 /**
369 * Render Settings Intro
370 *
371 * @since 3.1.0
372 * @access private
373 */
374 private function render_settings_intro() {
375 ?>
376 <h2><?php echo __( 'Experiments', 'elementor' ); ?></h2>
377 <p><?php echo sprintf( __( '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>
378 <p><?php echo __( '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>
379 <p><?php echo sprintf( __( '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>
380 <?php
381 }
382
383 /**
384 * Render Feature Settings Field
385 *
386 * @since 3.1.0
387 * @access private
388 *
389 * @param array $feature
390 */
391 private function render_feature_settings_field( array $feature ) {
392 ?>
393 <div class="e-experiment__content">
394 <select id="e-experiment-<?php echo $feature['name']; ?>" class="e-experiment__select" name="<?php echo $this->get_feature_option_key( $feature['name'] ); ?>">
395 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
396 <option value="<?php echo $state_key; ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
397 <?php } ?>
398 </select>
399 <p class="description"><?php echo $feature['description']; ?></p>
400 <div class="e-experiment__status"><?php echo sprintf( __( 'Status: %s', 'elementor' ), $this->release_statuses[ $feature['release_status'] ] ); ?></div>
401 </div>
402 <?php
403 }
404
405 /**
406 * Get Feature Settings Label HTML
407 *
408 * @since 3.1.0
409 * @access private
410 *
411 * @param array $feature
412 *
413 * @return string
414 */
415 private function get_feature_settings_label_html( array $feature ) {
416 ob_start();
417
418 $is_feature_active = $this->is_feature_active( $feature['name'] );
419
420 $indicator_classes = 'e-experiment__title__indicator';
421
422 if ( $is_feature_active ) {
423 $indicator_classes .= ' e-experiment__title__indicator--active';
424 }
425
426 if ( self::STATE_DEFAULT === $feature['state'] ) {
427 $indicator_tooltip = $is_feature_active ? __( 'Active by default', 'elementor' ) : __( 'Inactive by default', 'elementor' );
428 } else {
429 $indicator_tooltip = self::STATE_ACTIVE === $feature['state'] ? __( 'Active', 'elementor' ) : __( 'Inactive', 'elementor' );
430 }
431 ?>
432 <div class="e-experiment__title">
433 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; ?>"></div>
434 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; ?>"><?php echo $feature['title']; ?></label>
435 </div>
436 <?php
437
438 return ob_get_clean();
439 }
440
441 /**
442 * Get Feature Settings Label HTML
443 *
444 * @since 3.1.0
445 * @access private
446 *
447 * @param string $feature_name
448 *
449 * @return int
450 */
451 private function get_saved_feature_state( $feature_name ) {
452 return get_option( $this->get_feature_option_key( $feature_name ) );
453 }
454
455 /**
456 * Get Feature Actual State
457 *
458 * @since 3.1.0
459 * @access private
460 *
461 * @param array $feature
462 *
463 * @return string
464 */
465 private function get_feature_actual_state( array $feature ) {
466 if ( self::STATE_DEFAULT !== $feature['state'] ) {
467 return $feature['state'];
468 }
469
470 return $feature['default'];
471 }
472
473 /**
474 * On Feature State Change
475 *
476 * @since 3.1.0
477 * @access private
478 *
479 * @param array $old_feature_data
480 * @param string $new_state
481 */
482 private function on_feature_state_change( array $old_feature_data, $new_state ) {
483 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
484
485 $new_feature_data = $this->get_features( $old_feature_data['name'] );
486
487 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
488
489 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
490
491 if ( $actual_old_state === $actual_new_state ) {
492 return;
493 }
494
495 Plugin::$instance->files_manager->clear_cache();
496
497 if ( $new_feature_data['on_state_change'] ) {
498 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
499 }
500 }
501
502 public function __construct() {
503 $this->init_states();
504
505 $this->init_release_statuses();
506
507 $this->init_features();
508
509 if ( is_admin() ) {
510 $page_id = Settings::PAGE_ID;
511
512 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
513 $this->register_settings_fields( $settings );
514 }, 11 );
515 }
516 }
517 }
518