Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extract to wp_enqueue_block_support
  • Loading branch information
oandregal committed Feb 16, 2022
commit 8cbc00ecf49d632428d01003edb96f344e3526c0
20 changes: 1 addition & 19 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +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 );
}

// Styles should be loaded in the head for block themes
// and classic themes by default.
//
// Only for classic themes that opt into loading separate block assets
// we should load the styles in the body.
//
// See https://core.trac.wordpress.org/ticket/53494.
$separate_assets = wp_should_load_separate_core_block_assets();
$is_classic_theme = ! wp_is_block_theme();
$action_hook_name = 'wp_enqueue_scripts';
if ( $is_classic_theme && $separate_assets ) {
$action_hook_name = 'wp_footer';
}
add_action(
$action_hook_name,
function () use ( $style ) {
echo $style;
}
);
wp_enqueue_block_support( $style );

return $content;
}
Expand Down
22 changes: 2 additions & 20 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here
// because we only want to match against the value, not the CSS attribute.
$gap_value = preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
$style = gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support, $gap_value );
$style = '<style>' . gutenberg_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support, $gap_value ) . '</style>';
// This assumes the hook only applies to blocks with a single wrapper.
// I think this is a reasonable limitation for that particular hook.
$content = preg_replace(
Expand All @@ -166,25 +166,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
1
);

// Styles should be loaded in the head for block themes
// and classic themes by default.
//
// Only for classic themes that opt into loading separate block assets
// we should load the styles in the body.
//
// See https://core.trac.wordpress.org/ticket/53494.
$separate_assets = wp_should_load_separate_core_block_assets();
$is_classic_theme = ! wp_is_block_theme();
$action_hook_name = 'wp_enqueue_scripts';
if ( $is_classic_theme && $separate_assets ) {
$action_hook_name = 'wp_footer';
}
add_action(
$action_hook_name,
function () use ( $style ) {
echo '<style>' . $style . '</style>';
}
);
wp_enqueue_block_support( $style );

return $content;
}
Expand Down
26 changes: 26 additions & 0 deletions lib/compat/wordpress-5.9/wp-enqueue-block-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

if ( ! function_exists( 'wp_enqueue_block_support' ) ) {
/**
* Styles should be loaded in the head for block themes.
*
* For classic ones we need to enqueue them to the body
* because the wp_head action (and wp_enqueue_scripts)
* happens before the render_block.
*
* See https://core.trac.wordpress.org/ticket/53494.
*/
function wp_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;
}
);
}
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/global-styles.php';
require __DIR__ . '/pwa.php';

require __DIR__ . '/compat/wordpress-5.9/wp-enqueue-block-support.php';
require __DIR__ . '/block-supports/elements.php';
require __DIR__ . '/block-supports/colors.php';
require __DIR__ . '/block-supports/typography.php';
Expand Down