Skip to content

Commit

Permalink
now works for the editor too!
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Jun 9, 2021
1 parent 384e64b commit 74bab5a
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ function gutenberg_migrate_old_typography_shape( $metadata ) {
* @param bool $register Whether the stylesheet should be registered.
* If set to false, then assume it has already been registered,
* and only enqueue it.
*
* @return void
*/
function gutenberg_enqueue_block_style( $block_name, $args, $register = true ) {
$args = wp_parse_args(
Expand All @@ -497,34 +499,42 @@ function gutenberg_enqueue_block_style( $block_name, $args, $register = true ) {
)
);

// Register the stylesheet.
if ( $register ) {
wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
}
/**
* Callback function to register and enqueue styles.
*
* @param string $content When the callback is used for the render_block filter,
* the content needs to be returned so the function parameter
* is to ensure the content exists.
*
* @return string
*/
$callback = function( $content ) use ( $args, $register ) {
// Register the stylesheet.
if ( $register ) {
wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
}

// Add `path` data if provided.
if ( isset( $args['path'] ) ) {
wp_style_add_data( $args['handle'], 'path', $args['path'] );
}
// Add `path` data if provided.
if ( isset( $args['path'] ) ) {
wp_style_add_data( $args['handle'], 'path', $args['path'] );
}

// Enqueue asset.
if ( ! gutenberg_should_load_separate_block_assets() ) {
// Enqueue the stylesheet.
wp_enqueue_style( $args['handle'] );
} else {
add_filter(
"render_block_$block_name",
/**
* Filters the content of a single block.
*
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
*/
function( $block_content ) use ( $args ) {
wp_enqueue_style( $args['handle'] );
return $block_content;
}
);

return $content;
};

$hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
if ( gutenberg_should_load_separate_block_assets() ) {
$hook = "render_block_$block_name";
}

// Enqueue assets in the frontend.
add_filter( $hook, $callback );

// Enqueue assets in the editor.
add_action( 'enqueue_block_editor_assets', $callback );
}

/**
Expand Down

0 comments on commit 74bab5a

Please sign in to comment.