Skip to content

Commit da86fae

Browse files
committed
Performance: Use filesystem read instead of HTTP request for admin color scheme
Replace wp_remote_get() with file_get_contents() to read the local CSS file directly, avoiding unnecessary HTTP overhead on every front-end page load. Fixes #65080
1 parent 9f8a3b1 commit da86fae

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,20 +1466,28 @@ function wp_admin_bar_add_color_scheme_to_front_end() {
14661466
$url = $color->url ?? '';
14671467

14681468
if ( $url ) {
1469-
$response = wp_remote_get( $url );
1470-
if ( ! is_wp_error( $response ) ) {
1471-
$css = $response['body'];
1472-
if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) {
1473-
$start_position = strpos( $css, '#wpadminbar' );
1474-
$end_position = strpos( $css, '.wp-pointer' );
1475-
if ( false !== $end_position && $end_position > $start_position ) {
1476-
$css = substr( $css, $start_position, $end_position - $start_position );
1477-
if ( SCRIPT_DEBUG ) {
1478-
$css = str_replace( '/* Pointers */', '', $css );
1479-
}
1469+
// Convert URL to file path for local filesystem read.
1470+
if ( SCRIPT_DEBUG ) {
1471+
$css_file = ABSPATH . 'wp-admin/css/colors/' . $color_scheme . '/colors.css';
1472+
} else {
1473+
$css_file = ABSPATH . 'wp-admin/css/colors/' . $color_scheme . '/colors.min.css';
1474+
}
1475+
1476+
if ( ! file_exists( $css_file ) ) {
1477+
return;
1478+
}
1479+
1480+
$css = file_get_contents( $css_file );
1481+
if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) {
1482+
$start_position = strpos( $css, '#wpadminbar' );
1483+
$end_position = strpos( $css, '.wp-pointer' );
1484+
if ( false !== $end_position && $end_position > $start_position ) {
1485+
$css = substr( $css, $start_position, $end_position - $start_position );
1486+
if ( SCRIPT_DEBUG ) {
1487+
$css = str_replace( '/* Pointers */', '', $css );
14801488
}
1481-
wp_add_inline_style( 'admin-bar', $css );
14821489
}
1490+
wp_add_inline_style( 'admin-bar', $css );
14831491
}
14841492
}
14851493
}

0 commit comments

Comments
 (0)