Skip to content

Commit

Permalink
Reverse old embed HTML if it has videopress URLs in it (#14249)
Browse files Browse the repository at this point in the history
* Reverse old embed HTML if it has videopress URLs in it to an oembed-able link.
  • Loading branch information
gravityrail authored and kraftbj committed Dec 18, 2019
1 parent d026d45 commit 3a0fb0b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions modules/videopress/utility-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '%<embed[^>]*+>(?:\s*</embed>)?%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.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<testsuite name="verification-tools">
<directory prefix="test_" suffix=".php">tests/php/modules/verification-tools</directory>
</testsuite>
<testsuite name="videopress">
<directory prefix="test_" suffix=".php">tests/php/modules/videopress</directory>
</testsuite>
<testsuite name="wpcom-block-editor">
<directory prefix="test_" suffix=".php">tests/php/modules/wpcom-block-editor</directory>
</testsuite>
Expand Down
31 changes: 31 additions & 0 deletions tests/php/modules/videopress/test_functions.utility-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Class WP_Test_Jetpack_VideoPress_Utility_Functions
*
* Note that modules/videopress/utility-functions.php is automatically loaded and does not need to be required in this file.
*
* @package Jetpack
*/

/**
* Tests Jetpack_VideoPress_Utility_Functions
*/
class WP_Test_Jetpack_VideoPress_Utility_Functions extends WP_UnitTestCase {

/**
* Tests the VideoPress Flash to oEmbedable URL filter.
*
* @author kraftbj
* @covers jetpack_videopress_flash_embed_filter
* @since 8.1.0
*/
public function test_jetpack_videopress_flash_embed_filter_flash() {
$content = '<p><embed src="http://v.wordpress.com/YtfS78jH" type="application/x-shockwave-flash" width="600" height="338"></embed></p>';
$contains = 'https://videopress.com/v/YtfS78jH';

$filtered = jetpack_videopress_flash_embed_filter( $content );

$this->assertContains( $contains, $filtered );
}

}

0 comments on commit 3a0fb0b

Please sign in to comment.