<!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>[BuddyPress] [1839] trunk:
  Added support for significantly more advanced activity stream filtering.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd>1839</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2009-09-10 01:24:01 +0000 (Thu, 10 Sep 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Added support for significantly more advanced activity stream filtering.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityclassesphp">trunk/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunkbpactivitybpactivitytemplatetagsphp">trunk/bp-activity/bp-activity-templatetags.php</a></li>
<li><a href="#trunkbpactivityphp">trunk/bp-activity.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-classes.php (1838 => 1839)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-classes.php        2009-09-09 23:57:11 UTC (rev 1838)
+++ trunk/bp-activity/bp-activity-classes.php        2009-09-10 01:24:01 UTC (rev 1839)
</span><span class="lines">@@ -143,7 +143,7 @@
</span><span class="cx">                 return $wpdb-&gt;query( $wpdb-&gt;prepare( &quot;DELETE FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d&quot;, $user_id ) );
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        function get_activity_for_user( $user_id, $max_items, $since, $limit, $page, $filter ) {
</del><ins>+        function get_activity_for_user( $user_id, $max_items, $limit, $page, $filter ) {
</ins><span class="cx">                 global $wpdb, $bp;
</span><span class="cx"> 
</span><span class="cx">                 $since = strtotime($since);
</span><span class="lines">@@ -154,20 +154,21 @@
</span><span class="cx">                 if ( $max_items )
</span><span class="cx">                         $max_sql = $wpdb-&gt;prepare( &quot;LIMIT %d&quot;, $max_items );
</span><span class="cx">                 
</span><ins>+                /* Sort out filtering */
</ins><span class="cx">                 if ( $filter )
</span><del>-                        $filter_sql = $wpdb-&gt;prepare( &quot;AND component_name = %s&quot;, $filter );
</del><ins>+                        $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
</ins><span class="cx">                 
</span><span class="cx">                 if ( $limit &amp;&amp; $page &amp;&amp; $max_items )
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d AND date_recorded &gt;= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql&quot;, $user_id, $since ) );
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql&quot;, $user_id ) );
</ins><span class="cx">                 else
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d AND date_recorded &gt;= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql&quot;, $user_id, $since ) );
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql&quot;, $user_id ) );
</ins><span class="cx">                 
</span><del>-                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT count(id) FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d AND date_recorded &gt;= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql&quot;, $user_id, $since ) );
</del><ins>+                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT count(id) FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql&quot;, $user_id ) );
</ins><span class="cx">                 
</span><span class="cx">                 return array( 'activities' =&gt; $activities, 'total' =&gt; (int)$total_activities );
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        function get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $limit, $page, $filter ) {
</del><ins>+        function get_activity_for_friends( $user_id, $max_items, $max_items_per_friend, $limit, $page, $filter ) {
</ins><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">                 
</span><span class="cx">                 // TODO: Max items per friend not yet implemented.
</span><span class="lines">@@ -181,11 +182,10 @@
</span><span class="cx">                 if ( $max_items )
</span><span class="cx">                         $max_sql = $wpdb-&gt;prepare( &quot;LIMIT %d&quot;, $max_items );
</span><span class="cx"> 
</span><ins>+                /* Sort out filtering */
</ins><span class="cx">                 if ( $filter )
</span><del>-                        $filter_sql = $wpdb-&gt;prepare( &quot;AND component_name = %s&quot;, $filter );
</del><ins>+                        $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
</ins><span class="cx"> 
</span><del>-                $since = strtotime($since);
-
</del><span class="cx">                 $friend_ids = friends_get_friend_user_ids( $user_id );
</span><span class="cx"> 
</span><span class="cx">                 if ( !$friend_ids )
</span><span class="lines">@@ -194,11 +194,11 @@
</span><span class="cx">                 $friend_ids = implode( ',', $friend_ids );
</span><span class="cx">                 
</span><span class="cx">                 if ( $limit &amp;&amp; $page &amp;&amp; $max_items )
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded &gt;= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql&quot;, $since ) ); 
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql&quot;  ) ); 
</ins><span class="cx">                 else
</span><del>-                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded &gt;= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql&quot;, $since ) );                         
</del><ins>+                        $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql&quot; ) );                         
</ins><span class="cx"> 
</span><del>-                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT DISTINCT count(user_id) FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded &gt;= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $max_sql&quot;, $since ) ); 
</del><ins>+                $total_activities = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT DISTINCT count(user_id) FROM {$bp-&gt;activity-&gt;table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $max_sql&quot; ) ); 
</ins><span class="cx">                 
</span><span class="cx">                 return array( 'activities' =&gt; $activities, 'total' =&gt; (int)$total_activities );
</span><span class="cx">         }
</span><span class="lines">@@ -211,10 +211,11 @@
</span><span class="cx">                 
</span><span class="cx">                 if ( $max )
</span><span class="cx">                         $max_sql = $wpdb-&gt;prepare( &quot;LIMIT %d&quot;, $max );
</span><del>-
</del><ins>+                        
+                /* Sort out filtering */
</ins><span class="cx">                 if ( $filter )
</span><del>-                        $filter_sql = $wpdb-&gt;prepare( &quot;AND component_name = %s&quot;, $filter );
-
</del><ins>+                        $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
+                
</ins><span class="cx">                 if ( $limit &amp;&amp; $page &amp;&amp; $max )
</span><span class="cx">                         $activities = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT * FROM {$bp-&gt;activity-&gt;table_name} WHERE hide_sitewide = 0 $filter_sql ORDER BY date_recorded DESC $pag_sql&quot; ) );
</span><span class="cx">                 else
</span><span class="lines">@@ -247,6 +248,80 @@
</span><span class="cx">                 return $activity_feed;        
</span><span class="cx">         }
</span><span class="cx">         
</span><ins>+        function get_filter_sql( $filter_array ) {
+                global $wpdb;
+                
+                if ( !empty( $filter_array['object'] ) ) {
+                        $object_filter = explode( ',', $filter_array['object'] );
+                        $object_sql = ' AND ( ';
+                        
+                        $counter = 1;
+                        foreach( (array) $object_filter as $object ) {
+                                $object_sql .= $wpdb-&gt;prepare( &quot;component_name = %s&quot;, trim( $object ) );
+                                
+                                if ( $counter != count( $object_filter ) )
+                                        $object_sql .= ' || ';
+                                
+                                $counter++;
+                        }
+                        
+                        $object_sql .= ' )';
+                }
+
+                if ( !empty( $filter_array['action'] ) ) {
+                        $action_filter = explode( ',', $filter_array['action'] );
+                        $action_sql = ' AND ( ';
+                        
+                        $counter = 1;
+                        foreach( (array) $action_filter as $action ) {
+                                $action_sql .= $wpdb-&gt;prepare( &quot;component_action = %s&quot;, trim( $action ) );
+                                
+                                if ( $counter != count( $action_filter ) )
+                                        $action_sql .= ' || ';
+                                
+                                $counter++;
+                        }
+                        
+                        $action_sql .= ' )';
+                }
+
+                if ( !empty( $filter_array['primary_id'] ) ) {
+                        $pid_filter = explode( ',', $filter_array['action'] );
+                        $pid_sql = ' AND ( ';
+                        
+                        $counter = 1;
+                        foreach( (array) $pid_filter as $pid ) {
+                                $pid_sql .= $wpdb-&gt;prepare( &quot;item_id = %s&quot;, trim( $pid ) );
+                                
+                                if ( $counter != count( $pid_filter ) )
+                                        $pid_sql .= ' || ';
+                                
+                                $counter++;
+                        }
+                        
+                        $pid_sql .= ' )';
+                }
+
+                if ( !empty( $filter_array['secondary_id'] ) ) {
+                        $sid_filter = explode( ',', $filter_array['action'] );
+                        $sid_sql = ' AND ( ';
+                        
+                        $counter = 1;
+                        foreach( (array) $sid_filter as $sid ) {
+                                $sid_sql .= $wpdb-&gt;prepare( &quot;secondary_item_id = %s&quot;, trim( $sid ) );
+                                
+                                if ( $counter != count( $sid_filter ) )
+                                        $sid_sql .= ' || ';
+                                
+                                $counter++;
+                        }
+                        
+                        $sid_sql .= ' )';
+                }
+                
+                return $object_sql . $action_sql . $pid_sql . $sid_sql;
+        }
+        
</ins><span class="cx">         function get_last_updated() {
</span><span class="cx">                 global $bp, $wpdb;
</span><span class="cx">                 
</span></span></pre></div>
<a id="trunkbpactivitybpactivitytemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-templatetags.php (1838 => 1839)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-templatetags.php        2009-09-09 23:57:11 UTC (rev 1838)
+++ trunk/bp-activity/bp-activity-templatetags.php        2009-09-10 01:24:01 UTC (rev 1839)
</span><span class="lines">@@ -16,7 +16,7 @@
</span><span class="cx"> 
</span><span class="cx">         var $full_name;
</span><span class="cx"> 
</span><del>-        function bp_activity_template( $type, $user_id, $per_page, $max, $timeframe, $filter ) {
</del><ins>+        function bp_activity_template( $type, $user_id, $per_page, $max, $filter ) {
</ins><span class="cx">                 global $bp;
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;pag_page = isset( $_REQUEST['acpage'] ) ? intval( $_REQUEST['acpage'] ) : 1;
</span><span class="lines">@@ -27,10 +27,10 @@
</span><span class="cx">                         $this-&gt;activities = bp_activity_get_sitewide_activity( $max, $this-&gt;pag_num, $this-&gt;pag_page, $filter );
</span><span class="cx">                 
</span><span class="cx">                 if ( $type == 'personal' )
</span><del>-                        $this-&gt;activities = bp_activity_get_user_activity( $user_id, $max, $timeframe, $this-&gt;pag_num, $this-&gt;pag_page, $filter );
</del><ins>+                        $this-&gt;activities = bp_activity_get_user_activity( $user_id, $max, $this-&gt;pag_num, $this-&gt;pag_page, $filter );
</ins><span class="cx"> 
</span><span class="cx">                 if ( $type == 'friends' &amp;&amp; ( bp_is_home() || is_site_admin() || $bp-&gt;loggedin_user-&gt;id == $user_id ) )
</span><del>-                        $this-&gt;activities = bp_activity_get_friends_activity( $user_id, $max, $timeframe, false, $this-&gt;pag_num, $this-&gt;pag_page, $filter );
</del><ins>+                        $this-&gt;activities = bp_activity_get_friends_activity( $user_id, $max, false, $this-&gt;pag_num, $this-&gt;pag_page, $filter );
</ins><span class="cx">                 
</span><span class="cx">                 if ( !$max || $max &gt;= (int)$this-&gt;activities['total'] )
</span><span class="cx">                         $this-&gt;total_activity_count = (int)$this-&gt;activities['total'];
</span><span class="lines">@@ -88,7 +88,7 @@
</span><span class="cx">                 if ( $this-&gt;current_activity + 1 &lt; $this-&gt;activity_count ) {
</span><span class="cx">                         return true;
</span><span class="cx">                 } elseif ( $this-&gt;current_activity + 1 == $this-&gt;activity_count ) {
</span><del>-                        do_action('loop_end');
</del><ins>+                        do_action('activity_loop_end');
</ins><span class="cx">                         // Do some cleaning up after the loop
</span><span class="cx">                         $this-&gt;rewind_activities();
</span><span class="cx">                 }
</span><span class="lines">@@ -107,7 +107,7 @@
</span><span class="cx">                         $this-&gt;activity = (object) $this-&gt;activity;
</span><span class="cx"> 
</span><span class="cx">                 if ( $this-&gt;current_activity == 0 ) // loop has just started
</span><del>-                        do_action('loop_start');
</del><ins>+                        do_action('activity_loop_start');
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -124,14 +124,18 @@
</span><span class="cx"> 
</span><span class="cx"> function bp_has_activities( $args = '' ) {
</span><span class="cx">         global $bp, $activities_template, $bp_activity_user_id, $bp_activity_limit;
</span><del>-
</del><ins>+        
+        /* Note: any params used for filtering can be a single value, or multiple values comma seperated. */
+        
</ins><span class="cx">         $defaults = array(
</span><span class="cx">                 'type' =&gt; 'sitewide',
</span><del>-                'user_id' =&gt; false,
</del><span class="cx">                 'per_page' =&gt; 25,
</span><span class="cx">                 'max' =&gt; false,
</span><del>-                'timeframe' =&gt; '-4 weeks',
-                'filter' =&gt; false
</del><ins>+                'user_id' =&gt; false, // user_id to filter on
+                'object' =&gt; false, // object to filter on e.g. groups, profile, status, friends
+                'action' =&gt; false, // action to filter on e.g. new_wire_post, new_forum_post, profile_updated
+                'primary_id' =&gt; false, // object ID to filter on e.g. a group_id or forum_id or blog_id etc.
+                'secondary_id' =&gt; false, // secondary object ID to filter on e.g. a post_id
</ins><span class="cx">         );
</span><span class="cx"> 
</span><span class="cx">         $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -161,9 +165,11 @@
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         if ( isset( $_GET['afilter'] ) )
</span><del>-                $filter = $_GET['afilter'];
-
-        $activities_template = new BP_Activity_Template( $type, $user_id, $per_page, $max, $timeframe, $filter );                
</del><ins>+                $filter = array( 'object' =&gt; $_GET['afilter'] );
+        else
+                $filter = array( 'object' =&gt; $object, 'action' =&gt; $action, 'primary_id' =&gt; $primary_id, 'secondary_id' =&gt; $secondary_id );
+        
+        $activities_template = new BP_Activity_Template( $type, $user_id, $per_page, $max, $filter );                
</ins><span class="cx">         return $activities_template-&gt;has_activities();
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity.php (1838 => 1839)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity.php        2009-09-09 23:57:11 UTC (rev 1838)
+++ trunk/bp-activity.php        2009-09-10 01:24:01 UTC (rev 1839)
</span><span class="lines">@@ -351,12 +351,12 @@
</span><span class="cx">         return BP_Activity_Activity::get_sitewide_activity( $max_items, $pag_num, $pag_page, $filter );
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-function bp_activity_get_user_activity( $user_id, $max_items = 30, $since = '-4 weeks', $pag_num = false, $pag_page = false, $filter = false ) {
-        return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $since, $pag_num, $pag_page, $filter );
</del><ins>+function bp_activity_get_user_activity( $user_id, $max_items = 30, $pag_num = false, $pag_page = false, $filter = false ) {
+        return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $pag_num, $pag_page, $filter );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-function bp_activity_get_friends_activity( $user_id, $max_items = 30, $since = '-4 weeks', $max_items_per_friend = false, $pag_num = false, $pag_page = false, $filter = false ) {
-        return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $pag_num, $pag_page, $filter );
</del><ins>+function bp_activity_get_friends_activity( $user_id, $max_items = 30, $max_items_per_friend = false, $pag_num = false, $pag_page = false, $filter = false ) {
+        return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $max_items_per_friend, $pag_num, $pag_page, $filter );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> function bp_activity_remove_data( $user_id ) {
</span></span></pre>
</div>
</div>

</body>
</html>