Skip to content

Commit

Permalink
Move blockGap support to spacing.php
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Aug 16, 2021
1 parent c1a740b commit c3f2081
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
54 changes: 0 additions & 54 deletions lib/block-supports/dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) {
// Height support to be added in near future.
// Width support to be added in near future.

if ( $has_gap_support ) {
$gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null );

if ( is_string( $gap_value ) ) {
$styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value );
}
}

return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
}

Expand All @@ -80,50 +72,6 @@ function gutenberg_skip_dimensions_serialization( $block_type ) {
$dimensions_support['__experimentalSkipSerialization'];
}

/**
* Renders the dimensions support to the block wrapper, for supports that
* require block-level server-side rendering, for example blockGap support
* which uses CSS variables.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_render_dimensions_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) {
return $block_content;
}

$id = uniqid();
$style = sprintf(
'.wp-container-%s { --wp--style--block-gap: %s; }',
$id,
esc_attr( $block['attrs']['style']['spacing']['blockGap'] )
);

// This assumes the hook only applies to blocks with a single wrapper.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="wp-container-' . $id . ' ',
$block_content,
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>';
}
);

return $content;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'dimensions',
Expand All @@ -132,5 +80,3 @@ function () use ( $style ) {
'apply' => 'gutenberg_apply_dimensions_support',
)
);

add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 );
56 changes: 56 additions & 0 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {

$has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false );
$has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false );
$has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
$styles = array();

if ( $has_padding_support ) {
Expand All @@ -71,6 +72,14 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
}
}

if ( $has_gap_support ) {
$gap_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'blockGap' ), null );

if ( is_string( $gap_value ) ) {
$styles[] = sprintf( '--wp--style--block-gap: %s', $gap_value );
}
}

return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
}

Expand All @@ -90,6 +99,51 @@ function gutenberg_skip_spacing_serialization( $block_type ) {
$spacing_support['__experimentalSkipSerialization'];
}


/**
* Renders the spacing support to the block wrapper, for supports that
* require block-level server-side rendering, for example blockGap support
* which uses CSS variables.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_render_spacing_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$has_gap_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) {
return $block_content;
}

$id = uniqid();
$style = sprintf(
'.wp-container-%s { --wp--style--block-gap: %s; }',
$id,
esc_attr( $block['attrs']['style']['spacing']['blockGap'] )
);

// This assumes the hook only applies to blocks with a single wrapper.
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="wp-container-' . $id . ' ',
$block_content,
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>';
}
);

return $content;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'spacing',
Expand All @@ -98,3 +152,5 @@ function gutenberg_skip_spacing_serialization( $block_type ) {
'apply' => 'gutenberg_apply_spacing_support',
)
);

add_filter( 'render_block', 'gutenberg_render_spacing_support', 10, 2 );

0 comments on commit c3f2081

Please sign in to comment.