Make WordPress Core

Changeset 52556 for trunk


Ignore:
Timestamp:
01/11/2022 03:57:09 PM (3 years ago)
Author:
hellofromTonya
Message:

Editor: Fix enqueueing additional styles in wp_enqueue_block_style() to print only when blocks render.

In a block theme, additional block styles registered using the wp_enqueue_block_style function should only get printed when the block exists on a page. However, they currently always get rendered.

This commit is a backport from Gutenberg that fixes the issue by printing the styles when a block renders.

Follow-up to [52069].

Props poena, aristath, Mamaduka.
Fixes #54787.

File:
1 edited

Legend:

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

    r52354 r52556  
    12551255    $hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
    12561256    if ( wp_should_load_separate_core_block_assets() ) {
    1257         $hook = "render_block_$block_name";
     1257        /**
     1258         * Callback function to register and enqueue styles.
     1259         *
     1260         * @param string $content The block content.
     1261         * @param array  $block   The full block, including name and attributes.
     1262         * @return string Block content.
     1263         */
     1264        $callback_separate = static function( $content, $block ) use ( $block_name, $callback ) {
     1265            if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
     1266                return $callback( $content );
     1267            }
     1268            return $content;
     1269        };
     1270
     1271        /*
     1272         * The filter's callback here is an anonymous function because
     1273         * using a named function in this case is not possible.
     1274         *
     1275         * The function cannot be unhooked, however, users are still able
     1276         * to dequeue the stylesheets registered/enqueued by the callback
     1277         * which is why in this case, using an anonymous function
     1278         * was deemed acceptable.
     1279         */
     1280        add_filter( 'render_block', $callback_separate, 10, 2 );
     1281        return;
    12581282    }
    12591283
Note: See TracChangeset for help on using the changeset viewer.