Embeds: Provide a cached text fallback.

Sometimes, embedded sites might suffer from less than 100% uptime. Instead of leaving the embedding site with a big blank space where the embed should be, let's fall back to a link to the embedded post, so there's at least some context for the post.

Fixes #34462.



git-svn-id: https://develop.svn.wordpress.org/trunk@35437 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2015-10-29 23:10:46 +00:00
parent 4832d8d933
commit 63d9e9df96
4 changed files with 66 additions and 13 deletions

View File

@ -165,6 +165,7 @@ add_filter( 'the_title_rss', 'ent2ncr', 8 );
add_filter( 'the_title_rss', 'esc_html' ); add_filter( 'the_title_rss', 'esc_html' );
add_filter( 'the_content_rss', 'ent2ncr', 8 ); add_filter( 'the_content_rss', 'ent2ncr', 8 );
add_filter( 'the_content_feed', 'wp_staticize_emoji' ); add_filter( 'the_content_feed', 'wp_staticize_emoji' );
add_filter( 'the_content_feed', '_oembed_filter_feed_content' );
add_filter( 'the_excerpt_rss', 'convert_chars' ); add_filter( 'the_excerpt_rss', 'convert_chars' );
add_filter( 'the_excerpt_rss', 'ent2ncr', 8 ); add_filter( 'the_excerpt_rss', 'ent2ncr', 8 );
add_filter( 'comment_author_rss', 'ent2ncr', 8 ); add_filter( 'comment_author_rss', 'ent2ncr', 8 );

View File

@ -461,7 +461,9 @@ function get_post_embed_html( $post = null, $width, $height ) {
$embed_url = get_post_embed_url( $post ); $embed_url = get_post_embed_url( $post );
$output = "<script type='text/javascript'>\n"; $output = '<blockquote><a href="' . get_permalink( $post ) . '">' . get_the_title( $post ) . "</a></blockquote>\n";
$output .= "<script type='text/javascript'>\n";
$output .= "<!--//--><![CDATA[//><!--\n"; $output .= "<!--//--><![CDATA[//><!--\n";
if ( SCRIPT_DEBUG ) { if ( SCRIPT_DEBUG ) {
$output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ); $output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' );
@ -752,7 +754,11 @@ function wp_filter_oembed_result( $result, $data, $url ) {
} }
$allowed_html = array( $allowed_html = array(
'iframe' => array( 'a' => array(
'href' => true,
),
'blockquote' => array(),
'iframe' => array(
'src' => true, 'src' => true,
'width' => true, 'width' => true,
'height' => true, 'height' => true,
@ -766,13 +772,20 @@ function wp_filter_oembed_result( $result, $data, $url ) {
); );
$html = wp_kses( $result, $allowed_html ); $html = wp_kses( $result, $allowed_html );
preg_match( '|^.*(<iframe.*?></iframe>).*$|m', $html, $iframes );
if ( empty( $iframes ) ) { preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content );
// We require at least the iframe to exist.
if ( empty( $content[2] ) ) {
return false; return false;
} }
$html = $content[1] . $content[2];
$html = str_replace( '<iframe', '<iframe sandbox="allow-scripts" security="restricted"', $iframes[1] ); if ( ! empty( $content[1] ) ) {
// We have a blockquote to fall back on. Hide the iframe by default.
$html = str_replace( '<iframe', '<iframe style="display:none;"', $html );
}
$html = str_replace( '<iframe', '<iframe sandbox="allow-scripts" security="restricted"', $html );
preg_match( '/ src=[\'"]([^\'"]*)[\'"]/', $html, $results ); preg_match( '/ src=[\'"]([^\'"]*)[\'"]/', $html, $results );
@ -782,6 +795,7 @@ function wp_filter_oembed_result( $result, $data, $url ) {
$url = esc_url( "{$results[1]}#?secret=$secret" ); $url = esc_url( "{$results[1]}#?secret=$secret" );
$html = str_replace( $results[0], " src=\"$url\" data-secret=\"$secret\"", $html ); $html = str_replace( $results[0], " src=\"$url\" data-secret=\"$secret\"", $html );
$html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html );
} }
return $html; return $html;
@ -910,3 +924,16 @@ function print_embed_scripts() {
</script> </script>
<?php <?php
} }
/**
* Prepare the oembed HTML to be displayed in an RSS feed.
*
* @since 4.4.0
* @access private
*
* @param string $content The content to filter.
* @return string The filtered content.
*/
function _oembed_filter_feed_content( $content ) {
return str_replace( '<iframe sandbox="allow-scripts" security="restricted" style="display:none;"', '<iframe sandbox="allow-scripts" security="restricted"', $content );
}

