From 5b86a63bcebd7249d5e9ae532d7a1d8a0ec1c5fb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 7 Aug 2019 16:26:15 -0700 Subject: [PATCH 1/2] Improve handling of unlisted Vimeo videos --- includes/embeds/class-amp-vimeo-embed.php | 11 ++++--- tests/php/test-amp-vimeo-embed.php | 36 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/includes/embeds/class-amp-vimeo-embed.php b/includes/embeds/class-amp-vimeo-embed.php index 9dcf3468e7b..af5aa5ee1fa 100644 --- a/includes/embeds/class-amp-vimeo-embed.php +++ b/includes/embeds/class-amp-vimeo-embed.php @@ -156,16 +156,15 @@ public function render( $args ) { * Determine the video ID from the URL. * * @param string $url URL. - * @return integer Video ID. + * @return int Video ID. */ private function get_video_id_from_url( $url ) { - $parsed_url = wp_parse_url( $url ); - parse_str( $parsed_url['path'], $path ); + $path = wp_parse_url( $url, PHP_URL_PATH ); + // @todo This will not get the private key for unlisted videos (which look like https://vimeo.com/123456789/abcdef0123), but amp-vimeo doesn't support them currently anyway. $video_id = ''; - if ( $path ) { - $tok = explode( '/', $parsed_url['path'] ); - $video_id = end( $tok ); + if ( $path && preg_match( ':^/(\d+):', $path, $matches ) ) { + $video_id = $matches[1]; } return $video_id; diff --git a/tests/php/test-amp-vimeo-embed.php b/tests/php/test-amp-vimeo-embed.php index db2786e21aa..b067483ee2b 100644 --- a/tests/php/test-amp-vimeo-embed.php +++ b/tests/php/test-amp-vimeo-embed.php @@ -1,6 +1,22 @@ [ @@ -13,6 +29,11 @@ public function get_conversion_data() { '

' . PHP_EOL, ], + 'url_unlisted' => [ + 'https://vimeo.com/172355597/abcdef0123' . PHP_EOL, + '

' . PHP_EOL, + ], + 'shortcode_unnamed_attr_as_url' => [ '[vimeo https://vimeo.com/172355597]' . PHP_EOL, '' . PHP_EOL, @@ -32,7 +53,12 @@ public function get_conversion_data() { } /** + * Test conversion. + * * @dataProvider get_conversion_data + * + * @param string $source Source. + * @param string $expected Expected. */ public function test__conversion( $source, $expected ) { $embed = new AMP_Vimeo_Embed_Handler(); @@ -42,6 +68,11 @@ public function test__conversion( $source, $expected ) { $this->assertEquals( $expected, $filtered_content ); } + /** + * Get scripts data. + * + * @return array Scripts data. + */ public function get_scripts_data() { return [ 'not_converted' => [ @@ -56,7 +87,12 @@ public function get_scripts_data() { } /** + * Test get_scripts(). + * * @dataProvider get_scripts_data + * + * @param string $source Source. + * @param string $expected Expected. */ public function test__get_scripts( $source, $expected ) { $embed = new AMP_Vimeo_Embed_Handler(); From 8855993ae3cd3cdc72ae5ef943dfb0e422342e57 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 9 Aug 2019 07:14:06 -0700 Subject: [PATCH 2/2] Fix class name in covers phpdoc tag --- tests/php/test-amp-vimeo-embed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/test-amp-vimeo-embed.php b/tests/php/test-amp-vimeo-embed.php index b067483ee2b..fa0914a7987 100644 --- a/tests/php/test-amp-vimeo-embed.php +++ b/tests/php/test-amp-vimeo-embed.php @@ -8,7 +8,7 @@ /** * Class AMP_Vimeo_Embed_Test * - * @covers AMP_SoundCloud_Embed_Handler + * @covers AMP_Vimeo_Embed_Handler */ class AMP_Vimeo_Embed_Test extends WP_UnitTestCase {