Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Layout
Flex_Layout_Renderer::compute_widths_for_flex_layout
Compute widths for blocks in flex layout.
Method of the class: Flex_Layout_Renderer{}
No Hooks.
Returns
Array.
Usage
// private - for code of main (parent) class only $result = $this->compute_widths_for_flex_layout( $parsed_block, $flex_gap ): array;
- $parsed_block(array) (required)
- Parsed block.
- $flex_gap(float) (required)
- Flex gap.
Flex_Layout_Renderer::compute_widths_for_flex_layout() Flex Layout Renderer::compute widths for flex layout code WC 10.6.2
private function compute_widths_for_flex_layout( array $parsed_block, 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 = Styles_Helper::parse_value( $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'] ? Styles_Helper::parse_value( $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;
}