Skip to content

Commit 9e5caee

Browse files
kienstrajeherve
authored andcommitted
Migrate some of the AMP [soundcloud] shortcode conversion to J… (#14028)
* Migrate some of the AMP [soundcloud] shortcode handling to Jetpack The AMP plugin allows more $url values. For example, it allows: [soundcloud https://soundcloud.com/bonifansius/example-track] Whereas Jetpack's non-AMP handling only allows [souncloud url=<url here>]. @see https://github.com/ampproject/amp-wp/blob/e798258c22c465d60b4bd259f31d32c52817d6c7/includes/embeds/class-amp-soundcloud-embed.php#L128 * Copy a comment from the AMP plugin to this This is important to explain where the conversion will happen. * Commit Jeremy's suggestion to check for the URL being empty Co-Authored-By: Jeremy Herve <jeremy@tagada.hu> * Add a unit test for the AMP [soundcloud] logic This should run only if it's an AMP endpoint, and if the URL is not empty(). * Skip this test on wp.com environments, copying Jeremy's snippet Use Jeremy's snippet that we've used on other shortcode PRs.
1 parent 3e5dd2f commit 9e5caee

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

modules/shortcodes/soundcloud.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @return string Widget embed code HTML
3131
*/
3232
function soundcloud_shortcode( $atts, $content = null ) {
33+
global $wp_embed;
3334

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

112-
// Build our list of Souncloud parameters.
113+
if (
114+
class_exists( 'Jetpack_AMP_Support' )
115+
&& Jetpack_AMP_Support::is_amp_request()
116+
&& ! empty( $options['url'] )
117+
&& 'api.soundcloud.com' !== wp_parse_url( $options['url'], PHP_URL_HOST )
118+
) {
119+
// Defer to oEmbed if an oEmbeddable URL is provided.
120+
return $wp_embed->shortcode( $options, $options['url'] );
121+
}
122+
123+
// Build our list of Soundcloud parameters.
113124
$query_args = array(
114125
'url' => rawurlencode( $options['url'] ),
115126
);

tests/php/modules/shortcodes/test-class.soundcloud.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,40 @@ public function test_shortcodes_soundcloud_reversal_embed() {
116116

117117
$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>' );
118118
}
119+
120+
/**
121+
* Tests the shortcode output on an AMP endpoint.
122+
*
123+
* @covers ::soundcloud_shortcode
124+
* @since 8.0.0
125+
*/
126+
public function tests_shortcodes_soundcloud_amp() {
127+
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
128+
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
129+
return;
130+
}
131+
132+
// Simulate the oEmbed filter in the AMP plugin that should run on calling $wp_embed->shortcode().
133+
$oembed_markup = '<amp-soundcloud></amp-soundcloud>';
134+
add_filter(
135+
'embed_oembed_html',
136+
static function( $cache ) use ( $oembed_markup ) {
137+
unset( $cache );
138+
return $oembed_markup;
139+
}
140+
);
141+
142+
$content_with_url = '[soundcloud url="https://soundcloud.com/necmusic/mozart-concerto-for-piano-no-2"]';
143+
$content_with_empty_url = '[soundcloud url=""]';
144+
145+
// If the URL is empty, the AMP logic should not run.
146+
$this->assertNotContains( $oembed_markup, do_shortcode( $content_with_empty_url ) );
147+
148+
// This is still not an AMP endpoint, so the AMP logic should not run.
149+
$this->assertNotContains( $oembed_markup, do_shortcode( $content_with_url ) );
150+
151+
// Now that this is an AMP endpoint with a URL value, the AMP logic should run.
152+
add_filter( 'jetpack_is_amp_request', '__return_true' );
153+
$this->assertEquals( $oembed_markup, do_shortcode( $content_with_url ) );
154+
}
119155
}

0 commit comments

Comments
 (0)