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