PluginProbe
Elementor Website Builder – more than just a page builder / 3.29.2
Elementor Website Builder – more than just a page builder v3.29.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 +100 -52 4.1.23.29.2 View file →
@@ -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 ) {
@@ -161,9 +158,9 @@
161 158 *
162 159 * Called on elementor/document/before_save, remove document from global & set saving flag.
163 160 *
164 161 * @param Document $document
165 - * @param array $data new settings to save.
162 + * @param array $data new settings to save.
166 163 */
167 164 public function before_document_save( $document, $data ) {
168 165 $current_status = get_post_status( $document->get_post() );
169 166 $new_status = isset( $data['settings']['post_status'] ) ? $data['settings']['post_status'] : '';
@@ -194,10 +191,10 @@
194 191 * On status change.
195 192 *
196 193 * Called on transition_post_status.
197 194 *
198 - * @param string $new_status
199 - * @param string $old_status
195 + * @param string $new_status
196 + * @param string $old_status
200 197 * @param \WP_Post $post
201 198 */
202 199 public function on_status_change( $new_status, $old_status, $post ) {
203 200 if ( wp_is_post_autosave( $post ) ) {
@@ -310,13 +307,13 @@
310 307 * Increase controls count.
311 308 *
312 309 * Increase controls count, for each element.
313 310 *
314 - * @param array &$element_ref
311 + * @param array &$element_ref
315 312 * @param string $tab
316 313 * @param string $section
317 314 * @param string $control
318 - * @param int $count
315 + * @param int $count
319 316 */
320 317 private function increase_controls_count( &$element_ref, $tab, $section, $control, $count ) {
321 318 if ( ! isset( $element_ref['controls'][ $tab ] ) ) {
322 319 $element_ref['controls'][ $tab ] = [];
@@ -333,14 +330,81 @@
333 330 $element_ref['controls'][ $tab ][ $section ][ $control ] += $count;
334 331 }
335 332
336 333 /**
334 + * Add Controls
335 + *
336 + * Add's controls to this element_ref, returns changed controls count.
337 + *
338 + * @param array $settings_controls
339 + * @param array $element_controls
340 + * @param array &$element_ref
341 + *
342 + * @return int ($changed_controls_count).
343 + */
344 + private function add_controls( $settings_controls, $element_controls, &$element_ref ) {
345 + $changed_controls_count = 0;
346 +
347 + // Loop over all element settings.
348 + foreach ( $settings_controls as $control => $value ) {
349 + if ( empty( $element_controls[ $control ] ) ) {
350 + continue;
351 + }
352 +
353 + $control_config = $element_controls[ $control ];
354 +
355 + if ( ! isset( $control_config['section'], $control_config['default'] ) ) {
356 + continue;
357 + }
358 +
359 + $tab = $control_config['tab'];
360 + $section = $control_config['section'];
361 +
362 + // If setting value is not the control default.
363 + if ( $value !== $control_config['default'] ) {
364 + $this->increase_controls_count( $element_ref, $tab, $section, $control, 1 );
365 +
366 + ++$changed_controls_count;
367 + }
368 + }
369 +
370 + return $changed_controls_count;
371 + }
372 +
373 + /**
374 + * Add general controls.
375 + *
376 + * Extract general controls to element ref, return clean `$settings_control`.
377 + *
378 + * @param array $settings_controls
379 + * @param array &$element_ref
380 + *
381 + * @return array ($settings_controls).
382 + */
383 + private function add_general_controls( $settings_controls, &$element_ref ) {
384 + if ( ! empty( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] ) ) {
385 + $settings_controls = array_merge( $settings_controls, $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] );
386 +
387 + // Add dynamic count to controls under `general` tab.
388 + $this->increase_controls_count(
389 + $element_ref,
390 + self::GENERAL_TAB,
391 + Manager::DYNAMIC_SETTING_KEY,
392 + 'count',
393 + count( $settings_controls[ Manager::DYNAMIC_SETTING_KEY ] )
394 + );
395 + }
396 +
397 + return $settings_controls;
398 + }
399 +
400 + /**
337 401 * Add to global.
338 402 *
339 403 * Add's usage to global (update database).
340 404 *
341 405 * @param string $doc_name
342 - * @param array $doc_usage
406 + * @param array $doc_usage
343 407 */
344 408 private function add_to_global( $doc_name, $doc_usage ) {
345 409 $global_usage = get_option( self::OPTION_NAME, [] );
346 410
@@ -440,11 +504,10 @@
440 504 * @return array
441 505 */
442 506 private function get_elements_usage( $elements ) {
443 507 $usage = [];
444 - $registry = $this->get_calculator_registry();
445 508
446 - Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage, $registry ) {
509 + Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( &$usage ) {
447 510 if ( empty( $element['widgetType'] ) ) {
448 511 $type = $element['elType'];
449 512 $element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
450 513 } else {
@@ -451,57 +514,42 @@
451 514 $type = $element['widgetType'];
452 515 $element_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
453 516 }
454 517
455 - try {
456 - $calculator = $registry->get_calculator_for( $element, $element_instance );
518 + if ( ! isset( $usage[ $type ] ) ) {
519 + $usage[ $type ] = [
520 + 'count' => 0,
521 + 'control_percent' => 0,
522 + 'controls' => [],
523 + ];
524 + }
457 525
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 - );
526 + $usage[ $type ]['count']++;
469 527
470 - if ( ! isset( $usage[ $type ] ) ) {
471 - $usage[ $type ] = [
472 - 'count' => 0,
473 - 'control_percent' => 0,
474 - 'controls' => [],
475 - ];
476 - }
477 - $usage[ $type ]['count']++;
528 + if ( ! $element_instance ) {
529 + return $element;
478 530 }
479 531
480 - return $element;
481 - } );
532 + $element_controls = $element_instance->get_controls();
482 533
483 - return $usage;
484 - }
534 + if ( isset( $element['settings'] ) ) {
535 + $settings_controls = $element['settings'];
536 + $element_ref = &$usage[ $type ];
485 537
486 - /**
487 - * @return Element_Usage_Calculator_Registry
488 - */
489 - private function get_calculator_registry(): Element_Usage_Calculator_Registry {
490 - static $registry = null;
538 + // Add dynamic values.
539 + $settings_controls = $this->add_general_controls( $settings_controls, $element_ref );
491 540
492 - if ( null === $registry ) {
493 - $calculators = [];
541 + $changed_controls_count = $this->add_controls( $settings_controls, $element_controls, $element_ref );
494 542
495 - if ( Atomic_Widgets_Module::is_active() ) {
496 - $calculators[] = new Atomic_Element_Usage_Calculator();
543 + $percent = ! empty( $element_controls ) ? $changed_controls_count / ( count( $element_controls ) / 100 ) : 0;
544 +
545 + $usage[ $type ] ['control_percent'] = (int) round( $percent );
497 546 }
498 547
499 - $registry = new Element_Usage_Calculator_Registry( $calculators );
500 - $registry->set_fallback( new Legacy_Element_Usage_Calculator() );
501 - }
548 + return $element;
549 + } );
502 550
503 - return $registry;
551 + return $usage;
504 552 }
505 553
506 554 /**
507 555 * Save document usage.
@@ -525,9 +573,9 @@
525 573 $document->update_meta( self::META_KEY, $usage );
526 574
527 575 $this->add_to_global( $document->get_name(), $usage );
528 576 } catch ( \Exception $exception ) {
529 - Logger::warning( $exception->getMessage(), [
577 + Plugin::$instance->logger->get_logger()->error( $exception->getMessage(), [
530 578 'document_id' => $document->get_id(),
531 579 'document_name' => $document->get_name(),
532 580 ] );
533 581