Skip to content

Commit f40b60d

Browse files
Media: Fix playlist shortcodes not rendering correctly if the first playlist is broken.
The playlist shortcode has a base set of JavaScript that should only be loaded once. Previously, this JS was only loaded the first time a playlist shortcode was processed. If the first playlist was broken, because the media file was missing for instance, this would break all other playlists on the page. This commit introduces a new static variable to keep track of whether the necessary JavaScript has been loaded instead. Props iamadisingh, abcd95, justlevine, jorbin, rollybueno, Guido07111975. Fixes #63583. git-svn-id: https://develop.svn.wordpress.org/trunk@60678 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d76a54a commit f40b60d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/wp-includes/media.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3056,6 +3056,8 @@ function wp_playlist_shortcode( $attr ) {
30563056
static $instance = 0;
30573057
++$instance;
30583058

3059+
static $is_loaded = false;
3060+
30593061
if ( ! empty( $attr['ids'] ) ) {
30603062
// 'ids' is explicitly ordered, unless you specify otherwise.
30613063
if ( empty( $attr['orderby'] ) ) {
@@ -3237,7 +3239,7 @@ function wp_playlist_shortcode( $attr ) {
32373239

32383240
ob_start();
32393241

3240-
if ( 1 === $instance ) {
3242+
if ( ! $is_loaded ) {
32413243
/**
32423244
* Prints and enqueues playlist scripts, styles, and JavaScript templates.
32433245
*
@@ -3247,6 +3249,7 @@ function wp_playlist_shortcode( $attr ) {
32473249
* @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'.
32483250
*/
32493251
do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] );
3252+
$is_loaded = true;
32503253
}
32513254
?>
32523255
<div class="wp-playlist wp-<?php echo $safe_type; ?>-playlist wp-playlist-<?php echo $safe_style; ?>">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @group media
4+
* @covers ::wp_playlist_shortcode
5+
*/
6+
class Tests_Media_Wp_Playlist_Shortcode extends WP_UnitTestCase {
7+
8+
/**
9+
* @ticket 63583
10+
*/
11+
public function test_should_load_scripts_exactly_once_when_first_playlist_is_invalid() {
12+
global $wp_scripts;
13+
14+
$attachment_id = self::factory()->attachment->create_upload_object(
15+
DIR_TESTDATA . '/uploads/small-audio.mp3'
16+
);
17+
18+
wp_playlist_shortcode( array( 'ids' => '999999' ) );
19+
wp_playlist_shortcode( array( 'ids' => (string) $attachment_id ) );
20+
wp_playlist_shortcode( array( 'ids' => (string) $attachment_id ) );
21+
22+
$queue_count = array_count_values( $wp_scripts->queue );
23+
24+
$this->assertArrayHasKey( 'wp-playlist', $queue_count, 'wp-playlist handle should be in the queue.' );
25+
$this->assertSame( 1, $queue_count['wp-playlist'], 'The wp-playlist script handle should appear exactly once in the queue.' );
26+
}
27+
}

0 commit comments

Comments
 (0)