Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks

Media_Text::build_email_layoutprivateWC 1.0

Build the email-friendly layout for media-text blocks.

Method of the class: Media_Text{}

No Hooks.

Returns

String. Rendered HTML.

Usage

// private - for code of main (parent) class only
$result = $this->build_email_layout( $media_content, $text_content, $block_attrs, $block_content, $rendering_context ): string;
$media_content(string) (required)
Media HTML content.
$text_content(string) (required)
Text content.
$block_attrs(array) (required)
Block attributes.
$block_content(string) (required)
Original block content.
$rendering_context(Rendering_Context) (required)
Rendering context.

Media_Text::build_email_layout() code WC 10.5.0

private function build_email_layout( string $media_content, string $text_content, array $block_attrs, string $block_content, Rendering_Context $rendering_context ): string {
	// Get original wrapper classes from block content.
	$original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? '';

	// Get layout attributes.
	$media_position     = $block_attrs['mediaPosition'] ?? 'left';
	$vertical_alignment = $this->get_vertical_alignment_from_attributes( $block_attrs );
	$media_width        = $this->get_media_width_from_attributes( $block_attrs );
	$text_width         = 100 - $media_width; // Text takes the remaining width.

	// Handle image linking for any linkDestination type that has an href.
	if ( ! empty( $block_attrs['href'] ) ) {
		$media_content = $this->wrap_media_with_link( $media_content, $block_attrs['href'] );
	}

	// Get block styles using the Styles_Helper.
	$block_styles = Styles_Helper::get_block_styles( $block_attrs, $rendering_context, array( 'padding', 'border', 'background', 'background-color', 'color' ) );
	$block_styles = Styles_Helper::extend_block_styles(
		$block_styles,
		array(
			'width'           => '100%',
			'border-collapse' => 'collapse',
			'text-align'      => 'left',
		)
	);

	// Apply class and style attributes to the wrapper table.
	$table_attrs = array(
		'class' => 'email-block-media-text ' . $original_wrapper_classname,
		'style' => $block_styles['css'],
		'align' => 'left',
		'width' => '100%',
	);

	// Build individual table cells.
	$media_cell_attrs = array(
		'style'  => sprintf( 'width: %d%%; padding: 10px; vertical-align: %s;', $media_width, $vertical_alignment ),
		'valign' => $vertical_alignment,
	);
	$text_cell_attrs  = array(
		'style'  => sprintf( 'width: %d%%; padding: 0 8%%; vertical-align: %s;', $text_width, $vertical_alignment ),
		'valign' => $vertical_alignment,
	);

	$media_cell = Table_Wrapper_Helper::render_table_cell( $media_content, $media_cell_attrs );
	$text_cell  = Table_Wrapper_Helper::render_table_cell( $text_content, $text_cell_attrs );

	// Order cells based on media position.
	if ( 'right' === $media_position ) {
		// Text first, then media.
		$cells = $text_cell . $media_cell;
	} else {
		// Media first, then text (default left position).
		$cells = $media_cell . $text_cell;
	}

	// Use render_cell = false to avoid wrapping in an extra <td>.
	return Table_Wrapper_Helper::render_table_wrapper( $cells, $table_attrs, array(), array(), false );
}