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

Gallery::extract_image_from_html │ private │ WC 1.0

Extract and sanitize image with optional link and caption from HTML content. This is the unified method that handles all image extraction scenarios.

Method of the class: Gallery{}

No Hooks.

Returns

string. Sanitized image HTML with proper link and caption handling.

Usage

// private - for code of main (parent) class only
$result = $this->extract_image_from_html( $html_content, ?string $aspect_ratio, $cell_width, $image_attrs ): string;
$html_content(string) (required)
HTML content containing the image.
?string $aspect_ratio
.
Default: null
$cell_width(int)
Estimated display width of the gallery cell in px.
$image_attrs(array)
Parsed attributes of the core/image block (id, sizeSlug, ...).
Default: array()

Gallery::extract_image_from_html() code WC 11.1.2

private function extract_image_from_html( string $html_content, ?string $aspect_ratio = null, int $cell_width = 0, array $image_attrs = array() ): string {
	$result = '';

	// First, try to find a linked image (most common case).
	if ( preg_match( '/<a[^>]*href=(["\'])(.*?)\1[^>]*>(\s*<img[^>]*>)\s*<\/a>/s', $html_content, $link_matches ) ) {
		// Validate and sanitize the link URL.
		$sanitized_url = esc_url( $link_matches[2] );
		$sanitized_img = $this->prepare_image_html( $link_matches[3], $aspect_ratio, $cell_width, $image_attrs );
		if ( '' !== $sanitized_img ) {
			if ( ! empty( $sanitized_url ) ) {
				$result .= '<a href="' . $sanitized_url . '">' . $sanitized_img . '</a>';
			} else {
				// If the URL is invalid, extract just the image without the link.
				$result .= $sanitized_img;
			}
		}
	} elseif ( preg_match( '/<img[^>]*>/', $html_content, $img_matches ) ) {
		// Image is not linked - just extract the img element with sanitization.
		$sanitized_img = $this->prepare_image_html( $img_matches[0], $aspect_ratio, $cell_width, $image_attrs );
		if ( '' !== $sanitized_img ) {
			$result .= $sanitized_img;
		}
	}

	// Extract the caption if it exists (handle both figcaption and span formats).
	// Enhanced security: validate container attributes before extracting content.
	if ( preg_match( '/(<figcaption[^>]*>)(.*?)(<\/figcaption>)/s', $html_content, $caption_matches ) ) {
		// Validate the figcaption container attributes for security.
		if ( Html_Processing_Helper::validate_container_attributes( $caption_matches[1] . $caption_matches[3] ) ) {
			$sanitized_caption = Html_Processing_Helper::sanitize_caption_html( $caption_matches[2] );
			$result           .= '<br><div class="wp-element-caption" style="font-size: 13px; line-height: 1.0;">' . $sanitized_caption . '</div>';
		}
	} elseif ( preg_match( '/(<span class="wp-element-caption"[^>]*>)(.*?)(<\/span>)/s', $html_content, $caption_matches ) ) {
		// Validate the span container attributes for security.
		if ( Html_Processing_Helper::validate_container_attributes( $caption_matches[1] . $caption_matches[3] ) ) {
			$sanitized_caption = Html_Processing_Helper::sanitize_caption_html( $caption_matches[2] );
			$result           .= '<br><div class="wp-element-caption" style="font-size: 13px; line-height: 1.0;">' . $sanitized_caption . '</div>';
		}
	}

	return $result;
}