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

509 lines 13.8 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
254 /**
255 * Init States
256 *
257 * @since 3.1.0
258 * @access private
259 */
260 private function init_states() {
261 $this->states = [
262 self::STATE_DEFAULT => __( 'Default', 'elementor' ),
263 self::STATE_ACTIVE => __( 'Active', 'elementor' ),
264 self::STATE_INACTIVE => __( 'Inactive', 'elementor' ),
265 ];
266 }
267
268 /**
269 * Init Statuses
270 *
271 * @since 3.1.0
272 * @access private
273 */
274 private function init_release_statuses() {
275 $this->release_statuses = [
276 self::RELEASE_STATUS_DEV => __( 'Development', 'elementor' ),
277 self::RELEASE_STATUS_ALPHA => __( 'Alpha', 'elementor' ),
278 self::RELEASE_STATUS_BETA => __( 'Beta', 'elementor' ),
279 self::RELEASE_STATUS_RC => __( 'Release Candidate', 'elementor' ),
280 self::RELEASE_STATUS_STABLE => __( 'Stable', 'elementor' ),
281 ];
282 }
283
284 /**
285 * Init Features
286 *
287 * @since 3.1.0
288 * @access private
289 */
290 private function init_features() {
291 $this->features = [];
292
293 $this->add_default_features();
294
295 do_action( 'elementor/experiments/default-features-registered', $this );
296 }
297
298 /**
299 * Register Settings Fields
300 *
301 * @param Settings $settings
302 *
303 * @since 3.1.0
304 * @access private
305 *
306 */
307 private function register_settings_fields( Settings $settings ) {
308 $features = $this->get_features();
309
310 $fields = [];
311
312 foreach ( $features as $feature_name => $feature ) {
313 if ( ! $feature['mutable'] ) {
314 unset( $features[ $feature_name ] );
315
316 continue;
317 }
318
319 $feature_key = 'experiment-' . $feature_name;
320
321 $fields[ $feature_key ]['label'] = $this->get_feature_settings_label_html( $feature );
322
323 $fields[ $feature_key ]['field_args'] = $feature;
324
325 $fields[ $feature_key ]['render'] = function( $feature ) {
326 $this->render_feature_settings_field( $feature );
327 };
328 }
329
330 if ( ! $features ) {
331 $fields['no_features'] = [
332 'label' => __( 'No available experiments', 'elementor' ),
333 'field_args' => [
334 'type' => 'raw_html',
335 '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' ),
336 ],
337 ];
338 }
339
340 if ( ! Tracker::is_allow_track() ) {
341 $fields += $settings->get_usage_fields();
342 }
343
344 $settings->add_tab(
345 'experiments', [
346 'label' => __( 'Experiments', 'elementor' ),
347 'sections' => [
348 'experiments' => [
349 'callback' => function() {
350 $this->render_settings_intro();
351 },
352 'fields' => $fields,
353 ],
354 ],
355 ]
356 );
357 }
358
359 /**
360 * Render Settings Intro
361 *
362 * @since 3.1.0
363 * @access private
364 */
365 private function render_settings_intro() {
366 ?>
367 <h2><?php echo __( 'Experiments', 'elementor' ); ?></h2>
368 <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>
369 <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>
370 <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>
371 <?php
372 }
373
374 /**
375 * Render Feature Settings Field
376 *
377 * @since 3.1.0
378 * @access private
379 *
380 * @param array $feature
381 */
382 private function render_feature_settings_field( array $feature ) {
383 ?>
384 <div class="e-experiment__content">
385 <select id="e-experiment-<?php echo $feature['name']; ?>" class="e-experiment__select" name="<?php echo $this->get_feature_option_key( $feature['name'] ); ?>">
386 <?php foreach ( $this->states as $state_key => $state_title ) { ?>
387 <option value="<?php echo $state_key; ?>" <?php selected( $state_key, $feature['state'] ); ?>><?php echo $state_title; ?></option>
388 <?php } ?>
389 </select>
390 <p class="description"><?php echo $feature['description']; ?></p>
391 <div class="e-experiment__status"><?php echo sprintf( __( 'Status: %s', 'elementor' ), $this->release_statuses[ $feature['release_status'] ] ); ?></div>
392 </div>
393 <?php
394 }
395
396 /**
397 * Get Feature Settings Label HTML
398 *
399 * @since 3.1.0
400 * @access private
401 *
402 * @param array $feature
403 *
404 * @return string
405 */
406 private function get_feature_settings_label_html( array $feature ) {
407 ob_start();
408
409 $is_feature_active = $this->is_feature_active( $feature['name'] );
410
411 $indicator_classes = 'e-experiment__title__indicator';
412
413 if ( $is_feature_active ) {
414 $indicator_classes .= ' e-experiment__title__indicator--active';
415 }
416
417 if ( self::STATE_DEFAULT === $feature['state'] ) {
418 $indicator_tooltip = $is_feature_active ? __( 'Active by default', 'elementor' ) : __( 'Inactive by default', 'elementor' );
419 } else {
420 $indicator_tooltip = self::STATE_ACTIVE === $feature['state'] ? __( 'Active', 'elementor' ) : __( 'Inactive', 'elementor' );
421 }
422 ?>
423 <div class="e-experiment__title">
424 <div class="<?php echo $indicator_classes; ?>" data-tooltip="<?php echo $indicator_tooltip; ?>"></div>
425 <label class="e-experiment__title__label" for="e-experiment-<?php echo $feature['name']; ?>"><?php echo $feature['title']; ?></label>
426 </div>
427 <?php
428
429 return ob_get_clean();
430 }
431
432 /**
433 * Get Feature Settings Label HTML
434 *
435 * @since 3.1.0
436 * @access private
437 *
438 * @param string $feature_name
439 *
440 * @return int
441 */
442 private function get_saved_feature_state( $feature_name ) {
443 return get_option( $this->get_feature_option_key( $feature_name ) );
444 }
445
446 /**
447 * Get Feature Actual State
448 *
449 * @since 3.1.0
450 * @access private
451 *
452 * @param array $feature
453 *
454 * @return string
455 */
456 private function get_feature_actual_state( array $feature ) {
457 if ( self::STATE_DEFAULT !== $feature['state'] ) {
458 return $feature['state'];
459 }
460
461 return $feature['default'];
462 }
463
464 /**
465 * On Feature State Change
466 *
467 * @since 3.1.0
468 * @access private
469 *
470 * @param array $old_feature_data
471 * @param string $new_state
472 */
473 private function on_feature_state_change( array $old_feature_data, $new_state ) {
474 $this->features[ $old_feature_data['name'] ]['state'] = $new_state;
475
476 $new_feature_data = $this->get_features( $old_feature_data['name'] );
477
478 $actual_old_state = $this->get_feature_actual_state( $old_feature_data );
479
480 $actual_new_state = $this->get_feature_actual_state( $new_feature_data );
481
482 if ( $actual_old_state === $actual_new_state ) {
483 return;
484 }
485
486 Plugin::$instance->files_manager->clear_cache();
487
488 if ( $new_feature_data['on_state_change'] ) {
489 $new_feature_data['on_state_change']( $actual_old_state, $actual_new_state );
490 }
491 }
492
493 public function __construct() {
494 $this->init_states();
495
496 $this->init_release_statuses();
497
498 $this->init_features();
499
500 if ( is_admin() ) {
501 $page_id = Settings::PAGE_ID;
502
503 add_action( "elementor/admin/after_create_settings/{$page_id}", function( Settings $settings ) {
504 $this->register_settings_fields( $settings );
505 }, 11 );
506 }
507 }
508 }
509