MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Layout

Flex_Layout_Renderer::compute_widths_for_flex_layout()privateWC 1.0

Compute widths for blocks in flex layout.

Method of the class: Flex_Layout_Renderer{}

No Hooks.

Return

Array.

Usage

// private - for code of main (parent) class only
$result = $this->compute_widths_for_flex_layout( $parsed_block, $settings_controller, $flex_gap ): array;
$parsed_block(array) (required)
Parsed block.
$settings_controller(Settings_Controller) (required)
Settings controller.
$flex_gap(float) (required)
Flex gap.

Flex_Layout_Renderer::compute_widths_for_flex_layout() code WC 9.8.1

private function compute_widths_for_flex_layout( array $parsed_block, Settings_Controller $settings_controller, float $flex_gap ): array {
	// When there is no parent width we can't compute widths so auto width will be used.
	if ( ! isset( $parsed_block['email_attrs']['width'] ) ) {
		return $parsed_block['innerBlocks'] ?? array();
	}
	$blocks_count     = count( $parsed_block['innerBlocks'] );
	$total_used_width = 0; // Total width assuming items without set width would consume proportional width.
	$parent_width     = $settings_controller->parse_number_from_string_with_pixels( $parsed_block['email_attrs']['width'] );
	$inner_blocks     = $parsed_block['innerBlocks'] ?? array();

	foreach ( $inner_blocks as $key => $block ) {
		$block_width_percent = ( $block['attrs']['width'] ?? 0 ) ? intval( $block['attrs']['width'] ) : 0;
		$block_width         = floor( $parent_width * ( $block_width_percent / 100 ) );
		// If width is not set, we assume it's 25% of the parent width.
		$total_used_width += $block_width ? $block_width : floor( $parent_width * ( 25 / 100 ) );

		if ( ! $block_width ) {
			$inner_blocks[ $key ]['email_attrs']['layout_width'] = null; // Will be rendered as auto.
			continue;
		}
		$inner_blocks[ $key ]['email_attrs']['layout_width'] = $this->get_width_without_gap( $block_width, $flex_gap, $block_width_percent ) . 'px';
	}

	// When there is only one block, or percentage is set reasonably we don't need to adjust and just render as set by user.
	if ( $blocks_count <= 1 || ( $total_used_width <= $parent_width ) ) {
		return $inner_blocks;
	}

	foreach ( $inner_blocks as $key => $block ) {
		$proportional_space_overflow   = $parent_width / $total_used_width;
		$block_width                   = $block['email_attrs']['layout_width'] ? $settings_controller->parse_number_from_string_with_pixels( $block['email_attrs']['layout_width'] ) : 0;
		$block_proportional_width      = $block_width * $proportional_space_overflow;
		$block_proportional_percentage = ( $block_proportional_width / $parent_width ) * 100;
		$inner_blocks[ $key ]['email_attrs']['layout_width'] = $block_width ? $this->get_width_without_gap( $block_proportional_width, $flex_gap, $block_proportional_percentage ) . 'px' : null;
	}
	return $inner_blocks;
}