Skip to content

Commit 63f513c

Browse files
CopilotswissspidyCopilot
authored
Add --search option to wp profile hook to filter results by callback name (#211)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 6ac6c7a commit 63f513c

File tree

3 files changed

+69
-57
lines changed

3 files changed

+69
-57
lines changed

features/profile-hook.feature

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Feature: Profile a specific hook
22

3-
@require-wp-4.0
43
Scenario: Profile all hooks when a specific hook isn't specified
54
Given a WP install
65

@@ -20,21 +19,6 @@ Feature: Profile a specific hook
2019
| callback | cache_hits | cache_misses |
2120
| sanitize_comment_cookies() | 0 | 0 |
2221

23-
@less-than-php-7 @require-wp-4.0
24-
Scenario: Profile an intermediate stage hook
25-
Given a WP install
26-
27-
When I run `wp profile hook wp_head:before --fields=callback,cache_hits,cache_misses`
28-
Then STDOUT should be a table containing rows:
29-
| callback | cache_hits | cache_misses |
30-
| parse_blocks() | 0 | 0 |
31-
| _get_wptexturize_split_regex() | 0 | 0 |
32-
And STDOUT should not contain:
33-
"""
34-
WP_CLI\Profile\Profiler->wp_tick_profile_begin()
35-
"""
36-
37-
@require-wp-4.0
3822
Scenario: Profile a hook before the template is loaded
3923
Given a WP install
4024

@@ -43,7 +27,6 @@ Feature: Profile a specific hook
4327
| callback |
4428
And STDERR should be empty
4529

46-
@require-wp-4.0
4730
Scenario: Profile a hook without any callbacks
4831
Given a WP install
4932

@@ -65,7 +48,7 @@ Feature: Profile a specific hook
6548
<meta name="generator"
6649
"""
6750

68-
@require-wp-4.0 @less-than-wp-6.9
51+
@less-than-wp-6.9
6952
Scenario: Profile the shutdown hook
7053
Given a WP install
7154
And a wp-content/mu-plugins/shutdown.php file:
@@ -107,7 +90,6 @@ Feature: Profile a specific hook
10790
| total (3) | 0 | 1 |
10891
And STDERR should be empty
10992

110-
@require-wp-4.0
11193
Scenario: Indicate where a callback is defined with profiling a hook
11294
Given a WP install
11395
And a wp-content/mu-plugins/custom-action.php file:
@@ -127,63 +109,67 @@ Feature: Profile a specific hook
127109
| total (1) | | 0 | 1 |
128110
And STDERR should be empty
129111

130-
Scenario: Hooks should only be called once
112+
Scenario: Search for callbacks by name pattern on a specific hook
131113
Given a WP install
132-
And a wp-content/mu-plugins/action-test.php file:
114+
And a wp-content/mu-plugins/search-test.php file:
133115
"""
134116
<?php
135-
add_action( 'init', function(){
136-
static $i;
137-
if ( ! isset( $i ) ) {
138-
$i = 0;
139-
}
140-
$i++;
141-
WP_CLI::warning( 'Called ' . $i );
142-
});
117+
function wp_cli_search_hook_one() {}
118+
function wp_cli_search_hook_two() {}
119+
function unrelated_callback() {}
120+
add_action( 'init', 'wp_cli_search_hook_one' );
121+
add_action( 'init', 'wp_cli_search_hook_two' );
122+
add_action( 'init', 'unrelated_callback' );
143123
"""
144124

145-
When I try `wp profile hook init`
146-
Then STDERR should be:
125+
When I run `wp profile hook init --fields=callback --search=wp_cli_search_hook`
126+
Then STDOUT should contain:
147127
"""
148-
Warning: Called 1
128+
wp_cli_search_hook_one()
149129
"""
150-
151-
@less-than-php-7 @require-wp-4.0
152-
Scenario: Profile the mu_plugins:before hook
153-
Given a WP install
154-
And a wp-content/mu-plugins/awesome-file.php file:
130+
And STDOUT should contain:
155131
"""
156-
<?php
157-
function awesome_func() {
158-
// does nothing
159-
}
160-
awesome_func();
132+
wp_cli_search_hook_two()
161133
"""
162-
163-
When I run `wp profile hook muplugins_loaded:before --fields=callback`
164-
Then STDOUT should contain:
134+
And STDOUT should not contain:
165135
"""
166-
wp-content/mu-plugins/awesome-file.php
136+
unrelated_callback()
167137
"""
138+
And STDERR should be empty
168139

169-
@less-than-php-7 @require-wp-4.0
170-
Scenario: Profile the :after hooks
140+
Scenario: Search for callbacks by name pattern across all hooks
171141
Given a WP install
142+
And a wp-content/mu-plugins/search-all-test.php file:
143+
"""
144+
<?php
145+
function wp_cli_search_all_hook() {}
146+
add_action( 'init', 'wp_cli_search_all_hook' );
147+
"""
172148

173-
When I run `wp profile hook wp_loaded:after`
149+
When I run `wp profile hook --all --fields=callback --search=wp_cli_search_all_hook`
174150
Then STDOUT should contain:
175151
"""
176-
do_action()
152+
wp_cli_search_all_hook()
177153
"""
154+
And STDERR should be empty
178155

179-
When I run `wp profile hook wp:after`
180-
Then STDOUT should contain:
156+
Scenario: Hooks should only be called once
157+
Given a WP install
158+
And a wp-content/mu-plugins/action-test.php file:
181159
"""
182-
do_action_ref_array()
160+
<?php
161+
add_action( 'init', function(){
162+
static $i;
163+
if ( ! isset( $i ) ) {
164+
$i = 0;
165+
}
166+
$i++;
167+
WP_CLI::warning( 'Called ' . $i );
168+
});
183169
"""
184170

185-
When I run `wp profile hook wp_footer:after`
186-
Then STDOUT should contain:
171+
When I try `wp profile hook init`
172+
Then STDERR should be:
187173
"""
188-
do_action()
174+
Warning: Called 1
189175
"""

features/profile.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Feature: Basic profile usage
88
"""
99
usage: wp profile eval <php-code> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
1010
or: wp profile eval-file <file> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
11-
or: wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
11+
or: wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>] [--search=<pattern>]
1212
or: wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
1313
1414
See 'wp help profile <command>' for more information on a specific command.

