Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Video::extract_video_url
Extract video URL from block content.
Method of the class: Video{}
No Hooks.
Returns
String. Video URL or empty string.
Usage
// private - for code of main (parent) class only $result = $this->extract_video_url( $block_content ): string;
- $block_content(string) (required)
- Block content HTML.
Video::extract_video_url() Video::extract video url code WC 10.5.0
private function extract_video_url( string $block_content ): string {
// Use Dom_Document_Helper for robust HTML parsing.
$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 ) {
return '';
}
// 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 ) {
return '';
}
// Get the inner HTML content from the wrapper div.
$inner_html = $dom_helper->get_element_inner_html( $wrapper_element );
// Look for HTTP/HTTPS URLs in the inner HTML content.
if ( preg_match( '/(?<![a-zA-Z0-9.-])https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}[a-zA-Z0-9\/?=&%-]*(?![a-zA-Z0-9.-])/', $inner_html, $matches ) ) {
$url = $matches[0];
// Decode HTML entities and validate URL.
$url = html_entity_decode( $url, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
// Validate the URL.
if ( filter_var( $url, FILTER_VALIDATE_URL ) && wp_http_validate_url( $url ) ) {
return $url;
}
}
return '';
}