<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[12153] trunk/wp-includes: Add a bunch of new filters.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12153">12153</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-11-06 14:22:23 +0000 (Fri, 06 Nov 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://trac.wordpress.org/ticket/10337">#10337</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesclassoembedphp">trunk/wp-includes/class-oembed.php</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesclassoembedphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-oembed.php (12152 => 12153)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-oembed.php        2009-11-06 10:53:41 UTC (rev 12152)
+++ trunk/wp-includes/class-oembed.php        2009-11-06 14:22:23 UTC (rev 12153)
</span><span class="lines">@@ -47,8 +47,11 @@
</span><span class="cx">                         'http://revision3.com/*' => array( 'http://revision3.com/api/oembed/', false ),
</span><span class="cx">                         'http://i*.photobucket.com/albums/*' => array( 'http://photobucket.com/oembed', false ),
</span><span class="cx">                         'http://gi*.photobucket.com/groups/*' => array( 'http://photobucket.com/oembed', false ),
</span><del>-                        '#http://(www\.)?scribd.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true)
</del><ins>+                        '#http://(www\.)?scribd.com/.*#i' => array( 'http://www.scribd.com/services/oembed', true ),
</ins><span class="cx">                 ) );
</span><ins>+
+                // 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 );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="lines">@@ -87,7 +90,7 @@
</span><span class="cx">                 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) )
</span><span class="cx">                         return false;
</span><span class="cx">
</span><del>-                return apply_filters( 'oembed_output', $this->data2html( $data, $url ), $url, $args );
</del><ins>+                return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><span class="lines">@@ -206,18 +209,40 @@
</span><span class="cx">                                         return false;
</span><span class="cx">
</span><span class="cx">                                 $title = ( !empty($data->title) ) ? $data->title : '';
</span><del>-                                return '<img src="' . esc_attr( clean_url( $data->url ) ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />';
</del><ins>+                                $return = '<img src="' . esc_attr( clean_url( $data->url ) ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />';
+                                break;
</ins><span class="cx">
</span><span class="cx">                         case 'video':
</span><span class="cx">                         case 'rich':
</span><del>-                                return ( !empty($data->html) ) ? $data->html : false;
</del><ins>+                                $return = ( !empty($data->html) ) ? $data->html : false;
+                                break;
</ins><span class="cx">
</span><span class="cx">                         case 'link':
</span><del>-                                return ( !empty($data->title) ) ? '<a href="' . clean_url($url) . '">' . esc_html($data->title) . '</a>' : false;
</del><ins>+                                $return = ( !empty($data->title) ) ? '<a href="' . clean_url($url) . '">' . esc_html($data->title) . '</a>' : false;
+                                break;
+
+                        default;
+                                $return = false;
</ins><span class="cx">                 }
</span><span class="cx">
</span><del>-                return false;
</del><ins>+                // 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 );
</ins><span class="cx">         }
</span><ins>+
+        /**
+         * 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;
+        }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (12152 => 12153)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2009-11-06 10:53:41 UTC (rev 12152)
+++ trunk/wp-includes/media.php        2009-11-06 14:22:23 UTC (rev 12153)
</span><span class="lines">@@ -927,6 +927,9 @@
</span><span class="cx">                 if ( get_option('embed_autourls') )
</span><span class="cx">                         add_filter( 'the_content', array(&$this, 'autoembed'), 8 );
</span><span class="cx">
</span><ins>+                // After a post is saved, invalidate the oEmbed cache
+                add_action( 'save_post', array(&$this, 'delete_oembed_caches') );
+
</ins><span class="cx">                 // After a post is saved, cache oEmbed items via AJAX
</span><span class="cx">                 add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') );
</span><span class="cx">         }
</span><span class="lines">@@ -1048,7 +1051,7 @@
</span><span class="cx">                         foreach ( $handlers as $id => $handler ) {
</span><span class="cx">                                 if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
</span><span class="cx">                                         if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
</span><del>-                                                return $return;
</del><ins>+                                                return apply_filters( 'embed_handler_html', $return, $url, $attr );
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -1070,7 +1073,7 @@
</span><span class="cx">                                         return $this->maybe_make_link( $url );
</span><span class="cx">
</span><span class="cx">                                 if ( !empty($cache) )
</span><del>-                                        return $cache;
</del><ins>+                                        return apply_filters( 'embed_oembed_html', $cache, $url, $attr );
</ins><span class="cx">                         }
</span><span class="cx">
</span><span class="cx">                         // Use oEmbed to get the HTML
</span><span class="lines">@@ -1086,7 +1089,7 @@
</span><span class="cx">
</span><span class="cx">                         // If there was a result, return it
</span><span class="cx">                         if ( $html )
</span><del>-                                return $html;
</del><ins>+                                return apply_filters( 'embed_oembed_html', $html, $url, $attr );
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 // Still unknown
</span><span class="lines">@@ -1094,6 +1097,21 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /**
</span><ins>+         * 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 );
+                }
+        }
+
+        /**
</ins><span class="cx">          * Triggers a caching of all oEmbed results.
</span><span class="cx">          *
</span><span class="cx">          * @param int $post_ID Post ID to do the caching for.
</span><span class="lines">@@ -1101,17 +1119,9 @@
</span><span class="cx">         function cache_oembed( $post_ID ) {
</span><span class="cx">                 $post = get_post( $post_ID );
</span><span class="cx">
</span><del>-                // post_type check is incase of "save_post" usage
</del><span class="cx">                 if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
</span><span class="cx">                         return;
</span><span class="cx">
</span><del>-                // 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 );
-                }
-
</del><span class="cx">                 // Trigger a caching
</span><span class="cx">                 if ( !empty($post->post_content) ) {
</span><span class="cx">                         $this->post_ID = $post->ID;
</span><span class="lines">@@ -1161,7 +1171,8 @@
</span><span class="cx">          * @return string Linked URL or the original URL.
</span><span class="cx">          */
</span><span class="cx">         function maybe_make_link( $url ) {
</span><del>-                return ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
</del><ins>+                $output = ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
+                return apply_filters( 'embed_maybe_make_link', $output, $url );
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> $wp_embed = new WP_Embed();
</span></span></pre>
</div>
</div>
</body>
</html>