| @@ -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 | |
| @@ -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 ) { |
| @@ -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 ) ) { |
| @@ -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 = ! empty( $element_controls ) ? $changed_controls_count / ( count( $element_controls ) / 100 ) : 0; | |
| 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,9 +571,9 @@ | ||
| 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 | |