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

Load block support styles in the <head> for block themes. #38750

Merged
merged 8 commits into from
Feb 16, 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
12 changes: 2 additions & 10 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
}
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );

$style = "<style>.$class_name a{" . $link_color_declaration . ";}</style>\n";
$style = ".$class_name a{" . $link_color_declaration . ';}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit pick: we can do $style = ".$class_name a{$link_color_declaration;}";.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, that's a syntax error (having a ; next to a variable $link_color_declaration). Note that the existing string concatenation also has it in separate strings for the same reason.


// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
// Retrieve the opening tag of the first HTML element.
Expand All @@ -65,15 +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 );
}

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// See https://core.trac.wordpress.org/ticket/53494.
add_action(
'wp_footer',
function () use ( $style ) {
echo $style;
}
);
gutenberg_enqueue_block_support( $style );

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

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// See https://core.trac.wordpress.org/ticket/53494.
add_action(
'wp_footer',
function () use ( $style ) {
echo '<style>' . $style . '</style>';
}
);
gutenberg_enqueue_block_support( $style );

return $content;
}
Expand Down
26 changes: 26 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,29 @@ 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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function wp_add_inline_style, which is similar receives a styles string and outputs it and it receives the styles without "style" tag so I guess this function should also not include the style tag.

On another question would it make sense to use wp_add_inline_style in this function instead of wp_enqueue_scripts? So for example a plugin can remove our styles etc...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function wp_add_inline_style, which is similar receives a styles string and outputs it and it receives the styles without "style" tag so I guess this function should also not include the style tag.

Can do that.

On another question would it make sense to use wp_add_inline_style in this function instead of wp_enqueue_scripts? So for example a plugin can remove our styles etc...

I'm on the fence here. My first instinct is to do not change how this works because I don't know why we made it this way in the first place. This PR just fixes the issue with block themes enqueuing the block supports in the body when they should use the head.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3ed7cd0 consumers no longer need to provide the <style> tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a reserved block supports style handle. That doesn't output the link element but can be used for inline styles.

Maybe it's something we can explore for 6.0.

youknowriad marked this conversation as resolved.
Show resolved Hide resolved
$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";
}
);
}