diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 2cfa173c630..4d607f2af93 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -447,6 +447,7 @@ function amp_get_content_embed_handlers( $post = null ) { 'AMP_Reddit_Embed_Handler' => array(), 'AMP_Tumblr_Embed_Handler' => array(), 'AMP_Gallery_Embed_Handler' => array(), + 'AMP_Gfycat_Embed_Handler' => array(), 'WPCOM_AMP_Polldaddy_Embed' => array(), ), $post diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index c86307f0a77..8c252673655 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -39,6 +39,7 @@ class AMP_Autoloader { 'AMP_DailyMotion_Embed_Handler' => 'includes/embeds/class-amp-dailymotion-embed', 'AMP_Facebook_Embed_Handler' => 'includes/embeds/class-amp-facebook-embed', 'AMP_Gallery_Embed_Handler' => 'includes/embeds/class-amp-gallery-embed', + 'AMP_Gfycat_Embed_Handler' => 'includes/embeds/class-amp-gfycat-embed-handler', 'AMP_Core_Block_Handler' => 'includes/embeds/class-amp-core-block-handler', 'AMP_Instagram_Embed_Handler' => 'includes/embeds/class-amp-instagram-embed', 'AMP_Issuu_Embed_Handler' => 'includes/embeds/class-amp-issuu-embed-handler', diff --git a/includes/embeds/class-amp-gfycat-embed-handler.php b/includes/embeds/class-amp-gfycat-embed-handler.php new file mode 100644 index 00000000000..7948d7a1d0f --- /dev/null +++ b/includes/embeds/class-amp-gfycat-embed-handler.php @@ -0,0 +1,81 @@ +factory()->post->create_and_get(); + } + } + + /** + * Get conversion data. + * + * @return array + */ + public function get_conversion_data() { + return array( + 'no_embed' => array( + '

Hello world.

', + '

Hello world.

' . PHP_EOL, + ), + + 'url_simple' => array( + 'https://gfycat.com/tautwhoppingcougar' . PHP_EOL, + '

' . PHP_EOL, + ), + + 'url_with_detail' => array( + 'https://gfycat.com/gifs/detail/tautwhoppingcougar' . PHP_EOL, + '

' . PHP_EOL, + ), + + 'url_with_params' => array( + 'https://gfycat.com/gifs/detail/tautwhoppingcougar?foo=bar' . PHP_EOL, + '

' . PHP_EOL, + ), + + ); + } + + /** + * Test conversion. + * + * @param string $source Source. + * @param string $expected Expected. + * @dataProvider get_conversion_data + */ + public function test__conversion( $source, $expected ) { + $embed = new AMP_Gfycat_Embed_Handler(); + $embed->register_embed(); + $filtered_content = apply_filters( 'the_content', $source ); + + $this->assertEquals( $expected, $filtered_content ); + } + + /** + * Get scripts data. + * + * @return array + */ + public function get_scripts_data() { + return array( + 'not_converted' => array( + '

Hello World.

', + array(), + ), + 'converted' => array( + 'https://www.gfycat.com/gifs/detail/tautwhoppingcougar' . PHP_EOL, + array( 'amp-gfycat' => true ), + ), + ); + } + + /** + * Test scripts. + * + * @param string $source Source. + * @param string $expected Expected. + * @dataProvider get_scripts_data + */ + public function test__get_scripts( $source, $expected ) { + $embed = new AMP_Gfycat_Embed_Handler(); + $embed->register_embed(); + $source = apply_filters( 'the_content', $source ); + + $whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) ); + $whitelist_sanitizer->sanitize(); + + $scripts = array_merge( + $embed->get_scripts(), + $whitelist_sanitizer->get_scripts() + ); + + $this->assertEquals( $expected, $scripts ); + } +}