Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Table::process_table_content
Process table content to ensure email client compatibility.
Method of the class: Table{}
No Hooks.
Returns
String.
Usage
// private - for code of main (parent) class only $result = $this->process_table_content( $block_content, $parsed_block, $rendering_context, $is_striped_table ): string;
- $block_content(string) (required)
- Block content.
- $parsed_block(array) (required)
- Parsed block.
- $rendering_context(Rendering_Context) (required)
- Rendering context.
- $is_striped_table(true|false)
- Whether this is a striped table.
Default:false
Table::process_table_content() Table::process table content code WC 10.4.3
private function process_table_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context, bool $is_striped_table = false ): string {
$html = new \WP_HTML_Tag_Processor( $block_content );
// Extract custom border color and width from block attributes.
$custom_border_color = $this->get_custom_border_color( $parsed_block, $rendering_context );
$custom_border_width = $this->get_custom_border_width( $parsed_block );
// Use custom border color if available, otherwise fall back to default.
if ( $custom_border_color ) {
$border_color = $custom_border_color;
} else {
// Get theme styles once to avoid repeated calls.
$email_styles = $rendering_context->get_theme_styles();
$border_color = Html_Processing_Helper::sanitize_color( $parsed_block['email_attrs']['color'] ?? $email_styles['color']['text'] ?? '#000000' );
}
// Track row context for striped styling.
$current_section = ''; // Table sections: thead, tbody, tfoot.
$row_count = 0;
// Process table elements.
while ( $html->next_tag() ) {
$tag_name = $html->get_tag();
if ( 'TABLE' === $tag_name ) {
// Ensure table has proper email attributes.
$html->set_attribute( 'border', '1' );
$html->set_attribute( 'cellpadding', '8' );
$html->set_attribute( 'cellspacing', '0' );
$html->set_attribute( 'role', 'presentation' );
$html->set_attribute( 'width', '100%' );
// Get existing style and add email-specific styles.
$existing_style = (string) ( $html->get_attribute( 'style' ) ?? '' );
// Check for fixed layout class and apply table-layout: fixed.
$class_attr = (string) ( $html->get_attribute( 'class' ) ?? '' );
$table_layout = $this->has_fixed_layout( $class_attr ) ? 'table-layout: fixed; ' : '';
// Use border-collapse: collapse to ensure consistent borders between table and cells.
$email_table_styles = "{$table_layout}border-collapse: collapse; width: 100%;";
$existing_style = rtrim( $existing_style, "; \t\n\r\0\x0B" );
$new_style = $existing_style ? $existing_style . '; ' . $email_table_styles : $email_table_styles;
$html->set_attribute( 'style', $new_style );
// Remove problematic classes from the table but keep has-fixed-layout and alignment classes for editor UI.
$class_attr = Html_Processing_Helper::clean_css_classes( $class_attr );
$html->set_attribute( 'class', $class_attr );
} elseif ( 'THEAD' === $tag_name ) {
$current_section = 'thead';
$row_count = 0;
} elseif ( 'TBODY' === $tag_name ) {
$current_section = 'tbody';
$row_count = 0;
} elseif ( 'TFOOT' === $tag_name ) {
$current_section = 'tfoot';
$row_count = 0;
} elseif ( 'TR' === $tag_name ) {
++$row_count;
} elseif ( 'TD' === $tag_name || 'TH' === $tag_name ) {
// Ensure table cells have proper email attributes with borders and padding.
$html->set_attribute( 'valign', 'top' );
// Get existing style and add email-specific styles with borders and padding.
$existing_style = (string) ( $html->get_attribute( 'style' ) ?? '' );
$existing_style = rtrim( $existing_style, "; \t\n\r\0\x0B" );
$border_width = $custom_border_width ? $custom_border_width : '1px';
$border_style = $this->get_custom_border_style( $parsed_block );
// Extract cell-specific text alignment.
$cell_text_align = $this->get_cell_text_alignment( $html );
$email_cell_styles = "vertical-align: top; border: {$border_width} {$border_style} {$border_color}; padding: 8px; text-align: {$cell_text_align};";
// Add thicker borders for header and footer cells when no custom border is set.
$email_cell_styles = $this->add_header_footer_borders( $html, $email_cell_styles, $border_color, $current_section, $custom_border_width );
// Add striped styling for tbody rows (first row gets background, then alternates).
if ( $is_striped_table && 'tbody' === $current_section && 1 === $row_count % 2 ) {
$email_cell_styles .= ' background-color: #f8f9fa;';
}
$new_cell_style = $existing_style ? $existing_style . '; ' . $email_cell_styles : $email_cell_styles;
$html->set_attribute( 'style', $new_cell_style );
}
}
return $html->get_updated_html();
}