Make WordPress Core

Changeset 52747


Ignore:
Timestamp:
02/17/2022 01:07:46 PM (3 years ago)
Author:
jorgefilipecosta
Message:

Script Loader: Load block support styles in the head for block themes.

The dynamic block styles for layout and elements should be loaded in the head for block themes. While that should also be the case for classic themes, the current mechanism we use (render_block) does not allow us to do that, hence, this PR doesn't change anything for them and will be loaded the body.

Props oandregal, youknowriad, wpsoul.
Merges [52741] to the 5.9 branch.
Fixes #55148.

Location:
branches/5.9
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.9

  • branches/5.9/src/wp-includes/block-supports/elements.php

    r52069 r52747  
    4747    $link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
    4848
    49     $style = "<style>.$class_name a{" . $link_color_declaration . ";}</style>\n";
     49    $style = ".$class_name a{" . $link_color_declaration . ';}';
    5050
    5151    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
     
    6969    }
    7070
    71     /*
    72      * Ideally styles should be loaded in the head, but blocks may be parsed
    73      * after that, so loading in the footer for now.
    74      * See https://core.trac.wordpress.org/ticket/53494.
    75      */
    76     add_action(
    77         'wp_footer',
    78         static function () use ( $style ) {
    79             echo $style;
    80         }
    81     );
     71    wp_enqueue_block_support( $style );
    8272
    8373    return $content;
  • branches/5.9/src/wp-includes/block-supports/layout.php

    r52434 r52747  
    176176    );
    177177
    178     /*
    179      * Ideally styles should be loaded in the head, but blocks may be parsed
    180      * after that, so loading in the footer for now.
    181      * See https://core.trac.wordpress.org/ticket/53494.
    182      */
    183     add_action(
    184         'wp_footer',
    185         static function () use ( $style ) {
    186             echo '<style>' . $style . '</style>';
    187         }
    188     );
     178    wp_enqueue_block_support( $style );
    189179
    190180    return $content;
  • branches/5.9/src/wp-includes/blocks.php

    r52560 r52747  
    13291329}
    13301330add_filter( 'block_type_metadata', '_wp_multiple_block_styles' );
     1331
     1332/**
     1333 * This function takes care of adding inline styles
     1334 * in the proper place, depending on the theme in use.
     1335 *
     1336 * For block themes, it's loaded in the head.
     1337 * For classic ones, it's loaded in the body
     1338 * because the wp_head action (and wp_enqueue_scripts)
     1339 * happens before the render_block.
     1340 *
     1341 * See https://core.trac.wordpress.org/ticket/53494.
     1342 *
     1343 * @param string $style String containing the CSS styles to be added.
     1344 */
     1345function wp_enqueue_block_support( $style ) {
     1346    $action_hook_name = 'wp_footer';
     1347    if ( wp_is_block_theme() ) {
     1348        $action_hook_name = 'wp_enqueue_scripts';
     1349    }
     1350    add_action(
     1351        $action_hook_name,
     1352        function () use ( $style ) {
     1353            echo "<style>$style</style>\n";
     1354        }
     1355    );
     1356}
Note: See TracChangeset for help on using the changeset viewer.