Skip to content

Commit 5b79ac3

Browse files
committed
Merge branch 'main' into copilot/add-wp-profile-queries
2 parents 1764ddd + 4dd8368 commit 5b79ac3

7 files changed

Lines changed: 96 additions & 11 deletions

File tree

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/.actrc export-ignore
2+
/.distignore export-ignore
3+
/.editorconfig export-ignore
4+
/.github export-ignore
5+
/.gitignore export-ignore
6+
/.typos.toml export-ignore
7+
/AGENTS.md export-ignore
8+
/behat.yml export-ignore
9+
/features export-ignore
10+
/phpcs.xml.dist export-ignore
11+
/phpstan.neon.dist export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/tests export-ignore
14+
/wp-cli.yml export-ignore

.github/workflows/copilot-setup-steps.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ jobs:
2121

2222
- name: Check existence of composer.json file
2323
id: check_composer_file
24-
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3
25-
with:
26-
files: "composer.json"
24+
run: echo "files_exists=$(test -f composer.json && echo true || echo false)" >> "$GITHUB_OUTPUT"
2725

2826
- name: Set up PHP environment
2927
if: steps.check_composer_file.outputs.files_exists == 'true'
30-
uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1 # v2
28+
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # v2
3129
with:
3230
php-version: 'latest'
3331
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
@@ -38,7 +36,7 @@ jobs:
3836

3937
- name: Install Composer dependencies & cache dependencies
4038
if: steps.check_composer_file.outputs.files_exists == 'true'
41-
uses: ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520 # v3
39+
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v3
4240
env:
4341
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
4442
with:

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ render based on the main query, and renders it.
115115
Profile key metrics for WordPress hooks (actions and filters).
116116

117117
~~~
118-
wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
118+
wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>] [--search=<pattern>]
119119
~~~
120120

121121
In order to profile callbacks on a specific hook, the action or filter
@@ -161,6 +161,9 @@ will need to execute during the course of the request.
161161
[--orderby=<fields>]
162162
Set orderby which field.
163163

164+
[--search=<pattern>]
165+
Filter callbacks to those matching the given search pattern (case-insensitive).
166+
164167
**EXAMPLES**
165168

166169
# Profile a hook.
@@ -382,7 +385,7 @@ current theme.
382385

383386
## Installing
384387

385-
Installing this package requires WP-CLI v2.12 or greater. Update to the latest stable release with `wp cli update`.
388+
Installing this package requires WP-CLI v2.13 or greater. Update to the latest stable release with `wp cli update`.
386389

387390
Once you've done so, you can install the latest stable version of this package with:
388391

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"authors": [],
88
"require": {
99
"php": ">=7.2.24",
10-
"wp-cli/wp-cli": "^2.12"
10+
"wp-cli/wp-cli": "^2.13"
1111
},
1212
"require-dev": {
1313
"wp-cli/wp-cli-tests": "^5"

features/profile-hook.feature

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Feature: Profile a specific hook
1111
| template_redirect |
1212
And STDERR should be empty
1313

14-
@require-wp-4.4
1514
Scenario: Profile all callbacks when --all flag is used
1615
Given a WP install
1716

@@ -109,7 +108,50 @@ Feature: Profile a specific hook
109108
| total (1) | | 0 | 1 |
110109
And STDERR should be empty
111110

112-
@require-wp-4.4
111+
Scenario: Search for callbacks by name pattern on a specific hook
112+
Given a WP install
113+
And a wp-content/mu-plugins/search-test.php file:
114+
"""
115+
<?php
116+
function wp_cli_search_hook_one() {}
117+
function wp_cli_search_hook_two() {}
118+
function unrelated_callback() {}
119+
add_action( 'init', 'wp_cli_search_hook_one' );
120+
add_action( 'init', 'wp_cli_search_hook_two' );
121+
add_action( 'init', 'unrelated_callback' );
122+
"""
123+
124+
When I run `wp profile hook init --fields=callback --search=wp_cli_search_hook`
125+
Then STDOUT should contain:
126+
"""
127+
wp_cli_search_hook_one()
128+
"""
129+
And STDOUT should contain:
130+
"""
131+
wp_cli_search_hook_two()
132+
"""
133+
And STDOUT should not contain:
134+
"""
135+
unrelated_callback()
136+
"""
137+
And STDERR should be empty
138+
139+
Scenario: Search for callbacks by name pattern across all hooks
140+
Given a WP install
141+
And a wp-content/mu-plugins/search-all-test.php file:
142+
"""
143+
<?php
144+
function wp_cli_search_all_hook() {}
145+
add_action( 'init', 'wp_cli_search_all_hook' );
146+
"""
147+
148+
When I run `wp profile hook --all --fields=callback --search=wp_cli_search_all_hook`
149+
Then STDOUT should contain:
150+
"""
151+
wp_cli_search_all_hook()
152+
"""
153+
And STDERR should be empty
154+
113155
Scenario: Hooks should only be called once
114156
Given a WP install
115157
And a wp-content/mu-plugins/action-test.php file:

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 queries [--url=<url>] [--hook=<hook>] [--callback=<callback>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
1313
or: wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<fields>]
1414

src/Command.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class Command {
127127
* | total (7) | 1.0335s | 77.42% |
128128
* +--------------------------+---------+-------------+
129129
*
130+
* @skipglobalargcheck
130131
* @when before_wp_load
131132
*/
132133
public function stage( $args, $assoc_args ) {
@@ -233,6 +234,9 @@ public function stage( $args, $assoc_args ) {
233234
* [--orderby=<fields>]
234235
* : Set orderby which field.
235236
*
237+
* [--search=<pattern>]
238+
* : Filter callbacks to those matching the given search pattern (case-insensitive).
239+
*
236240
* ## EXAMPLES
237241
*
238242
* # Profile a hook.
@@ -251,6 +255,7 @@ public function stage( $args, $assoc_args ) {
251255
* | total (7) | 8 | 0 |
252256
* +--------------------------------+------------+--------------+
253257
*
258+
* @skipglobalargcheck
254259
* @when before_wp_load
255260
*/
256261
public function hook( $args, $assoc_args ) {
@@ -291,6 +296,13 @@ public function hook( $args, $assoc_args ) {
291296
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
292297
$loggers = self::shine_spotlight( $loggers, $metrics );
293298
}
299+
$search = Utils\get_flag_value( $assoc_args, 'search', false );
300+
if ( false !== $search && '' !== $search ) {
301+
if ( ! $focus ) {
302+
WP_CLI::error( '--search requires --all or a specific hook.' );
303+
}
304+
$loggers = self::filter_by_callback( $loggers, $search );
305+
}
294306
$formatter->display_items( $loggers, true, $order, $orderby );
295307
}
296308

@@ -700,4 +712,20 @@ private static function shine_spotlight( $loggers, $metrics ) {
700712

701713
return $loggers;
702714
}
715+
716+
/**
717+
* Filter loggers to only those whose callback name matches a pattern.
718+
*
719+
* @param array $loggers
720+
* @param string $pattern
721+
* @return array
722+
*/
723+
private static function filter_by_callback( $loggers, $pattern ) {
724+
return array_filter(
725+
$loggers,
726+
function ( $logger ) use ( $pattern ) {
727+
return isset( $logger->callback ) && false !== stripos( $logger->callback, $pattern );
728+
}
729+
);
730+
}
703731
}

0 commit comments

Comments
 (0)