From 54c4de13edfaf3b9fbc1b67d6ba384618614d59e Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 25 Sep 2023 17:04:41 +0000 Subject: [PATCH] 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 --- src/wp-admin/includes/admin-filters.php | 3 +- src/wp-includes/admin-bar.php | 55 ++++++++---- src/wp-includes/default-filters.php | 9 +- src/wp-includes/deprecated.php | 88 ++++++++++++++++++- src/wp-includes/embed.php | 24 ++--- src/wp-includes/formatting.php | 48 +++++----- src/wp-includes/theme-templates.php | 22 ++--- tests/phpunit/tests/blocks/editor.php | 2 + tests/phpunit/tests/oembed/template.php | 2 + .../theme/wpAddGlobalStylesForBlocks.php | 5 ++ 10 files changed, 191 insertions(+), 67 deletions(-) diff --git a/src/wp-admin/includes/admin-filters.php b/src/wp-admin/includes/admin-filters.php index 8e3da338c5..b5adb946cf 100644 --- a/src/wp-admin/includes/admin-filters.php +++ b/src/wp-admin/includes/admin-filters.php @@ -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' ); diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index ca35a56ae8..d58e9874ed 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -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"'; - ?> - media="print">#wpadminbar { display:none; } - - 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']; } - - + > + + + + > + 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; + } + + + media="print">#wpadminbar { display:none; } + + media="screen"> + html { margin-top: 32px !important; } + @media screen and ( max-width: 782px ) { + html { margin-top: 46px !important; } + } + + - > - - - -> -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; -} - - - - - 'Example', ); diff --git a/tests/phpunit/tests/oembed/template.php b/tests/phpunit/tests/oembed/template.php index fca6aadd95..12092ff669 100644 --- a/tests/phpunit/tests/oembed/template.php +++ b/tests/phpunit/tests/oembed/template.php @@ -10,6 +10,8 @@ class Tests_Embed_Template extends WP_UnitTestCase { global $wp_scripts; $wp_scripts = null; + + remove_action( 'wp_print_styles', 'print_emoji_styles' ); } public function tear_down() { diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index 02ae7e0253..b009ac7233 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -18,6 +18,11 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase { */ private $test_blocks = array(); + public function set_up() { + parent::set_up(); + remove_action( 'wp_print_styles', 'print_emoji_styles' ); + } + public function tear_down() { // Unregister test blocks. if ( ! empty( $this->test_blocks ) ) {