]*\ssrc=["\']([^"\']*)["\'][^>]*/?>#', $block_content ); // If we have neither an attachment ID nor a src in the HTML content, return empty. if ( ! $has_attachment_id && ! $has_src_in_html ) { return ''; } // If we have a valid source, proceed with normal rendering. $rendered_content = $this->render_content( $block_content, $parsed_block, $rendering_context ); // If render_content returns empty (e.g., invalid URL), return empty string. if ( empty( $rendered_content ) ) { return ''; } return $this->add_spacer_with_context( $rendered_content, $parsed_block['email_attrs'] ?? array(), $rendering_context ); } /** * Renders the audio block content as an audio player for email. * * @param string $block_content Block content. * @param array $parsed_block Parsed block. * @param Rendering_Context $rendering_context Rendering context (required by parent contract but unused in this implementation). * @return string */ protected function render_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $attr = $parsed_block['attrs']; // Get URL and length. if ( isset( $attr['id'] ) ) { // Audio file from site's media library. $url = \wp_get_attachment_url( $attr['id'] ); $meta = \get_post_meta( $attr['id'], '_wp_attachment_metadata', true ); $length = ( is_array( $meta ) && isset( $meta['length_formatted'] ) && is_string( $meta['length_formatted'] ) ) ? $meta['length_formatted'] : ''; } else { // Audio file from external URL. preg_match( '#]*\ssrc=["\']([^"\']*)["\'][^>]*/?>#', $block_content, $audio ); $url = isset( $audio[1] ) ? $audio[1] : $attr['src'] ?? ''; $length = null; } // Validate URL with proper ordering and comprehensive checks. if ( empty( $url ) ) { return ''; } // Validate URL type and format. if ( ! str_starts_with( $url, 'data:audio/' ) && ! str_starts_with( $url, '/' ) && ! str_starts_with( $url, 'https://' ) ) { // Reject everything else (http://, ftp://, relative paths, etc.). return ''; } // For HTTPS URLs, validate with wp_http_validate_url. if ( str_starts_with( $url, 'https://' ) && ! wp_http_validate_url( $url ) ) { return ''; } // Get spacing from email_attrs for better consistency with core blocks. $email_attrs = $parsed_block['email_attrs'] ?? array(); $table_margin_style = ''; if ( ! empty( $email_attrs ) && class_exists( '\WP_Style_Engine' ) ) { // Get margin for table styling. $table_margin_style = \WP_Style_Engine::compile_css( array_intersect_key( $email_attrs, array_flip( array( 'margin' ) ) ), '' ); } $icon_image = $this->get_audio_icon_url(); $label = ! empty( $attr['label'] ) ? $attr['label'] : __( 'Listen to the audio', 'woocommerce' ); // Add duration to label if available. if ( ! empty( $length ) ) { $label .= ' (' . esc_html( (string) $length ) . ')'; } $audio_url = esc_url( $url ); // Define pill-style colors and styling. $background_color = '#f6f7f7'; $border_color = '#AAA'; $icon_size = '18px'; $font_size = '14px'; $start_side = $rendering_context->get_start_side(); $end_side = $rendering_context->get_end_side(); // Generate the icon content. $icon_content = sprintf( '%4$s', $audio_url, esc_attr( $icon_size ), esc_url( $icon_image ), // translators: %s is the audio player icon. sprintf( __( '%s icon', 'woocommerce' ), __( 'Audio', 'woocommerce' ) ), esc_attr( $start_side ), esc_attr( $end_side ) ); $icon_content = Table_Wrapper_Helper::render_table_cell( $icon_content, array( 'style' => sprintf( 'vertical-align:middle;font-size:%s;', $font_size ) ) ); // Generate the label content. $label_content = sprintf( ' %2$s ', $audio_url, esc_html( $label ), esc_attr( $end_side ) ); $label_cell_style = sprintf( 'vertical-align:middle;font-size:%s;', $font_size ); $label_content = Table_Wrapper_Helper::render_table_cell( $label_content, array( 'style' => $label_cell_style ) ); // Combine icon and label tables. $audio_content = $icon_content . $label_content; // Create the main pill-style table. $main_table_styles = sprintf( 'background-color: %s; border-radius: 9999px; float: none; border: 1px solid %s; border-collapse: separate;', $background_color, $border_color ); $main_table_attrs = array( 'align' => $rendering_context->get_default_text_align(), 'style' => $main_table_styles, ); $main_table = Table_Wrapper_Helper::render_table_wrapper( $audio_content, $main_table_attrs, array(), array(), false ); // Create the main wrapper table. $table_style = 'width: 100%;'; if ( ! empty( $table_margin_style ) ) { $table_style = $table_margin_style . '; ' . $table_style; } else { $table_style = 'margin: 16px 0; ' . $table_style; } $table_attrs = array( 'style' => $table_style, ); $cell_attrs = array( 'style' => 'min-width: 100%; vertical-align: middle; word-break: break-word; text-align: ' . $rendering_context->get_default_text_align() . ';', ); $main_wrapper = Table_Wrapper_Helper::render_table_wrapper( $main_table, $table_attrs, $cell_attrs ); return Table_Wrapper_Helper::render_outlook_table_wrapper( $main_wrapper, array( 'align' => $rendering_context->get_default_text_align() ) ); } /** * Gets the audio icon URL. * * @return string The audio icon URL. */ private function get_audio_icon_url(): string { $file_name = '/icons/audio/audio-play.png'; return plugins_url( $file_name, __FILE__ ); } }