From 424cc274f1a8d576eade59070211abeebe3c98c6 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 6 Nov 2009 14:22:23 +0000 Subject: [PATCH] Add a bunch of new filters. Use one of them to remove new lines from Scribd embeds. Invalid oEmbed post meta cache without using JS. Props Viper007Bond. see #10337 git-svn-id: http://svn.automattic.com/wordpress/trunk@12153 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-oembed.php | 37 ++++++++++++++++++++++++++++++------ wp-includes/media.php | 35 ++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/wp-includes/class-oembed.php b/wp-includes/class-oembed.php index 0943de4d1..b039dc89e 100644 --- a/wp-includes/class-oembed.php +++ b/wp-includes/class-oembed.php @@ -47,8 +47,11 @@ class WP_oEmbed { 'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ), 'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ), 'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ), - '#http://(www\.)?scribd.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true) + '#http://(www\.)?scribd.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ), ) ); + + // Fix Scribd embeds. They contain new lines in the middle of the HTML which breaks wpautop(). + add_filter( 'oembed_dataparse', array(&$this, 'strip_scribd_newlines'), 10, 3 ); } /** @@ -87,7 +90,7 @@ class WP_oEmbed { if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) return false; - return apply_filters( 'oembed_output', $this->data2html( $data, $url ), $url, $args ); + return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); } /** @@ -206,17 +209,39 @@ class WP_oEmbed { return false; $title = ( !empty($data->title) ) ? $data->title : ''; - return '' . esc_attr($title) . ''; + $return = '' . esc_attr($title) . ''; + break; case 'video': case 'rich': - return ( !empty($data->html) ) ? $data->html : false; + $return = ( !empty($data->html) ) ? $data->html : false; + break; case 'link': - return ( !empty($data->title) ) ? '' . esc_html($data->title) . '' : false; + $return = ( !empty($data->title) ) ? '' . esc_html($data->title) . '' : false; + break; + + default; + $return = false; } - return false; + // You can use this filter to add support for custom data types or to filter the result + return apply_filters( 'oembed_dataparse', $return, $data, $url ); + } + + /** + * Strip new lines from the HTML if it's a Scribd embed. + * + * @param string $html Existing HTML. + * @param object $data Data object from WP_oEmbed::data2html() + * @param string $url The original URL passed to oEmbed. + * @return string Possibly modified $html + */ + function strip_scribd_newlines( $html, $data, $url ) { + if ( preg_match( '#http://(www\.)?scribd.com/.*#i', $url ) ) + $html = str_replace( array( "\r\n", "\n" ), '', $html ); + + return $html; } } diff --git a/wp-includes/media.php b/wp-includes/media.php index 03201b30c..fe907de4a 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -927,6 +927,9 @@ class WP_Embed { if ( get_option('embed_autourls') ) add_filter( 'the_content', array(&$this, 'autoembed'), 8 ); + // After a post is saved, invalidate the oEmbed cache + add_action( 'save_post', array(&$this, 'delete_oembed_caches') ); + // After a post is saved, cache oEmbed items via AJAX add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') ); } @@ -1048,7 +1051,7 @@ class WP_Embed { foreach ( $handlers as $id => $handler ) { if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) - return $return; + return apply_filters( 'embed_handler_html', $return, $url, $attr ); } } } @@ -1070,7 +1073,7 @@ class WP_Embed { return $this->maybe_make_link( $url ); if ( !empty($cache) ) - return $cache; + return apply_filters( 'embed_oembed_html', $cache, $url, $attr ); } // Use oEmbed to get the HTML @@ -1086,13 +1089,28 @@ class WP_Embed { // If there was a result, return it if ( $html ) - return $html; + return apply_filters( 'embed_oembed_html', $html, $url, $attr ); } // Still unknown return $this->maybe_make_link( $url ); } + /** + * Delete all oEmbed caches. + * + * @param int $post_ID Post ID to delete the caches for. + */ + function delete_oembed_caches( $post_ID ) { + $post_metas = get_post_custom_keys( $post_ID ); + if ( empty($post_metas) ) + return; + foreach( (array) $post_metas as $post_meta_key ) { + if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) + delete_post_meta( $post_ID, $post_meta_key ); + } + } + /** * Triggers a caching of all oEmbed results. * @@ -1101,17 +1119,9 @@ class WP_Embed { function cache_oembed( $post_ID ) { $post = get_post( $post_ID ); - // post_type check is incase of "save_post" usage if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) ) return; - // Dump existing caches - $post_metas = get_post_custom_keys( $post->ID ); - foreach( $post_metas as $post_meta_key ) { - if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) - delete_post_meta( $post->ID, $post_meta_key ); - } - // Trigger a caching if ( !empty($post->post_content) ) { $this->post_ID = $post->ID; @@ -1161,7 +1171,8 @@ class WP_Embed { * @return string Linked URL or the original URL. */ function maybe_make_link( $url ) { - return ( $this->linkifunknown ) ? '' . esc_html($url) . '' : $url; + $output = ( $this->linkifunknown ) ? '' . esc_html($url) . '' : $url; + return apply_filters( 'embed_maybe_make_link', $output, $url ); } } $wp_embed = new WP_Embed();