| @@ -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. |
| @@ -161,9 +155,9 @@ | ||
| 161 | 155 | * |
| 162 | 156 | * Called on elementor/document/before_save, remove document from global & set saving flag. |
| 163 | 157 | * |
| 164 | 158 | * @param Document $document |
| 165 | - * @param array $data new settings to save. | |
| 159 | + * @param array $data new settings to save. | |
| 166 | 160 | */ |
| 167 | 161 | public function before_document_save( $document, $data ) { |
| 168 | 162 | $current_status = get_post_status( $document->get_post() ); |
| 169 | 163 | $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : ''; |
| @@ -194,10 +188,10 @@ | ||
| 194 | 188 | * On status change. |
| 195 | 189 | * |
| 196 | 190 | * Called on transition_post_status. |
| 197 | 191 | * |
| 198 | - * @param string $new_status | |
| 199 | - * @param string $old_status | |
| 192 | + * @param string $new_status | |
| 193 | + * @param string $old_status | |
| 200 | 194 | * @param \WP_Post $post |
| 201 | 195 | */ |
| 202 | 196 | public function on_status_change( $new_status, $old_status, $post ) { |
| 203 | 197 | if ( wp_is_post_autosave( $post ) ) { |
| @@ -278,12 +272,11 @@ | ||
| 278 | 272 | if ( 0 === $offset ) { |
| 279 | 273 | delete_option( self::OPTION_NAME ); |
| 280 | 274 | } |
| 281 | 275 | |
| 282 | - $post_types = get_post_types( [ 'public' => true ] ); | |
| 276 | + $post_types = get_post_types( array( 'public' => true ) ); | |
| 283 | 277 | |
| 284 | 278 | $query = new \WP_Query( [ |
| 285 | - 'no_found_rows' => true, | |
| 286 | 279 | 'meta_key' => '_elementor_data', |
| 287 | 280 | 'post_type' => $post_types, |
| 288 | 281 | 'post_status' => [ 'publish', 'private' ], |
| 289 | 282 | 'posts_per_page' => $limit, |
| @@ -310,13 +303,13 @@ | ||
| 310 | 303 | * Increase controls count. |
| 311 | 304 | * |
| 312 | 305 | * Increase controls count, for each element. |
| 313 | 306 | * |
| 314 | - * @param array &$element_ref | |
| 307 | + * @param array &$element_ref | |
| 315 | 308 | * @param string $tab |
| 316 | 309 | * @param string $section |
| 317 | 310 | * @param string $control |
| 318 | - * @param int $count | |
| 311 | + * @param int $count | |
| 319 | 312 | */ |
| 320 | 313 | private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) { |
| 321 | 314 | if ( ! isset( $element_ref['controls'][ $tab ] ) ) { |
| 322 | 315 | $element_ref['controls'][ $tab ] = []; |
| @@ -333,14 +326,81 @@ | ||
| 333 | 326 | $element_ref['controls'][ $tab ][ $section ][ $control ] += $count; |
| 334 | 327 | } |
| 335 | 328 | |
| 336 | 329 | /** |
| 330 | + * Add Controls | |
| 331 | + * | |
| 332 | + * Add's controls to this element_ref, returns changed controls count. | |
| 333 | + * | |
| 334 | + * @param array $settings_controls | |
| 335 | + * @param array $element_controls | |
| 336 | + * @param array &$element_ref | |
| 337 | + * | |
| 338 | + * @return int ($changed_controls_count). | |
| 339 | + */ | |
| 340 | + private function add_controls( $settings_controls, $element_controls, &$element_ref ) { | |
| 341 | + $changed_controls_count = 0; | |
| 342 | + | |
| 343 | + // Loop over all element settings. | |
| 344 | + foreach ( $settings_controls as $control => $value ) { | |
| 345 | + if ( empty( $element_controls[ $control ] ) ) { | |
| 346 | + continue; | |
| 347 | + } | |
| 348 | + | |
| 349 | + $control_config = $element_controls[ $control ]; | |
| 350 | + | |
| 351 | + if ( ! isset( $control_config['section'], $control_config['default'] ) ) { | |
| 352 | + continue; | |
| 353 | + } | |
| 354 | + | |
| 355 | + $tab = $control_config['tab']; | |
| 356 | + $section = $control_config['section']; | |
| 357 | + | |
| 358 | + // If setting value is not the control default. | |
| 359 | + if ( $value !== $control_config['default'] ) { | |
| 360 | + $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 ); | |
| 361 | + | |
| 362 | + $changed_controls_count++; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + return $changed_controls_count; | |
| 367 | + } | |
| 368 | + | |
| 369 | + /** | |
| 370 | + * Add general controls. | |
| 371 | + * | |
| 372 | + * Extract general controls to element ref, return clean `$settings_control`. | |
| 373 | + * | |
| 374 | + * @param array $settings_controls | |
| 375 | + * @param array &$element_ref | |
| 376 | + * | |
| 377 | + * @return array ($settings_controls). | |
| 378 | + */ | |
| 379 | + private function add_general_controls( $settings_controls, &$element_ref ) { | |
| 380 | + if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) { | |
| 381 | + $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ); | |
| 382 | + | |
| 383 | + // Add dynamic count to controls under `general` tab. | |
| 384 | + $this->increase_controls_count( | |
| 385 | + $element_ref, | |
| 386 | + self::GENERAL_TAB, | |
| 387 | + Manager::DYNAMIC_SETTING_KEY, | |
| 388 | + 'count', | |
| 389 | + count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) | |
| 390 | + ); | |
| 391 | + } | |
| 392 | + | |
| 393 | + return $settings_controls; | |
| 394 | + } | |
| 395 | + | |
| 396 | + /** | |
| 337 | 397 | * Add to global. |
| 338 | 398 | * |
| 339 | 399 | * Add's usage to global (update database). |
| 340 | 400 | * |
| 341 | 401 | * @param string $doc_name |
| 342 | - * @param array $doc_usage | |
| 402 | + * @param array $doc_usage | |
| 343 | 403 | */ |
| 344 | 404 | private function add_to_global( $doc_name, $doc_usage ) { |
| 345 | 405 | $global_usage = get_option( self::OPTION_NAME, [] ); |
| 346 | 406 | |
| @@ -440,11 +500,10 @@ | ||
| 440 | 500 | * @return array |
| 441 | 501 | */ |
| 442 | 502 | private function get_elements_usage( $elements ) { |
| 443 | 503 | $usage = []; |
| 444 | - $registry = $this->get_calculator_registry(); | |
| 445 | 504 | |
| 446 | - Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage, $registry ) { | |
| 505 | + Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) { | |
| 447 | 506 | if ( empty( $element['widgetType'] ) ) { |
| 448 | 507 | $type = $element['elType']; |
| 449 | 508 | $element_instance = Plugin::$instance->elements_manager->get_element_types( $type ); |
| 450 | 509 | } else { |
| @@ -451,57 +510,42 @@ | ||
| 451 | 510 | $type = $element['widgetType']; |
| 452 | 511 | $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type ); |
| 453 | 512 | } |
| 454 | 513 | |
| 455 | - try { | |
| 456 | - $calculator = $registry->get_calculator_for( $element, $element_instance ); | |
| 514 | + if ( ! isset( $usage[ $type ] ) ) { | |
| 515 | + $usage[ $type ] = [ | |
| 516 | + 'count' => 0, | |
| 517 | + 'control_percent' => 0, | |
| 518 | + 'controls' => [], | |
| 519 | + ]; | |
| 520 | + } | |
| 457 | 521 | |
| 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 | - ); | |
| 522 | + $usage[ $type ]['count']++; | |
| 469 | 523 | |
| 470 | - if ( ! isset( $usage[ $type ] ) ) { | |
| 471 | - $usage[ $type ] = [ | |
| 472 | - 'count' => 0, | |
| 473 | - 'control_percent' => 0, | |
| 474 | - 'controls' => [], | |
| 475 | - ]; | |
| 476 | - } | |
| 477 | - $usage[ $type ]['count']++; | |
| 524 | + if ( ! $element_instance ) { | |
| 525 | + return $element; | |
| 478 | 526 | } |
| 479 | 527 | |
| 480 | - return $element; | |
| 481 | - } ); | |
| 528 | + $element_controls = $element_instance->get_controls(); | |
| 482 | 529 | |
| 483 | - return $usage; | |
| 484 | - } | |
| 530 | + if ( isset( $element['settings'] ) ) { | |
| 531 | + $settings_controls = $element['settings']; | |
| 532 | + $element_ref = &$usage[ $type ]; | |
| 485 | 533 | |
| 486 | - /** | |
| 487 | - * @return Element_Usage_Calculator_Registry | |
| 488 | - */ | |
| 489 | - private function get_calculator_registry(): Element_Usage_Calculator_Registry { | |
| 490 | - static $registry = null; | |
| 534 | + // Add dynamic values. | |
| 535 | + $settings_controls = $this->add_general_controls( $settings_controls, $element_ref ); | |
| 491 | 536 | |
| 492 | - if ( null === $registry ) { | |
| 493 | - $calculators = []; | |
| 537 | + $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref ); | |
| 494 | 538 | |
| 495 | - if ( Atomic_Widgets_Module::is_active() ) { | |
| 496 | - $calculators[] = new Atomic_Element_Usage_Calculator(); | |
| 539 | + $percent = $changed_controls_count / ( count( $element_controls ) / 100 ); | |
| 540 | + | |
| 541 | + $usage[ $type ] ['control_percent'] = (int) round( $percent ); | |
| 497 | 542 | } |
| 498 | 543 | |
| 499 | - $registry = new Element_Usage_Calculator_Registry( $calculators ); | |
| 500 | - $registry->set_fallback( new Legacy_Element_Usage_Calculator() ); | |
| 501 | - } | |
| 544 | + return $element; | |
| 545 | + } ); | |
| 502 | 546 | |
| 503 | - return $registry; | |
| 547 | + return $usage; | |
| 504 | 548 | } |
| 505 | 549 | |
| 506 | 550 | /** |
| 507 | 551 | * Save document usage. |
| @@ -525,56 +569,13 @@ | ||
| 525 | 569 | $document->update_meta( self::META_KEY, $usage ); |
| 526 | 570 | |
| 527 | 571 | $this->add_to_global( $document->get_name(), $usage ); |
| 528 | 572 | } 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 | - } | |
| 573 | + return; // Do nothing. | |
| 574 | + }; | |
| 536 | 575 | } |
| 537 | 576 | } |
| 538 | 577 | |
| 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 | 578 | /** |
| 578 | 579 | * Add system info report. |
| 579 | 580 | */ |
| 580 | 581 | public function add_system_info_report() { |
| @@ -581,13 +582,8 @@ | ||
| 581 | 582 | System_Info::add_report( 'usage', [ |
| 582 | 583 | 'file_name' => __DIR__ . '/usage-reporter.php', |
| 583 | 584 | 'class_name' => __NAMESPACE__ . '\Usage_Reporter', |
| 584 | 585 | ] ); |
| 585 | - | |
| 586 | - System_Info::add_report( 'settings', [ | |
| 587 | - 'file_name' => __DIR__ . '/settings-reporter.php', | |
| 588 | - 'class_name' => __NAMESPACE__ . '\Settings_Reporter', | |
| 589 | - ] ); | |
| 590 | 586 | } |
| 591 | 587 | |
| 592 | 588 | /** |
| 593 | 589 | * Usage module constructor. |
| @@ -596,12 +592,8 @@ | ||
| 596 | 592 | * |
| 597 | 593 | * @access public |
| 598 | 594 | */ |
| 599 | 595 | public function __construct() { |
| 600 | - if ( ! Tracker::is_allow_track() ) { | |
| 601 | - return; | |
| 602 | - } | |
| 603 | - | |
| 604 | 596 | add_action( 'transition_post_status', [ $this, 'on_status_change' ], 10, 3 ); |
| 605 | 597 | add_action( 'before_delete_post', [ $this, 'on_before_delete_post' ] ); |
| 606 | 598 | |
| 607 | 599 | add_action( 'elementor/document/before_save', [ $this, 'before_document_save' ], 10, 2 ); |