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

Gallery::normalize_image_for_emailprivateWC 1.0

Normalize a gallery <img> for email: drop the web-only class and rein in an oversized raw width.

The block editor stores the intrinsic width/height of the original file (e.g. width="2560"). Outlook honors that raw width literally — blowing a thumbnail-sized cell wide open. The core/image renderer avoids this with add_image_dimensions(); the gallery path (which sizes to a per-cell width rather than the block width) needs the equivalent tailored to its cell model. We clamp the width down to the cell it renders in, but only when the stored width exceeds it, scaling any height to keep the aspect ratio. An image with no explicit width is left responsive (no attribute added), and a width already at or below the cell width is untouched — so the concrete dimensions the aspect-ratio crop sets for a server-cropped file, and the deliberately dimensionless CSS-crop fallback, are both preserved.

The other web-only attributes core emits (srcset, sizes, loading, decoding) are already stripped upstream by Html_Processing_Helper::sanitize_image_html(), whose allowlist keeps only src/alt/width/height/class/style; the class is the one web-only attribute it preserves, so that is all we remove here (matching the core/image renderer, which also drops it).

Метод класса: Gallery{}

Хуков нет.

Возвращает

string. The normalized <img> HTML.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->normalize_image_for_email( $img_html, $cell_width ): string;
$img_html(строка) (обязательный)
Sanitized <img> HTML.
$cell_width(int) (обязательный)
Estimated display width of the gallery cell in px.

Код Gallery::normalize_image_for_email() WC 11.1.1

private function normalize_image_for_email( string $img_html, int $cell_width ): string {
	if ( '' === $img_html ) {
		return $img_html;
	}

	$html = new \WP_HTML_Tag_Processor( $img_html );
	if ( ! $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
		return $img_html;
	}

	// Drop the web-only class the sanitizer preserves (harmless in email, and the core/image
	// renderer strips it too). remove_attribute() is a no-op when the attribute is absent.
	$html->remove_attribute( 'class' );

	// Rein in a raw width wider than the cell it renders in (e.g. a 2560px original in a 20%-wide
	// cell). We only shrink an explicit, oversized width — never add one to an otherwise responsive
	// image, and never touch a width already sized to (or below) the cell — so the aspect-ratio
	// crop's concrete server-crop dimensions are left intact.
	if ( $cell_width > 0 ) {
		$raw_width = $html->get_attribute( 'width' );
		$width     = is_string( $raw_width ) && is_numeric( $raw_width ) ? (int) $raw_width : 0;

		if ( $width > $cell_width ) {
			$raw_height = $html->get_attribute( 'height' );
			$height     = is_string( $raw_height ) && is_numeric( $raw_height ) ? (int) $raw_height : 0;
			if ( $height > 0 ) {
				// Scale the height by the same factor so the image keeps its aspect ratio.
				$scaled_height = max( 1, (int) round( $height * ( $cell_width / $width ) ) );
				$html->set_attribute( 'height', esc_attr( (string) $scaled_height ) );
			}
			$html->set_attribute( 'width', esc_attr( (string) $cell_width ) );
		}
	}

	return $html->get_updated_html();
}