<!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][8747] trunk: Ensure that the 'Comments' dropdown filter on the activity directory includes new- and old-style comment activity</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 { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { 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/8747">8747</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-08-01 19:58:47 +0000 (Fri, 01 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Ensure that the 'Comments' dropdown filter on the activity directory includes new- and old-style comment activity

The change in BuddyPress 2.0 to storing blog comment activity in
activity_comment items resulted in the 'Comments' dropdown filter breaking: it
continued to return only 'new_blog_comment' items. This changeset introduces
a number of filters to ensure that both the appropriate 'activity_comment'
and 'new_blog_comment' items are included when passing 'action=new_blog_comment'
to bp_has_activities().

See <a href="http://buddypress.trac.wordpress.org/ticket/5608">#5608</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpblogsbpblogsactivityphp">trunk/src/bp-blogs/bp-blogs-activity.php</a></li>
<li><a href="#trunktestsphpunittestcasesactivitytemplatephp">trunk/tests/phpunit/testcases/activity/template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpblogsbpblogsactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-blogs/bp-blogs-activity.php (8746 => 8747)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-blogs/bp-blogs-activity.php 2014-08-01 14:31:16 UTC (rev 8746)
+++ trunk/src/bp-blogs/bp-blogs-activity.php    2014-08-01 19:58:47 UTC (rev 8747)
</span><span class="lines">@@ -657,6 +657,74 @@
</span><span class="cx"> add_action( 'trashed_post_comments', 'bp_blogs_remove_activity_meta_for_trashed_comments', 10, 2 );
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Filter 'new_blog_comment' bp_has_activities() loop to include new- and old-style blog activity comment items.
+ *
+ * In BuddyPress 2.0, the schema for storing activity items related to blog
+ * posts changed. Instead creating new top-level 'new_blog_comment' activity
+ * items, blog comments are recorded in the activity stream as comments on the
+ * 'new_blog_post' activity items corresponding to the parent post. This filter
+ * ensures that the 'new_blog_comment' filter in bp_has_activities() (which
+ * powers the 'Comments' filter in the activity directory dropdown) includes
+ * both old-style and new-style activity comments.
+ *
+ * This implementation involves filtering the activity queries directly, and
+ * should be considered a stopgap. The proper solution would involve enabling
+ * multiple query condition clauses, connected by an OR, in the bp_has_activities()
+ * API.
+ *
+ * @since BuddyPress (2.1.0)
+ *
+ * @param array $args Arguments passed from bp_parse_args() in bp_has_activities().
+ * @return array $args
+ */
+function bp_blogs_new_blog_comment_query_backpat( $args ) {
+       // Bail if this is not a 'new_blog_comment' query
+       if ( 'new_blog_comment' !== $args['action'] ) {
+               return $args;
+       }
+
+       // display_comments=stream is required to show new-style
+       // 'activity_comment' items inline
+       $args['display_comments'] = 'stream';
+
+       // For the remaining clauses, we filter the SQL query directly
+       add_filter( 'bp_activity_paged_activities_sql', '_bp_blogs_new_blog_comment_query_backpat_filter' );
+       add_filter( 'bp_activity_total_activities_sql', '_bp_blogs_new_blog_comment_query_backpat_filter' );
+
+       // Return the original arguments
+       return $args;
+}
+add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' );
+
+/**
+ * Filter activity SQL to include new- and old-style 'new_blog_comment' activity items.
+ *
+ * @since BuddyPress (2.1.0)
+ *
+ * @access private
+ * @see bp_blogs_new_blog_comment_query_backpat()
+ *
+ * @param string $query SQL query as assembled in BP_Activity_Activity::get().
+ * @return string $query Modified SQL query.
+ */
+function _bp_blogs_new_blog_comment_query_backpat_filter( $query ) {
+       $bp = buddypress();
+
+       // The query passed to the filter is for old-style 'new_blog_comment'
+       // items. We include new-style 'activity_comment' items by running a
+       // subquery inside of a large OR clause.
+       $activity_comment_subquery = "SELECT a.id FROM {$bp->activity->table_name} a INNER JOIN {$bp->activity->table_name_meta} am ON (a.id = am.activity_id) WHERE am.meta_key = 'bp_blogs_post_comment_id' AND a.type = 'activity_comment'";
+
+       // WHERE ( [original WHERE clauses] OR a.id IN (activity_comment subquery) )
+       $query = preg_replace( '|WHERE (.*?) ORDER|', 'WHERE ( ( $1 ) OR ( a.id IN ( ' . $activity_comment_subquery . ' ) ) ) ORDER', $query );
+
+       // Don't run this on future queries
+       remove_filter( current_filter(), '_bp_blogs_new_blog_comment_query_backpat_filter' );
+
+       return $query;
+}
+
+/**
</ins><span class="cx">  * Utility function to set up some variables for use in the activity loop.
</span><span class="cx">  *
</span><span class="cx">  * Grabs the blog's comment depth and the post's open comment status options
</span></span></pre></div>
<a id="trunktestsphpunittestcasesactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/activity/template.php (8746 => 8747)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/activity/template.php      2014-08-01 14:31:16 UTC (rev 8746)
+++ trunk/tests/phpunit/testcases/activity/template.php 2014-08-01 19:58:47 UTC (rev 8747)
</span><span class="lines">@@ -309,6 +309,63 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * @group bp_has_activities
+        */
+       public function test_bp_has_activities_with_type_new_blog_comments() {
+               add_filter( 'bp_disable_blogforum_comments', '__return_false' );
+
+               $now = time();
+               $a1 = $this->factory->activity->create( array(
+                       'content' => 'Life Rules',
+                       'component' => 'blogs',
+                       'type' => 'new_blog_post',
+                       'recorded_time' => date( 'Y-m-d H:i:s', $now ),
+               ) );
+               $a2 = $this->factory->activity->create( array(
+                       'content' => 'Life Drools',
+                       'component' => 'blogs',
+                       'type' => 'new_blog_comment',
+                       'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
+               ) );
+
+               // This one will show up in the stream because it's a comment
+               // on a blog post
+               $a3 = bp_activity_new_comment( array(
+                       'activity_id' => $a1,
+                       'content' => 'Candy is good',
+                       'recorded_time' => date( 'Y-m-d H:i:s', $now - 200 ),
+               ) );
+
+               $a4 = $this->factory->activity->create( array(
+                       'content' => 'Life Rulez',
+                       'component' => 'activity',
+                       'type' => 'activity_update',
+                       'recorded_time' => date( 'Y-m-d H:i:s', $now - 300 ),
+               ) );
+
+               // This one should not show up in the stream because it's a
+               // comment on an activity item
+               $a5 = bp_activity_new_comment( array(
+                       'activity_id' => $a4,
+                       'content' => 'Candy is great',
+                       'recorded_time' => date( 'Y-m-d H:i:s', $now - 400 ),
+               ) );
+               global $activities_template;
+
+               // prime
+               bp_has_activities( array(
+                       'component' => 'blogs',
+                       'action' => 'new_blog_comment',
+               ) );
+
+               $this->assertEquals( array( $a3, $a2 ), wp_parse_id_list( wp_list_pluck( $activities_template->activities, 'id' ) ) );
+
+               // Clean up
+               $activities_template = null;
+               remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
+       }
+
+       /**
</ins><span class="cx">    * @group bp_activity_can_comment_reply
</span><span class="cx">   */
</span><span class="cx">  public function test_bp_activity_can_comment_reply_thread_comments_on() {
</span></span></pre>
</div>
</div>

</body>
</html>