Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Image::parse_block_content
Parse block content to get image URL, image HTML and caption HTML.
Method of the class: Image{}
No Hooks.
Returns
Array{imageUrl:. string, image: string, caption: string, class: string, anchor_tag_href: string, anchor_data_link_href: string}|null
Usage
// private - for code of main (parent) class only $result = $this->parse_block_content( $block_content ): ?array;
- $block_content(string) (required)
- Block content.
Image::parse_block_content() Image::parse block content code WC 10.6.2
private function parse_block_content( string $block_content ): ?array {
// If block's image is not set, we don't need to parse the content.
if ( empty( $block_content ) ) {
return null;
}
$dom_helper = new Dom_Document_Helper( $block_content );
$figure_tag = $dom_helper->find_element( 'figure' );
if ( ! $figure_tag ) {
return null;
}
$img_tag = $dom_helper->find_element( 'img' );
if ( ! $img_tag ) {
return null;
}
$image_src = $dom_helper->get_attribute_value( $img_tag, 'src' );
$image_class = $dom_helper->get_attribute_value( $img_tag, 'class' );
$image_html = $dom_helper->get_outer_html( $img_tag );
$figcaption = $dom_helper->find_element( 'figcaption' );
$figcaption_html = $figcaption ? $dom_helper->get_outer_html( $figcaption ) : '';
$figcaption_html = str_replace( array( '<figcaption', '</figcaption>' ), array( '<span', '</span>' ), $figcaption_html );
$anchor_tag = $dom_helper->find_element( 'a' );
$anchor_tag_href = $anchor_tag ? $dom_helper->get_attribute_value( $anchor_tag, 'href' ) : '';
$anchor_data_link_href = $anchor_tag ? $dom_helper->get_attribute_value( $anchor_tag, 'data-link-href' ) : '';
return array(
'imageUrl' => $image_src ? $image_src : '',
'image' => $this->cleanup_image_html( $image_html ),
'caption' => $figcaption_html ? $figcaption_html : '',
'class' => $image_class ? $image_class : '',
'anchor_tag_href' => $anchor_tag_href ? $anchor_tag_href : '',
'anchor_data_link_href' => $anchor_data_link_href ? $anchor_data_link_href : '',
);
}