Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Supports: Load Styles on <head> backport to core. #38880

Merged
merged 6 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
}

gutenberg_enqueue_block_support( $style );
gutenberg_enqueue_block_support_styles( $style );

return $content;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
1
);

gutenberg_enqueue_block_support( $style );
gutenberg_enqueue_block_support_styles( $style );

return $content;
}
Expand Down
26 changes: 0 additions & 26 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,29 +590,3 @@ function gutenberg_multiple_block_styles( $metadata ) {
return $metadata;
}
add_filter( 'block_type_metadata', 'gutenberg_multiple_block_styles' );

/**
* This function takes care of adding inline styles
* in the proper place, depending on the theme in use.
*
* For block themes, it's loaded in the head.
* For classic ones, it's loaded in the body
* because the wp_head action (and wp_enqueue_scripts)
* happens before the render_block.
*
* See https://core.trac.wordpress.org/ticket/53494.
*
* @param string $style String containing the CSS styles to be added.
*/
function gutenberg_enqueue_block_support( $style ) {
$action_hook_name = 'wp_footer';
if ( wp_is_block_theme() ) {
$action_hook_name = 'wp_enqueue_scripts';
}
add_action(
$action_hook_name,
function () use ( $style ) {
echo "<style>$style</style>\n";
}
);
}
26 changes: 26 additions & 0 deletions lib/compat/wordpress-5.9/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,29 @@ function gutenberg_enqueue_global_styles_assets() {
}
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_assets' );
add_action( 'wp_footer', 'gutenberg_enqueue_global_styles_assets' );

/**
* This function takes care of adding inline styles
* in the proper place, depending on the theme in use.
*
* For block themes, it's loaded in the head.
* For classic ones, it's loaded in the body
* because the wp_head action (and wp_enqueue_scripts)
* happens before the render_block.
*
* @link https://core.trac.wordpress.org/ticket/53494.
*
* @param string $style String containing the CSS styles to be added.
*/
function gutenberg_enqueue_block_support_styles( $style ) {
$action_hook_name = 'wp_footer';
if ( wp_is_block_theme() ) {
$action_hook_name = 'wp_enqueue_scripts';
}
add_action(
$action_hook_name,
static function () use ( $style ) {
echo "<style>$style</style>\n";
}
);
}