src/Command.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public function stage( $args, $assoc_args ) {
233233
* [--orderby=<fields>]
234234
* : Set orderby which field.
235235
*
236+
* [--search=<pattern>]
237+
* : Filter callbacks to those matching the given search pattern (case-insensitive).
238+
*
236239
* ## EXAMPLES
237240
*
238241
* # Profile a hook.
@@ -291,6 +294,13 @@ public function hook( $args, $assoc_args ) {
291294
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
292295
$loggers = self::shine_spotlight( $loggers, $metrics );
293296
}
297+
$search = Utils\get_flag_value( $assoc_args, 'search', false );
298+
if ( false !== $search && '' !== $search ) {
299+
if ( ! $focus ) {
300+
WP_CLI::error( '--search requires --all or a specific hook.' );
301+
}
302+
$loggers = self::filter_by_callback( $loggers, $search );
303+
}
294304
$formatter->display_items( $loggers, true, $order, $orderby );
295305
}
296306

@@ -534,4 +544,20 @@ private static function shine_spotlight( $loggers, $metrics ) {
534544

535545
return $loggers;
536546
}
547+
548+
/**
549+
* Filter loggers to only those whose callback name matches a pattern.
550+
*
551+
* @param array $loggers
552+
* @param string $pattern
553+
* @return array
554+
*/
555+
private static function filter_by_callback( $loggers, $pattern ) {
556+
return array_filter(
557+
$loggers,
558+
function ( $logger ) use ( $pattern ) {
559+
return isset( $logger->callback ) && false !== stripos( $logger->callback, $pattern );
560+
}
561+
);
562+
}
537563
}

0 commit comments

Comments
 (0)