Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Gallery::extract_image_from_html
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;
- $html_content(string) (required)
- HTML content containing the image.
Gallery::extract_image_from_html() Gallery::extract image from html code WC 10.8.1
private function extract_image_from_html( string $html_content ): 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] );
if ( ! empty( $sanitized_url ) ) {
$sanitized_img = Html_Processing_Helper::sanitize_image_html( $link_matches[3] );
if ( '' !== $sanitized_img ) {
$result .= '<a href="' . $sanitized_url . '">' . $sanitized_img . '</a>';
}
} else {
// If URL is invalid, extract just the image without link.
$sanitized_img = Html_Processing_Helper::sanitize_image_html( $link_matches[3] );
if ( '' !== $sanitized_img ) {
$result .= $sanitized_img;
}
}
} elseif ( preg_match( '/<img[^>]*>/', $html_content, $img_matches ) ) {
// Image is not linked - just extract the img element with sanitization.
$sanitized_img = Html_Processing_Helper::sanitize_image_html( $img_matches[0] );
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;
}