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 $anchor_data_link_href ): string;
$parsed_block(array) (required)
Parsed block.
$rendering_context(Rendering_Context) (required)
Rendering context.
?string $caption(required)
.
?string $anchor_tag_href(required)
.
?string $anchor_data_link_href
.
Default: null

Image::get_block_wrapper() code WC 10.9.1

private function get_block_wrapper( array $parsed_block, Rendering_Context $rendering_context, ?string $caption, ?string $anchor_tag_href, ?string $anchor_data_link_href = null ): 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'] ?? $rendering_context->get_default_text_align();

	// Map block alignment to valid HTML/CSS alignment values.
	// "full" and "wide" are not valid text-align or table align values.
	$css_align = in_array( $align, array( 'full', 'wide' ), true ) ? 'center' : $rendering_context->resolve_text_align( $align );

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

	$cell_attrs = array(
		'align' => $css_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;',
		'align' => $css_align,
	);

	$image_content = '{image_content}';
	if ( $anchor_tag_href ) {
		$data_link_attr = $anchor_data_link_href
			? sprintf( ' data-link-href="%s"', esc_attr( $anchor_data_link_href ) )
			: '';
		$image_content  = sprintf(
			'<a href="%s"%s rel="noopener nofollow" target="_blank">%s</a>',
			esc_url( $anchor_tag_href ),
			$data_link_attr,
			'{image_content}'
		);
	}

	// Wrap image in a border wrapper table that won't expand to 100% on mobile.
	// This ensures borders stay tight around the image regardless of screen size.
	$border_wrapper_styles = array(
		'border-collapse' => 'separate',
		'border-spacing'  => '0px',
	);
	$border_wrapper_attrs  = array(
		'class' => 'email-image-border-wrapper',
		'style' => \WP_Style_Engine::compile_css( $border_wrapper_styles, '' ),
	);
	$border_cell_attrs     = array(
		'class' => 'email-image-border-cell',
	);
	$image_content         = Table_Wrapper_Helper::render_table_wrapper( $image_content, $border_wrapper_attrs, $border_cell_attrs );

	$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 );
}