<!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/*'                =&gt; array( 'http://revision3.com/api/oembed/',         false ),
</span><span class="cx">                         'http://i*.photobucket.com/albums/*'    =&gt; array( 'http://photobucket.com/oembed',            false ),
</span><span class="cx">                         'http://gi*.photobucket.com/groups/*'   =&gt; array( 'http://photobucket.com/oembed',            false ),
</span><del>-                        '#http://(www\.)?scribd.com/.*#i'       =&gt; array( 'http://www.scribd.com/services/oembed',    true)
</del><ins>+                        '#http://(www\.)?scribd.com/.*#i'       =&gt; 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(&amp;$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-&gt;fetch( $provider, $url, $args ) )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><del>-                return apply_filters( 'oembed_output', $this-&gt;data2html( $data, $url ), $url, $args );
</del><ins>+                return apply_filters( 'oembed_result', $this-&gt;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-&gt;title) ) ? $data-&gt;title : '';
</span><del>-                                return '&lt;img src=&quot;' . esc_attr( clean_url( $data-&gt;url ) ) . '&quot; alt=&quot;' . esc_attr($title) . '&quot; width=&quot;' . esc_attr($data-&gt;width) . '&quot; height=&quot;' . esc_attr($data-&gt;height) . '&quot; /&gt;';
</del><ins>+                                $return = '&lt;img src=&quot;' . esc_attr( clean_url( $data-&gt;url ) ) . '&quot; alt=&quot;' . esc_attr($title) . '&quot; width=&quot;' . esc_attr($data-&gt;width) . '&quot; height=&quot;' . esc_attr($data-&gt;height) . '&quot; /&gt;';
+                                break;
</ins><span class="cx"> 
</span><span class="cx">                         case 'video':
</span><span class="cx">                         case 'rich':
</span><del>-                                return ( !empty($data-&gt;html) ) ? $data-&gt;html : false;
</del><ins>+                                $return = ( !empty($data-&gt;html) ) ? $data-&gt;html : false;
+                                break;
</ins><span class="cx"> 
</span><span class="cx">                         case 'link':
</span><del>-                                return ( !empty($data-&gt;title) ) ? '&lt;a href=&quot;' . clean_url($url) . '&quot;&gt;' . esc_html($data-&gt;title) . '&lt;/a&gt;' : false;
</del><ins>+                                $return = ( !empty($data-&gt;title) ) ? '&lt;a href=&quot;' . clean_url($url) . '&quot;&gt;' . esc_html($data-&gt;title) . '&lt;/a&gt;' : 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( &quot;\r\n&quot;, &quot;\n&quot; ), '', $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(&amp;$this, 'autoembed'), 8 );
</span><span class="cx"> 
</span><ins>+                // After a post is saved, invalidate the oEmbed cache
+                add_action( 'save_post', array(&amp;$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(&amp;$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 =&gt; $handler ) {
</span><span class="cx">                                 if ( preg_match( $handler['regex'], $url, $matches ) &amp;&amp; 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-&gt;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 &quot;save_post&quot; usage
</del><span class="cx">                 if ( empty($post-&gt;ID) || !in_array( $post-&gt;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-&gt;ID );
-                foreach( $post_metas as $post_meta_key ) {
-                        if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
-                                delete_post_meta( $post-&gt;ID, $post_meta_key );
-                }
-
</del><span class="cx">                 // Trigger a caching
</span><span class="cx">                 if ( !empty($post-&gt;post_content) ) {
</span><span class="cx">                         $this-&gt;post_ID = $post-&gt;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-&gt;linkifunknown ) ? '&lt;a href=&quot;' . esc_attr($url) . '&quot;&gt;' . esc_html($url) . '&lt;/a&gt;' : $url;
</del><ins>+                $output = ( $this-&gt;linkifunknown ) ? '&lt;a href=&quot;' . esc_attr($url) . '&quot;&gt;' . esc_html($url) . '&lt;/a&gt;' : $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>