diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index b9c7872d0cc07..22f610264caf1 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -1443,9 +1443,9 @@ function _get_admin_bar_pref( $context = 'front', $user = 0 ) { * * @since 7.0.0 * - * @global array $_wp_admin_css_colors Registered administration color schemes. + * @global array $_wp_admin_css_colors Registered administration color schemes. */ -function wp_admin_bar_add_color_scheme_to_front_end() { +function wp_admin_bar_add_color_scheme_to_front_end(): void { if ( is_admin() ) { return; } @@ -1463,23 +1463,41 @@ function wp_admin_bar_add_color_scheme_to_front_end() { } $color = $_wp_admin_css_colors[ $color_scheme ] ?? null; - $url = $color->url ?? ''; - - if ( $url ) { - $response = wp_remote_get( $url ); - if ( ! is_wp_error( $response ) ) { - $css = $response['body']; - if ( is_string( $css ) && str_contains( $css, '#wpadminbar' ) ) { - $start_position = strpos( $css, '#wpadminbar' ); - $end_position = strpos( $css, '.wp-pointer' ); - if ( false !== $end_position && $end_position > $start_position ) { - $css = substr( $css, $start_position, $end_position - $start_position ); - if ( SCRIPT_DEBUG ) { - $css = str_replace( '/* Pointers */', '', $css ); - } - } - wp_add_inline_style( 'admin-bar', $css ); - } + if ( ! is_object( $color ) || ! isset( $color->url ) || ! is_string( $color->url ) ) { + return; + } + + $path = wp_parse_url( $color->url, PHP_URL_PATH ); + if ( ! $path || ! str_ends_with( $path, '.css' ) ) { + return; + } + + $css_admin_relative_path = strstr( $path, '/wp-admin/' ); + if ( false === $css_admin_relative_path ) { + return; + } + + $css_path = untrailingslashit( ABSPATH ) . $css_admin_relative_path; + if ( ! is_readable( $css_path ) ) { + return; + } + + $css = file_get_contents( $css_path ); + if ( false === $css ) { + return; + } + + $start_position = strpos( $css, '#wpadminbar' ); + if ( false === $start_position ) { + return; + } + + $end_position = strpos( $css, '.wp-pointer' ); + if ( false !== $end_position && $end_position > $start_position ) { + $css = substr( $css, $start_position, $end_position - $start_position ); + if ( SCRIPT_DEBUG ) { + $css = str_replace( '/* Pointers */', '', $css ); } } + wp_add_inline_style( 'admin-bar', $css ); } diff --git a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php index 170129580a3e7..e7b865bdf060c 100644 --- a/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php +++ b/tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php @@ -10,6 +10,26 @@ */ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase { + public ?WP_Styles $original_styles; + + public ?WP_Screen $original_screen; + + public function set_up(): void { + global $wp_styles, $current_screen; + parent::set_up(); + + $this->original_styles = $wp_styles; + $this->original_screen = $current_screen; + unset( $wp_styles, $current_screen ); + } + + public function tear_down(): void { + global $wp_styles, $current_screen; + $wp_styles = $this->original_styles; + $current_screen = $this->original_screen; + parent::tear_down(); + } + /** * Tests that PHP warnings are not thrown when wp_style_loader_src() is called * before the `$_wp_admin_css_colors` global is set within the admin. @@ -20,12 +40,41 @@ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase { * * @ticket 61302 * @ticket 64762 + * + * @covers ::wp_admin_bar_add_color_scheme_to_front_end + */ + public function test_without_wp_admin_css_colors_global_frontend(): void { + unset( $GLOBALS['current_screen'] ); + $this->assertFalse( is_admin(), 'Expected not admin.' ); + wp_styles(); + wp_admin_bar_add_color_scheme_to_front_end(); + $inline_styles = wp_styles()->get_data( 'admin-bar', 'after' ); + $this->assertIsArray( $inline_styles, 'Expected inline styles to be added.' ); + $inline_css = implode( "\n", $inline_styles ); + $this->assertStringContainsString( '#wpadminbar', $inline_css ); + $this->assertStringNotContainsString( '/* Pointers */', $inline_css ); + $this->assertStringNotContainsString( '.wp-pointer', $inline_css ); + + $style_loader_src = wp_style_loader_src( '', 'colors' ); + $this->assertIsString( $style_loader_src ); + $this->assertStringContainsString( '/colors.css', $style_loader_src ); + } + + /** + * Tests that nothing is done when in the admin. + * + * @ticket 61302 + * @ticket 64762 + * + * @covers ::wp_admin_bar_add_color_scheme_to_front_end */ - public function test_without_wp_admin_css_colors_global() { - if ( is_admin() ) { - $this->assertFalse( wp_style_loader_src( '', 'colors' ) ); - } else { - $this->assertStringContainsString( '/colors.css', wp_style_loader_src( '', 'colors' ) ); - } + public function test_without_wp_admin_css_colors_global_admin(): void { + global $wp_styles; + set_current_screen( 'index.php' ); + $wp_styles = null; + $this->assertTrue( is_admin(), 'Expected admin.' ); + wp_styles(); + wp_admin_bar_add_color_scheme_to_front_end(); + $this->assertFalse( wp_styles()->get_data( 'admin-bar', 'after' ), 'Expected no inline style to be added in the admin.' ); } }