wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_render_spacing_gap_support › WordPress Function

Since5.9.0
Deprecatedn/a
wp_render_spacing_gap_support ( $block_content, $block )
Access:
  • private
Parameters: (2)
  • (string) $block_content Rendered block content.
    Required: Yes
  • (array) $block Block object.
    Required: Yes
Returns:
  • (string) Filtered block content.
Defined at:
Codex:

Renders the spacing gap support to the block wrapper, to ensure that the CSS variable is rendered in all environments.



Source

function wp_render_spacing_gap_support( $block_content, $block ) {
	$block_type      = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
	$has_gap_support = block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
	if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) {
		return $block_content;
	}

	$gap_value = $block['attrs']['style']['spacing']['blockGap'];

	// Skip if gap value contains unsupported characters.
	// 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.
	if ( preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ) {
		return $block_content;
	}

	$style = sprintf(
		'--wp--style--block-gap: %s',
		esc_attr( $gap_value )
	);

	// Attempt to update an existing style attribute on the wrapper element.
	$injected_style = preg_replace(
		'/^([^>.]+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/',
		'$1$2' . $style . '; ',
		$block_content,
		1
	);

	// If there is no existing style attribute, add one to the wrapper element.
	if ( $injected_style === $block_content ) {
		$injected_style = preg_replace(
			'/<([a-zA-Z0-9]+)([ >])/',
			'<$1 style="' . $style . '"$2',
			$block_content,
			1
		);
	};

	return $injected_style;
}