PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.1
Elementor Website Builder – more than just a page builder v3.8.1
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 +104 -101 4.2.23.8.1 View file →
@@ -2,15 +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 9 use Elementor\Tracker;
14 10
15 11 if ( ! defined( 'ABSPATH' ) ) {
16 12 exit; // Exit if accessed directly.
@@ -20,8 +16,9 @@
20 16 * Elementor usage module.
21 17 *
22 18 * Elementor usage module handler class is responsible for registering and
23 19 * managing Elementor usage data.
20 + *
24 21 */
25 22 class Module extends BaseModule {
26 23 const GENERAL_TAB = 'general';
27 24 const META_KEY = '_elementor_controls_usage';
@@ -52,9 +49,9 @@
52 49 *
53 50 * Remove 'wp-' from $doc_type for BC, support doc type change since 2.7.0.
54 51 *
55 52 * @param \Elementor\Core\Documents_Manager $doc_class
56 - * @param String $doc_type
53 + * @param String $doc_type
57 54 *
58 55 * @return int
59 56 */
60 57 public function get_doc_type_count( $doc_class, $doc_type ) {
@@ -86,9 +83,9 @@
86 83 * Get formatted usage.
87 84 *
88 85 * Retrieve formatted usage, for frontend.
89 86 *
90 - * @param String $format Optional. Default is 'html'.
87 + * @param String format
91 88 *
92 89 * @return array
93 90 */
94 91 public function get_formatted_usage( $format = 'html' ) {
@@ -126,10 +123,8 @@
126 123 } else {
127 124 $widget_title = $element_type;
128 125 }
129 126
130 - $widget_title = apply_filters( 'elementor/usage/elements/element_title', $widget_title, $element_type );
131 -
132 127 $elements[ $widget_title ] = $data['count'];
133 128 }
134 129
135 130 // Sort elements by key.
@@ -161,9 +156,9 @@
161 156 *
162 157 * Called on elementor/document/before_save, remove document from global & set saving flag.
163 158 *
164 159 * @param Document $document
165 - * @param array $data new settings to save.
160 + * @param array $data new settings to save.
166 161 */
167 162 public function before_document_save( $document, $data ) {
168 163 $current_status = get_post_status( $document->get_post() );
169 164 $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : '';
@@ -194,10 +189,10 @@
194 189 * On status change.
195 190 *
196 191 * Called on transition_post_status.
197 192 *
198 - * @param string $new_status
199 - * @param string $old_status
193 + * @param string $new_status
194 + * @param string $old_status
200 195 * @param \WP_Post $post
201 196 */
202 197 public function on_status_change( $new_status, $old_status, $post ) {
203 198 if ( wp_is_post_autosave( $post ) ) {
@@ -278,9 +273,9 @@
278 273 if ( 0 === $offset ) {
279 274 delete_option( self::OPTION_NAME );
280 275 }
281 276
282 - $post_types = get_post_types( [ 'public' => true ] );
277 + $post_types = get_post_types( array( 'public' => true ) );
283 278
284 279 $query = new \WP_Query( [
285 280 'no_found_rows' => true,
286 281 'meta_key' => '_elementor_data',
@@ -310,13 +305,13 @@
310 305 * Increase controls count.
311 306 *
312 307 * Increase controls count, for each element.
313 308 *
314 - * @param array &$element_ref
309 + * @param array &$element_ref
315 310 * @param string $tab
316 311 * @param string $section
317 312 * @param string $control
318 - * @param int $count
313 + * @param int $count
319 314 */
320 315 private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) {
321 316 if ( ! isset( $element_ref['controls'][ $tab ] ) ) {
322 317 $element_ref['controls'][ $tab ] = [];
@@ -333,14 +328,81 @@
333 328 $element_ref['controls'][ $tab ][ $section ][ $control ] += $count;
334 329 }
335 330
336 331 /**
332 + * Add Controls
333 + *
334 + * Add's controls to this element_ref, returns changed controls count.
335 + *
336 + * @param array $settings_controls
337 + * @param array $element_controls
338 + * @param array &$element_ref
339 + *
340 + * @return int ($changed_controls_count).
341 + */
342 + private function add_controls( $settings_controls, $element_controls, &$element_ref ) {
343 + $changed_controls_count = 0;
344 +
345 + // Loop over all element settings.
346 + foreach ( $settings_controls as $control => $value ) {
347 + if ( empty( $element_controls[ $control ] ) ) {
348 + continue;
349 + }
350 +
351 + $control_config = $element_controls[ $control ];
352 +
353 + if ( ! isset( $control_config['section'], $control_config['default'] ) ) {
354 + continue;
355 + }
356 +
357 + $tab = $control_config['tab'];
358 + $section = $control_config['section'];
359 +
360 + // If setting value is not the control default.
361 + if ( $value !== $control_config['default'] ) {
362 + $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 );
363 +
364 + $changed_controls_count++;
365 + }
366 + }
367 +
368 + return $changed_controls_count;
369 + }
370 +
371 + /**
372 + * Add general controls.
373 + *
374 + * Extract general controls to element ref, return clean `$settings_control`.
375 + *
376 + * @param array $settings_controls
377 + * @param array &$element_ref
378 + *
379 + * @return array ($settings_controls).
380 + */
381 + private function add_general_controls( $settings_controls, &$element_ref ) {
382 + if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) {
383 + $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] );
384 +
385 + // Add dynamic count to controls under `general` tab.
386 + $this->increase_controls_count(
387 + $element_ref,
388 + self::GENERAL_TAB,
389 + Manager::DYNAMIC_SETTING_KEY,
390 + 'count',
391 + count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] )
392 + );
393 + }
394 +
395 + return $settings_controls;
396 + }
397 +
398 + /**
337 399 * Add to global.
338 400 *
339 401 * Add's usage to global (update database).
340 402 *
341 403 * @param string $doc_name
342 - * @param array $doc_usage
404 + * @param array $doc_usage
343 405 */
344 406 private function add_to_global( $doc_name, $doc_usage ) {
345 407 $global_usage = get_option( self::OPTION_NAME, [] );
346 408
@@ -440,11 +502,10 @@
440 502 * @return array
441 503 */
442 504 private function get_elements_usage( $elements ) {
443 505 $usage = [];
444 - $registry = $this->get_calculator_registry();
445 506
446 - Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage, $registry ) {
507 + Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) {
447 508 if ( empty( $element['widgetType'] ) ) {
448 509 $type = $element['elType'];
449 510 $element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
450 511 } else {
@@ -451,57 +512,42 @@
451 512 $type = $element['widgetType'];
452 513 $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
453 514 }
454 515
455 - try {
456 - $calculator = $registry->get_calculator_for( $element, $element_instance );
516 + if ( ! isset( $usage[ $type ] ) ) {
517 + $usage[ $type ] = [
518 + 'count' => 0,
519 + 'control_percent' => 0,
520 + 'controls' => [],
521 + ];
522 + }
457 523
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 - );
524 + $usage[ $type ]['count']++;
469 525
470 - if ( ! isset( $usage[ $type ] ) ) {
471 - $usage[ $type ] = [
472 - 'count' => 0,
473 - 'control_percent' => 0,
474 - 'controls' => [],
475 - ];
476 - }
477 - $usage[ $type ]['count']++;
526 + if ( ! $element_instance ) {
527 + return $element;
478 528 }
479 529
480 - return $element;
481 - } );
530 + $element_controls = $element_instance->get_controls();
482 531
483 - return $usage;
484 - }
532 + if ( isset( $element['settings'] ) ) {
533 + $settings_controls = $element['settings'];
534 + $element_ref = &$usage[ $type ];
485 535
486 - /**
487 - * @return Element_Usage_Calculator_Registry
488 - */
489 - private function get_calculator_registry(): Element_Usage_Calculator_Registry {
490 - static $registry = null;
536 + // Add dynamic values.
537 + $settings_controls = $this->add_general_controls( $settings_controls, $element_ref );
491 538
492 - if ( null === $registry ) {
493 - $calculators = [];
539 + $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref );
494 540
495 - if ( Atomic_Widgets_Module::is_active() ) {
496 - $calculators[] = new Atomic_Element_Usage_Calculator();
541 + $percent = $changed_controls_count / ( count( $element_controls ) / 100 );
542 +
543 + $usage[ $type ] ['control_percent'] = (int) round( $percent );
497 544 }
498 545
499 - $registry = new Element_Usage_Calculator_Registry( $calculators );
500 - $registry->set_fallback( new Legacy_Element_Usage_Calculator() );
501 - }
546 + return $element;
547 + } );
502 548
503 - return $registry;
549 + return $usage;
504 550 }
505 551
506 552 /**
507 553 * Save document usage.
@@ -525,56 +571,18 @@
525 571 $document->update_meta( self::META_KEY, $usage );
526 572
527 573 $this->add_to_global( $document->get_name(), $usage );
528 574 } catch ( \Exception $exception ) {
529 - Logger::warning( $exception->getMessage(), [
575 + Plugin::$instance->logger->get_logger()->error( $exception->getMessage(), [
530 576 'document_id' => $document->get_id(),
531 577 'document_name' => $document->get_name(),
532 578 ] );
533 579
534 580 return;
535 - }
581 + };
536 582 }
537 583 }
538 584
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 585 /**
578 586 * Add system info report.
579 587 */
580 588 public function add_system_info_report() {
@@ -580,13 +588,8 @@
580 588 public function add_system_info_report() {
581 589 System_Info::add_report( 'usage', [
582 590 'file_name' => __DIR__ . '/usage-reporter.php',
583 591 'class_name' => __NAMESPACE__ . '\Usage_Reporter',
584 - ] );
585 -
586 - System_Info::add_report( 'settings', [
587 - 'file_name' => __DIR__ . '/settings-reporter.php',
588 - 'class_name' => __NAMESPACE__ . '\Settings_Reporter',
589 592 ] );
590 593 }
591 594
592 595 /**