inner_blocks ) ) { foreach ( $block->inner_blocks as $inner_block ) { if ( 'core/playlist-track' === $inner_block->name ) { $track_attributes = $inner_block->attributes; if ( empty( $track_attributes['id'] ) ) { continue; } $track_id = 'track-' . count( $playlist_tracks ); $playlist_tracks[] = $track_id; // Extract track metadata from block attributes. $title = isset( $track_attributes['title'] ) && ! empty( $track_attributes['title'] ) ? $track_attributes['title'] : __( 'Unknown title' ); $artist = $track_attributes['artist'] ?? ''; $album = $track_attributes['album'] ?? ''; $image = $track_attributes['image'] ?? ''; $image_alt = $track_attributes['imageAlt'] ?? ''; $url = $track_attributes['src'] ?? ''; $aria_label = $title; if ( $title && $artist && $album ) { $aria_label = sprintf( /* translators: %1$s: track title, %2$s: artist name, %3$s: album name. */ _x( '%1$s by %2$s from the album %3$s', 'track title, artist name, album name' ), $title, $artist, $album ); } // Data is passed to wp_interactivity_state() which JSON-encodes it, // so we use wp_strip_all_tags() instead of esc_html() to prevent // HTML injection without double-encoding. URLs still use esc_url(). $tracks_data[ $track_id ] = array( 'url' => esc_url( $url ), 'title' => wp_strip_all_tags( $title ), 'artist' => wp_strip_all_tags( $artist ), 'album' => wp_strip_all_tags( $album ), 'image' => esc_url( $image ), 'imageAlt' => wp_strip_all_tags( $image_alt ), 'ariaLabel' => wp_strip_all_tags( $aria_label ), ); } } } if ( empty( $playlist_tracks ) ) { return ''; } wp_enqueue_script_module( '@wordpress/block-library/playlist/view' ); // Add the playlist tracks to the global state, // but keep them isolated from other playlists with the help of playlistId. wp_interactivity_state( 'core/playlist', array( 'playlists' => array( $playlist_id => array( 'tracks' => $tracks_data, ), ), ) ); // Add waveform player container with translated button labels. $label_play = esc_attr__( 'Play' ); $label_pause = esc_attr__( 'Pause' ); $label_seek = esc_attr__( 'Seek' ); /* translators: %1$s: current audio time, %2$s: total audio duration. */ $label_seek_value = esc_attr_x( '%1$s of %2$s', 'audio current time of total duration' ); $waveform_color_attribute = ''; $waveform_gradient_attribute = ''; $waveform_background_color_attribute = ''; $waveform_background_gradient_attribute = ''; if ( ! empty( $attributes['waveformColor'] ) ) { $waveform_color_attribute = sprintf( ' data-waveform-player-color="%s"', esc_attr( $attributes['waveformColor'] ) ); } if ( ! empty( $attributes['waveformGradient'] ) ) { $waveform_gradient_attribute = sprintf( ' data-waveform-player-gradient="%s"', esc_attr( $attributes['waveformGradient'] ) ); } if ( ! empty( $attributes['waveformBackgroundColor'] ) ) { $waveform_background_color_attribute = sprintf( ' data-waveform-player-background-color="%s"', esc_attr( $attributes['waveformBackgroundColor'] ) ); } if ( ! empty( $attributes['waveformBackgroundGradient'] ) ) { $waveform_background_gradient_attribute = sprintf( ' data-waveform-player-background-gradient="%s"', esc_attr( $attributes['waveformBackgroundGradient'] ) ); } $html = '
'; // Add the waveform player container inside the figure. $figure = null; preg_match( '/