Make WordPress Core


Ignore:
Timestamp:
09/25/2023 05:04:41 PM (2 years ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/admin-bar.php

    r56548 r56682  
    12261226
    12271227/**
    1228  * Prints style and scripts for the admin bar.
    1229  *
    1230  * @since 3.1.0
    1231  */
    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
    1237 }
    1238 
    1239 /**
    1240  * Prints default admin bar callback.
    1241  *
    1242  * @since 3.1.0
    1243  */
    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; }
    1251     }
    1252 </style>
    1253     <?php
     1228 * Enqueues inline style to hide the admin bar when printing.
     1229 *
     1230 * @since 6.4.0
     1231 */
     1232function 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; } }' );
     1241}
     1242
     1243/**
     1244 * Enqueues inline bump styles to make room for the admin bar.
     1245 *
     1246 * @since 6.4.0
     1247 */
     1248function 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';
     1256    }
     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
Note: See TracChangeset for help on using the changeset viewer.