Skip to content

Commit 54c4de1

Browse files
committed
Script Loader: Replace hardcoded output of style tags with calls to wp_add_inline_style.
In this commit, enhancements have been made by replacing manually constructed style tags with calls to `wp_add_inline_style`. Previously, numerous style tags were generated and output directly in the header, resulting in redundant code and bypassing the core's style enqueueing system. This approach made it challenging for third-party developers to manage and control the output of these style tags. To ensure backward compatibility, the following functions have been deprecated and replaced: - print_embed_styles - print_emoji_styles - wp_admin_bar_header - _admin_bar_bump_cb Backward compatibility shims have also been added, ensuring that if these functions were previously unhooked from there actions, they will continue to not output a style tag. However, for the following functions, conversion to use inline styles was not feasible due to the potential disruption it might cause by changing the style tag IDs, potentially breaking JavaScript functionality for a number of plugins in the repository: - custom-background - wp-custom These changes improve code maintainability and enhance the flexibility and control available to developers when managing style outputs within WordPress core. Props spacedmonkey, hlunter, westonruter, flixos90. Fixes #58775. git-svn-id: https://develop.svn.wordpress.org/trunk@56682 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8d8b843 commit 54c4de1

10 files changed

Lines changed: 192 additions & 68 deletions

File tree

src/wp-admin/includes/admin-filters.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
add_action( 'admin_print_scripts', 'print_emoji_detection_script' );
6161
add_action( 'admin_print_scripts', 'print_head_scripts', 20 );
6262
add_action( 'admin_print_footer_scripts', '_wp_footer_scripts' );
63-
add_action( 'admin_print_styles', 'print_emoji_styles' );
63+
add_action( 'admin_enqueue_scripts', 'wp_enqueue_emoji_styles' );
64+
add_action( 'admin_print_styles', 'print_emoji_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_emoji_styles().
6465
add_action( 'admin_print_styles', 'print_admin_styles', 20 );
6566

6667
add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' );

src/wp-includes/admin-bar.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,32 +1225,51 @@ function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
12251225
}
12261226

