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

Migrate to Jetpack the [instagram] shortcode AMP handling #14053

Merged
merged 4 commits into from
Nov 19, 2019
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
21 changes: 21 additions & 0 deletions modules/shortcodes/instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ function jetpack_shortcode_instagram( $atts ) {
return '';
}

if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
$url_pattern = '#http(s?)://(www\.)?instagr(\.am|am\.com)/p/([^/?]+)#i';
jeherve marked this conversation as resolved.
Show resolved Hide resolved
preg_match( $url_pattern, $atts['url'], $matches );
if ( ! $matches ) {
return sprintf(
'<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>',
esc_url( $atts['url'] )
);
}

$shortcode_id = end( $matches );
$width = ! empty( $atts['width'] ) ? $atts['width'] : 600;
$height = ! empty( $atts['height'] ) ? $atts['height'] : 600;
jeherve marked this conversation as resolved.
Show resolved Hide resolved
return sprintf(
'<amp-instagram data-shortcode="%1$s" layout="responsive" width="%2$d" height="%3$d" data-captioned></amp-instagram>',
esc_attr( $shortcode_id ),
absint( $width ),
absint( $height )
);
}

return $wp_embed->shortcode( $atts, $atts['url'] );
}
add_shortcode( 'instagram', 'jetpack_shortcode_instagram' );
80 changes: 80 additions & 0 deletions tests/php/modules/shortcodes/test-class.instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,84 @@ public function test_shortcode_instagram_via_oembed_http_request() {
$shortcode_content
);
}

/**
* Gets the test data for test_shortcodes_instagram_amp().
*
* @return array The test data.
*/
public function get_instagram_amp_data() {
$shortcode_id = 'BnMOk_FFsxg';
$url_with_id = 'https://www.instagram.com/p/' . $shortcode_id;
$short_url_with_id = 'https://instagr.am/p/' . $shortcode_id;
$wrong_url_with_id = 'https://www.twitter.com/p/' . $shortcode_id;
$default_dimension = 600;

return array(
'no_attribute' => array(
'[instagram]',
'',
),
'plain_url' => array(
'[instagram ' . $url_with_id . ']',
'',
),
'non_instagram_url' => array(
'[instagram url=' . $wrong_url_with_id . ']',
'<a href="' . $wrong_url_with_id . '" class="amp-wp-embed-fallback">' . $wrong_url_with_id . '</a>',
),
'url_value_as_attribute' => array(
'[instagram url=' . $url_with_id . ']',
'<amp-instagram data-shortcode="'. $shortcode_id .'" layout="responsive" width="' . $default_dimension . '" height="' . $default_dimension . '" data-captioned></amp-instagram>',
),
'short_url_value_as_attribute' => array(
'[instagram url=' . $short_url_with_id . ']',
'<amp-instagram data-shortcode="'. $shortcode_id .'" layout="responsive" width="' . $default_dimension . '" height="' . $default_dimension . '" data-captioned></amp-instagram>',
),
'width_in_attributes' => array(
'[instagram url=' . $url_with_id . ' width=300]',
'<amp-instagram data-shortcode="'. $shortcode_id .'" layout="responsive" width="300" height="' . $default_dimension . '" data-captioned></amp-instagram>',
),
'width_and_height_in_attributes' => array(
'[instagram url=' . $url_with_id . ' width=300 height="200"]',
'<amp-instagram data-shortcode="'. $shortcode_id .'" layout="responsive" width="300" height="200" data-captioned></amp-instagram>',
),
'0_width_in_attributes' => array(
'[instagram url=' . $url_with_id . ' width=0]',
'<amp-instagram data-shortcode="'. $shortcode_id .'" layout="responsive" width="' . $default_dimension . '" height="' . $default_dimension . '" data-captioned></amp-instagram>',
),
);
}

/**
* Test the AMP-compatible [instagram] shortcode on an AMP endpoint.
*
* @dataProvider get_instagram_amp_data
* @since 8.0.0
*
* @param string $shortcode_content The shortcode content, as entered in the editor.
* @param string $expected The expected return value of the function.
*/
public function test_shortcodes_instagram_amp( $shortcode_content, $expected ) {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
return;
}

add_filter( 'jetpack_is_amp_request', '__return_true' );
kienstra marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEquals( $expected, do_shortcode( $shortcode_content ) );
}

/**
* Test that the AMP [instagram] shortcode logic doesn't run on a non-AMP endpoint.
*
* @dataProvider get_instagram_amp_data
* @since 8.0.0
*
* @param string $shortcode_content The shortcode as entered in the editor.
*/
public function test_shortcodes_instagram_non_amp( $shortcode_content ) {
add_filter( 'jetpack_is_amp_request', '__return_false' );
$this->assertNotContains( 'amp-instagram', do_shortcode( $shortcode_content ) );
}
}