-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathhelper.php
More file actions
610 lines (546 loc) · 18.9 KB
/
helper.php
File metadata and controls
610 lines (546 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
/**
* Helper functions used for Enqueued Assets Health Check.
*
* @package performance-lab
* @since 1.0.0
*/
declare( strict_types = 1 );
// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd
/**
* Audit blocking assets on the front page.
*
* @since 4.0.0
*
* @return array{
* response: WP_Error|array{
* headers: WpOrg\Requests\Utility\CaseInsensitiveDictionary,
* body: string,
* response: array{
* code: int|false,
* message: string|false,
* },
* },
* assets: array{
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* }
* } An array containing response and blocking assets.
*/
function perflab_aea_audit_blocking_assets(): array {
$response = wp_remote_get(
add_query_arg( 'cache_bust', (string) wp_rand(), home_url( '/' ) ),
array(
'timeout' => 10,
'headers' => array_merge(
array(
'Accept' => 'text/html',
),
perflab_get_http_basic_authorization_headers()
),
)
);
$result = array(
'response' => $response,
'assets' => array(
'scripts' => array(),
'styles' => array(),
),
);
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return $result;
}
$html = wp_remote_retrieve_body( $response );
if ( '' === $html ) {
return $result;
}
$processor = new WP_HTML_Tag_Processor( $html );
while ( $processor->next_tag() ) {
$tag = $processor->get_tag();
if ( 'SCRIPT' === $tag ) {
$src = $processor->get_attribute( 'src' );
// Skip scripts with empty or whitespace-only src attributes as they never load external scripts which block rendering.
if ( ! is_string( $src ) ) {
continue;
}
$src = trim( $src );
if ( '' === $src ) {
continue;
}
// Note that when the "type" attribute is absent or empty, the element is treated as a classic JavaScript script.
$type = $processor->get_attribute( 'type' );
// Skip external script with "async" or "defer" attributes.
if ( null !== $processor->get_attribute( 'async' ) || null !== $processor->get_attribute( 'defer' ) ) {
continue;
}
// Skip external script with a "type" attribute set to "module" as they are deferred by default.
if ( 'module' === strtolower( (string) $type ) ) {
continue;
}
// Skip external script with a "type" attribute that is not JavaScript.
if (
is_string( $type ) &&
'' !== $type &&
! (
str_contains( $type, 'javascript' ) ||
str_contains( $type, 'ecmascript' ) ||
str_contains( $type, 'jscript' ) ||
str_contains( $type, 'livescript' )
)
) {
continue;
}
$size = perflab_aea_get_asset_size( $src );
$result['assets']['scripts'][] = array(
'src' => $src,
'size' => is_wp_error( $size ) ? null : $size,
'error' => is_wp_error( $size ) ? $size : null,
);
} elseif ( 'LINK' === $tag ) {
$rel = $processor->get_attribute( 'rel' );
if ( 'stylesheet' !== strtolower( (string) $rel ) ) {
continue;
}
$media = $processor->get_attribute( 'media' );
if ( is_string( $media ) && 1 !== preg_match( '/^\s*(all|screen)\b/i', $media ) ) {
continue;
}
$href = $processor->get_attribute( 'href' );
// Skip stylesheets with empty or whitespace-only href attributes since they may be using a deferred loading technique.
if ( ! is_string( $href ) ) {
continue;
}
$href = trim( $href );
if ( '' === $href ) {
continue;
}
$size = perflab_aea_get_asset_size( $href );
$result['assets']['styles'][] = array(
'src' => $href,
'size' => is_wp_error( $size ) ? null : $size,
'error' => is_wp_error( $size ) ? $size : null,
);
}
}
return $result;
}
/**
* Callback for enqueued_blocking_assets test.
*
* @since 4.0.0
*
* @return array{
* label: string,
* status: 'good'|'recommended',
* badge: array{label: string, color: non-empty-string},
* description: string,
* actions: string,
* test: string
* } Result.
*/
function perflab_aea_enqueued_blocking_assets_test(): array {
$result = array(
'label' => __( 'Any blocking assets do not appear to be particularly problematic', 'performance-lab' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Performance', 'performance-lab' ),
'color' => 'blue',
),
'description' => '',
'actions' => '',
'test' => 'enqueued_blocking_assets',
);
$audit_result = perflab_aea_audit_blocking_assets();
$retrieval_failure_result = perflab_aea_blocking_assets_retrieval_failure( $audit_result['response'] );
if ( null !== $retrieval_failure_result ) {
return array_merge( $result, $retrieval_failure_result );
}
$scripts_result = perflab_aea_enqueued_blocking_scripts( $audit_result['assets'] );
$styles_result = perflab_aea_enqueued_blocking_styles( $audit_result['assets'] );
$result['description'] .= perflab_aea_generate_blocking_assets_table( $audit_result['assets'] );
$result['description'] .= $scripts_result['description'];
$result['description'] .= $styles_result['description'];
if (
'good' !== $scripts_result['status'] ||
'good' !== $styles_result['status']
) {
$result['label'] = __( 'Your site may have a problem with blocking assets', 'performance-lab' );
$result['status'] = 'recommended';
$result['actions'] = sprintf(
/* translators: 1: HelpHub URL. 2: Link description. 3.URL to clean cache. 4. Clean Cache text. */
'<p><a target="_blank" href="%1$s">%2$s</a></p>',
esc_url( __( 'https://wordpress.org/support/article/optimization/', 'performance-lab' ) ),
__( 'More info about performance optimization', 'performance-lab' )
);
}
return $result;
}
/**
* Callback for enqueued_blocking_assets test via AJAX.
*
* @since 4.0.0
*/
function perflab_aea_enqueued_ajax_blocking_assets_test(): void {
check_ajax_referer( 'health-check-site-status' );
if ( ! current_user_can( 'view_site_health_checks' ) ) {
wp_send_json_error();
}
wp_send_json_success( perflab_aea_enqueued_blocking_assets_test() );
}
/**
* Prepares the blocking scripts audit result.
*
* @since 4.0.0
*
* @phpstan-param array{
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* } $blocking_assets
*
* @param array<string, mixed> $blocking_assets Array of blocking assets.
* @return array{status: 'good'|'recommended', description: string} Result.
*/
function perflab_aea_enqueued_blocking_scripts( array $blocking_assets ): array {
$enqueued_scripts = count( $blocking_assets['scripts'] );
$bytes_enqueued = array_reduce(
$blocking_assets['scripts'],
static function ( $carry, $asset ): int {
return $carry + ( $asset['size'] ?? 0 );
},
0
);
$result = array(
'status' => 'good',
'description' => sprintf(
'<p>%s</p>',
esc_html(
sprintf(
/* translators: 1: Number of blocking styles. 2.Styles size. */
_n(
'The amount of %1$s blocking script (size: %2$s) is acceptable.',
'The amount of %1$s blocking scripts (size: %2$s) is acceptable.',
$enqueued_scripts,
'performance-lab'
),
$enqueued_scripts,
size_format( $bytes_enqueued )
)
)
),
);
/**
* Filters number of enqueued scripts to trigger warning.
*
* @since 1.0.0
*
* @param int $scripts_threshold Scripts threshold number. Default 30.
*/
$scripts_threshold = apply_filters( 'perflab_aea_enqueued_scripts_threshold', 30 );
/**
* Filters size of enqueued scripts to trigger warning.
*
* @since 1.0.0
*
* @param int $scripts_size_threshold Enqueued Scripts size (in bytes) threshold. Default 300000.
*/
$scripts_size_threshold = apply_filters( 'perflab_aea_enqueued_scripts_byte_size_threshold', 300000 );
if ( $enqueued_scripts > $scripts_threshold || $bytes_enqueued > $scripts_size_threshold ) {
$result['status'] = 'recommended';
$result['description'] = sprintf(
'<p>%s</p>',
esc_html(
sprintf(
/* translators: 1: Number of blocking scripts. 2. Scripts size. */
_n(
'Your website has %1$s blocking script (size: %2$s). Try to reduce the number or to concatenate them.',
'Your website has %1$s blocking scripts (size: %2$s). Try to reduce the number or to concatenate them.',
$enqueued_scripts,
'performance-lab'
),
$enqueued_scripts,
size_format( $bytes_enqueued )
)
)
);
}
// If one of the assets had an error, then fail the test even if under the threshold.
foreach ( $blocking_assets['scripts'] as $script ) {
if ( is_wp_error( $script['error'] ) ) {
$result['status'] = 'recommended';
break;
}
}
return $result;
}
/**
* Prepares the blocking styles audit result.
*
* @since 4.0.0
*
* @phpstan-param array{
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* } $blocking_assets
*
* @param array<string, mixed> $blocking_assets Array of blocking assets.
* @return array{status: 'good'|'recommended', description: string} Result.
*/
function perflab_aea_enqueued_blocking_styles( array $blocking_assets ): array {
$enqueued_styles = count( $blocking_assets['styles'] );
$bytes_enqueued = array_reduce(
$blocking_assets['styles'],
static function ( $carry, $asset ): int {
return $carry + ( $asset['size'] ?? 0 );
},
0
);
$result = array(
'status' => 'good',
'description' => sprintf(
'<p>%s</p>',
esc_html(
sprintf(
/* translators: 1: Number of blocking styles. 2. Styles size. */
_n(
'The amount of %1$s blocking style (size: %2$s) is acceptable.',
'The amount of %1$s blocking styles (size: %2$s) is acceptable.',
$enqueued_styles,
'performance-lab'
),
$enqueued_styles,
size_format( $bytes_enqueued )
)
)
),
);
/**
* Filters number of enqueued styles to trigger warning.
*
* @since 1.0.0
*
* @param int $styles_threshold Styles threshold number. Default 10.
*/
$styles_threshold = apply_filters( 'perflab_aea_enqueued_styles_threshold', 10 );
/**
* Filters size of enqueued styles to trigger warning.
*
* @since 1.0.0
*
* @param int $styles_size_threshold Enqueued styles size (in bytes) threshold. Default 100000.
*/
$styles_size_threshold = apply_filters( 'perflab_aea_enqueued_styles_byte_size_threshold', 100000 );
if ( $enqueued_styles > $styles_threshold || $bytes_enqueued > $styles_size_threshold ) {
$result['status'] = 'recommended';
$result['description'] = sprintf(
'<p>%s</p>',
esc_html(
sprintf(
/* translators: 1: Number of blocking styles. 2.Styles size. */
_n(
'Your website has %1$s blocking style (size: %2$s). Try to reduce the number or to concatenate them.',
'Your website has %1$s blocking styles (size: %2$s). Try to reduce the number or to concatenate them.',
$enqueued_styles,
'performance-lab'
),
$enqueued_styles,
size_format( $bytes_enqueued )
)
)
);
}
// If one of the assets had an error, then fail the test even if under the threshold.
foreach ( $blocking_assets['styles'] as $style ) {
if ( is_wp_error( $style['error'] ) ) {
$result['status'] = 'recommended';
break;
}
}
return $result;
}
/**
* Handles the failure of retrieving the home page to analyze blocking assets.
*
* @since 4.0.0
*
* @phpstan-param WP_Error|array{
* headers: WpOrg\Requests\Utility\CaseInsensitiveDictionary,
* body: string,
* response: array{
* code: int|false,
* message: string|false,
* },
* } $response
*
* @param WP_Error|array<string, mixed> $response The response from the home page retrieval.
* @return array{status: 'recommended', description: string}|null Result, or null if there was no failure.
*/
function perflab_aea_blocking_assets_retrieval_failure( $response ): ?array {
$result = array(
'label' => __( 'Unable to check site for blocking assets', 'performance-lab' ),
'status' => 'recommended',
'description' => '',
);
if ( is_array( $response ) ) {
$code = wp_remote_retrieve_response_code( $response );
$message = wp_remote_retrieve_response_message( $response );
$body = wp_remote_retrieve_body( $response );
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
if ( is_array( $content_type ) ) {
$content_type = array_pop( $content_type );
}
// No error.
if ( 200 === $code && '' !== $body ) {
return null;
}
if ( '' === $body ) {
$result['description'] .= '<p>' . esc_html__( 'While retrieving the home page to analyze the blocking assets, the request was successfully but response body was empty.', 'performance-lab' ) . '</p>';
}
if ( 200 !== $code ) {
$result['description'] .= '<p>' . wp_kses(
sprintf(
/* translators: %d is the HTTP status code, %s is the status header description */
__( 'While retrieving the home page to analyze the blocking assets, the request returned with an HTTP status of <code>%1$d %2$s</code>.', 'performance-lab' ),
(int) $code,
esc_html( $message )
),
array( 'code' => array() )
) . '</p>';
}
if ( '' !== $body ) {
$result['description'] .= '<details>';
$result['description'] .= '<summary>' . esc_html__( 'Raw response:', 'performance-lab' ) . '</summary>';
if ( is_string( $content_type ) && str_contains( $content_type, 'html' ) ) {
$escaped_content = htmlspecialchars( $body, ENT_QUOTES, 'UTF-8' );
$result['description'] .= '<iframe srcdoc="' . $escaped_content . '" sandbox width="100%" height="300"></iframe>';
} else {
$result['description'] .= '<pre style="white-space: pre-wrap">' . esc_html( $body ) . '</pre>';
}
$result['description'] .= '</details>';
}
} else {
$result['description'] = '<p>' . wp_kses(
sprintf(
/* translators: %1$s is the error code */
esc_html__( 'There was an error while retrieving the home page to analyze the blocking assets, with the error code %1$s and the following message:', 'performance-lab' ),
'<code>' . esc_html( (string) $response->get_error_code() ) . '</code>'
),
array( 'code' => array() )
) . '</p><blockquote>' . esc_html( $response->get_error_message() ) . '</blockquote>';
}
return $result;
}
/**
* Gets the size of the asset in bytes.
*
* @since 4.0.0
*
* @param string $resource_url URL of the resource.
* @return int|WP_Error Size of the asset in bytes or WP_Error if the request fails.
*/
function perflab_aea_get_asset_size( string $resource_url ) {
$response = wp_remote_get(
$resource_url,
array(
'timeout' => 10,
'headers' => perflab_get_http_basic_authorization_headers(),
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error(
'http_error',
wp_kses(
sprintf(
/* translators: %d is the HTTP status code, %s is the status header description */
__( 'Failed to retrieve the above asset with an HTTP status of <code>%1$d %2$s</code>.', 'performance-lab' ),
(int) wp_remote_retrieve_response_code( $response ),
esc_html( wp_remote_retrieve_response_message( $response ) )
),
array( 'code' => array() )
)
);
}
// TODO: A non-cacheable response should also be considered an error.
// TODO: A size of zero could be considered an error too.
return strlen( wp_remote_retrieve_body( $response ) );
}
/**
* Gets headers for HTTP Basic authorization headers.
*
* @since 4.0.0
*
* @return array{ Authorization?: non-empty-string } Headers with copied Basic auth headers.
*/
function perflab_get_http_basic_authorization_headers(): array {
$headers = array();
if ( isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) {
$user = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) );
$pass = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
$headers['Authorization'] = 'Basic ' . base64_encode( $user . ':' . $pass ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- base64_encode() is used here to encode the credentials for forwarding basic auth headers.
}
return $headers;
}
/**
* Generates a table of blocking assets.
*
* @since 4.0.0
*
* @phpstan-param array{
* scripts: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* styles: array<array{ src: string, size: int|null, error: WP_Error|null }>,
* } $blocking_assets
*
* @param array<string, mixed> $blocking_assets Array of blocking assets.
* @return string HTML table of blocking assets.
*/
function perflab_aea_generate_blocking_assets_table( array $blocking_assets ): string {
if ( 0 === count( $blocking_assets['scripts'] ) && 0 === count( $blocking_assets['styles'] ) ) {
return '';
}
$table = '<table class="wp-list-table widefat striped"><thead><tr>';
$table .= '<th scope="col">' . esc_html__( 'Type', 'performance-lab' ) . '</th>';
$table .= '<th scope="col">' . esc_html__( 'Source', 'performance-lab' ) . '</th>';
$table .= '<th scope="col">' . esc_html__( 'Size', 'performance-lab' ) . '</th>';
$table .= '<th scope="col">' . esc_html__( 'Status', 'performance-lab' ) . '</th>';
$table .= '</tr></thead><tbody>';
$asset_types = array(
'scripts' => __( 'Script', 'performance-lab' ),
'styles' => __( 'Style', 'performance-lab' ),
);
foreach ( $asset_types as $type => $label ) {
if ( isset( $blocking_assets[ $type ] ) && is_array( $blocking_assets[ $type ] ) ) {
foreach ( $blocking_assets[ $type ] as $asset ) {
$table .= is_wp_error( $asset['error'] ) ? '<tr style="background-color: #ffecec;">' : '<tr>';
$table .= '<td>' . esc_html( $label ) . '</td>';
$table .= '<td>' . esc_url( $asset['src'] );
if ( is_wp_error( $asset['error'] ) ) {
$table .= '<p>' . wp_kses( $asset['error']->get_error_message(), array( 'code' => array() ) ) . '</p>';
}
$table .= '</td>';
$table .= '<td>';
if ( is_int( $asset['size'] ) ) {
$table .= str_replace( ' ', ' ', (string) size_format( $asset['size'] ) );
} else {
$table .= esc_html__( 'N/A', 'performance-lab' );
}
$table .= '</td>';
$table .= '<td>';
if ( is_wp_error( $asset['error'] ) ) {
$table .= esc_html__( 'Error', 'performance-lab' );
} else {
$table .= esc_html__( 'OK', 'performance-lab' );
}
$table .= '</td>';
$table .= '</tr>';
}
}
}
$table .= '</tbody></table>';
return $table;
}