diff --git a/includes/class-amp-post-template.php b/includes/class-amp-post-template.php index 3600501aa21..b67edb1209b 100644 --- a/includes/class-amp-post-template.php +++ b/includes/class-amp-post-template.php @@ -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' ); @@ -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(), diff --git a/includes/embeds/class-amp-soundcloud-embed.php b/includes/embeds/class-amp-soundcloud-embed.php new file mode 100644 index 00000000000..d44268d39af --- /dev/null +++ b/includes/embeds/class-amp-soundcloud-embed.php @@ -0,0 +1,95 @@ +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; + } +} diff --git a/tests/test-amp-soundcloud-embed.php b/tests/test-amp-soundcloud-embed.php new file mode 100644 index 00000000000..e0a13950736 --- /dev/null +++ b/tests/test-amp-soundcloud-embed.php @@ -0,0 +1,69 @@ + array( + '
Hello world.
', + 'Hello world.
' . PHP_EOL + ), + + 'url_simple' => array( + 'https://api.soundcloud.com/tracks/89299804' . PHP_EOL, + 'Hello World.
', + 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 ); + } +}