Automattic\WooCommerce\Blocks\BlockTypes

ClassicTemplate::add_alignment_class_to_wrapper()publicWC 1.0

Get HTML markup with the right classes by attributes. This function appends the classname at the first element that have the class attribute. Based on the experience, all the wrapper elements have a class attribute.

Method of the class: ClassicTemplate{}

No Hooks.

Return

String. Rendered block type output.

Usage

$ClassicTemplate = new ClassicTemplate();
$ClassicTemplate->add_alignment_class_to_wrapper( $content, $block );
$content(string) (required)
Block content.
$block(array) (required)
Parsed block data.

ClassicTemplate::add_alignment_class_to_wrapper() code WC 8.7.0

public function add_alignment_class_to_wrapper( string $content, array $block ) {
	if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) {
		return $content;
	}

	$attributes = (array) $block['attrs'];

	// Set the default alignment to wide.
	if ( ! isset( $attributes['align'] ) ) {
		$attributes['align'] = 'wide';
	}

	$align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes );

	if ( ! isset( $align_class_and_style['class'] ) ) {
		return $content;
	}

	// Find the first tag.
	$first_tag = '<[^<>]+>';
	$matches   = array();
	preg_match( $first_tag, $content, $matches );

	// If there is a tag, but it doesn't have a class attribute, add the class attribute.
	if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) {
		$pattern_before_tag_closing = '/.+?(?=>)/';
		return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 );
	}

	// If there is a tag, and it has a class already, add the class attribute.
	$pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/';
	return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 );
}