From 3a0fb0bb0e2285d0ee62ade24a714d04715ac365 Mon Sep 17 00:00:00 2001 From: Daniel Walmsley Date: Wed, 18 Dec 2019 09:15:45 -0800 Subject: [PATCH] Reverse old embed HTML if it has videopress URLs in it (#14249) * Reverse old embed HTML if it has videopress URLs in it to an oembed-able link. --- modules/videopress/utility-functions.php | 34 +++++++++++++++++++ phpunit.xml.dist | 3 ++ .../test_functions.utility-functions.php | 31 +++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/php/modules/videopress/test_functions.utility-functions.php diff --git a/modules/videopress/utility-functions.php b/modules/videopress/utility-functions.php index 989eaf5251b14..e91f1b886d5f5 100644 --- a/modules/videopress/utility-functions.php +++ b/modules/videopress/utility-functions.php @@ -692,3 +692,37 @@ function videopress_get_attachment_url( $post_id ) { // If the URL is a string, return it. Otherwise, we shouldn't to avoid errors downstream, so null. return ( is_string( $return ) ) ? $return : null; } + +/** + * Converts VideoPress flash embeds into oEmbed-able URLs. + * + * Older VideoPress embed depended on Flash, which no longer work, + * so let us convert them to an URL that WordPress can oEmbed. + * + * Note that this file is always loaded via modules/module-extras.php and is not dependent on module status. + * + * @param string $content the content. + * @return string filtered content + */ +function jetpack_videopress_flash_embed_filter( $content ) { + $regex = '%]*+>(?:\s*)?%i'; + $content = preg_replace_callback( + $regex, + function( $matches, $orig_html = null ) { + $embed_code = $matches[0]; + $url_matches = array(); + + // get video ID from flash URL. + $url_matched = preg_match( '/src="http:\/\/v.wordpress.com\/([^"]+)"/', $embed_code, $url_matches ); + + if ( $url_matched ) { + $video_id = $url_matches[1]; + return "https://videopress.com/v/$video_id"; + } + }, + $content + ); + return $content; +} + +add_filter( 'the_content', 'jetpack_videopress_flash_embed_filter', 7 ); // Needs to be priority 7 to allow Core to oEmbed. diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7e3768f975ed1..5d735e82c38a9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -60,6 +60,9 @@ tests/php/modules/verification-tools + + tests/php/modules/videopress + tests/php/modules/wpcom-block-editor diff --git a/tests/php/modules/videopress/test_functions.utility-functions.php b/tests/php/modules/videopress/test_functions.utility-functions.php new file mode 100644 index 0000000000000..5655af9165a97 --- /dev/null +++ b/tests/php/modules/videopress/test_functions.utility-functions.php @@ -0,0 +1,31 @@ +

'; + $contains = 'https://videopress.com/v/YtfS78jH'; + + $filtered = jetpack_videopress_flash_embed_filter( $content ); + + $this->assertContains( $contains, $filtered ); + } + +}