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



wp_render_layout_support_flag › WordPress Function

Since5.8.0
Deprecatedn/a
wp_render_layout_support_flag ( $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 layout config to the block wrapper.



Source

function wp_render_layout_support_flag( $block_content, $block ) {
	$block_type     = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
	$support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );

	if ( ! $support_layout ) {
		return $block_content;
	}

	$block_gap             = wp_get_global_settings( array( 'spacing', 'blockGap' ) );
	$default_layout        = wp_get_global_settings( array( 'layout' ) );
	$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false;
	$default_block_layout  = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() );
	$used_layout           = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout;
	if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) {
		if ( ! $default_layout ) {
			return $block_content;
		}
		$used_layout = $default_layout;
	}

	$id    = uniqid();
	$style = wp_get_layout_style( ".wp-container-$id", $used_layout, $has_block_gap_support );
	// 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(
		'/' . 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',
		static function () use ( $style ) {
			echo '<style>' . $style . '</style>';
		}
	);

	return $content;
}