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

Update how Imgur IDs are parsed #5485

Merged
merged 2 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
90 changes: 14 additions & 76 deletions includes/embeds/class-amp-imgur-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,14 @@ class AMP_Imgur_Embed_Handler extends AMP_Base_Embed_Handler {
* Register embed.
*/
public function register_embed() {
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.9', '<' ) ) {

// Before 4.9 the embedding Imgur is not working properly, register embed for that case.
wp_embed_register_handler( 'amp-imgur', self::URL_PATTERN, [ $this, 'oembed' ], -1 );
} else {
add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10, 3 );
}
add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10, 3 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.9', '<' ) ) {
wp_embed_unregister_handler( 'amp-imgur', -1 );
} else {
remove_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10 );
}
}

/**
* Oembed.
*
* @param array $matches Matches.
* @param array $attr Attributes.
* @param string $url URL.
* @return string Embed.
*/
public function oembed( $matches, $attr, $url ) {
return $this->render( [ 'url' => $url ] );
}

/**
* Render embed.
*
* @param array $args Args.
* @return string Embed.
*/
public function render( $args ) {
$args = wp_parse_args(
$args,
[
'url' => false,
]
);

if ( empty( $args['url'] ) ) {
return '';
}

$this->did_convert_elements = true;

$id = $this->get_imgur_id_from_url( $args['url'] );
if ( false === $id ) {
return '';
}
return AMP_HTML_Utils::build_tag(
'amp-imgur',
[
'width' => $this->args['width'],
'height' => $this->args['height'],
'data-imgur-id' => $id,
]
);
remove_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 10 );
}

/**
Expand All @@ -99,14 +43,12 @@ public function render( $args ) {
*/
public function filter_embed_oembed_html( $return, $url, $attr ) {
$parsed_url = wp_parse_url( $url );
if ( false !== strpos( $parsed_url['host'], 'imgur.com' ) ) {
if ( preg_match( '/width=["\']?(\d+)/', $return, $matches ) ) {
$attr['width'] = $matches[1];
}
if ( preg_match( '/height=["\']?(\d+)/', $return, $matches ) ) {
$attr['height'] = $matches[1];
}

if ( ! isset( $parsed_url['host'], $parsed_url['path'] ) ) {
return $return;
}

if ( false !== strpos( $parsed_url['host'], 'imgur.com' ) ) {
if ( empty( $attr['height'] ) ) {
return $return;
}
Expand All @@ -118,7 +60,7 @@ public function filter_embed_oembed_html( $return, $url, $attr ) {
$attributes['width'] = 'auto';
}

$attributes['data-imgur-id'] = $this->get_imgur_id_from_url( $url );
$attributes['data-imgur-id'] = $this->get_imgur_id_from_url( $parsed_url );
if ( false === $attributes['data-imgur-id'] ) {
return $return;
}
Expand All @@ -134,19 +76,15 @@ public function filter_embed_oembed_html( $return, $url, $attr ) {
/**
* Get Imgur ID from URL.
*
* @param string $url URL.
* @param array $parsed_url Parsed URL.
* @return bool|string ID / false.
*/
protected function get_imgur_id_from_url( $url ) {
$parsed_url = wp_parse_url( $url );
$pieces = explode( '/gallery/', $parsed_url['path'] );
if ( ! isset( $pieces[1] ) ) {
if ( ! preg_match( '/\/([A-Za-z0-9]+)/', $parsed_url['path'], $matches ) ) {
return false;
}
return $matches[1];
protected function get_imgur_id_from_url( $parsed_url ) {

if ( ! preg_match( '#^(?:/(a|gallery))?/([A-Za-z0-9]+)#', $parsed_url['path'], $matches ) ) {
return false;
}

return $pieces[1];
return ! empty( $matches[1] ) ? "a/{$matches[2]}" : $matches[2];
}
}
19 changes: 13 additions & 6 deletions tests/php/test-class-amp-imgur-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static function( $pre, $r, $url ) {
$body = '{"version":"1.0","type":"rich","provider_name":"Imgur","provider_url":"https:\/\/imgur.com","width":500,"height":750,"html":"<blockquote class=\"imgur-embed-pub\" lang=\"en\" data-id=\"fmHGADZ\"><a href=\"https:\/\/imgur.com\/fmHGADZ\">View post on imgur.com<\/a><\/blockquote><script async src=\"\/\/s.imgur.com\/min\/embed.js\" charset=\"utf-8\"><\/script>"}'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
} elseif ( false !== strpos( $url, '1ApvcWB' ) ) {
$body = '{"version":"1.0","type":"rich","provider_name":"Imgur","provider_url":"https:\/\/imgur.com","width":500,"height":750,"html":"<blockquote class=\"imgur-embed-pub\" lang=\"en\" data-id=\"a\/1ApvcWB\"><a href=\"https:\/\/imgur.com\/a\/1ApvcWB\">Oops, all baby yoda<\/a><\/blockquote><script async src=\"\/\/s.imgur.com\/min\/embed.js\" charset=\"utf-8\"><\/script>"}'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
} elseif ( false !== strpos( $url, 'rAG6Q2w' ) ) {
$body = '{"version":"1.0","type":"rich","provider_name":"Imgur","provider_url":"https:\/\/imgur.com","width":540,"height":500,"html":"<blockquote class=\"imgur-embed-pub\" lang=\"en\" data-id=\"a\/rAG6Q2w\"><a href=\"https:\/\/imgur.com\/a\/rAG6Q2w\">View post on imgur.com<\/a><\/blockquote><script async src=\"\/\/s.imgur.com\/min\/embed.js\" charset=\"utf-8\"><\/script>"}'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
} else {
return $pre;
}
Expand Down Expand Up @@ -61,24 +63,29 @@ public function get_conversion_data() {
$height = 750;

return [
'no_embed' => [
'no_embed' => [
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL,
],

'url_simple' => [
'url_simple' => [
'https://imgur.com/fmHGADZ' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="fmHGADZ"></amp-imgur></p>' . PHP_EOL,
],

'url_with_detail' => [
'album_url' => [
'https://imgur.com/a/rAG6Q2w' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="a/rAG6Q2w"></amp-imgur></p>' . PHP_EOL,
],

'gallery_url' => [
'https://imgur.com/gallery/1ApvcWB' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="1ApvcWB"></amp-imgur></p>' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="a/1ApvcWB"></amp-imgur></p>' . PHP_EOL,
],

'url_with_params' => [
'gallery_url__with_params' => [
'https://imgur.com/gallery/1ApvcWB?foo=bar' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="1ApvcWB"></amp-imgur></p>' . PHP_EOL,
'<p><amp-imgur width="' . $width . '" height="' . $height . '" data-imgur-id="a/1ApvcWB"></amp-imgur></p>' . PHP_EOL,
],

];
Expand Down