PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.2
Elementor Website Builder – more than just a page builder v3.1.2
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
← All changes | modules/usage/module.php +106 -115 4.2.03.1.2 View file →
@@ -2,16 +2,11 @@
2 2 namespace Elementor\Modules\Usage;
3 3
4 4 use Elementor\Core\Base\Document;
5 5 use Elementor\Core\Base\Module as BaseModule;
6 -use Elementor\Modules\AtomicWidgets\Logger\Logger;
7 -use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module;
8 -use Elementor\Modules\AtomicWidgets\Usage\Atomic_Element_Usage_Calculator;
6 +use Elementor\Core\DynamicTags\Manager;
9 7 use Elementor\Modules\System_Info\Module as System_Info;
10 -use Elementor\Modules\Usage\Calculators\Legacy_Element_Usage_Calculator;
11 8 use Elementor\Plugin;
12 -use Elementor\Settings;
13 -use Elementor\Tracker;
14 9
15 10 if ( ! defined( 'ABSPATH' ) ) {
16 11 exit; // Exit if accessed directly.
17 12 }
@@ -20,8 +15,9 @@
20 15 * Elementor usage module.
21 16 *
22 17 * Elementor usage module handler class is responsible for registering and
23 18 * managing Elementor usage data.
19 + *
24 20 */
25 21 class Module extends BaseModule {
26 22 const GENERAL_TAB = 'general';
27 23 const META_KEY = '_elementor_controls_usage';
@@ -52,9 +48,9 @@
52 48 *
53 49 * Remove 'wp-' from $doc_type for BC, support doc type change since 2.7.0.
54 50 *
55 51 * @param \Elementor\Core\Documents_Manager $doc_class
56 - * @param String $doc_type
52 + * @param String $doc_type
57 53 *
58 54 * @return int
59 55 */
60 56 public function get_doc_type_count( $doc_class, $doc_type ) {
@@ -86,9 +82,9 @@
86 82 * Get formatted usage.
87 83 *
88 84 * Retrieve formatted usage, for frontend.
89 85 *
90 - * @param String $format Optional. Default is 'html'.
86 + * @param String format
91 87 *
92 88 * @return array
93 89 */
94 90 public function get_formatted_usage( $format = 'html' ) {
@@ -126,10 +122,8 @@
126 122 } else {
127 123 $widget_title = $element_type;
128 124 }
129 125
130 - $widget_title = apply_filters( 'elementor/usage/elements/element_title', $widget_title, $element_type );
131 -
132 126 $elements[ $widget_title ] = $data['count'];
133 127 }
134 128
135 129 // Sort elements by key.
@@ -140,17 +134,16 @@
140 134 'elements' => $elements,
141 135 'count' => $doc_count,
142 136 ];
143 137
144 - // ' ? 1 : 0;' In sorters is compatibility for PHP8.0.
145 138 // Sort usage by title.
146 139 uasort( $usage, function( $a, $b ) {
147 - return ( $a['title'] > $b['title'] ) ? 1 : 0;
140 + return ( $a['title'] > $b['title'] );
148 141 } );
149 142
150 143 // If title includes '-' will have lower priority.
151 144 uasort( $usage, function( $a ) {
152 - return strpos( $a['title'], '-' ) ? 1 : 0;
145 + return strpos( $a['title'], '-' );
153 146 } );
154 147 }
155 148
156 149 return $usage;
@@ -161,9 +154,9 @@
161 154 *
162 155 * Called on elementor/document/before_save, remove document from global & set saving flag.
163 156 *
164 157 * @param Document $document
165 - * @param array $data new settings to save.
158 + * @param array $data new settings to save.
166 159 */
167 160 public function before_document_save( $document, $data ) {
168 161 $current_status = get_post_status( $document->get_post() );
169 162 $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : '';
@@ -194,10 +187,10 @@
194 187 * On status change.
195 188 *
196 189 * Called on transition_post_status.
197 190 *
198 - * @param string $new_status
199 - * @param string $old_status
191 + * @param string $new_status
192 + * @param string $old_status
200 193 * @param \WP_Post $post
201 194 */
202 195 public function on_status_change( $new_status, $old_status, $post ) {
203 196 if ( wp_is_post_autosave( $post ) ) {
@@ -278,12 +271,11 @@
278 271 if ( 0 === $offset ) {
279 272 delete_option( self::OPTION_NAME );
280 273 }
281 274
282 - $post_types = get_post_types( [ 'public' => true ] );
275 + $post_types = get_post_types( array( 'public' => true ) );
283 276
284 277 $query = new \WP_Query( [
285 - 'no_found_rows' => true,
286 278 'meta_key' => '_elementor_data',
287 279 'post_type' => $post_types,
288 280 'post_status' => [ 'publish', 'private' ],
289 281 'posts_per_page' => $limit,
@@ -310,13 +302,13 @@
310 302 * Increase controls count.
311 303 *
312 304 * Increase controls count, for each element.
313 305 *
314 - * @param array &$element_ref
306 + * @param array &$element_ref
315 307 * @param string $tab
316 308 * @param string $section
317 309 * @param string $control
318 - * @param int $count
310 + * @param int $count
319 311 */
320 312 private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) {
321 313 if ( ! isset( $element_ref['controls'][ $tab ] ) ) {
322 314 $element_ref['controls'][ $tab ] = [];
@@ -333,14 +325,81 @@
333 325 $element_ref['controls'][ $tab ][ $section ][ $control ] += $count;
334 326 }
335 327
336 328 /**
329 + * Add Controls
330 + *
331 + * Add's controls to this element_ref, returns changed controls count.
332 + *
333 + * @param array $settings_controls
334 + * @param array $element_controls
335 + * @param array &$element_ref
336 + *
337 + * @return int ($changed_controls_count).
338 + */
339 + private function add_controls( $settings_controls, $element_controls, &$element_ref ) {
340 + $changed_controls_count = 0;
341 +
342 + // Loop over all element settings.
343 + foreach ( $settings_controls as $control => $value ) {
344 + if ( empty( $element_controls[ $control ] ) ) {
345 + continue;
346 + }
347 +
348 + $control_config = $element_controls[ $control ];
349 +
350 + if ( ! isset( $control_config['section'], $control_config['default'] ) ) {
351 + continue;
352 + }
353 +
354 + $tab = $control_config['tab'];
355 + $section = $control_config['section'];
356 +
357 + // If setting value is not the control default.
358 + if ( $value !== $control_config['default'] ) {
359 + $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 );
360 +
361 + $changed_controls_count++;
362 + }
363 + }
364 +
365 + return $changed_controls_count;
366 + }
367 +
368 + /**
369 + * Add general controls.
370 + *
371 + * Extract general controls to element ref, return clean `$settings_control`.
372 + *
373 + * @param array $settings_controls
374 + * @param array &$element_ref
375 + *
376 + * @return array ($settings_controls).
377 + */
378 + private function add_general_controls( $settings_controls, &$element_ref ) {
379 + if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) {
380 + $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] );
381 +
382 + // Add dynamic count to controls under `general` tab.
383 + $this->increase_controls_count(
384 + $element_ref,
385 + self::GENERAL_TAB,
386 + Manager::DYNAMIC_SETTING_KEY,
387 + 'count',
388 + count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] )
389 + );
390 + }
391 +
392 + return $settings_controls;
393 + }
394 +
395 + /**
337 396 * Add to global.
338 397 *
339 398 * Add's usage to global (update database).
340 399 *
341 400 * @param string $doc_name
342 - * @param array $doc_usage
401 + * @param array $doc_usage
343 402 */
344 403 private function add_to_global( $doc_name, $doc_usage ) {
345 404 $global_usage = get_option( self::OPTION_NAME, [] );
346 405
@@ -440,11 +499,10 @@
440 499 * @return array
441 500 */
442 501 private function get_elements_usage( $elements ) {
443 502 $usage = [];
444 - $registry = $this->get_calculator_registry();
445 503
446 - Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage, $registry ) {
504 + Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) {
447 505 if ( empty( $element['widgetType'] ) ) {
448 506 $type = $element['elType'];
449 507 $element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
450 508 } else {
@@ -451,57 +509,42 @@
451 509 $type = $element['widgetType'];
452 510 $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
453 511 }
454 512
455 - try {
456 - $calculator = $registry->get_calculator_for( $element, $element_instance );
513 + if ( ! isset( $usage[ $type ] ) ) {
514 + $usage[ $type ] = [
515 + 'count' => 0,
516 + 'control_percent' => 0,
517 + 'controls' => [],
518 + ];
519 + }
457 520
458 - if ( $calculator ) {
459 - $usage = $calculator->calculate( $element, $element_instance, $usage );
460 - }
461 - } catch ( \Throwable $e ) {
462 - Logger::warning(
463 - 'Usage calculation failed: ' . $e->getMessage(),
464 - [
465 - 'element_type' => $type,
466 - 'element_id' => $element['id'] ?? 'unknown',
467 - ]
468 - );
521 + $usage[ $type ]['count']++;
469 522
470 - if ( ! isset( $usage[ $type ] ) ) {
471 - $usage[ $type ] = [
472 - 'count' => 0,
473 - 'control_percent' => 0,
474 - 'controls' => [],
475 - ];
476 - }
477 - $usage[ $type ]['count']++;
523 + if ( ! $element_instance ) {
524 + return $element;
478 525 }
479 526
480 - return $element;
481 - } );
527 + $element_controls = $element_instance->get_controls();
482 528
483 - return $usage;
484 - }
529 + if ( isset( $element['settings'] ) ) {
530 + $settings_controls = $element['settings'];
531 + $element_ref = &$usage[ $type ];
485 532
486 - /**
487 - * @return Element_Usage_Calculator_Registry
488 - */
489 - private function get_calculator_registry(): Element_Usage_Calculator_Registry {
490 - static $registry = null;
533 + // Add dynamic values.
534 + $settings_controls = $this->add_general_controls( $settings_controls, $element_ref );
491 535
492 - if ( null === $registry ) {
493 - $calculators = [];
536 + $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref );
494 537
495 - if ( Atomic_Widgets_Module::is_active() ) {
496 - $calculators[] = new Atomic_Element_Usage_Calculator();
538 + $percent = $changed_controls_count / ( count( $element_controls ) / 100 );
539 +
540 + $usage[ $type ] ['control_percent'] = (int) round( $percent );
497 541 }
498 542
499 - $registry = new Element_Usage_Calculator_Registry( $calculators );
500 - $registry->set_fallback( new Legacy_Element_Usage_Calculator() );
501 - }
543 + return $element;
544 + } );
502 545
503 - return $registry;
546 + return $usage;
504 547 }
505 548
506 549 /**
507 550 * Save document usage.
@@ -525,56 +568,13 @@
525 568 $document->update_meta( self::META_KEY, $usage );
526 569
527 570 $this->add_to_global( $document->get_name(), $usage );
528 571 } catch ( \Exception $exception ) {
529 - Logger::warning( $exception->getMessage(), [
530 - 'document_id' => $document->get_id(),
531 - 'document_name' => $document->get_name(),
532 - ] );
533 -
534 - return;
535 - }
572 + return; // Do nothing.
573 + };
536 574 }
537 575 }
538 576
539 - public static function get_settings_usage() {
540 - $usage = [];
541 -
542 - $settings_tab = Plugin::$instance->settings->get_tabs();
543 - $settings = array_merge(
544 - $settings_tab[ Settings::TAB_GENERAL ]['sections'],
545 - $settings_tab[ Settings::TAB_ADVANCED ]['sections']
546 - );
547 -
548 - foreach ( $settings as $setting_data ) {
549 - foreach ( $setting_data['fields'] as $field_name => $field_data ) {
550 - $is_hidden_field = ( empty( $field_data['field_args']['type'] ) || 'hidden' === $field_data['field_args']['type'] );
551 -
552 - if ( $is_hidden_field ) {
553 - continue;
554 - }
555 -
556 - $setting_value = get_option( 'elementor_' . $field_name );
557 -
558 - if ( empty( $setting_value ) ) {
559 - continue;
560 - }
561 -
562 - $is_default_value = ( ! empty( $field_data['field_args']['std'] ) && $setting_value === $field_data['field_args']['std'] );
563 -
564 - if ( $is_default_value ) {
565 - continue;
566 - }
567 -
568 - $usage[ $field_name ] = $setting_value;
569 - }
570 - }
571 -
572 - $usage = apply_filters( 'elementor/system-info/usage/settings', $usage );
573 -
574 - return $usage;
575 - }
576 -
577 577 /**
578 578 * Add system info report.
579 579 */
580 580 public function add_system_info_report() {
@@ -581,13 +581,8 @@
581 581 System_Info::add_report( 'usage', [
582 582 'file_name' => __DIR__ . '/usage-reporter.php',
583 583 'class_name' => __NAMESPACE__ . '\Usage_Reporter',
584 584 ] );
585 -
586 - System_Info::add_report( 'settings', [
587 - 'file_name' => __DIR__ . '/settings-reporter.php',
588 - 'class_name' => __NAMESPACE__ . '\Settings_Reporter',
589 - ] );
590 585 }
591 586
592 587 /**
593 588 * Usage module constructor.
@@ -596,12 +591,8 @@
596 591 *
597 592 * @access public
598 593 */
599 594 public function __construct() {
600 - if ( ! Tracker::is_allow_track() ) {
601 - return;
602 - }
603 -
604 595 add_action( 'transition_post_status', [ $this, 'on_status_change' ], 10, 3 );
605 596 add_action( 'before_delete_post', [ $this, 'on_before_delete_post' ] );
606 597
607 598 add_action( 'elementor/document/before_save', [ $this, 'before_document_save' ], 10, 2 );