Skip to content

Commit

Permalink
Issue #841: Set a default height and width for 'audio.'
Browse files Browse the repository at this point in the history
Audio playlists have thumbnail images.
If the height and width aren't defined in $data,
Set fallbacks.
These are based on the fallback heights in:
wp_playlist_shortcode().
  • Loading branch information
Ryan Kienstra committed Feb 15, 2018
1 parent 168b9ba commit 7cae35c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
33 changes: 21 additions & 12 deletions includes/embeds/class-amp-playlist-embed-handler.php
Expand Up @@ -22,6 +22,20 @@ class AMP_Playlist_Embed_Handler extends AMP_Base_Embed_Handler {
*/
const SHORTCODE = 'playlist';

/**
* The default height of the thumbnail image for 'audio' playlist tracks.
*
* @var string
*/
const DEFAULT_THUMB_HEIGHT = 64;

/**
* The default width of the thumbnail image for 'audio' playlist tracks.
*
* @var string
*/
const DEFAULT_THUMB_WIDTH = 48;

/**
* The max width of the audio thumbnail image.
*
Expand Down Expand Up @@ -150,9 +164,7 @@ public function audio_playlist() {
foreach ( $this->data['tracks'] as $track ) :
$title = $this->get_title( $track );
$image_url = isset( $track['thumb']['src'] ) ? esc_url( $track['thumb']['src'] ) : '';
$thumb_dimensions = $this->get_thumb_dimensions( $track );
$image_height = isset( $thumb_dimensions['height'] ) ? $thumb_dimensions['height'] : '';
$image_width = isset( $thumb_dimensions['width'] ) ? $thumb_dimensions['width'] : '';
list( $image_height, $image_width ) = $this->get_thumb_dimensions( $track );

?>
<div>
Expand Down Expand Up @@ -229,21 +241,18 @@ public function video_playlist() {
* @return array $dimensions The height and width of the thumbnail image.
*/
public function get_thumb_dimensions( $track ) {
if ( ! isset( $track['thumb']['width'], $track['thumb']['height'] ) ) {
return array();
}
$original_width = intval( $track['thumb']['width'] );
$original_height = intval( $track['thumb']['height'] );
$original_height = isset( $track['thumb']['height'] ) ? intval( $track['thumb']['height'] ) : self::DEFAULT_THUMB_HEIGHT;
$original_width = isset( $track['thumb']['width'] ) ? intval( $track['thumb']['width'] ) : self::DEFAULT_THUMB_WIDTH;
$image_width = min( self::THUMB_MAX_WIDTH, $original_width );
if ( $original_width > self::THUMB_MAX_WIDTH ) {
$ratio = $original_width / self::THUMB_MAX_WIDTH;
$image_height = floor( $original_height / $ratio );
$image_height = intval( $original_height / $ratio );
} else {
$image_height = $original_height;
}
return array(
'height' => $image_height,
'width' => $image_width,
$image_height,
$image_width,
);
}

Expand Down Expand Up @@ -299,7 +308,7 @@ public function get_data( $attr ) {
$markup = wp_playlist_shortcode( $attr );
preg_match( self::PLAYLIST_REGEX, $markup, $matches );
if ( empty( $matches[1] ) ) {
return;
return array();
}
return json_decode( $matches[1], true );
}
Expand Down
26 changes: 21 additions & 5 deletions tests/test-class-amp-playlist-embed-handler.php
Expand Up @@ -153,7 +153,7 @@ public function test_get_thumb_dimensions() {
$track = array(
'thumb' => $dimensions,
);
$this->assertEquals( $dimensions, $this->instance->get_thumb_dimensions( $track ) );
$this->assertEquals( array_values( $dimensions ), $this->instance->get_thumb_dimensions( $track ) );

$dimensions = array(
'height' => 68,
Expand All @@ -162,25 +162,41 @@ public function test_get_thumb_dimensions() {
$track = array(
'thumb' => $dimensions,
);
$this->assertEquals( $dimensions, $this->instance->get_thumb_dimensions( $track ) );
$this->assertEquals( array_values( $dimensions ), $this->instance->get_thumb_dimensions( $track ) );

$dimensions = array(
'height' => 70,
'width' => 80.5,
);
$expected_dimensions = array(
'height' => 52,
'width' => 60,
52,
60,
);
$track = array(
'thumb' => $dimensions,
);
$this->assertEquals( $expected_dimensions, $this->instance->get_thumb_dimensions( $track ) );

$dimensions = array(
'width' => 80.5,
);
$track = array(
'thumb' => $dimensions,
);
$expected_dimensions = array(
48,
60,
);
$this->assertEquals( $expected_dimensions, $this->instance->get_thumb_dimensions( $track ) );

$track = array(
'thumb' => array(),
);
$this->assertEquals( array(), $this->instance->get_thumb_dimensions( $track ) );
$expected_dimensions = array(
AMP_Playlist_Embed_Handler::DEFAULT_THUMB_HEIGHT,
AMP_Playlist_Embed_Handler::DEFAULT_THUMB_WIDTH,
);
$this->assertEquals( $expected_dimensions, $this->instance->get_thumb_dimensions( $track ) );
}

/**
Expand Down

0 comments on commit 7cae35c

Please sign in to comment.