Skip to content
Open
4 changes: 4 additions & 0 deletions packages/block-library/src/playlist/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function render_block_core_playlist( $attributes, $content, $block ) {
continue;
}

if ( false === ( $track_attributes['metadata']['blockVisibility'] ?? null ) ) {
continue;
}

$track_id = 'track-' . count( $playlist_tracks );
$playlist_tracks[] = $track_id;

Expand Down
44 changes: 40 additions & 4 deletions packages/block-library/src/playlist/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ const { state } = store(
return;
}

const existing = playerState.get( ref );

// Start from the first track visible in the current viewport.
if (
! existing &&
! isTrackVisible( ref, context.tracks, context.currentId )
) {
const firstVisibleTrack = context.tracks.find(
( trackId ) =>
isTrackVisible( ref, context.tracks, trackId )
);
if ( firstVisibleTrack ) {
context.currentId = firstVisibleTrack;
}
Comment thread
yashjawale marked this conversation as resolved.
Resolved
}

const track =
state.playlists[ context.playlistId ]?.tracks[
context.currentId
Expand All @@ -69,8 +85,6 @@ const { state } = store(
return;
}

const existing = playerState.get( ref );

// Skip if we already initialized with this exact URL.
if ( existing?.url === track.url ) {
return;
Expand All @@ -87,6 +101,24 @@ const { state } = store(
{ lock: true }
);

/**
* Check whether a track is visible in the current viewport. Tracks hidden on
* some viewports still render their button, hidden with CSS.
*
* @param {Element} ref - The waveform player container.
* @param {string[]} trackIds - The playlist's track IDs, in button order.
* @param {string} trackId - The track ID to check.
* @return {boolean} Whether the track is visible.
*/
function isTrackVisible( ref, trackIds, trackId ) {
const button = ref
.closest( '.wp-block-playlist' )
?.querySelectorAll( '.wp-block-playlist-track__button' )[
trackIds.indexOf( trackId )
];
return ! button || button.getClientRects().length > 0;
Comment thread
yashjawale marked this conversation as resolved.
Resolved
}

/**
* Initialize the waveform player for a given element.
*
Expand Down Expand Up @@ -164,11 +196,15 @@ function initPlayer( ref, track, shouldAutoPlay, context ) {
waveformStyle: context.waveformStyle,
showPlayButtonArtwork,
onEnded: () => {
// Advance to next track (autoPlay handles playback).
// Advance to the next visible track (autoPlay handles playback).
const currentIndex = context.tracks.findIndex(
( trackId ) => trackId === context.currentId
);
const nextTrack = context.tracks[ currentIndex + 1 ];
const nextTrack = context.tracks
.slice( currentIndex + 1 )
.find( ( trackId ) =>
isTrackVisible( ref, context.tracks, trackId )
);
if ( nextTrack ) {
context.currentId = nextTrack;
}
Expand Down
43 changes: 43 additions & 0 deletions phpunit/blocks/render-block-playlist-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,49 @@ public function test_renders_multiple_tracks_in_context() {
$this->assertSame( array( 'track-0', 'track-1', 'track-2' ), $context['tracks'] );
}

/**
* @covers ::render_block_core_playlist
*/
public function test_skips_hidden_tracks() {
$markup = $this->build_playlist_markup(
array(),
array(
array(
'title' => 'Song One',
'src' => 'http://example.com/song1.mp3',
'metadata' => array( 'blockVisibility' => false ),
),
array(
'title' => 'Song Two',
'src' => 'http://example.com/song2.mp3',
),
array(
'title' => 'Song Three',
'src' => 'http://example.com/song3.mp3',
'metadata' => array(
'blockVisibility' => array(
'viewport' => array( 'mobile' => false ),
),
),
),
)
);

$output = do_blocks( $markup );
$p = new WP_HTML_Tag_Processor( $output );
$p->next_tag( 'figure' );
$context = json_decode( $p->get_attribute( 'data-wp-context' ), true );
$this->assertSame( array( 'track-0', 'track-1' ), $context['tracks'] );

$p->next_tag( array( 'class_name' => 'wp-block-playlist-track__button' ) );
$button_context = json_decode( $p->get_attribute( 'data-wp-context' ), true );
$this->assertSame( 'track-0', $button_context['trackId'] );

$state = wp_interactivity_state( 'core/playlist' );
$playlist = reset( $state['playlists'] );
$this->assertSame( 'http://example.com/song2.mp3', $playlist['tracks']['track-0']['url'] );
}

/**
* @covers ::render_block_core_playlist
* @covers ::render_block_core_playlist_track
Expand Down
83 changes: 83 additions & 0 deletions test/e2e/specs/editor/blocks/playlist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,89 @@
await expect( playerArtwork ).toHaveCount( 0 );
} );

test( 'skips hidden tracks on the frontend', async ( {

Check notice on line 384 in test/e2e/specs/editor/blocks/playlist.spec.js

View workflow job for this annotation

GitHub Actions / Run new e2e tests on trunk

This test fails on trunk, a good sign that it catches the bug.
page,
requestUtils,
} ) => {
const tracks = [
{
id: uploadedSecondAudio.id,
src: uploadedSecondAudio.source_url,
title: 'Hidden track',
metadata: { blockVisibility: false },
},
{
id: uploadedSecondAudio.id,
src: uploadedSecondAudio.source_url,
title: 'Viewport hidden track',
metadata: {
blockVisibility: {
viewport: {
mobile: false,
tablet: false,
desktop: false,
},
},
},
},
{
id: uploadedAudio.id,
src: uploadedAudio.source_url,
title: 'First visible track',
},
{
id: uploadedSecondAudio.id,
src: uploadedSecondAudio.source_url,
title: 'Second visible track',
},
];
const post = await requestUtils.createPost( {
title: 'Playlist hidden tracks',
status: 'publish',
content: [
'<!-- wp:playlist -->',
'<figure class="wp-block-playlist">',
'<ol class="wp-block-playlist__tracklist">',
...tracks.map(
( track ) =>
`<!-- wp:playlist-track ${ JSON.stringify(
track
) } /-->`
),
'</ol></figure>',
'<!-- /wp:playlist -->',
].join( '' ),
} );

await page.goto( post.link );

const player = page.locator( '.wp-block-playlist__waveform-player' );

await expect(
page.getByRole( 'button', {
name: /Hidden track/,
includeHidden: true,
} )
).toHaveCount( 0 );
await expect(
page.getByRole( 'button', {
name: /Viewport hidden track/,
includeHidden: true,
} )
).toBeHidden();
await expect(
player.getByRole( 'slider', { name: 'First visible track' } )
).toBeVisible();

await page
.getByRole( 'button', { name: /Second visible track/ } )
.click();

await expect(
player.getByRole( 'slider', { name: 'Second visible track' } )
).toBeVisible();
} );

test( 'keeps the playlist when the only added track fails to upload', async ( {
admin,
editor,
Expand Down
Loading