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

Add support for Instagram Reel permalinks #6170

Merged
merged 3 commits into from
May 3, 2021
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
42 changes: 29 additions & 13 deletions includes/embeds/class-amp-instagram-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

use AmpProject\Dom\Document;
use AmpProject\Tag;
use AmpProject\Attribute;

/**
* Class AMP_Instagram_Embed_Handler
Expand All @@ -16,7 +18,7 @@
*/
class AMP_Instagram_Embed_Handler extends AMP_Base_Embed_Handler {
const SHORT_URL_HOST = 'instagr.am';
const URL_PATTERN = '#https?:\/\/(www\.)?instagr(\.am|am\.com)\/(p|tv)\/([A-Za-z0-9-_]+)#i';
const URL_PATTERN = '#https?:\/\/(www\.)?instagr(\.am|am\.com)\/(p|tv|reel)\/([A-Za-z0-9-_]+)#i';

/**
* Default width.
Expand All @@ -42,7 +44,7 @@ class AMP_Instagram_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Tag.
*
* @var string AMP amp-facebook tag
* @var string AMP amp-instagram tag.
*/
private $amp_tag = 'amp-instagram';

Expand Down Expand Up @@ -170,20 +172,34 @@ public function sanitize_raw_embeds( Document $dom ) {
* @param DOMElement $node The DOMNode to adjust and replace.
*/
private function create_amp_instagram_and_replace_node( $dom, $node ) {
$instagram_id = $this->get_instagram_id_from_url( $node->getAttribute( 'data-instgrm-permalink' ) );
$permalink = $node->getAttribute( 'data-instgrm-permalink' );
$instagram_id = $this->get_instagram_id_from_url( $permalink );

$node_args = [
'data-shortcode' => $instagram_id,
'layout' => 'responsive',
'width' => $this->DEFAULT_WIDTH,
'height' => $this->DEFAULT_HEIGHT,
];
if ( $instagram_id ) {
$node_args = [
'data-shortcode' => $instagram_id,
'layout' => 'responsive',
'width' => $this->DEFAULT_WIDTH,
'height' => $this->DEFAULT_HEIGHT,
];

if ( true === $node->hasAttribute( 'data-instgrm-captioned' ) ) {
$node_args['data-captioned'] = '';
}
if ( true === $node->hasAttribute( 'data-instgrm-captioned' ) ) {
$node_args['data-captioned'] = '';
}

$new_node = AMP_DOM_Utils::create_node( $dom, $this->amp_tag, $node_args );
$new_node = AMP_DOM_Utils::create_node( $dom, $this->amp_tag, $node_args );
} else {
$new_node = AMP_DOM_Utils::create_node(
$dom,
Tag::A,
[
Attribute::HREF => esc_url_raw( $permalink ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In contrast with below, this “esc” is not actual escaping but rather sanitization. So this is fine.

Attribute::CLASS_ => 'amp-wp-embed-fallback',
]
);

$new_node->textContent = esc_html( $permalink );
pierlon marked this conversation as resolved.
Show resolved Hide resolved
}

$this->sanitize_embed_script( $node );

Expand Down
24 changes: 19 additions & 5 deletions tests/php/test-amp-instagram-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ class AMP_Instagram_Embed_Handler_Test extends WP_UnitTestCase {

public function get_conversion_data() {
return [
'no_embed' => [
'no_embed' => [
'<p>Hello world.</p>',
'<p>Hello world.</p>' . PHP_EOL,
],
'simple_url' => [
'simple_url' => [
'https://instagram.com/p/7-l0z_p4A4/' . PHP_EOL,
'<p><amp-instagram data-shortcode="7-l0z_p4A4" data-captioned layout="responsive" width="600" height="600"></amp-instagram></p>' . PHP_EOL,
],
'simple_tv_url' => [
'simple_tv_url' => [
'https://instagram.com/tv/7-l0z_p4A4/' . PHP_EOL,
'<p><amp-instagram data-shortcode="7-l0z_p4A4" data-captioned layout="responsive" width="600" height="600"></amp-instagram></p>' . PHP_EOL,
],
'short_url' => [
'simple_reel_url' => [
'https://instagram.com/reel/COWmlFLB_7P/' . PHP_EOL,
'<p><amp-instagram data-shortcode="COWmlFLB_7P" data-captioned layout="responsive" width="600" height="600"></amp-instagram></p>' . PHP_EOL,
],
'short_url' => [
'https://instagr.am/p/7-l0z_p4A4' . PHP_EOL,
'<p><amp-instagram data-shortcode="7-l0z_p4A4" data-captioned layout="responsive" width="600" height="600"></amp-instagram></p>' . PHP_EOL,
],
'short_tv_url' => [
'short_tv_url' => [
'https://instagr.am/tv/7-l0z_p4A4' . PHP_EOL,
'<p><amp-instagram data-shortcode="7-l0z_p4A4" data-captioned layout="responsive" width="600" height="600"></amp-instagram></p>' . PHP_EOL,
],
Expand Down Expand Up @@ -113,6 +117,11 @@ public function get_raw_embed_dataset() {
'<amp-instagram data-shortcode="BhsgU3jh6xE" layout="responsive" width="600" height="600"></amp-instagram>' . "\n\n",
],

'blockquote_reel_embed' => [
wpautop( '<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/reel/COWmlFLB_7P/"><div style="padding: 8px;">Lorem ipsum</div></blockquote> <script async defer src="//www.instagram.com/embed.js"></script>' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-instagram data-shortcode="COWmlFLB_7P" layout="responsive" width="600" height="600"></amp-instagram>' . "\n\n",
],

'blockquote_embed_notautop' => [
'<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/p/BhsgU3jh6xE/"><div style="padding: 8px;">Lorem ipsum</div></blockquote> <script async defer src="//www.instagram.com/embed.js"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-instagram data-shortcode="BhsgU3jh6xE" layout="responsive" width="600" height="600"></amp-instagram> ',
Expand All @@ -127,6 +136,11 @@ public function get_raw_embed_dataset() {
'<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/p/BhsgU3jh6xE/" data-instgrm-captioned><div style="padding: 8px;">Lorem ipsum</div></blockquote> <script async defer src="//www.instagram.com/embed.js"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<amp-instagram data-shortcode="BhsgU3jh6xE" layout="responsive" width="600" height="600" data-captioned=""></amp-instagram> ',
],

'blockquote_unsupported_permalink' => [
wpautop( '<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/unsupported/post_id"><div style="padding: 8px;">Lorem ipsum</div></blockquote> <script async defer src="//www.instagram.com/embed.js"></script>' ), // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript, WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
'<a href="https://www.instagram.com/unsupported/post_id" class="amp-wp-embed-fallback">https://www.instagram.com/unsupported/post_id</a>' . "\n\n",
],
];
}

Expand Down