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

Layout block support: Append layout styles to post content in REST API requests #35413

Closed
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
31 changes: 31 additions & 0 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ function () use ( $style ) {
}
);

// For requests via the REST API, append the styles to the end of the content.
// This ensures that in situations where `wp_footer` is never called, the
// styles are still available. This fixes an issue where layout styles are
// otherwise not rendered for the Post Content block within the site editor.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
add_filter(
'wp_render_layout_support_styles',
function ( $layout_styles ) use ( $style ) {
return $layout_styles . '<style>' . $style . '</style>';
},
10,
1
);
}

ramonjd marked this conversation as resolved.
Show resolved Hide resolved
return $content;
}

Expand All @@ -164,6 +179,22 @@ function () use ( $style ) {
}
add_filter( 'render_block', 'gutenberg_render_layout_support_flag', 10, 2 );

/**
* Append the rendered layout styles to the end of post content, if registered.
*
* This is used to ensure that in responses via the REST API, where `wp_footer`
* is not called, that styles are still available for blocks in post content.
*
* @see gutenberg_render_layout_support_flag()
*
* @param string $content Rendered post content.
* @return string Post content with layout styles appended.
*/
function gutenberg_append_layout_support_to_the_content( $content ) {
return $content . apply_filters( 'wp_render_layout_support_styles', '' );
}
add_filter( 'the_content', 'gutenberg_append_layout_support_to_the_content', 10, 1 );

/**
* For themes without theme.json file, make sure
* to restore the inner div for the group block
Expand Down