mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-21 06:39:02 +08:00
Merge pull request #89 from runcommand/hook-count
Hooks should only be called once
This commit is contained in:
commit
d0879c6737
2 changed files with 44 additions and 13 deletions
|
@ -45,6 +45,7 @@ Feature: Profile a specific hook
|
|||
| runcommand_shutdown_hook() | 0 | 1 |
|
||||
| wp_ob_end_flush_all() | 0 | 0 |
|
||||
| total | 0 | 1 |
|
||||
And STDERR should be empty
|
||||
|
||||
Scenario: Indicate where a callback is defined with profiling a hook
|
||||
Given a WP install
|
||||
|
@ -63,3 +64,25 @@ Feature: Profile a specific hook
|
|||
| callback | location | cache_hits | cache_misses |
|
||||
| runcommand_custom_action_hook() | mu-plugins/custom-action.php:2 | 0 | 1 |
|
||||
| total | | 0 | 1 |
|
||||
And STDERR should be empty
|
||||
|
||||
Scenario: Hooks should only be called once
|
||||
Given a WP install
|
||||
And a wp-content/mu-plugins/action-test.php file:
|
||||
"""
|
||||
<?php
|
||||
add_action( 'init', function(){
|
||||
static $i;
|
||||
if ( ! isset( $i ) ) {
|
||||
$i = 0;
|
||||
}
|
||||
$i++;
|
||||
WP_CLI::warning( 'Called ' . $i );
|
||||
});
|
||||
"""
|
||||
|
||||
When I run `wp profile hook init`
|
||||
Then STDERR should be:
|
||||
"""
|
||||
Warning: Called 1
|
||||
"""
|
||||
|
|
|
@ -11,7 +11,9 @@ class Command {
|
|||
private $focus_stage = null;
|
||||
private $stage_hooks = array();
|
||||
private $focus_hook = null;
|
||||
private $current_filter_callbacks = array();
|
||||
private $previous_filter = null;
|
||||
private $previous_filter_callbacks = null;
|
||||
private $filter_depth = 0;
|
||||
private $focus_query_offset = 0;
|
||||
|
||||
private static $exception_message = "Need to bail, because can't restore the hooks";
|
||||
|
@ -272,8 +274,18 @@ class Command {
|
|||
$this->loggers[ $current_filter ]->start();
|
||||
}
|
||||
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook ) {
|
||||
if ( ! is_null( $this->previous_filter_callbacks ) && 0 === $this->filter_depth ) {
|
||||
if ( is_a( $wp_filter[ $this->previous_filter ], 'WP_Hook' ) ) {
|
||||
$wp_filter[ $this->previous_filter ]->callbacks = $this->previous_filter_callbacks;
|
||||
} else {
|
||||
$wp_filter[ $this->previous_filter ] = $this->previous_filter_callbacks;
|
||||
}
|
||||
$this->previous_filter_callbacks = null;
|
||||
}
|
||||
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook && 0 === $this->filter_depth ) {
|
||||
$this->wrap_current_filter_callbacks( $current_filter );
|
||||
$this->filter_depth = 1;
|
||||
}
|
||||
|
||||
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 9999 );
|
||||
|
@ -284,16 +296,16 @@ class Command {
|
|||
*/
|
||||
private function wrap_current_filter_callbacks( $current_filter ) {
|
||||
global $wp_filter;
|
||||
$this->current_filter_callbacks = null;
|
||||
|
||||
if ( ! isset( $wp_filter[ $current_filter ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->previous_filter = $current_filter;
|
||||
if ( is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
|
||||
$callbacks = $this->current_filter_callbacks = $wp_filter[ $current_filter ]->callbacks;
|
||||
$callbacks = $this->previous_filter_callbacks = $wp_filter[ $current_filter ]->callbacks;
|
||||
} else {
|
||||
$callbacks = $this->current_filter_callbacks = $wp_filter[ $current_filter ];
|
||||
$callbacks = $this->previous_filter_callbacks = $wp_filter[ $current_filter ];
|
||||
}
|
||||
|
||||
if ( ! is_array( $callbacks ) ) {
|
||||
|
@ -339,6 +351,10 @@ class Command {
|
|||
$logger->stop_hook_timer();
|
||||
}
|
||||
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook ) {
|
||||
$this->filter_depth = 0;
|
||||
}
|
||||
|
||||
$current_filter = current_filter();
|
||||
if ( in_array( $current_filter, $this->stage_hooks ) ) {
|
||||
$this->loggers[ $current_filter ]->stop();
|
||||
|
@ -354,14 +370,6 @@ class Command {
|
|||
}
|
||||
}
|
||||
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook && ! is_null( $this->current_filter_callbacks ) ) {
|
||||
if ( is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
|
||||
$wp_filter[ $current_filter ]->callbacks = $this->current_filter_callbacks;
|
||||
} else {
|
||||
$wp_filter[ $current_filter ] = $this->current_filter_callbacks;
|
||||
}
|
||||
}
|
||||
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue