Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors
Spacing_Preprocessor::add_block_gaps
Adds spacing to blocks: margin-top for vertical gaps, horizontal padding for column gaps, and root padding for children of root-level containers.
Root padding is distributed from the outer email wrapper to individual block wrappers. Plain root-level containers (groups without post-content) delegate padding to their children instead of taking it themselves, so alignfull children can skip root padding and span the full email width.
A container that wraps post-content and has its own horizontal padding is a self-contained box: it takes the root padding as an inset itself, and its own padding is suppressed on the box and distributed to descendants as container padding (via a suppress-horizontal-padding flag). This lets full-width children break out of the box, and keeps the two paddings nesting (e.g. 30px outer +
24px own) instead of stacking on every block.
Method of the class: Spacing_Preprocessor{}
No Hooks.
Returns
Array.
Usage
// private - for code of main (parent) class only $result = $this->add_block_gaps( $parsed_blocks, $gap, $parent_block, $root_padding, $apply_root_padding, $container_padding, $variables_map, $gap_padding_side ): array;
- $parsed_blocks(array) (required)
- Parsed blocks.
- $gap(string)
- Gap.
Default:'' - $parent_block(array|null)
- Parent block.
Default:null - $root_padding(array)
- Root horizontal padding with
'left'and'right'keys.
Default:array() - $apply_root_padding(true|false)
- Whether this block should receive root padding (delegated by parent container).
Default:false - $container_padding(array)
- Container horizontal padding with
'left'and'right'keys.
Default:array() - $variables_map(array)
- Map of CSS variable names to resolved values for preset resolution.
Default:array() - $gap_padding_side(string)
- Physical padding side for generated column gaps.
Default:'padding-left'
Spacing_Preprocessor::add_block_gaps() Spacing Preprocessor::add block gaps code WC 11.0.1
private function add_block_gaps( array $parsed_blocks, string $gap = '', $parent_block = null, array $root_padding = array(), bool $apply_root_padding = false, array $container_padding = array(), array $variables_map = array(), string $gap_padding_side = 'padding-left' ): array {
foreach ( $parsed_blocks as $key => $block ) {
$block_name = $block['blockName'] ?? '';
$parent_block_name = $parent_block['blockName'] ?? '';
// Ensure that email_attrs are set.
$block['email_attrs'] = $block['email_attrs'] ?? array();
/**
* Do not add a gap to:
* - first child
* - parent block is a buttons block (where buttons are side by side).
*/
if ( 0 !== $key && $gap && 'core/buttons' !== $parent_block_name ) {
$block['email_attrs']['margin-top'] = $gap;
}
// Handle horizontal gap for columns: apply physical padding to column children (except the first).
// Only an explicitly defined column blockGap is applied. We intentionally do not
// derive it from the global (vertical) block spacing.
if ( 'core/columns' === $parent_block_name && 0 !== $key && null !== $parent_block ) {
$columns_gap = $this->get_columns_block_gap( $parent_block );
if ( $columns_gap ) {
$block['email_attrs'][ $gap_padding_side ] = $columns_gap;
}
}
// Distribute horizontal padding.
//
// A container that wraps post-content AND has its own horizontal padding
// is a self-contained box: it takes the root padding as an inset itself,
// while its own padding is suppressed on the box and distributed to
// descendants as container padding (so full-width children can still
// break out of it). Because the box is inset, post-content ends up
// narrower than contentSize — the signal Content_Renderer uses to drop
// root padding for the user blocks in the second pass. Without the inset,
// the two paddings would stack on every block (e.g. 30px + 24px = 54px).
$is_root_level = null === $parent_block;
$is_container = in_array( $block_name, self::CONTAINER_BLOCKS, true );
$alignment = $block['attrs']['align'] ?? null;
$has_zero_padding = $this->has_zero_horizontal_padding( $block );
$has_own_padding = $this->has_explicit_horizontal_padding( $block );
$post_content_block_names = $this->get_post_content_block_names();
$is_post_content = in_array( $block_name, $post_content_block_names, true );
$wraps_post_content = $is_container && $this->contains_post_content( $block );
$is_box = $wraps_post_content && $has_own_padding && ! $has_zero_padding;
// A delegator passes padding down to its children instead of taking it,
// so each child is inset on its own and full-width children can break out.
$delegates = ! $is_box && (
( $is_root_level && $is_container && ! $has_own_padding )
|| ( $apply_root_padding && $is_post_content )
|| $wraps_post_content
);
// Everything else applies the padding to itself, except full-width and
// explicitly zero-padded blocks.
$is_recipient = ! $delegates && ! $is_post_content && ! $has_zero_padding && 'full' !== $alignment;
if ( $is_recipient && ( $apply_root_padding || $is_root_level ) && ! empty( $root_padding ) ) {
$block['email_attrs']['root-padding-left'] = $root_padding['left'];
$block['email_attrs']['root-padding-right'] = $root_padding['right'];
}
$applied_container = $is_recipient && ! empty( $container_padding );
if ( $applied_container ) {
$block['email_attrs']['container-padding-left'] = $container_padding['left'];
$block['email_attrs']['container-padding-right'] = $container_padding['right'];
}
// Pass padding on to the children. Container padding keeps flowing down
// until a block applies it, then stops — so a nested block (e.g. an image
// inside a column) doesn't get it a second time.
$children_container_pad = $applied_container ? array() : $container_padding;
if ( $is_box ) {
$block_padding = $this->get_block_horizontal_padding( $block, $variables_map );
if ( ! empty( $block_padding ) ) {
$children_container_pad = $block_padding;
$block['email_attrs']['suppress-horizontal-padding'] = true;
}
}
$block['innerBlocks'] = $this->add_block_gaps( $block['innerBlocks'] ?? array(), $gap, $block, $root_padding, $delegates, $children_container_pad, $variables_map, $gap_padding_side );
$parsed_blocks[ $key ] = $block;
}
return $parsed_blocks;
}