MailPoet\EmailEditor\Integrations\Core\Renderer\Blocks

Image::parse_block_content()privateWC 1.0

Parse block content to get image URL, image HTML and caption HTML.

Method of the class: Image{}

No Hooks.

Return

Array{imageUrl:. string, image: string, caption: string, class: 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() code WC 9.8.1

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 );

	return array(
		'imageUrl' => $image_src ? $image_src : '',
		'image'    => $this->cleanup_image_html( $image_html ),
		'caption'  => $figcaption_html ? $figcaption_html : '',
		'class'    => $image_class ? $image_class : '',
	);
}