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

Image::add_image_dimensionsprivateWC 1.0

Settings width and height attributes for images is important for MS Outlook.

Method of the class: Image{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->add_image_dimensions( $block_content, $parsed_block ): string;
$block_content(string) (required)
Block content.
$parsed_block(array) (required)
Parsed block.

Image::add_image_dimensions() code WC 10.9.1

private function add_image_dimensions( string $block_content, array $parsed_block ): string {
	$html = new \WP_HTML_Tag_Processor( $block_content );
	if ( $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
		// Getting height from styles and if it's set, we set the height attribute.
		/** @var string $styles */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
		$styles = $html->get_attribute( 'style' ) ?? '';
		$styles = Styles_Helper::parse_styles_to_array( $styles );
		$height = $styles['height'] ?? null;
		if ( $height && 'auto' !== $height ) {
			$height = Styles_Helper::parse_value( $height );
			/* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */
			$html->set_attribute( 'height', esc_attr( $height ) );
		}

		if ( isset( $parsed_block['attrs']['width'] ) ) {
			$width = Styles_Helper::parse_value( $parsed_block['attrs']['width'] );
			/* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */
			$html->set_attribute( 'width', esc_attr( $width ) );
		}
		$block_content = $html->get_updated_html();
	}

	return $block_content;
}