Skip to content

Commit 8fb8768

Browse files
CopilotswissspidyCopilot
authored
Permit running requests as frontend vs. backend based on URL (#216)
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 4dd8368 commit 8fb8768

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

features/profile-stage.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,32 @@ Feature: Profile the template render stage
171171
| hook |
172172
| init |
173173
| wp_loaded:after |
174+
175+
@require-wp-4.0
176+
Scenario: Admin URL runs as a backend request and skips frontend stages
177+
Given a WP install
178+
179+
When I run `wp profile stage --url=example.com/wp-admin/ --context=admin --fields=stage`
180+
Then STDOUT should be a table containing rows:
181+
| stage |
182+
| bootstrap |
183+
And STDOUT should not contain:
184+
"""
185+
main_query
186+
"""
187+
And STDOUT should not contain:
188+
"""
189+
template
190+
"""
191+
And STDERR should be empty
192+
193+
@require-wp-4.0
194+
Scenario: Admin URL without --context=admin emits an error
195+
Given a WP install
196+
197+
When I try `wp profile stage --url=example.com/wp-admin/ --fields=stage`
198+
Then STDERR should contain:
199+
"""
200+
Profiling an admin URL requires --context=admin.
201+
"""
202+
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 () {
@@ -437,6 +456,11 @@ private function load_wordpress_with_template() {
437456
$this->loggers[] = $logger;
438457
}
439458

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

0 commit comments

Comments
 (0)