Skip to content

Commit 421ac36

Browse files
committed
Toolbar: add CSS from admin color scheme on front-end.
This changeset introduces the `wp_admin_bar_add_color_scheme_to_front_end()` which is hooked on `admin_bar_init` in order to use the CSS from admin color schemes on the admin bar on front-end, as inline styles. Props sabernhardt, huzaifaalmesbah, audrasjb, johnbillion, noruzzaman, JeffPaul, joedolson, huzaifaalmesbah, amesplant, r1k0, shailu25. Fixes #64762. git-svn-id: https://develop.svn.wordpress.org/trunk@62025 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 474a396 commit 421ac36

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/wp-includes/admin-bar.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,3 +1437,49 @@ function _get_admin_bar_pref( $context = 'front', $user = 0 ) {
14371437

14381438
return 'true' === $pref;
14391439
}
1440+
1441+
/**
1442+
* Adds CSS from the administration color scheme stylesheet on the front end.
1443+
*
1444+
* @since 7.0.0
1445+
*
1446+
* @global array $_wp_admin_css_colors Registered administration color schemes.
1447+
*/
1448+
function wp_admin_bar_add_color_scheme_to_front_end() {
1449+
if ( is_admin() ) {
1450+
return;
1451+
}
1452+
1453+
global $_wp_admin_css_colors;
1454+
1455+
if ( empty( $_wp_admin_css_colors ) ) {
1456+
register_admin_color_schemes();
1457+
}
1458+
1459+
$color_scheme = get_user_option( 'admin_color' );
1460+
1461+
if ( empty( $color_scheme ) || ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
1462+
$color_scheme = 'modern';
1463+
}
1464+
1465+
$color = $_wp_admin_css_colors[ $color_scheme ] ?? null;
1466+
$url = $color->url ?? '';
1467+
1468+
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+
}
1480+
}
1481+
wp_add_inline_style( 'admin-bar', $css );
1482+
}
1483+
}
1484+
}
1485+
}

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@
708708
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
709709
add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`.
710710
add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
711+
add_action( 'admin_bar_init', 'wp_admin_bar_add_color_scheme_to_front_end', 0 );
711712

712713
// Former admin filters that can also be hooked on the front end.
713714
add_action( 'media_buttons', 'media_buttons' );

tests/phpunit/tests/dependencies/wpStyleLoaderSrc.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ class Tests_Dependencies_wpStyleLoaderSrc extends WP_UnitTestCase {
1212

1313
/**
1414
* Tests that PHP warnings are not thrown when wp_style_loader_src() is called
15-
* before the `$_wp_admin_css_colors` global is set.
15+
* before the `$_wp_admin_css_colors` global is set within the admin.
1616
*
1717
* The warnings that we should not see:
1818
* `Warning: Trying to access array offset on null`.
1919
* `Warning: Attempt to read property "url" on null`.
2020
*
2121
* @ticket 61302
22+
* @ticket 64762
2223
*/
2324
public function test_without_wp_admin_css_colors_global() {
24-
$this->assertFalse( wp_style_loader_src( '', 'colors' ) );
25+
if ( is_admin() ) {
26+
$this->assertFalse( wp_style_loader_src( '', 'colors' ) );
27+
} else {
28+
$this->assertStringContainsString( '/colors.css', wp_style_loader_src( '', 'colors' ) );
29+
}
2530
}
2631
}

0 commit comments

Comments
 (0)