Skip to content

Commit 4db03c2

Browse files
westonruteradamsilversteinemrikol
authored
Merge pull request #2159 from WordPress/fix/server-timing-for-db-queries
Add notices and improved type checking for `$wpdb->queries` when attempting to compute server-timing for database queries Co-authored-by: westonruter <[email protected]> Co-authored-by: adamsilverstein <[email protected]> Co-authored-by: emrikol <[email protected]>
2 parents ed4d600 + 4a8aa77 commit 4db03c2

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

plugins/performance-lab/includes/server-timing/defaults.php

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
* @since 1.8.0
2626
*/
2727
function perflab_register_default_server_timing_before_template_metrics(): void {
28-
$calculate_before_template_metrics = static function (): void {
28+
$current_function = __FUNCTION__;
29+
30+
$calculate_before_template_metrics = static function () use ( $current_function ): void {
2931
// WordPress execution prior to serving the template.
3032
perflab_server_timing_register_metric(
3133
'before-template',
@@ -44,17 +46,52 @@ function perflab_register_default_server_timing_before_template_metrics(): void
4446
perflab_server_timing_register_metric(
4547
'before-template-db-queries',
4648
array(
47-
'measure_callback' => static function ( $metric ): void {
49+
'measure_callback' => static function ( $metric ) use ( $current_function ): void {
4850
// This should never happen, but some odd database implementations may be doing it wrong.
4951
if ( ! isset( $GLOBALS['wpdb']->queries ) || ! is_array( $GLOBALS['wpdb']->queries ) ) {
52+
wp_trigger_error(
53+
$current_function,
54+
esc_html(
55+
sprintf(
56+
/* translators: 1: before-template-db-queries, 2: $wpdb->queries */
57+
__( 'Unable to compute server timing for "%1$s" because %2$s is not an array.', 'performance-lab' ),
58+
'before-template-db-queries',
59+
'$wpdb->queries'
60+
)
61+
)
62+
);
5063
return;
5164
}
5265

66+
/**
67+
* Query times.
68+
*
69+
* @var float[] $query_times
70+
*/
71+
$query_times = array();
72+
foreach ( $GLOBALS['wpdb']->queries as $query ) {
73+
if ( ! is_array( $query ) || ! isset( $query[1] ) || ! is_float( $query[1] ) ) {
74+
wp_trigger_error(
75+
$current_function,
76+
esc_html(
77+
sprintf(
78+
/* translators: 1: before-template-db-queries, 2: $wpdb->queries */
79+
__( 'Unable to compute server timing for "%1$s" because a saved query in %2$s lacks the elapsed time as the second array value.', 'performance-lab' ),
80+
'before-template-db-queries',
81+
'$wpdb->queries'
82+
)
83+
)
84+
);
85+
return;
86+
}
87+
$query_times[] = $query[1];
88+
}
89+
5390
// Store this value in a global to later subtract it from total query time after template.
5491
$GLOBALS['perflab_query_time_before_template'] = array_reduce(
55-
$GLOBALS['wpdb']->queries,
56-
static function ( $acc, $query ) {
57-
return $acc + $query[1];
92+
$query_times,
93+
static function ( float $acc, float $query_time ): float {
94+
return $acc + $query_time;
5895
},
5996
0.0
6097
);
@@ -155,19 +192,35 @@ static function (): void {
155192
array(
156193
'measure_callback' => static function ( $metric ): void {
157194
// This global should typically be set when this is called, but check just in case.
158-
if ( ! isset( $GLOBALS['perflab_query_time_before_template'] ) ) {
195+
if ( ! isset( $GLOBALS['perflab_query_time_before_template'] ) || ! is_float( $GLOBALS['perflab_query_time_before_template'] ) ) {
159196
return;
160197
}
161198

162199
// This should never happen, but some odd database implementations may be doing it wrong.
163200
if ( ! isset( $GLOBALS['wpdb']->queries ) || ! is_array( $GLOBALS['wpdb']->queries ) ) {
201+
// A notice is already emitted above, but if $perflab_query_time_before_template was not
202+
// set, then this condition wouldn't be checked in the first place.
164203
return;
165204
}
166205

206+
/**
207+
* Query times.
208+
*
209+
* @var float[] $query_times
210+
*/
211+
$query_times = array();
212+
foreach ( $GLOBALS['wpdb']->queries as $query ) {
213+
if ( ! is_array( $query ) || ! isset( $query[1] ) || ! is_float( $query[1] ) ) {
214+
// A notice is already emitted above.
215+
return;
216+
}
217+
$query_times[] = $query[1];
218+
}
219+
167220
$total_query_time = array_reduce(
168-
$GLOBALS['wpdb']->queries,
169-
static function ( $acc, $query ) {
170-
return $acc + $query[1];
221+
$query_times,
222+
static function ( float $acc, float $query_time ): float {
223+
return $acc + $query_time;
171224
},
172225
0.0
173226
);

0 commit comments

Comments
 (0)