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.
Built from https://develop.svn.wordpress.org/trunk@56682


git-svn-id: http://core.svn.wordpress.org/trunk@56194 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-25 17:06:34 +00:00
parent bfbcb02444
commit 72f19abd94
8 changed files with 183 additions and 68 deletions

View File

@ -60,7 +60,8 @@ if ( ! is_customize_preview() ) {
add_action( 'admin_print_scripts', 'print_emoji_detection_script' );
add_action( 'admin_print_scripts', 'print_head_scripts', 20 );
add_action( 'admin_print_footer_scripts', '_wp_footer_scripts' );
add_action( 'admin_print_styles', 'print_emoji_styles' );
add_action( 'admin_enqueue_scripts', 'wp_enqueue_emoji_styles' );
add_action( 'admin_print_styles', 'print_emoji_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_emoji_styles().
add_action( 'admin_print_styles', 'print_admin_styles', 20 );
add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' );

View File

@ -1225,32 +1225,51 @@ function wp_admin_bar_add_secondary_groups( $wp_admin_bar ) {
}
/**
* Prints style and scripts for the admin bar.
* Enqueues inline style to hide the admin bar when printing.
*
* @since 3.1.0
* @since 6.4.0
*/
function wp_admin_bar_header() {
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
<?php
function wp_enqueue_admin_bar_header_styles() {
// Back-compat for plugins that disable functionality by unhooking this action.
$action = is_admin() ? 'admin_head' : 'wp_head';
if ( ! has_action( $action, 'wp_admin_bar_header' ) ) {
return;
}
remove_action( $action, 'wp_admin_bar_header' );
wp_add_inline_style( 'admin-bar', '@media print { #wpadminbar { display:none; } }' );
}
/**
* Prints default admin bar callback.
* Enqueues inline bump styles to make room for the admin bar.
*
* @since 3.1.0
* @since 6.4.0
*/
function _admin_bar_bump_cb() {
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?> media="screen">
html { margin-top: 32px !important; }
@media screen and ( max-width: 782px ) {
html { margin-top: 46px !important; }
function wp_enqueue_admin_bar_bump_styles() {
if ( current_theme_supports( 'admin-bar' ) ) {
$admin_bar_args = get_theme_support( 'admin-bar' );
$header_callback = $admin_bar_args[0]['callback'];
}
</style>
<?php
if ( empty( $header_callback ) ) {
$header_callback = '_admin_bar_bump_cb';
}
if ( '_admin_bar_bump_cb' !== $header_callback ) {
return;
}
// Back-compat for plugins that disable functionality by unhooking this action.
if ( ! has_action( 'wp_head', $header_callback ) ) {
return;
}
remove_action( 'wp_head', $header_callback );
$css = '
@media screen { html { margin-top: 32px !important; } }
@media screen and ( max-width: 782px ) { html { margin-top: 46px !important; } }
';
wp_add_inline_style( 'admin-bar', $css );
}
/**

View File

@ -357,7 +357,8 @@ add_action( 'switch_theme', 'wp_clean_theme_json_cache' );
add_action( 'start_previewing_theme', 'wp_clean_theme_json_cache' );
add_action( 'after_switch_theme', '_wp_menus_changed' );
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
add_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' );
add_action( 'wp_print_styles', 'print_emoji_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_emoji_styles().
if ( isset( $_GET['replytocom'] ) ) {
add_filter( 'wp_robots', 'wp_robots_no_robots' );
@ -647,6 +648,9 @@ add_action( 'widgets_init', '_wp_block_theme_register_classic_sidebars', 1 );
// Don't remove. Wrong way to disable.
add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
add_action( 'admin_init', '_wp_admin_bar_init' );
add_action( 'wp_enqueue_scripts', 'wp_enqueue_admin_bar_bump_styles' );
add_action( 'wp_enqueue_scripts', 'wp_enqueue_admin_bar_header_styles' );
add_action( 'admin_enqueue_scripts', 'wp_enqueue_admin_bar_header_styles' );
add_action( 'before_signup_header', '_wp_admin_bar_init' );
add_action( 'activate_header', '_wp_admin_bar_init' );
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
@ -668,7 +672,8 @@ add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );
add_action( 'embed_head', 'enqueue_embed_scripts', 1 );
add_action( 'embed_head', 'print_emoji_detection_script' );
add_action( 'embed_head', 'print_embed_styles' );
add_action( 'embed_head', 'wp_enqueue_embed_styles', 9 );
add_action( 'embed_head', 'print_embed_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_embed_styles().
add_action( 'embed_head', 'wp_print_head_scripts', 20 );
add_action( 'embed_head', 'wp_print_styles', 20 );
add_action( 'embed_head', 'wp_robots' );

View File

@ -5871,6 +5871,92 @@ function _wp_theme_json_webfonts_handler() {
add_action( 'admin_init', $fn_generate_and_enqueue_editor_styles );
}
/**
* Prints the CSS in the embed iframe header.
*
* @since 4.4.0
* @deprecated 6.4.0 Use wp_enqueue_embed_styles() instead.
*/
function print_embed_styles() {
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_embed_styles' );
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
$suffix = SCRIPT_DEBUG ? '' : '.min';
?>
<style<?php echo $type_attr; ?>>
<?php echo file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ); ?>
</style>
<?php
}
/**
* Prints the important emoji-related styles.
*
* @since 4.2.0
* @deprecated 6.4.0 Use wp_enqueue_emoji_styles() instead.
*/
function print_emoji_styles() {
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_emoji_styles' );
static $printed = false;
if ( $printed ) {
return;
}
$printed = true;
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?>>
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 0.07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
<?php
}
/**
* Prints style and scripts for the admin bar.
*
* @since 3.1.0
* @deprecated 6.4.0 Use wp_enqueue_admin_bar_header_styles() instead.
*/
function wp_admin_bar_header() {
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_admin_bar_header_styles' );
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?> media="print">#wpadminbar { display:none; }</style>
<?php
}
/**
* Prints default admin bar callback.
*
* @since 3.1.0
* @deprecated 6.4.0 Use wp_enqueue_admin_bar_bump_styles() instead.
*/
function _admin_bar_bump_cb() {
_deprecated_function( __FUNCTION__, '6.4.0', 'wp_enqueue_admin_bar_bump_styles' );
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?> media="screen">
html { margin-top: 32px !important; }
@media screen and ( max-width: 782px ) {
html { margin-top: 46px !important; }
}
</style>
<?php
}
/**
* Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors.
*

View File

@ -1059,18 +1059,22 @@ function enqueue_embed_scripts() {
}
/**
* Prints the CSS in the embed iframe header.
* Enqueues the CSS in the embed iframe header.
*
* @since 4.4.0
* @since 6.4.0
*/
function print_embed_styles() {
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
$suffix = SCRIPT_DEBUG ? '' : '.min';
?>
<style<?php echo $type_attr; ?>>
<?php echo file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ); ?>
</style>
<?php
function wp_enqueue_embed_styles() {
// Back-compat for plugins that disable functionality by unhooking this action.
if ( ! has_action( 'embed_head', 'print_embed_styles' ) ) {
return;
}
remove_action( 'embed_head', 'print_embed_styles' );
$suffix = wp_scripts_get_suffix();
$handle = 'wp-embed-template';
wp_register_style( $handle, false );
wp_add_inline_style( $handle, file_get_contents( ABSPATH . WPINC . "/css/wp-embed-template$suffix.css" ) );
wp_enqueue_style( $handle );
}
/**

View File

@ -5858,24 +5858,20 @@ function wp_spaces_regexp() {
}
/**
* Prints the important emoji-related styles.
* Enqueues the important emoji-related styles.
*
* @since 4.2.0
* @since 6.4.0
*/
function print_emoji_styles() {
static $printed = false;
if ( $printed ) {
function wp_enqueue_emoji_styles() {
// Back-compat for plugins that disable functionality by unhooking this action.
$action = is_admin() ? 'admin_print_styles' : 'wp_print_styles';
if ( ! has_action( $action, 'print_emoji_styles' ) ) {
return;
}
remove_action( $action, 'print_emoji_styles' );
$printed = true;
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?>>
img.wp-smiley,
img.emoji {
$emoji_styles = '
img.wp-smiley, img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
@ -5885,9 +5881,11 @@ img.emoji {
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
<?php
}';
$handle = 'wp-emoji-styles';
wp_register_style( $handle, false );
wp_add_inline_style( $handle, $emoji_styles );
wp_enqueue_style( $handle );
}
/**

View File

@ -118,14 +118,8 @@ function the_block_template_skip_link() {
if ( ! $_wp_current_template_content ) {
return;
}
?>
<?php
/**
* Print the skip-link styles.
*/
?>
<style id="skip-link-styles">
$skip_link_styles = '
.skip-link.screen-reader-text {
border: 0;
clip: rect(1px,1px,1px,1px);
@ -154,9 +148,17 @@ function the_block_template_skip_link() {
top: 5px;
width: auto;
z-index: 100000;
}
</style>
<?php
}';
$handle = 'wp-block-template-skip-link';
/**
* Print the skip-link styles.
*/
wp_register_style( $handle, false );
wp_add_inline_style( $handle, $skip_link_styles );
wp_enqueue_style( $handle );
/**
* Print the skip-link script.
*/

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56681';
$wp_version = '6.4-alpha-56682';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.