<!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" />
<title>[BuddyPress][4937] trunk: Tweak BP_Embed implementation; fix caching and PHP warning.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/4937">4937</a></dd>
<dt>Author</dt> <dd>djpaul</dd>
<dt>Date</dt> <dd>2011-08-06 16:24:46 +0000 (Sat, 06 Aug 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Tweak BP_Embed implementation; fix caching and PHP warning. Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3429">#3429</a>, props r-a-y</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityfunctionsphp">trunk/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunkbpcorebpcoreclassesphp">trunk/bp-core/bp-core-classes.php</a></li>
<li><a href="#trunkbpthemesbpdefault_incajaxphp">trunk/bp-themes/bp-default/_inc/ajax.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-functions.php (4936 => 4937)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-functions.php        2011-08-06 16:20:52 UTC (rev 4936)
+++ trunk/bp-activity/bp-activity-functions.php        2011-08-06 16:24:46 UTC (rev 4937)
</span><span class="lines">@@ -923,7 +923,7 @@
</span><span class="cx">         if ( isset( $activity_obj-&gt;current_comment ) ) {
</span><span class="cx">                 $activity_obj = $activity_obj-&gt;current_comment;
</span><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         if ( 'new_blog_post' == $activity_obj-&gt;type || 'new_blog_comment' == $activity_obj-&gt;type || 'new_forum_topic' == $activity_obj-&gt;type || 'new_forum_post' == $activity_obj-&gt;type )
</span><span class="cx">                 $link = $activity_obj-&gt;primary_link;
</span><span class="cx">         else {
</span><span class="lines">@@ -994,9 +994,12 @@
</span><span class="cx"> /** Embeds *******************************************************************/
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Grabs the activity ID and attempts to retrieve the oEmbed cache (if it exists)
</del><ins>+ * Grabs the activity update ID and attempts to retrieve the oEmbed cache (if it exists)
</ins><span class="cx">  * during the activity loop.  If no cache and link is embeddable, cache it.
</span><span class="cx">  *
</span><ins>+ * This does not cover recursive activity comments, as they do not use a real loop.
+ * For that, see {@link bp_activity_comment_embed()}.
+ *
</ins><span class="cx">  * @see BP_Embed
</span><span class="cx">  * @see bp_embed_activity_cache()
</span><span class="cx">  * @see bp_embed_activity_save_cache()
</span><span class="lines">@@ -1011,6 +1014,57 @@
</span><span class="cx"> add_action( 'activity_loop_start', 'bp_activity_embed' );
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Grabs the activity comment ID and attempts to retrieve the oEmbed cache (if it exists)
+ * when BP is recursing through activity comments {@link bp_activity_recurse_comments()}.
+ * If no cache and link is embeddable, cache it.
+ *
+ * @see BP_Embed
+ * @see bp_embed_activity_cache()
+ * @see bp_embed_activity_save_cache()
+ * @package BuddyPress Activity
+ * @since 1.5
+ */
+function bp_activity_comment_embed() {
+        add_filter( 'embed_post_id',         'bp_get_activity_comment_id'          );
+        add_filter( 'bp_embed_get_cache',    'bp_embed_activity_cache',      10, 3 );
+        add_action( 'bp_embed_update_cache', 'bp_embed_activity_save_cache', 10, 3 );
+}
+add_action( 'bp_before_activity_comment', 'bp_activity_comment_embed' );
+
+/**
+ * When a user clicks on a &quot;Read More&quot; item, make sure embeds are correctly parsed and shown for the expanded content.
+ *
+ * @global object $bp BuddyPress global settings
+ * @param BP_Activity_Activity $activity The activity that is being expanded
+ * @see BP_Embed
+ * @since 1.5
+ */
+function bp_dtheme_embed_read_more( $activity ) {
+        global $bp;
+
+        $bp-&gt;activity-&gt;read_more_id = $activity-&gt;id;
+
+        add_filter( 'embed_post_id',            create_function( '', 'global $bp; return $bp-&gt;activity-&gt;read_more_id;' ) ); 
+        add_filter( 'bp_embed_get_cache',       'bp_embed_activity_cache',      10, 3 );
+        add_action( 'bp_embed_update_cache',    'bp_embed_activity_save_cache', 10, 3 );
+}
+add_action( 'bp_dtheme_get_single_activity_content', 'bp_dtheme_embed_read_more' );
+
+/**
+ * Removes the 'embed_post_id' filter after {@link bp_activity_recurse_comments()}
+ * is rendered to avoid conflict with the 'embed_post_id' filter in
+ * {@link bp_activity_embed()} or any other component embeds.
+ *
+ * @see bp_activity_comment_embed()
+ * @package BuddyPress Activity
+ * @since 1.5
+ */
+function bp_activity_comment_embed_after_recurse() {
+        remove_filter( 'embed_post_id', 'bp_get_activity_comment_id' );
+}
+add_action( 'bp_after_activity_comment', 'bp_activity_comment_embed_after_recurse' );
+
+/**
</ins><span class="cx">  * Wrapper function for {@link bp_activity_get_meta()}.
</span><span class="cx">  * Used during {@link BP_Embed::parse_oembed()} via {@link bp_activity_embed()}.
</span><span class="cx">  *
</span></span></pre></div>
<a id="trunkbpcorebpcoreclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-classes.php (4936 => 4937)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-classes.php        2011-08-06 16:20:52 UTC (rev 4936)
+++ trunk/bp-core/bp-core-classes.php        2011-08-06 16:24:46 UTC (rev 4937)
</span><span class="lines">@@ -1168,7 +1168,7 @@
</span><span class="cx">                 // Look for known internal handlers
</span><span class="cx">                 ksort( $this-&gt;handlers );
</span><span class="cx">                 foreach ( $this-&gt;handlers as $priority =&gt; $handlers ) {
</span><del>-                        foreach ( $handlers as $id =&gt; $handler ) {
</del><ins>+                        foreach ( $handlers as $hid =&gt; $handler ) {
</ins><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><span class="cx">                                                 return apply_filters( 'embed_handler_html', $return, $url, $attr );
</span><span class="lines">@@ -1177,7 +1177,7 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 // Get object ID
</span><del>-                $id = apply_filters( 'embed_post_id', $id );
</del><ins>+                $id = apply_filters( 'embed_post_id', 0 );
</ins><span class="cx"> 
</span><span class="cx">                 // Is oEmbed discovery on?
</span><span class="cx">                 $attr['discover'] = ( apply_filters( 'bp_embed_oembed_discover', false ) &amp;&amp; current_user_can( 'unfiltered_html' ) );
</span><span class="lines">@@ -1219,6 +1219,8 @@
</span><span class="cx">          * @return string The embed HTML on success, otherwise the original URL.
</span><span class="cx">          */
</span><span class="cx">         function parse_oembed( $id, $url, $attr, $rawattr ) {
</span><ins>+                $id = intval( $id );
+
</ins><span class="cx">                 if ( $id ) {
</span><span class="cx">                         // Setup the cachekey
</span><span class="cx">                         $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );
</span><span class="lines">@@ -1229,7 +1231,7 @@
</span><span class="cx"> 
</span><span class="cx">                         // Grab cache and return it if available
</span><span class="cx">                         if ( !empty( $cache ) ) {
</span><del>-                                return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $rawattr );
</del><ins>+                                return apply_filters( 'bp_embed_oembed_html', $cache, $url, $attr, $rawattr );
</ins><span class="cx"> 
</span><span class="cx">                         // If no cache, ping the oEmbed provider and cache the result
</span><span class="cx">                         } else {
</span></span></pre></div>
<a id="trunkbpthemesbpdefault_incajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-themes/bp-default/_inc/ajax.php (4936 => 4937)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-themes/bp-default/_inc/ajax.php        2011-08-06 16:20:52 UTC (rev 4936)
+++ trunk/bp-themes/bp-default/_inc/ajax.php        2011-08-06 16:24:46 UTC (rev 4937)
</span><span class="lines">@@ -339,13 +339,13 @@
</span><span class="cx">         if ( !$activity )
</span><span class="cx">                 exit(); // todo: error?
</span><span class="cx"> 
</span><del>-        // Activity content retrieved through AJAX should run through normal filters, but not be
-        // truncated
</del><ins>+        do_action_ref_array( 'bp_dtheme_get_single_activity_content', array( &amp;$activity ) );
+
+        // Activity content retrieved through AJAX should run through normal filters, but not be truncated
</ins><span class="cx">         remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
</span><span class="cx">         $content = apply_filters( 'bp_get_activity_content_body', $activity-&gt;content );
</span><span class="cx"> 
</span><span class="cx">         echo $content;
</span><del>-
</del><span class="cx">         exit();
</span><span class="cx"> }
</span><span class="cx"> add_action( 'wp_ajax_get_single_activity_content', 'bp_dtheme_get_single_activity_content' );
</span></span></pre>
</div>
</div>

</body>
</html>