Skip to content

Commit 4e585ae

Browse files
authored
Merge branch 'main' into copilot/add-wp-profile-queries
2 parents bb8a0b3 + 8590872 commit 4e585ae

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,5 +421,9 @@ Want to contribute a new feature? Please first [open a new issue](https://github
421421

422422
Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally.
423423

424+
### License
425+
426+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
427+
424428

425429
*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*

features/profile-stage.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,32 @@ Feature: Profile the template render stage
165165
| hook |
166166
| init |
167167
| wp_loaded:after |
168+
169+
@require-wp-4.0
170+
Scenario: Admin URL runs as a backend request and skips frontend stages
171+
Given a WP install
172+
173+
When I run `wp profile stage --url=example.com/wp-admin/ --context=admin --fields=stage`
174+
Then STDOUT should be a table containing rows:
175+
| stage |
176+
| bootstrap |
177+
And STDOUT should not contain:
178+
"""
179+
main_query
180+
"""
181+
And STDOUT should not contain:
182+
"""
183+
template
184+
"""
185+
And STDERR should be empty
186+
187+
@require-wp-4.0
188+
Scenario: Admin URL without --context=admin emits an error
189+
Given a WP install
190+
191+
When I try `wp profile stage --url=example.com/wp-admin/ --fields=stage`
192+
Then STDERR should contain:
193+
"""
194+
Profiling an admin URL requires --context=admin.
195+
"""
196+
And the return code should be 1

src/Profiler.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Profiler {
4848
private $tick_cache_hit_offset = null;
4949
private $tick_cache_miss_offset = null;
5050

51+
private $is_admin_request = false;
52+
5153
public function __construct( $type, $focus ) {
5254
$this->type = $type;
5355
$this->focus = $focus;
@@ -77,6 +79,23 @@ public function get_loggers() {
7779
* Run the profiler against WordPress
7880
*/
7981
public function run() {
82+
$url = WP_CLI::get_runner()->config['url'];
83+
$path = '';
84+
if ( ! empty( $url ) ) {
85+
$parsed_url = WP_CLI\Utils\parse_url( $url );
86+
if ( false !== $parsed_url && isset( $parsed_url['path'] ) ) {
87+
$path = $parsed_url['path'];
88+
} else {
89+
// Fallback for cases where $url is just a path.
90+
$path = $url;
91+
}
92+
}
93+
$this->is_admin_request = ! empty( $path ) && (bool) preg_match( '#/wp-admin(/|$|\?)#i', $path );
94+
95+
if ( $this->is_admin_request && 'admin' !== WP_CLI::get_runner()->config['context'] ) {
96+
WP_CLI::error( 'Profiling an admin URL requires --context=admin.' );
97+
}
98+
8099
WP_CLI::add_wp_hook(
81100
'muplugins_loaded',
82101
function () {
@@ -438,6 +457,11 @@ private function load_wordpress_with_template() {
438457
$this->loggers[] = $logger;
439458
}
440459

460+
// Skip main_query and template stages for admin requests.
461+
if ( $this->is_admin_request ) {
462+
return;
463+
}
464+
441465
// Set up main_query main WordPress query.
442466
if ( 'stage' === $this->type ) {
443467
if ( 'main_query' === $this->focus ) {

0 commit comments

Comments
 (0)