Fixed handling of reoccurring hook events.

This commit is contained in:
Andrey Savchenko committed 2019-01-29 16:23:49 +02:00
1 parent b4c49f3c67
commit a678997c44
2 files changed
+33

No files matched your search

+3
View File
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased ## Unreleased
### Fixed
- handling of reoccurring hook events
## 3.1 - 2019-01-22 ## 3.1 - 2019-01-22
### Added ### Added
+30
View File
@@ -26,6 +26,9 @@ class Hook_Collector extends Stopwatch_Collector {
/** @var Hook_Formatter */ /** @var Hook_Formatter */
protected $formatter; protected $formatter;
/** @var array $calls Tracks number of calls for recurrent events. */
private $calls = [];
/** /**
* @param Stopwatch $stopwatch Stopwatch instance. * @param Stopwatch $stopwatch Stopwatch instance.
* @param Hook_Event_Config_Interface[] $event_configs Starts and stops configuration. * @param Hook_Event_Config_Interface[] $event_configs Starts and stops configuration.
@@ -97,6 +100,8 @@ class Hook_Collector extends Stopwatch_Collector {
if ( '' !== $start ) { if ( '' !== $start ) {
add_action( $start, function ( $input = null ) use ( $event, $category, $start, $stop ) { add_action( $start, function ( $input = null ) use ( $event, $category, $start, $stop ) {
$event .= $this->get_count_suffix( $event, true );
if ( 'Sidebar' === $event ) { if ( 'Sidebar' === $event ) {
$event = $input; $event = $input;
} }
@@ -115,6 +120,8 @@ class Hook_Collector extends Stopwatch_Collector {
if ( '' !== $stop ) { if ( '' !== $stop ) {
add_action( $stop, function ( $input = null ) use ( $event ) { add_action( $stop, function ( $input = null ) use ( $event ) {
$event .= $this->get_count_suffix( $event );
if ( 'Sidebar' === $event ) { if ( 'Sidebar' === $event ) {
$event = $input; $event = $input;
} }
@@ -126,6 +133,29 @@ class Hook_Collector extends Stopwatch_Collector {
} }
} }
/**
* Get the suffix to use if event occurs multiple times.
*
* @param string $event Event name.
* @param boolean $increment Flag to increment count.
*
* @return string
*/
private function get_count_suffix( string $event, bool $increment = false ): string {
if ( ! isset( $this->calls[ $event ] ) ) {
$this->calls[ $event ] = 0;
}
if ( $increment ) {
$this->calls[ $event ] ++;
}
$count = (int) $this->calls[ $event ];
return $count > 1 ? " ({$count})" : '';
}
/** /**
* When theme is done possibly add vendor-specific events. * When theme is done possibly add vendor-specific events.
*/ */