Skip to content

Commit 44a9171

Browse files
Copilotswissspidy
andcommitted
Permit running requests as frontend vs. backend
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent a3841ec commit 44a9171

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

features/profile-stage.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,21 @@ 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/ --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

src/Profiler.php

Lines changed: 13 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,9 @@ public function get_loggers() {
7779
* Run the profiler against WordPress
7880
*/
7981
public function run() {
82+
$url = WP_CLI::get_runner()->config['url'];
83+
$this->is_admin_request = ! empty( $url ) && (bool) preg_match( '#/wp-admin(/|$)#i', $url );
84+
8085
WP_CLI::add_wp_hook(
8186
'muplugins_loaded',
8287
function () {
@@ -97,6 +102,9 @@ function () {
97102
if ( ! defined( 'SAVEQUERIES' ) ) {
98103
define( 'SAVEQUERIES', true );
99104
}
105+
if ( $this->is_admin_request && ! defined( 'WP_ADMIN' ) ) {
106+
define( 'WP_ADMIN', true );
107+
}
100108
}
101109
);
102110
if (
@@ -437,6 +445,11 @@ private function load_wordpress_with_template() {
437445
$this->loggers[] = $logger;
438446
}
439447

448+
// Skip main_query and template stages for admin requests.
449+
if ( $this->is_admin_request ) {
450+
return;
451+
}
452+
440453
// Set up main_query main WordPress query.
441454
if ( 'stage' === $this->type ) {
442455
if ( 'main_query' === $this->focus ) {

0 commit comments

Comments
 (0)