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

Embed::extract_url_from_contentprivateWC 1.0

Extract URL from block content using DOM parsing.

Method of the class: Embed{}

No Hooks.

Returns

String. Extracted URL or empty string.

Usage

// private - for code of main (parent) class only
$result = $this->extract_url_from_content( $block_content ): string;
$block_content(string) (required)
Block content HTML.

Embed::extract_url_from_content() code WC 10.4.3

private function extract_url_from_content( string $block_content ): string {
	$dom_helper = new Dom_Document_Helper( $block_content );

	// Find the wp-block-embed__wrapper div.
	$wrapper_element = $dom_helper->find_element( 'div' );
	if ( $wrapper_element ) {
		// Check if this div has the correct class.
		$class_attr = $dom_helper->get_attribute_value( $wrapper_element, 'class' );
		if ( strpos( $class_attr, 'wp-block-embed__wrapper' ) !== false ) {
			// Get the text content (URL) from the div.
			$url = trim( $wrapper_element->textContent ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

			// Decode HTML entities and validate URL.
			$url = html_entity_decode( $url, ENT_QUOTES | ENT_HTML5, 'UTF-8' );

			// Validate the extracted URL.
			if ( $this->is_valid_url( $url ) ) {
				return $url;
			}
		}
	}

	return '';
}