-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |