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 some of the AMP [soundcloud] shortcode conversion to Jetpack #14028

Merged
merged 5 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
13 changes: 12 additions & 1 deletion modules/shortcodes/soundcloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @return string Widget embed code HTML
*/
function soundcloud_shortcode( $atts, $content = null ) {
global $wp_embed;

// Custom shortcode options.
$shortcode_options = array_merge(
Expand Down Expand Up @@ -109,7 +110,17 @@ function soundcloud_shortcode( $atts, $content = null ) {
$options['visual'] = false;
}

// Build our list of Souncloud parameters.
if (
class_exists( 'Jetpack_AMP_Support' )
&& Jetpack_AMP_Support::is_amp_request()
&& ! empty( $options['url'] )
&& 'api.soundcloud.com' !== wp_parse_url( $options['url'], PHP_URL_HOST )
) {
// Defer to oEmbed if an oEmbeddable URL is provided.
return $wp_embed->shortcode( $options, $options['url'] );
}

// Build our list of Soundcloud parameters.
$query_args = array(
'url' => rawurlencode( $options['url'] ),
);
Expand Down
36 changes: 36 additions & 0 deletions tests/php/modules/shortcodes/test-class.soundcloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,40 @@ public function test_shortcodes_soundcloud_reversal_embed() {

$this->assertEquals( $shortcode_content, '<a href="https://player.soundcloud.com/player.swf?url=http://api.soundcloud.com/tracks/70198773">https://player.soundcloud.com/player.swf?url=http://api.soundcloud.com/tracks/70198773</a>' );
}

/**
* Tests the shortcode output on an AMP endpoint.
*
* @covers ::soundcloud_shortcode
* @since 8.0.0
*/
public function tests_shortcodes_soundcloud_amp() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
return;
}

// Simulate the oEmbed filter in the AMP plugin that should run on calling $wp_embed->shortcode().
$oembed_markup = '<amp-soundcloud></amp-soundcloud>';
add_filter(
'embed_oembed_html',
static function( $cache ) use ( $oembed_markup ) {
unset( $cache );
return $oembed_markup;
}
);

$content_with_url = '[soundcloud url="https://soundcloud.com/necmusic/mozart-concerto-for-piano-no-2"]';
$content_with_empty_url = '[soundcloud url=""]';

// If the URL is empty, the AMP logic should not run.
$this->assertNotContains( $oembed_markup, do_shortcode( $content_with_empty_url ) );

// This is still not an AMP endpoint, so the AMP logic should not run.
$this->assertNotContains( $oembed_markup, do_shortcode( $content_with_url ) );

// Now that this is an AMP endpoint with a URL value, the AMP logic should run.
add_filter( 'jetpack_is_amp_request', '__return_true' );
$this->assertEquals( $oembed_markup, do_shortcode( $content_with_url ) );
}
}