Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Embed::render_link_fallback
Render a simple link fallback for non-supported embeds.
Method of the class: Embed{}
No Hooks.
Returns
String. Rendered link or empty string if no valid URL.
Usage
// private - for code of main (parent) class only $result = $this->render_link_fallback( $attr, $block_content, $parsed_block, $rendering_context ): string;
- $attr(array) (required)
- Block attributes.
- $block_content(string) (required)
- Block content.
- $parsed_block(array) (required)
- Parsed block.
- $rendering_context(Rendering_Context) (required)
- Rendering context.
Embed::render_link_fallback() Embed::render link fallback code WC 10.9.4
private function render_link_fallback( array $attr, string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
// Try to get URL from attributes first.
$url = $attr['url'] ?? '';
// If no URL in attributes, try to extract from block content.
if ( empty( $url ) ) {
// First try the standard wrapper div extraction.
$url = $this->extract_url_from_content( $block_content );
// If still no URL, try to find any HTTP/HTTPS URL in the entire content.
if ( empty( $url ) ) {
$dom_helper = new Dom_Document_Helper( $block_content );
$body_element = $dom_helper->find_element( 'body' );
if ( $body_element ) {
$text_content = $body_element->textContent; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
// Look for HTTP/HTTPS URLs in the text content.
$url = Html_Processing_Helper::extract_url_from_text( $text_content );
}
}
}
// If still no URL, try to use provider-specific base URL if we have a provider.
if ( empty( $url ) && isset( $attr['providerNameSlug'] ) ) {
$url = $this->get_provider_base_url( $attr['providerNameSlug'] );
}
// Validate URL with both filter_var and wp_http_validate_url.
if ( ! $this->is_valid_url( $url ) ) {
return '';
}
// Get link text - use custom label if provided, otherwise use provider label for base URLs or URL.
if ( ! empty( $attr['label'] ) ) {
$link_text = $attr['label'];
} else {
// Check if this is a provider base URL (like https://open.spotify.com/).
$provider = $attr['providerNameSlug'] ?? '';
$base_url = $this->get_provider_base_url( $provider );
if ( ! empty( $base_url ) && $url === $base_url ) {
// Use provider-specific label for base URLs.
$link_text = $this->get_provider_label( $provider, $attr );
} else {
// Use the URL itself for specific URLs.
$link_text = $url;
}
}
// Get color from email attributes or theme styles.
$email_styles = $rendering_context->get_theme_styles();
$link_color = $parsed_block['email_attrs']['color'] ?? $email_styles['color']['text'] ?? '#0073aa';
// Sanitize color value to ensure it's a valid hex color or CSS variable.
$link_color = Html_Processing_Helper::sanitize_color( $link_color );
// Create a simple link.
$link_html = sprintf(
'<a href="%s" target="_blank" rel="noopener nofollow" style="color: %s; text-decoration: underline;">%s</a>',
esc_url( $url ),
esc_attr( $link_color ),
esc_html( $link_text )
);
// Wrap with spacer if we have email attributes.
return $this->add_spacer_with_context(
$link_html,
$parsed_block['email_attrs'] ?? array(),
$rendering_context
);
}