Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Post_Template::get_column_count
Determine how many columns the grid should render.
Prefers the block's own layout.columnCount attribute and falls back to the columns-N class WordPress core stamps on the rendered list, so it still works when the parsed attributes are sparse. Non-grid/flex layouts always resolve to a single (stacked) column.
Method of the class: Post_Template{}
No Hooks.
Returns
int. Column count (at least 1).
Usage
// private - for code of main (parent) class only $result = $this->get_column_count( $parsed_block, $dom, $list_element ): int;
- $parsed_block(array) (required)
- Parsed block data.
- $dom(Dom_Document_Helper) (required)
- Parsed block content.
- $list_element(DOMElement) (required)
- The post-template list element.
Post_Template::get_column_count() Post Template::get column count code WC 11.1.2
private function get_column_count( array $parsed_block, Dom_Document_Helper $dom, \DOMElement $list_element ): int {
$layout = $parsed_block['attrs']['layout'] ?? array();
$type = is_array( $layout ) && isset( $layout['type'] ) && is_string( $layout['type'] ) ? $layout['type'] : '';
// A layout that is neither grid nor flex (default, constrained, flow) stacks in one column.
if ( '' !== $type && 'grid' !== $type && 'flex' !== $type ) {
return 1;
}
$columns = 0;
if ( is_array( $layout ) && isset( $layout['columnCount'] ) && is_numeric( $layout['columnCount'] ) ) {
$columns = (int) $layout['columnCount'];
}
// Fallback: read the `columns-N` class WordPress core adds to the list wrapper.
if ( $columns < 1 && preg_match( '/(?:^|\s)columns-(\d+)(?:\s|$)/', $dom->get_attribute_value( $list_element, 'class' ), $matches ) ) {
$columns = (int) $matches[1];
}
if ( $columns < 1 ) {
return 1;
}
return min( self::MAX_COLUMNS, $columns );
}