Skip to content

Commit

Permalink
Extend WordPress.tv embed handler to also handle VideoPress
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 22, 2020
1 parent 1ec497a commit bb0a315
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
12 changes: 4 additions & 8 deletions includes/embeds/class-amp-wordpress-tv-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
/**
* Class AMP_WordPress_TV_Embed_Handler
*
* This sanitizes embeds both for WordPress.tv and for VideoPress (which use the same underlying infrastructure).
*
* @since 1.4
*/
class AMP_WordPress_TV_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* The URL pattern to determine if an embed URL is for this type, copied from WP_oEmbed.
*
* @see https://github.com/WordPress/wordpress-develop/blob/e13480/src/wp-includes/class-wp-oembed.php#L64
*/
const URL_PATTERN = '#https?://wordpress\.tv/.*#i';

/**
* Register embed.
*/
Expand All @@ -42,7 +37,8 @@ public function unregister_embed() {
* @return string The filtered embed markup.
*/
public function filter_oembed_html( $cache, $url ) {
if ( ! preg_match( self::URL_PATTERN, $url ) ) {
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! in_array( $host, [ 'wordpress.tv', 'videopress.com' ], true ) ) {
return $cache;
}

Expand Down
50 changes: 37 additions & 13 deletions tests/php/test-class-amp-wordpress-tv-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,30 @@ public function mock_http_request( $preempt, $r, $url ) {
return $preempt;
}

if ( false === strpos( $url, 'wordpress.tv' ) ) {
return $preempt;
$body = null;

$host = wp_parse_url( $url, PHP_URL_HOST );
if ( 'wordpress.tv' === $host ) {
$body = '{"type":"video","version":"1.0","title":null,"width":500,"height":281,"html":"<iframe width=\'500\' height=\'281\' src=\'https:\\/\\/video.wordpress.com\\/embed\\/yFCmLMGL?hd=0\' frameborder=\'0\' allowfullscreen><\\/iframe><script src=\'https:\\/\\/v0.wordpress.com\\/js\\/next\\/videopress-iframe.js?m=1435166243\'></script>"}'; // phpcs:ignore
} elseif ( 'videopress.com' === $host ) {
$body = '{"version":"1.0","provider_name":"VideoPress","provider_url":"https:\/\/videopress.com\/","type":"video","title":"VideoPress Demo","width":560,"height":316,"thumbnail_url":"https:\/\/videos.files.wordpress.com\/kUJmAcSf\/bbb_sunflower_1080p_30fps_normal_scruberthumbnail_2.jpg","thumbnail_width":560,"thumbnail_height":316,"html":"<iframe width=\'560\' height=\'316\' src=\'https:\/\/video.wordpress.com\/embed\/kUJmAcSf?hd=0\' frameborder=\'0\' allowfullscreen><\/iframe><script src=\'https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1435166243\'><\/script>"}'; // phpcs:ignore
}

if ( isset( $body ) ) {
return [
'body' => $body,
'headers' => [],
'response' => [
'code' => 200,
'message' => 'ok',
],
'cookies' => [],
'http_response' => null,
];
}

unset( $r );
return [
'body' => '{"type":"video","version":"1.0","title":null,"width":500,"height":281,"html":"<iframe width=\'500\' height=\'281\' src=\'https:\\/\\/video.wordpress.com\\/embed\\/yFCmLMGL?hd=0\' frameborder=\'0\' allowfullscreen><\\/iframe><script src=\'https:\\/\\/v0.wordpress.com\\/js\\/next\\/videopress-iframe.js?m=1435166243\'></script>"}', // phpcs:ignore
'headers' => [],
'response' => [
'code' => 200,
'message' => 'ok',
],
'cookies' => [],
'http_response' => null,
];
return $preempt;
}

/**
Expand All @@ -74,10 +84,24 @@ public function test_script_removal() {
<!-- /wp:core-embed/wordpress-tv -->
';

$videopress_block = '
<!-- wp:core-embed/videopress {"url":"https://videopress.com/v/kUJmAcSf","type":"video","providerNameSlug":"videopress","className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed-videopress wp-block-embed is-type-video is-provider-videopress wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://videopress.com/v/kUJmAcSf
</div></figure>
<!-- /wp:core-embed/videopress -->
';

$handler->register_embed();

$rendered = apply_filters( 'the_content', $wordpress_tv_block );
$this->assertStringContains( '<iframe', $rendered );
$this->assertStringContains( 'video.wordpress.com/embed', $rendered );
$this->assertStringContains( 'https://video.wordpress.com/embed/yFCmLMGL', $rendered );
$this->assertStringNotContains( '<script', $rendered );

$rendered = apply_filters( 'the_content', $videopress_block );
$this->assertStringContains( '<iframe', $rendered );
$this->assertStringContains( 'https://video.wordpress.com/embed/kUJmAcSf', $rendered );
$this->assertStringNotContains( '<script', $rendered );
}

Expand Down

0 comments on commit bb0a315

Please sign in to comment.