MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors

Blocks_Width_Preprocessor::add_missing_column_widths()privateWC 1.0

Add missing column widths

Method of the class: Blocks_Width_Preprocessor{}

No Hooks.

Return

Array.

Usage

// private - for code of main (parent) class only
$result = $this->add_missing_column_widths( $columns, $columns_width ): array;
$columns(array) (required)
Columns.
$columns_width(float) (required)
Columns width.

Blocks_Width_Preprocessor::add_missing_column_widths() code WC 9.8.1

private function add_missing_column_widths( array $columns, float $columns_width ): array {
	$columns_count_with_defined_width = 0;
	$defined_column_width             = 0;
	$columns_count                    = count( $columns );
	foreach ( $columns as $column ) {
		if ( isset( $column['attrs']['width'] ) && ! empty( $column['attrs']['width'] ) ) {
			++$columns_count_with_defined_width;
			$defined_column_width += $this->convert_width_to_pixels( $column['attrs']['width'], $columns_width );
		} else {
			// When width is not set we need to add padding to the defined column width for better ratio accuracy.
			$defined_column_width += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['spacing']['padding']['left'] ?? '0px' );
			$defined_column_width += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['spacing']['padding']['right'] ?? '0px' );
			$border_width          = $column['attrs']['style']['border']['width'] ?? '0px';
			$defined_column_width += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['border']['left']['width'] ?? $border_width );
			$defined_column_width += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['border']['right']['width'] ?? $border_width );
		}
	}

	if ( $columns_count - $columns_count_with_defined_width > 0 ) {
		$default_columns_width = round( ( $columns_width - $defined_column_width ) / ( $columns_count - $columns_count_with_defined_width ), 2 );
		foreach ( $columns as $key => $column ) {
			if ( ! isset( $column['attrs']['width'] ) || empty( $column['attrs']['width'] ) ) {
				// Add padding to the specific column width because it's not included in the default width.
				$column_width                      = $default_columns_width;
				$column_width                     += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['spacing']['padding']['left'] ?? '0px' );
				$column_width                     += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['spacing']['padding']['right'] ?? '0px' );
				$border_width                      = $column['attrs']['style']['border']['width'] ?? '0px';
				$column_width                     += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['border']['left']['width'] ?? $border_width );
				$column_width                     += $this->parse_number_from_string_with_pixels( $column['attrs']['style']['border']['right']['width'] ?? $border_width );
				$columns[ $key ]['attrs']['width'] = "{$column_width}px";
			}
		}
	}
	return $columns;
}