Skip to content

Commit

Permalink
Fix detecting gfycat ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
miina committed May 14, 2018
1 parent 46edf1e commit e1d29a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
28 changes: 21 additions & 7 deletions includes/embeds/class-amp-gfycat-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AMP_Gfycat_Embed_Handler extends AMP_Base_Embed_Handler {
*
* @var string
*/
const URL_PATTERN = '#https?://(www\.)?gfycat\.com/gifs/detail/.*#i';
const URL_PATTERN = '#https?://(www\.)?gfycat\.com/.*#i';

/**
* Register embed.
Expand All @@ -42,27 +42,41 @@ public function unregister_embed() {
public function filter_embed_oembed_html( $return, $url, $attr ) {
$parsed_url = wp_parse_url( $url );
if ( false !== strpos( $parsed_url['host'], 'gfycat.com' ) ) {
if ( preg_match( '/width=(?:"|\')(\d+)(?:"|\')/', $return, $matches ) ) {
if ( preg_match( '/width=["\']?(\d+)/', $return, $matches ) ) {
$attr['width'] = $matches[1];
}
if ( preg_match( '/height=(?:"|\')(\d+)(?:"|\')/', $return, $matches ) ) {
if ( preg_match( '/height=["\']?(\d+)/', $return, $matches ) ) {
$attr['height'] = $matches[1];
}

$pieces = explode( '/detail/', $parsed_url['path'] );
if ( ! isset( $pieces[1] ) ) {
if ( empty( $attr['height'] ) ) {
return $return;
}

$data_gfyid = $pieces[1];
$layout = 'intrinsic';

if ( empty( $attr['width'] ) ) {
$layout = 'fixed-height';
$attr['width'] = 'auto';
}

$pieces = explode( '/detail/', $parsed_url['path'] );
if ( ! isset( $pieces[1] ) ) {
if ( ! preg_match( '/\/([A-Za-z0-9]+)/', $parsed_url['path'], $matches ) ) {
return $return;
}
$data_gfyid = $matches[1];
} else {
$data_gfyid = $pieces[1];
}

$return = AMP_HTML_Utils::build_tag(
'amp-gfycat',
array(
'width' => $attr['width'],
'height' => $attr['height'],
'data-gfyid' => $data_gfyid,
'layout' => 'intrinsic',
'layout' => $layout,
)
);
}
Expand Down
16 changes: 13 additions & 3 deletions tests/test-class-amp-gfycat-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ public function setUp() {
*/
public function get_conversion_data() {
return array(
'no_embed' => array(
'no_embed' => array(
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL,
),

'url_simple' => array(
'url_simple' => array(
'https://gfycat.com/tautwhoppingcougar' . PHP_EOL,
'<p><amp-gfycat width="500" height="281" data-gfyid="tautwhoppingcougar" layout="intrinsic"></amp-gfycat></p>' . PHP_EOL,
),

'url_with_detail' => array(
'https://gfycat.com/gifs/detail/tautwhoppingcougar' . PHP_EOL,
'<p><amp-gfycat width="500" height="750" data-gfyid="tautwhoppingcougar" layout="intrinsic"></amp-gfycat></p>' . PHP_EOL,
'<p><amp-gfycat width="500" height="281" data-gfyid="tautwhoppingcougar" layout="intrinsic"></amp-gfycat></p>' . PHP_EOL,
),

'url_with_params' => array(
'https://gfycat.com/gifs/detail/tautwhoppingcougar?foo=bar' . PHP_EOL,
'<p><amp-gfycat width="500" height="281" data-gfyid="tautwhoppingcougar" layout="intrinsic"></amp-gfycat></p>' . PHP_EOL,
),

);
Expand Down

0 comments on commit e1d29a8

Please sign in to comment.