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

Image::get_block_wrapperprivateWC 1.0

Based on MJML <mj-image> but because MJML doesn't support captions, our solution is a bit different

Method of the class: Image{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->get_block_wrapper( $parsed_block, $rendering_context, ?string $caption, ?string $anchor_tag_href ): string;
$parsed_block(array) (required)
Parsed block.
$rendering_context(Rendering_Context) (required)
Rendering context.
?string $caption(required)
.
?string $anchor_tag_href(required)
.

Image::get_block_wrapper() code WC 10.5.0

private function get_block_wrapper( array $parsed_block, Rendering_Context $rendering_context, ?string $caption, ?string $anchor_tag_href ): string {
	$styles = array(
		'border-collapse' => 'collapse',
		'border-spacing'  => '0px',
		'font-size'       => '0px',
		'vertical-align'  => 'top',
		'width'           => '100%',
	);

	$width                             = $parsed_block['attrs']['width'] ?? '100%';
	$wrapper_width                     = ( $width && '100%' !== $width ) ? $width : 'auto';
	$wrapper_styles                    = $styles;
	$wrapper_styles['width']           = $wrapper_width;
	$wrapper_styles['border-collapse'] = 'separate'; // Needed because of border radius.

	$caption_html = '';
	if ( $caption ) {
		// When the image is not aligned, the wrapper is set to 100% width due to caption that can be longer than the image.
		$caption_width                   = isset( $parsed_block['attrs']['align'] ) ? ( $parsed_block['attrs']['width'] ?? '100%' ) : '100%';
		$caption_wrapper_styles          = $styles;
		$caption_wrapper_styles['width'] = $caption_width;
		$caption_styles                  = $this->get_caption_styles( $rendering_context, $parsed_block );

		$caption_table_attrs = array(
			'class' => 'email-table-with-width',
			'style' => \WP_Style_Engine::compile_css( $caption_wrapper_styles, '' ),
			'width' => $caption_width,
		);

		$caption_cell_attrs = array(
			'style' => $caption_styles,
		);

		$caption_html = Table_Wrapper_Helper::render_table_wrapper( '{caption_content}', $caption_table_attrs, $caption_cell_attrs );
	}

	$styles['width'] = '100%';
	$align           = $parsed_block['attrs']['align'] ?? 'left';

	$table_attrs = array(
		'style' => \WP_Style_Engine::compile_css( $styles, '' ),
		'width' => '100%',
	);

	$cell_attrs = array(
		'align' => $align,
	);

	$image_table_attrs = array(
		'class' => 'email-table-with-width',
		'style' => \WP_Style_Engine::compile_css( $wrapper_styles, '' ),
		'width' => $wrapper_width,
	);

	$image_cell_attrs = array(
		'class' => 'email-image-cell',
		'style' => 'overflow: hidden;',
	);

	$image_content = '{image_content}';
	if ( $anchor_tag_href ) {
		$image_content = sprintf(
			'<a href="%s" rel="noopener nofollow" target="_blank">%s</a>',
			esc_url( $anchor_tag_href ),
			'{image_content}'
		);
	}
	$image_html    = Table_Wrapper_Helper::render_table_wrapper( $image_content, $image_table_attrs, $image_cell_attrs );
	$inner_content = $image_html . $caption_html;

	return Table_Wrapper_Helper::render_table_wrapper( $inner_content, $table_attrs, $cell_attrs );
}