<!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] [2533] trunk:
  Removing database hits inside BP loops to improve overall performance.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd>2533</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-02-02 12:40:49 +0000 (Tue, 02 Feb 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Removing database hits inside BP loops to improve overall performance.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcoreclassesphp">trunk/bp-core/bp-core-classes.php</a></li>
<li><a href="#trunkbpcorebpcoretemplatetagsphp">trunk/bp-core/bp-core-templatetags.php</a></li>
<li><a href="#trunkbpforumsbpforumstemplatetagsphp">trunk/bp-forums/bp-forums-templatetags.php</a></li>
<li><a href="#trunkbpforumsphp">trunk/bp-forums.php</a></li>
<li><a href="#trunkbpfriendsbpfriendstemplatetagsphp">trunk/bp-friends/bp-friends-templatetags.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatetagsphp">trunk/bp-groups/bp-groups-templatetags.php</a></li>
<li><a href="#trunkbpthemesbpdefault_incglobaljs">trunk/bp-themes/bp-default/_inc/global.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcoreclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-classes.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-classes.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-core/bp-core-classes.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -339,6 +339,27 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                /* Fetch whether or not the user is a friend */
+                if ( function_exists( 'friends_install' ) ) {
+                        $friend_status = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp-&gt;friends-&gt;table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )&quot;, $bp-&gt;loggedin_user-&gt;id, $bp-&gt;loggedin_user-&gt;id ) );
+                        for ( $i = 0; $i &lt; count( $paged_users ); $i++ ) {
+                                foreach ( $friend_status as $status ) {
+                                        if ( $status-&gt;initiator_user_id == $paged_users[$i]-&gt;id || $status-&gt;friend_user_id == $paged_users[$i]-&gt;id )
+                                                $paged_users[$i]-&gt;is_friend = $status-&gt;is_confirmed;
+                                }
+                        }
+                }
+
+                if ( 'active' != $type ) {
+                        $user_activity = $wpdb-&gt;get_results( &quot;SELECT user_id as id, meta_value as last_activity FROM &quot; . CUSTOM_USER_META_TABLE . &quot; WHERE meta_key = 'last_activity' AND user_id IN ( {$user_ids} )&quot; );
+                        for ( $i = 0; $i &lt; count( $paged_users ); $i++ ) {
+                                foreach ( $user_activity as $activity ) {
+                                        if ( $activity-&gt;id == $paged_users[$i]-&gt;id )
+                                                $paged_users[$i]-&gt;last_activity = $activity-&gt;last_activity;
+                                }
+                        }
+                }
+
</ins><span class="cx">                 /* Fetch the user's last_activity */
</span><span class="cx">                 if ( 'active' != $type ) {
</span><span class="cx">                         $user_activity = $wpdb-&gt;get_results( &quot;SELECT user_id as id, meta_value as last_activity FROM &quot; . CUSTOM_USER_META_TABLE . &quot; WHERE meta_key = 'last_activity' AND user_id IN ( {$user_ids} )&quot; );
</span></span></pre></div>
<a id="trunkbpcorebpcoretemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-templatetags.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-templatetags.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-core/bp-core-templatetags.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -341,7 +341,12 @@
</span><span class="cx">         global $members_template;
</span><span class="cx"> 
</span><span class="cx">         if ( function_exists( 'bp_add_friend_button' ) ) {
</span><del>-                echo bp_add_friend_button( $members_template-&gt;member-&gt;id );
</del><ins>+                if ( null === $members_template-&gt;member-&gt;is_friend )
+                        $friend_status = 'not_friends';
+                else
+                        $friend_status = ( 0 == $members_template-&gt;member-&gt;is_friend ) ? 'pending' : 'is_friend';
+
+                echo bp_add_friend_button( $members_template-&gt;member-&gt;id, $friend_status );
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpforumsbpforumstemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-forums/bp-forums-templatetags.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-forums/bp-forums-templatetags.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-forums/bp-forums-templatetags.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -92,6 +92,9 @@
</span><span class="cx">                         $this-&gt;topics = array_merge( (array)$stickies, (array)$standard );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                /* Fetch extra information for topics, so we don't have to query inside the loop */
+                $this-&gt;topics = bp_forums_get_topic_extras( &amp;$this-&gt;topics );
+
</ins><span class="cx">                 $this-&gt;pag_links = paginate_links( array(
</span><span class="cx">                         'base' =&gt; add_query_arg( array( 'p' =&gt; '%#%', 'n' =&gt; $this-&gt;pag_num ) ),
</span><span class="cx">                         'format' =&gt; '',
</span><span class="lines">@@ -347,10 +350,10 @@
</span><span class="cx">         function bp_get_the_topic_last_poster_name() {
</span><span class="cx">                 global $forum_template;
</span><span class="cx"> 
</span><del>-                if ( !$name = bp_core_get_userlink( $forum_template-&gt;topic-&gt;topic_last_poster ) )
</del><ins>+                if ( !$domain = bp_core_get_user_domain( $forum_template-&gt;topic-&gt;topic_last_poster, $forum_template-&gt;topic-&gt;topic_last_poster_nicename, $forum_template-&gt;topic-&gt;topic_last_poster_login ) )
</ins><span class="cx">                         return __( 'Deleted User', 'buddypress' );
</span><span class="cx"> 
</span><del>-                return apply_filters( 'bp_get_the_topic_last_poster_name', $name );
</del><ins>+                return apply_filters( 'bp_get_the_topic_last_poster_name', '&lt;a href=&quot;' . $domain . '&quot;&gt;' . $forum_template-&gt;topic-&gt;topic_last_poster_displayname . '&lt;/a&gt;' );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx"> function bp_the_topic_object_avatar( $args = '' ) {
</span><span class="lines">@@ -386,7 +389,7 @@
</span><span class="cx">                 $r = wp_parse_args( $args, $defaults );
</span><span class="cx">                 extract( $r, EXTR_SKIP );
</span><span class="cx"> 
</span><del>-                return apply_filters( 'bp_get_the_topic_last_poster_avatar', bp_core_fetch_avatar( array( 'item_id' =&gt; $forum_template-&gt;topic-&gt;topic_last_poster, 'type' =&gt; $type, 'width' =&gt; $width, 'height' =&gt; $height ) ) );
</del><ins>+                return apply_filters( 'bp_get_the_topic_last_poster_avatar', bp_core_fetch_avatar( array( 'email' =&gt; $forum_template-&gt;topic-&gt;topic_last_poster_email, 'item_id' =&gt; $forum_template-&gt;topic-&gt;topic_last_poster, 'type' =&gt; $type, 'width' =&gt; $width, 'height' =&gt; $height ) ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx"> function bp_the_topic_start_time() {
</span></span></pre></div>
<a id="trunkbpforumsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-forums.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-forums.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-forums.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -333,6 +333,43 @@
</span><span class="cx">         return $count;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+function bp_forums_get_topic_extras( $topics ) {
+        global $bp, $wpdb, $bbdb;
+
+        if ( empty( $topics ) )
+                return $topics;
+
+        /* Get the topic ids */
+        foreach ( $topics as $topic ) $topic_ids[] = $topic-&gt;topic_id;
+        $topic_ids = $wpdb-&gt;escape( join( ',', (array)$topic_ids ) );
+
+        /* Fetch the topic's last poster details */
+        $poster_details = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT t.topic_id, t.topic_last_poster, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb-&gt;users} u, {$bbdb-&gt;topics} t WHERE u.ID = t.topic_last_poster AND t.topic_id IN ( {$topic_ids} )&quot; ) );
+        for ( $i = 0; $i &lt; count( $topics ); $i++ ) {
+                foreach ( $poster_details as $poster ) {
+                        if ( $poster-&gt;topic_id == $topics[$i]-&gt;topic_id ) {
+                                $topics[$i]-&gt;topic_last_poster_email = $poster-&gt;user_email;
+                                $topics[$i]-&gt;topic_last_poster_nicename = $poster-&gt;user_nicename;
+                                $topics[$i]-&gt;topic_last_poster_login = $poster-&gt;user_login;
+                                $topics[$i]-&gt;topic_last_poster_displayname = $poster-&gt;display_name;
+                        }
+                }
+        }
+
+        /* Fetch fullname for the topic's last poster */
+        if ( function_exists( 'xprofile_install' ) ) {
+                $poster_names = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT t.topic_id, pd.value FROM {$bp-&gt;profile-&gt;table_name_data} pd, {$bbdb-&gt;topics} t WHERE pd.user_id = t.topic_last_poster AND pd.field_id = 1 AND t.topic_id IN ( {$topic_ids} )&quot; ) );
+                for ( $i = 0; $i &lt; count( $topics ); $i++ ) {
+                        foreach ( $poster_names as $name ) {
+                                if ( $name-&gt;topic_id == $topics[$i]-&gt;topic_id )
+                                        $topics[$i]-&gt;topic_last_poster_displayname = $name-&gt;value;
+                        }
+                }
+        }
+
+        return $topics;
+}
+
</ins><span class="cx"> /* Post Functions */
</span><span class="cx"> 
</span><span class="cx"> function bp_forums_get_topic_posts( $args = '' ) {
</span></span></pre></div>
<a id="trunkbpfriendsbpfriendstemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-friends/bp-friends-templatetags.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-friends/bp-friends-templatetags.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-friends/bp-friends-templatetags.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -114,10 +114,10 @@
</span><span class="cx">         &lt;?php
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-function bp_add_friend_button( $potential_friend_id = false ) {
-        echo bp_get_add_friend_button( $potential_friend_id );
</del><ins>+function bp_add_friend_button( $potential_friend_id = false, $friend_status = false ) {
+        echo bp_get_add_friend_button( $potential_friend_id, $friend_status );
</ins><span class="cx"> }
</span><del>-        function bp_get_add_friend_button( $potential_friend_id = false ) {
</del><ins>+        function bp_get_add_friend_button( $potential_friend_id = false, $friend_status = false ) {
</ins><span class="cx">                 global $bp, $friends_template;
</span><span class="cx"> 
</span><span class="cx">                 if ( !is_user_logged_in() )
</span><span class="lines">@@ -131,7 +131,8 @@
</span><span class="cx">                 if ( $bp-&gt;loggedin_user-&gt;id == $potential_friend_id )
</span><span class="cx">                         return false;
</span><span class="cx"> 
</span><del>-                $friend_status = friends_check_friendship_status( $bp-&gt;loggedin_user-&gt;id, $potential_friend_id );
</del><ins>+                if ( empty( $friend_status ) )
+                        $friend_status = friends_check_friendship_status( $bp-&gt;loggedin_user-&gt;id, $potential_friend_id );
</ins><span class="cx"> 
</span><span class="cx">                 $button = '&lt;div class=&quot;generic-button friendship-button ' . $friend_status . '&quot; id=&quot;friendship-button-' . $potential_friend_id . '&quot;&gt;';
</span><span class="cx">                 if ( 'pending' == $friend_status ) {
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-classes.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-groups/bp-groups-classes.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -47,11 +47,12 @@
</span><span class="cx">                         if ( $get_user_dataset ) {
</span><span class="cx">                                 $this-&gt;user_dataset = $this-&gt;get_user_dataset();
</span><span class="cx"> 
</span><del>-                                //if ( !$this-&gt;total_member_count ) {
</del><span class="cx">                                 $this-&gt;total_member_count = count( $this-&gt;user_dataset );
</span><span class="cx">                                 groups_update_groupmeta( $this-&gt;id, 'total_member_count', $this-&gt;total_member_count );
</span><del>-                                //}
</del><span class="cx">                         }
</span><ins>+
+                        /* Get group extras */
+                        $this-&gt;is_member = BP_Groups_Member::check_is_member( $bp-&gt;loggedin_user-&gt;id, $this-&gt;id );
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -335,6 +336,9 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm INNER JOIN {$bp-&gt;groups-&gt;table_name} g ON gm.group_id = g.id WHERE gm.meta_key = 'last_activity'{$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'newest' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -360,6 +364,10 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm INNER JOIN {$bp-&gt;groups-&gt;table_name} g ON gm.group_id = g.id WHERE gm.meta_key = 'last_activity'{$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                foreach ( $paged_groups as $group ) $group_ids[] = $group-&gt;id;
+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'active' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -386,6 +394,9 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm1, {$bp-&gt;groups-&gt;table_name_groupmeta} gm2, {$bp-&gt;groups-&gt;table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' {$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'popular' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -411,6 +422,9 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm1, {$bp-&gt;groups-&gt;table_name_groupmeta} gm2, {$bp-&gt;groups-&gt;table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' {$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'alphabetical' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -440,6 +454,9 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm1, {$bp-&gt;groups-&gt;table_name_groupmeta} gm2, {$bp-&gt;groups-&gt;table_name_groupmeta} gm3, {$bbdb-&gt;forums} f, {$bp-&gt;groups-&gt;table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) AND f.topics &gt; 0 {$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'most_forum_topics' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -469,6 +486,9 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm1, {$bp-&gt;groups-&gt;table_name_groupmeta} gm2, {$bp-&gt;groups-&gt;table_name_groupmeta} gm3, {$bbdb-&gt;forums} f, {$bp-&gt;groups-&gt;table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND g.id = gm3.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND (gm3.meta_key = 'forum_id' AND gm3.meta_value = f.forum_id) {$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'most_forum_posts' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -534,6 +554,9 @@
</span><span class="cx"> 
</span><span class="cx">                 $paged_groups = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm1, {$bp-&gt;groups-&gt;table_name_groupmeta} gm2, {$bp-&gt;groups-&gt;table_name} g WHERE g.id = gm1.group_id AND g.id = gm2.group_id AND gm2.meta_key = 'last_activity' AND gm1.meta_key = 'total_member_count' AND g.name LIKE '$letter%%' {$hidden_sql} {$search_sql} ORDER BY g.name ASC {$pag_sql}&quot;  ) );
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'letter' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -559,9 +582,38 @@
</span><span class="cx">                         $total_groups = $wpdb-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT COUNT(DISTINCT g.id) FROM {$bp-&gt;groups-&gt;table_name_groupmeta} gm INNER JOIN {$bp-&gt;groups-&gt;table_name} g ON gm.group_id = g.id WHERE gm.meta_key = 'last_activity'{$hidden_sql} {$search_sql}&quot; ) );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><ins>+                $group_ids = $wpdb-&gt;escape( join( ',', (array)$group_ids ) );
+                $paged_groups = BP_Groups_Group::get_group_extras( &amp;$paged_groups, $group_ids, 'random' );
+
</ins><span class="cx">                 return array( 'groups' =&gt; $paged_groups, 'total' =&gt; $total_groups );
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        function get_group_extras( $paged_groups, $group_ids, $type = false ) {
+                global $bp, $wpdb;
+
+                if ( empty( $group_ids ) )
+                        return $paged_groups;
+
+                /* Fetch the logged in users status within each group */
+                $user_status = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT group_id FROM {$bp-&gt;groups-&gt;table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_confirmed = 1 AND is_banned = 0&quot;, $bp-&gt;loggedin_user-&gt;id ) );
+                for ( $i = 0; $i &lt; count( $paged_groups ); $i++ ) {
+                        foreach ( $user_status as $group_id ) {
+                                if ( $group_id == $paged_groups[$i]-&gt;id )
+                                        $paged_groups[$i]-&gt;is_member = true;
+                        }
+                }
+
+                $user_banned = $wpdb-&gt;get_col( $wpdb-&gt;prepare( &quot;SELECT group_id FROM {$bp-&gt;groups-&gt;table_name_members} WHERE is_banned = 1 AND user_id = %d AND group_id IN ( {$group_ids} )&quot;, $bp-&gt;loggedin_user-&gt;id ) );
+                for ( $i = 0; $i &lt; count( $paged_groups ); $i++ ) {
+                        foreach ( $user_banned as $group_id ) {
+                                if ( $group_id == $paged_groups[$i]-&gt;id )
+                                        $paged_groups[$i]-&gt;is_banned = true;
+                        }
+                }
+
+                return $paged_groups;
+        }
+
</ins><span class="cx">         function delete_all_invites( $group_id ) {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-templatetags.php (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-templatetags.php        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-groups/bp-groups-templatetags.php        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -1079,10 +1079,10 @@
</span><span class="cx">         if ( !$group )
</span><span class="cx">                 $group =&amp; $groups_template-&gt;group;
</span><span class="cx"> 
</span><del>-        if ( groups_is_user_member( $bp-&gt;loggedin_user-&gt;id, $group-&gt;id ) )
-                return true;
</del><ins>+        if ( null == $group-&gt;is_member )
+                return false;
</ins><span class="cx"> 
</span><del>-        return false;
</del><ins>+        return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> function bp_group_accept_invite_link( $deprecated = false ) {
</span><span class="lines">@@ -1167,7 +1167,7 @@
</span><span class="cx">                 $group =&amp; $groups_template-&gt;group;
</span><span class="cx"> 
</span><span class="cx">         // If they're not logged in or are banned from the group, no join button.
</span><del>-        if ( !is_user_logged_in() || groups_is_user_banned( $bp-&gt;loggedin_user-&gt;id, $group-&gt;id ) )
</del><ins>+        if ( !is_user_logged_in() || $group-&gt;is_banned )
</ins><span class="cx">                 return false;
</span><span class="cx"> 
</span><span class="cx">         if ( !$group-&gt;status )
</span><span class="lines">@@ -1177,14 +1177,14 @@
</span><span class="cx"> 
</span><span class="cx">         switch ( $group-&gt;status ) {
</span><span class="cx">                 case 'public':
</span><del>-                        if ( BP_Groups_Member::check_is_member( $bp-&gt;loggedin_user-&gt;id, $group-&gt;id ) )
</del><ins>+                        if ( $group-&gt;is_member )
</ins><span class="cx">                                 echo '&lt;a class=&quot;leave-group&quot; href=&quot;' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '&quot;&gt;' . __( 'Leave Group', 'buddypress' ) . '&lt;/a&gt;';
</span><span class="cx">                         else
</span><span class="cx">                                 echo '&lt;a class=&quot;join-group&quot; href=&quot;' . wp_nonce_url( bp_get_group_permalink( $group ) . 'join', 'groups_join_group' ) . '&quot;&gt;' . __( 'Join Group', 'buddypress' ) . '&lt;/a&gt;';
</span><span class="cx">                 break;
</span><span class="cx"> 
</span><span class="cx">                 case 'private':
</span><del>-                        if ( BP_Groups_Member::check_is_member( $bp-&gt;loggedin_user-&gt;id, $group-&gt;id ) ) {
</del><ins>+                        if ( $group-&gt;is_member ) {
</ins><span class="cx">                                 echo '&lt;a class=&quot;leave-group&quot; href=&quot;' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '&quot;&gt;' . __( 'Leave Group', 'buddypress' ) . '&lt;/a&gt;';
</span><span class="cx">                         } else {
</span><span class="cx">                                 if ( !bp_group_has_requested_membership( $group ) )
</span></span></pre></div>
<a id="trunkbpthemesbpdefault_incglobaljs"></a>
<div class="modfile"><h4>Modified: trunk/bp-themes/bp-default/_inc/global.js (2532 => 2533)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-themes/bp-default/_inc/global.js        2010-02-02 08:33:39 UTC (rev 2532)
+++ trunk/bp-themes/bp-default/_inc/global.js        2010-02-02 12:40:49 UTC (rev 2533)
</span><span class="lines">@@ -796,12 +796,7 @@
</span><span class="cx"> 
</span><span class="cx">         /** Alternate Highlighting ******************************************/
</span><span class="cx"> 
</span><del>-        j('table tr').each( function(i) {
-                if ( i % 2 == 1 )
-                        j(this).addClass('alt');
-        });
-
-        j('div.message-box, ul#topic-post-list li').each( function(i) {
</del><ins>+        j('table tr, div.message-box, ul#topic-post-list li').each( function(i) {
</ins><span class="cx">                 if ( i % 2 != 1 )
</span><span class="cx">                         j(this).addClass('alt');
</span><span class="cx">         });
</span></span></pre>
</div>
</div>

</body>
</html>