12271227
/**
1228-
* Prints style and scripts for the admin bar.
1228+
* Enqueues inline style to hide the admin bar when printing.
12291229
*
1230-
* @since 3.1.0
1230+
* @since 6.4.0
12311231
*/
1232-
function wp_admin_bar_header() {
1233-
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
1234-
?>
1235-
<style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
1236-
<?php
1232+
function wp_enqueue_admin_bar_header_styles() {
1233+
// Back-compat for plugins that disable functionality by unhooking this action.
1234+
$action = is_admin() ? 'admin_head' : 'wp_head';
1235+
if ( ! has_action( $action, 'wp_admin_bar_header' ) ) {
1236+
return;
1237+
}
1238+
remove_action( $action, 'wp_admin_bar_header' );
1239+
1240+
wp_add_inline_style( 'admin-bar', '@media print { #wpadminbar { display:none; } }' );
12371241
}
12381242

12391243
/**
1240-
* Prints default admin bar callback.
1244+
* Enqueues inline bump styles to make room for the admin bar.
12411245
*
1242-
* @since 3.1.0
1246+
* @since 6.4.0
12431247
*/
1244-
function _admin_bar_bump_cb() {
1245-
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
1246-
?>
1247-
<style<?php echo $type_attr; ?> media="screen">
1248-
html { margin-top: 32px !important; }
1249-
@media screen and ( max-width: 782px ) {
1250-
html { margin-top: 46px !important; }
1248+
function wp_enqueue_admin_bar_bump_styles() {
1249+
if ( current_theme_supports( 'admin-bar' ) ) {
1250+
$admin_bar_args = get_theme_support( 'admin-bar' );
1251+
$header_callback = $admin_bar_args[0]['callback'];
1252+
}
1253+
1254+
if ( empty( $header_callback ) ) {
1255+
$header_callback = '_admin_bar_bump_cb';
12511256
}
1252-
</style>
1253-
<?php
1257+
1258+
if ( '_admin_bar_bump_cb' !== $header_callback ) {
1259+
return;
1260+
}
1261+
1262+
// Back-compat for plugins that disable functionality by unhooking this action.
1263+
if ( ! has_action( 'wp_head', $header_callback ) ) {
1264+
return;
1265+
}
1266+
remove_action( 'wp_head', $header_callback );
1267+
1268+
$css = '
1269+
@media screen { html { margin-top: 32px !important; } }
1270+
@media screen and ( max-width: 782px ) { html { margin-top: 46px !important; } }
1271+
';
1272+
wp_add_inline_style( 'admin-bar', $css );
12541273
}
12551274

12561275
/**

src/wp-includes/default-filters.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@
357357
add_action( 'start_previewing_theme', 'wp_clean_theme_json_cache' );
358358
add_action( 'after_switch_theme', '_wp_menus_changed' );
359359
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
360-
add_action( 'wp_print_styles', 'print_emoji_styles' );
360+
add_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' );
361+
add_action( 'wp_print_styles', 'print_emoji_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_emoji_styles().
361362

362363
if ( isset( $_GET['replytocom'] ) ) {
363364
add_filter( 'wp_robots', 'wp_robots_no_robots' );
@@ -647,6 +648,9 @@
647648
// Don't remove. Wrong way to disable.
648649
add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
649650
add_action( 'admin_init', '_wp_admin_bar_init' );
651+
add_action( 'wp_enqueue_scripts', 'wp_enqueue_admin_bar_bump_styles' );
652+
add_action( 'wp_enqueue_scripts', 'wp_enqueue_admin_bar_header_styles' );
653+
add_action( 'admin_enqueue_scripts', 'wp_enqueue_admin_bar_header_styles' );
650654
add_action( 'before_signup_header', '_wp_admin_bar_init' );
651655
add_action( 'activate_header', '_wp_admin_bar_init' );
652656
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
@@ -668,7 +672,8 @@
668672

669673
add_action( 'embed_head', 'enqueue_embed_scripts', 1 );
670674
add_action( 'embed_head', 'print_emoji_detection_script' );
671-
add_action( 'embed_head', 'print_embed_styles' );
675+
add_action( 'embed_head', 'wp_enqueue_embed_styles', 9 );
676+
add_action( 'embed_head', 'print_embed_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_embed_styles().
672677
add_action( 'embed_head', 'wp_print_head_scripts', 20 );
673678
add_action( 'embed_head', 'wp_print_styles', 20 );
674679
add_action( 'embed_head', 'wp_robots' );

src/wp-includes/deprecated.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5871,6 +5871,92 @@ function _wp_theme_json_webfonts_handler() {
58715871
add_action( 'admin_init', $fn_generate_and_enqueue_editor_styles );
58725872
}
58735873

5874+
/**
5875+
* Prints the CSS in the embed iframe header.
5876+
*
5877+
* @since 4.4.0
5878+
* @deprecated 6.4.0 Use wp_enqueue_embed_styles() instead.
5879+
*/
5880+
function print_embed_styles() {
5881+
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_embed_styles' );
5882+
5883+
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
5884+
$suffix = SCRIPT_DEBUG ? '' : '.min';
5885+
?>
5886+
<style<?php echo $type_attr; ?>>
5887+
<?php echo file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ); ?>
5888+
</style>
5889+
<?php
5890+
}
5891+
5892+
/**
5893+
* Prints the important emoji-related styles.
5894+
*
5895+
* @since 4.2.0
5896+
* @deprecated 6.4.0 Use wp_enqueue_emoji_styles() instead.
5897+
*/
5898+
function print_emoji_styles() {
5899+
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_emoji_styles' );
5900+
static $printed = false;
5901+
5902+
if ( $printed ) {
5903+
return;
5904+
}
5905+
5906+
$printed = true;
5907+
5908+
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
5909+
?>
5910+
<style<?php echo $type_attr; ?>>
5911+
img.wp-smiley,
5912+
img.emoji {
5913+
display: inline !important;
5914+
border: none !important;
5915+
box-shadow: none !important;
5916+
height: 1em !important;
5917+
width: 1em !important;
5918+
margin: 0 0.07em !important;
5919+
vertical-align: -0.1em !important;
5920+
background: none !important;
5921+
padding: 0 !important;
5922+
}
5923+
</style>
5924+
<?php
5925+
}
5926+
5927+
/**
5928+
* Prints style and scripts for the admin bar.
5929+
*
5930+
* @since 3.1.0
5931+
* @deprecated 6.4.0 Use wp_enqueue_admin_bar_header_styles() instead.
5932+
*/
5933+
function wp_admin_bar_header() {
5934+
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_admin_bar_header_styles' );
5935+
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
5936+
?>
5937+
<style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
5938+
<?php
5939+
}
5940+
5941+
/**
5942+
* Prints default admin bar callback.
5943+
*
5944+
* @since 3.1.0
5945+
* @deprecated 6.4.0 Use wp_enqueue_admin_bar_bump_styles() instead.
5946+
*/
5947+
function _admin_bar_bump_cb() {
5948+
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_admin_bar_bump_styles' );
5949+
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
5950+
?>
5951+
<style<?php echo $type_attr; ?> media="screen">
5952+
html { margin-top: 32px !important; }
5953+
@media screen and ( max-width: 782px ) {
5954+
html { margin-top: 46px !important; }
5955+
}
5956+
</style>
5957+
<?php
5958+
}
5959+
58745960
/**
58755961
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors.
58765962
*
@@ -5907,4 +5993,4 @@ function wp_update_https_detection_errors() {
59075993
$support_errors = wp_get_https_detection_errors();
59085994

59095995
update_option( 'https_detection_errors', $support_errors );
5910-
}
5996+
}

src/wp-includes/embed.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,18 +1059,22 @@ function enqueue_embed_scripts() {
10591059
}
10601060

10611061
/**
1062-
* Prints the CSS in the embed iframe header.
1062+
* Enqueues the CSS in the embed iframe header.
10631063
*
1064-
* @since 4.4.0
1064+
* @since 6.4.0
10651065
*/
1066-
function print_embed_styles() {
1067-
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
1068-
$suffix = SCRIPT_DEBUG ? '' : '.min';
1069-
?>
1070-
<style<?php echo $type_attr; ?>>
1071-
<?php echo file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ); ?>
1072-
</style>
1073-
<?php
1066+
function wp_enqueue_embed_styles() {
1067+
// Back-compat for plugins that disable functionality by unhooking this action.
1068+
if ( ! has_action( 'embed_head', 'print_embed_styles' ) ) {
1069+
return;
1070+
}
1071+
remove_action( 'embed_head', 'print_embed_styles' );
1072+
1073+
$suffix = wp_scripts_get_suffix();
1074+
$handle = 'wp-embed-template';
1075+
wp_register_style( $handle, false );
1076+
wp_add_inline_style( $handle, file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ) );
1077+
wp_enqueue_style( $handle );
10741078
}
10751079

10761080
/**

src/wp-includes/formatting.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,36 +5858,34 @@ function wp_spaces_regexp() {
58585858
}
58595859

58605860
/**
5861-
* Prints the important emoji-related styles.
5861+
* Enqueues the important emoji-related styles.
58625862
*
5863-
* @since 4.2.0
5863+
* @since 6.4.0
58645864
*/
5865-
function print_emoji_styles() {
5866-
static $printed = false;
5867-
5868-
if ( $printed ) {
5865+
function wp_enqueue_emoji_styles() {
5866+
// Back-compat for plugins that disable functionality by unhooking this action.
5867+
$action = is_admin() ? 'admin_print_styles' : 'wp_print_styles';
5868+
if ( ! has_action( $action, 'print_emoji_styles' ) ) {
58695869
return;
58705870
}
5871-
5872-
$printed = true;
5873-
5874-
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
5875-
?>
5876-
<style<?php echo $type_attr; ?>>
5877-
img.wp-smiley,
5878-
img.emoji {
5879-
display: inline !important;
5880-
border: none !important;
5881-
box-shadow: none !important;
5882-
height: 1em !important;
5883-
width: 1em !important;
5884-
margin: 0 0.07em !important;
5885-
vertical-align: -0.1em !important;
5886-
background: none !important;
5887-
padding: 0 !important;
5888-
}
5889-
</style>
5890-
<?php
5871+
remove_action( $action, 'print_emoji_styles' );
5872+
5873+
$emoji_styles = '
5874+
img.wp-smiley, img.emoji {
5875+
display: inline !important;
5876+
border: none !important;
5877+
box-shadow: none !important;
5878+
height: 1em !important;
5879+
width: 1em !important;
5880+
margin: 0 0.07em !important;
5881+
vertical-align: -0.1em !important;
5882+
background: none !important;
5883+
padding: 0 !important;
5884+
}';
5885+
$handle = 'wp-emoji-styles';
5886+
wp_register_style( $handle, false );
5887+
wp_add_inline_style( $handle, $emoji_styles );
5888+
wp_enqueue_style( $handle );
58915889
}
58925890

58935891
/**

src/wp-includes/theme-templates.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,8 @@ function the_block_template_skip_link() {
118118
if ( ! $_wp_current_template_content ) {
119119
return;
120120
}
121-
?>
122121

123-
<?php
124-
/**
125-
* Print the skip-link styles.
126-
*/
127-
?>
128-
<style id="skip-link-styles">
122+
$skip_link_styles = '
129123
.skip-link.screen-reader-text {
130124
border: 0;
131125
clip: rect(1px,1px,1px,1px);
@@ -154,9 +148,17 @@ function the_block_template_skip_link() {
154148
top: 5px;
155149
width: auto;
156150
z-index: 100000;
157-
}
158-
</style>
159-
<?php
151+
}';
152+
153+
$handle = 'wp-block-template-skip-link';
154+
155+
/**
156+
* Print the skip-link styles.
157+
*/
158+
wp_register_style( $handle, false );
159+
wp_add_inline_style( $handle, $skip_link_styles );
160+
wp_enqueue_style( $handle );
161+
160162
/**
161163
* Print the skip-link script.
162164
*/

tests/phpunit/tests/blocks/editor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function set_up() {
1818

1919
parent::set_up();
2020

21+
remove_action( 'wp_print_styles', 'print_emoji_styles' );
22+
2123
$args = array(
2224
'post_title' => 'Example',
2325
);

tests/phpunit/tests/oembed/template.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public function set_up() {
1010

1111
global $wp_scripts;
1212
$wp_scripts = null;
13+
14+
remove_action( 'wp_print_styles', 'print_emoji_styles' );
1315
}
1416

1517
public function tear_down() {

tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase {
1818
*/
1919
private $test_blocks = array();
2020

21+
public function set_up() {
22+
parent::set_up();
23+
remove_action( 'wp_print_styles', 'print_emoji_styles' );
24+
}
25+
2126
public function tear_down() {
2227
// Unregister test blocks.
2328
if ( ! empty( $this->test_blocks ) ) {

0 commit comments

Comments
 (0)