Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Cover::extract_background_image
Extract background image from block attributes or HTML content. Returns raw URL - escaping happens at final CSS output context.
Method of the class: Cover{}
No Hooks.
Returns
String. Background image URL or empty string.
Usage
// private - for code of main (parent) class only $result = $this->extract_background_image( $block_attrs, $block_content ): string;
- $block_attrs(array) (required)
- Block attributes.
- $block_content(string) (required)
- Original block content.
Cover::extract_background_image() Cover::extract background image code WC 10.7.0
private function extract_background_image( array $block_attrs, string $block_content ): string {
// First check block attributes for URL.
// Use esc_url_raw() to sanitize without HTML entity encoding.
if ( ! empty( $block_attrs['url'] ) ) {
return esc_url_raw( $block_attrs['url'] );
}
// Fallback: use HTML API to find background image src.
$html = new \WP_HTML_Tag_Processor( $block_content );
while ( $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
$class_attr = $html->get_attribute( 'class' );
// Check if this img tag has the wp-block-cover__image-background class.
if ( is_string( $class_attr ) && false !== strpos( $class_attr, 'wp-block-cover__image-background' ) ) {
$src = $html->get_attribute( 'src' );
if ( is_string( $src ) ) {
return esc_url_raw( $src );
}
}
}
return '';
}