Skip to content

Commit

Permalink
For WordPress.tv embed, Use an oembed filter instead of block filter (#…
Browse files Browse the repository at this point in the history
…4164)

As Weston pointed out,
it's still possible to have this embed in the classic editor.
  • Loading branch information
kienstra authored and westonruter committed Jan 22, 2020
1 parent 6aafa43 commit 7f15936
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 20 deletions.
30 changes: 18 additions & 12 deletions includes/embeds/class-amp-wordpress-tv-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,40 @@
*/
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.
*/
public function register_embed() {
add_filter( 'render_block', [ $this, 'filter_rendered_block' ], 10, 2 );
add_filter( 'embed_oembed_html', [ $this, 'filter_oembed_html' ], 10, 2 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
remove_filter( 'render_block', [ $this, 'filter_rendered_block' ], 10 );
remove_filter( 'embed_oembed_html', [ $this, 'filter_oembed_html' ], 10 );
}

/**
* Filters the content of a single block to make it AMP valid.
* Filters the oembed HTML to make it valid AMP.
*
* @param string $block_content The block content about to be appended.
* @param array $block The full block, including name and attributes.
* @return string Filtered block content.
* @param mixed $cache The cached rendered markup.
* @param string $url The embed URL.
* @return string The filtered embed markup.
*/
public function filter_rendered_block( $block_content, $block ) {
if ( ! isset( $block['blockName'] ) || 'core-embed/wordpress-tv' !== $block['blockName'] ) {
return $block_content;
public function filter_oembed_html( $cache, $url ) {
if ( ! preg_match( self::URL_PATTERN, $url ) ) {
return $cache;
}

$modified_block_content = preg_replace( '#<script(?:\s.*?)?>.*?</script>#s', '', $block_content );

return null !== $modified_block_content ? $modified_block_content : $block_content;
$modified_block_content = preg_replace( '#<script(?:\s.*?)?>.*?</script>#s', '', $cache );
return null !== $modified_block_content ? $modified_block_content : $cache;
}
}
80 changes: 72 additions & 8 deletions tests/php/test-class-amp-wordpress-tv-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ class Test_AMP_WordPress_TV_Embed_Handler extends WP_UnitTestCase {
* Set up.
*/
public function setUp() {
if ( ! function_exists( 'register_block_type' ) ) {
$this->markTestIncomplete( 'Files needed for testing missing.' );
}
if ( version_compare( get_bloginfo( 'version' ), '5.0', '<' ) ) {
$this->markTestSkipped( 'Missing required render_block filter.' );
}
parent::setUp();

add_filter( 'pre_http_request', [ $this, 'mock_http_request' ], 10, 3 );
}

Expand Down Expand Up @@ -61,7 +54,7 @@ public function mock_http_request( $preempt, $r, $url ) {
/**
* Test that the script tag that VideoPress adds is removed by the sanitizer.
*
* @covers AMP_WordPress_TV_Embed_Handler::filter_rendered_block()
* @covers AMP_WordPress_TV_Embed_Handler::filter_oembed_html()
*/
public function test_script_removal() {
$handler = new AMP_WordPress_TV_Embed_Handler();
Expand All @@ -83,4 +76,75 @@ public function test_script_removal() {
$this->assertContains( 'videopress.com/embed', $rendered );
$this->assertNotContains( '<script', $rendered );
}

/**
* Gets the test data for test_filter_oembed_html().
*
* @return array The test data.
*/
public function get_filter_oembed_data() {
$embed_without_script = '<p>Example Embed</p>';
$embed_with_script = $embed_without_script . '<script>doThis();</script>';

return [
'wrong_embed_url_domain' => [
$embed_without_script,
'https://incorrect.com',
null,
],
'wrong_embed_url_wordpress_com' => [
$embed_without_script,
'https://wordpress.com/123',
null,
],
'wrong_embed_url_no_protocol' => [
$embed_without_script,
'//wordpress.tv/',
null,
],
'correct_embed_url_http' => [
$embed_with_script,
'https://wordpress.tv/123',
$embed_without_script,
],
'correct_embed_url_https' => [
$embed_with_script,
'https://wordpress.tv/123',
$embed_without_script,
],
'correct_embed_url_no_script' => [
$embed_without_script,
'https://wordpress.tv/123',
null,
],
'correct_embed_url_text_script_not_tag' => [
'This is the script for the play',
'https://wordpress.tv/123',
null,
],
];
}

/**
* Test filter_oembed_html
*
* @dataProvider get_filter_oembed_data
* @covers AMP_WordPress_TV_Embed_Handler::filter_oembed_html()
*
* @param mixed $cache The cached markup.
* @param string $url The URL of the embed.
* @param string $expected The expected return value.
*/
public function test_filter_oembed_html( $cache, $url, $expected ) {
if ( null === $expected ) {
$expected = $cache;
}

$handler = new AMP_WordPress_TV_Embed_Handler();
$this->assertEquals(
$expected,
$handler->filter_oembed_html( $cache, $url )
);

}
}

0 comments on commit 7f15936

Please sign in to comment.