Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of unlisted Vimeo videos #2986

Merged
merged 2 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions tests/php/test-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
<?php
/**
* Class AMP_Vimeo_Embed_Test
*
* @package AMP
*/

/**
* Class AMP_Vimeo_Embed_Test
*
* @covers AMP_SoundCloud_Embed_Handler
westonruter marked this conversation as resolved.
Show resolved Hide resolved
*/
class AMP_Vimeo_Embed_Test extends WP_UnitTestCase {

/**
* Get conversion data.
*
* @return array
*/
public function get_conversion_data() {
return [
'no_embed' => [
Expand All @@ -13,6 +29,11 @@ public function get_conversion_data() {
'<p><amp-vimeo data-videoid="172355597" layout="responsive" width="600" height="338"></amp-vimeo></p>' . PHP_EOL,
],

'url_unlisted' => [
'https://vimeo.com/172355597/abcdef0123' . PHP_EOL,
'<p><amp-vimeo data-videoid="172355597" layout="responsive" width="600" height="338"></amp-vimeo></p>' . PHP_EOL,
],

'shortcode_unnamed_attr_as_url' => [
'[vimeo https://vimeo.com/172355597]' . PHP_EOL,
'<amp-vimeo data-videoid="172355597" layout="responsive" width="600" height="338"></amp-vimeo>' . PHP_EOL,
Expand All @@ -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();
Expand All @@ -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' => [
Expand All @@ -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();
Expand Down