| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/playlist-track` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/playlist-track` block on server. |
| 10 |
* |
| 11 |
* @since 6.9.0 |
| 12 |
* |
| 13 |
* @param array $attributes The block attributes. |
| 14 |
* |
| 15 |
* @return string Returns the Playlist Track. |
| 16 |
*/ |
| 17 |
function gutenberg_render_block_core_playlist_track( $attributes ) { |
| 18 |
if ( empty( $attributes['id'] ) ) { |
| 19 |
return ''; |
| 20 |
} |
| 21 |
|
| 22 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 23 |
|
| 24 |
$unique_id = $attributes['uniqueId'] ?? wp_unique_id( 'playlist-track-' ); |
| 25 |
$artist = $attributes['artist'] ?? ''; |
| 26 |
$length = $attributes['length'] ?? ''; |
| 27 |
$title = isset( $attributes['title'] ) && ! empty( $attributes['title'] ) ? $attributes['title'] : __( 'Unknown title' ); |
| 28 |
|
| 29 |
$context = wp_interactivity_data_wp_context( |
| 30 |
array( |
| 31 |
'uniqueId' => $unique_id, |
| 32 |
) |
| 33 |
); |
| 34 |
|
| 35 |
$html = '<li ' . $wrapper_attributes . '>'; |
| 36 |
$html .= '<button ' . $context . 'data-wp-on--click="actions.changeTrack" data-wp-bind--aria-current="state.isCurrentTrack" class="wp-block-playlist-track__button">'; |
| 37 |
|
| 38 |
$html .= '<span class="wp-block-playlist-track__content">'; |
| 39 |
if ( $title ) { |
| 40 |
$html .= '<span class="wp-block-playlist-track__title">' . wp_kses_post( $title ) . '</span>'; |
| 41 |
} |
| 42 |
if ( $artist ) { |
| 43 |
$html .= '<span class="wp-block-playlist-track__artist">' . wp_kses_post( $artist ) . '</span>'; |
| 44 |
} |
| 45 |
$html .= '</span>'; |
| 46 |
|
| 47 |
if ( $length ) { |
| 48 |
$html .= '<span class="wp-block-playlist-track__length">'; |
| 49 |
$html .= '<span class="screen-reader-text">' . esc_html__( 'Length:' ) . ' </span>'; |
| 50 |
$html .= esc_html( $length ); |
| 51 |
$html .= '</span>'; |
| 52 |
} |
| 53 |
|
| 54 |
$html .= '<span class="screen-reader-text">' . esc_html__( 'Select to play this track' ) . '</span>'; |
| 55 |
$html .= '</button>'; |
| 56 |
$html .= '</li>'; |
| 57 |
|
| 58 |
return $html; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Registers the `core/playlist-track` block on server. |
| 63 |
* |
| 64 |
* @since 6.9.0 |
| 65 |
*/ |
| 66 |
function gutenberg_register_block_core_playlist_track() { |
| 67 |
register_block_type_from_metadata( |
| 68 |
__DIR__ . '/playlist-track', |
| 69 |
array( |
| 70 |
'render_callback' => 'gutenberg_render_block_core_playlist_track', |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
add_action( 'init', 'gutenberg_register_block_core_playlist_track', 20 ); |
| 75 |
|