Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse old embed HTML if it has videopress URLs in it #14249

Merged
merged 5 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}

}