View File

@ -13,14 +13,22 @@
return; return;
} }
var iframes = document.querySelectorAll( '.wp-embedded-content[data-secret="' + data.secret + '"]' ); var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
i, source, height, sourceURL, targetURL;
for ( var i = 0; i < iframes.length; i++ ) { for ( i = 0; i < blockquotes.length; i++ ) {
var source = iframes[ i ]; blockquotes[ i ].style.display = 'none';
}
for ( i = 0; i < iframes.length; i++ ) {
source = iframes[ i ];
source.style.display = '';
/* Resize the iframe on request. */ /* Resize the iframe on request. */
if ( 'height' === data.message ) { if ( 'height' === data.message ) {
var height = data.value; height = data.value;
if ( height > 1000 ) { if ( height > 1000 ) {
height = 1000; height = 1000;
} else if ( height < 200 ) { } else if ( height < 200 ) {
@ -32,7 +40,9 @@
/* Link to a specific URL on request. */ /* Link to a specific URL on request. */
if ( 'link' === data.message ) { if ( 'link' === data.message ) {
var sourceURL = document.createElement( 'a' ), targetURL = document.createElement( 'a' ); sourceURL = document.createElement( 'a' );
targetURL = document.createElement( 'a' );
sourceURL.href = source.getAttribute( 'src' ); sourceURL.href = source.getAttribute( 'src' );
targetURL.href = data.value; targetURL.href = data.value;
@ -48,13 +58,14 @@
function onLoad() { function onLoad() {
var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ), var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ),
isIE11 = !!navigator.userAgent.match( /Trident.*rv\:11\./ ); isIE11 = !!navigator.userAgent.match( /Trident.*rv\:11\./ ),
iframes, iframeClone, i;
/* Remove security attribute from iframes in IE10 and IE11. */ /* Remove security attribute from iframes in IE10 and IE11. */
if ( isIE10 || isIE11 ) { if ( isIE10 || isIE11 ) {
var iframes = document.querySelectorAll( '.wp-embedded-content[security]' ), iframeClone; iframes = document.querySelectorAll( '.wp-embedded-content[security]' );
for ( var i = 0; i < iframes.length; i++ ) { for ( i = 0; i < iframes.length; i++ ) {
iframeClone = iframes[ i ].cloneNode( true ); iframeClone = iframes[ i ].cloneNode( true );
iframeClone.removeAttribute( 'security' ); iframeClone.removeAttribute( 'security' );
iframes[ i ].parentNode.insertBefore( iframeClone, iframes[ i ].nextSibling ); iframes[ i ].parentNode.insertBefore( iframeClone, iframes[ i ].nextSibling );

View File

@ -78,4 +78,18 @@ EOD;
$this->assertFalse( wp_filter_oembed_result( false, (object) array( 'type' => 'rich' ), '' ) ); $this->assertFalse( wp_filter_oembed_result( false, (object) array( 'type' => 'rich' ), '' ) );
$this->assertFalse( wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) ); $this->assertFalse( wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) );
} }
function test_filter_oembed_result_blockquote_adds_style_to_iframe() {
$html = '<blockquote></blockquote><iframe></iframe>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
$this->assertEquals( '<blockquote></blockquote><iframe sandbox="allow-scripts" security="restricted" style="display:none;"></iframe>', $actual );
}
function test_filter_oembed_result_allowed_html() {
$html = '<blockquote><strong><a href="" target=""></a></strong></blockquote><iframe></iframe>';
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
$this->assertEquals( '<blockquote><a href=""></a></blockquote><iframe sandbox="allow-scripts" security="restricted" style="display:none;"></iframe>', $actual );
}
} }