Skip to content

Commit

Permalink
Add support for amp-soundcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
amedina committed Feb 20, 2017
1 parent 0c2f106 commit f827814
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions includes/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

require_once( AMP__DIR__ . '/includes/embeds/class-amp-twitter-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-youtube-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-soundcloud-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-gallery-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-instagram-embed.php' );
require_once( AMP__DIR__ . '/includes/embeds/class-amp-vine-embed.php' );
Expand Down Expand Up @@ -223,6 +224,7 @@ private function build_post_content() {
apply_filters( 'amp_content_embed_handlers', array(
'AMP_Twitter_Embed_Handler' => array(),
'AMP_YouTube_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
Expand Down
95 changes: 95 additions & 0 deletions includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

class AMP_SoundCloud_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#https?://api\.soundcloud\.com/tracks/.*#i';
protected $DEFAULT_HEIGHT = 200;

private static $script_slug = 'amp-soundcloud';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-soundcloud-0.1.js';

public function register_embed() {
wp_embed_register_handler( 'amp-soundcloud', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
add_shortcode( 'soundcloud', array( $this, 'shortcode' ) );
}

public function unregister_embed() {
wp_embed_unregister_handler( 'amp-soundcloud', -1 );
remove_shortcode( 'soundcloud' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function oembed( $matches, $attr, $url, $rawattr ) {
$track_id = $this->get_track_id_from_url($url);
return $this->render( array(
'track_id' => $track_id
) );
}

public function shortcode( $attr ) {

$track_id = false;

if ( isset( $attr["id"] ) ) {
$track_id = $attr["id"];
} else {
$url = false;
if (isset($attr["url"])) {
$url = $attr["url"];
} elseif (isset($attr[0])) {
$url = $attr[0];
} elseif (function_exists('shortcode_new_to_old_params')) {
$url = shortcode_new_to_old_params($attr);
}

if (!empty($url)) {
$track_id = $this->get_track_id_from_url($url);
}
}

if (empty($track_id)) {
return '';
}

return $this->render( array(
'track_id' => $track_id,
) );
}

public function render( $args ) {
$args = wp_parse_args( $args, array(
'track_id' => false,
) );

if ( empty( $args['track_id'] ) ) {
return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
}

$this->did_convert_elements = true;

return AMP_HTML_Utils::build_tag(
'amp-soundcloud',
array(
'data-trackid' => $args['track_id'],
'layout' => 'fixed-height',
'height' => $this->args['height'],
)
);
}

private function get_track_id_from_url( $url ) {
$parsed_url = parse_url( $url );
$tok = explode("/", $parsed_url['path']);
$track_id = $tok[2];

return $track_id;
}
}
69 changes: 69 additions & 0 deletions tests/test-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

class AMP_SoundCloud_Embed_Test extends WP_UnitTestCase {
public function get_conversion_data() {
return array(
'no_embed' => array(
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL
),

'url_simple' => array(
'https://api.soundcloud.com/tracks/89299804' . PHP_EOL,
'<p><amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud></p>' . PHP_EOL
),

'shortcode_unnamed_attr_as_url' => array(
'[soundcloud https://api.soundcloud.com/tracks/89299804]' . PHP_EOL,
'<amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud>' . PHP_EOL
),

'shortcode_named_attr_url' => array(
'[soundcloud url=https://api.soundcloud.com/tracks/89299804]' . PHP_EOL,
'<amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud>' . PHP_EOL
),

'shortcode_named_attr_url' => array(
'[soundcloud id=89299804]' . PHP_EOL,
'<amp-soundcloud data-trackid="89299804" layout="fixed-height" height="200"></amp-soundcloud>' . PHP_EOL
),

);
}

/**
* @dataProvider get_conversion_data
*/
public function test__conversion( $source, $expected ) {
$embed = new AMP_SoundCloud_Embed_Handler();
$embed->register_embed();
$filtered_content = apply_filters( 'the_content', $source );

$this->assertEquals( $expected, $filtered_content );
}

public function get_scripts_data() {
return array(
'not_converted' => array(
'<p>Hello World.</p>',
array()
),
'converted' => array(
'https://api.soundcloud.com/tracks/89299804' . PHP_EOL,
array( 'amp-soundcloud' => 'https://cdn.ampproject.org/v0/amp-soundcloud-0.1.js' )
),
);
}

/**
* @dataProvider get_scripts_data
*/
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_SoundCloud_Embed_Handler();
$embed->register_embed();
apply_filters( 'the_content', $source );
$scripts = $embed->get_scripts();

$this->assertEquals( $expected, $scripts );
}
}

0 comments on commit f827814

Please sign in to comment.