MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors

Blocks_Width_Preprocessor::preprocess()publicWC 1.0

Method to preprocess the content before rendering

Method of the class: Blocks_Width_Preprocessor{}

No Hooks.

Return

Array.

Usage

$Blocks_Width_Preprocessor = new Blocks_Width_Preprocessor();
$Blocks_Width_Preprocessor->preprocess( $parsed_blocks, $layout, $styles ): array;
$parsed_blocks(array) (required)
Parsed blocks of the email.
$layout(array) (required)
-
$styles(array) (required)
-

Blocks_Width_Preprocessor::preprocess() code WC 9.8.2

public function preprocess( array $parsed_blocks, array $layout, array $styles ): array {
	foreach ( $parsed_blocks as $key => $block ) {
		// Layout width is recalculated for each block because full-width blocks don't exclude padding.
		$layout_width = $this->parse_number_from_string_with_pixels( $layout['contentSize'] );
		$alignment    = $block['attrs']['align'] ?? null;
		// Subtract padding from the block width if it's not full-width.
		if ( 'full' !== $alignment ) {
			$layout_width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['left'] ?? '0px' );
			$layout_width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['right'] ?? '0px' );
		}

		$width_input = $block['attrs']['width'] ?? '100%';
		// Currently we support only % and px units in case only the number is provided we assume it's %
		// because editor saves percent values as a number.
		$width_input = is_numeric( $width_input ) ? "$width_input%" : $width_input;
		$width       = $this->convert_width_to_pixels( $width_input, $layout_width );

		if ( 'core/columns' === $block['blockName'] ) {
			// Calculate width of the columns based on the layout width and padding.
			$columns_width        = $layout_width;
			$columns_width       -= $this->parse_number_from_string_with_pixels( $block['attrs']['style']['spacing']['padding']['left'] ?? '0px' );
			$columns_width       -= $this->parse_number_from_string_with_pixels( $block['attrs']['style']['spacing']['padding']['right'] ?? '0px' );
			$border_width         = $block['attrs']['style']['border']['width'] ?? '0px';
			$columns_width       -= $this->parse_number_from_string_with_pixels( $block['attrs']['style']['border']['left']['width'] ?? $border_width );
			$columns_width       -= $this->parse_number_from_string_with_pixels( $block['attrs']['style']['border']['right']['width'] ?? $border_width );
			$block['innerBlocks'] = $this->add_missing_column_widths( $block['innerBlocks'], $columns_width );
		}

		// Copy layout styles and update width and padding.
		$modified_layout                                = $layout;
		$modified_layout['contentSize']                 = "{$width}px";
		$modified_styles                                = $styles;
		$modified_styles['spacing']['padding']['left']  = $block['attrs']['style']['spacing']['padding']['left'] ?? '0px';
		$modified_styles['spacing']['padding']['right'] = $block['attrs']['style']['spacing']['padding']['right'] ?? '0px';

		$block['email_attrs']['width'] = "{$width}px";
		$block['innerBlocks']          = $this->preprocess( $block['innerBlocks'], $modified_layout, $modified_styles );
		$parsed_blocks[ $key ]         = $block;
	}
	return $parsed_blocks;
}