<!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] [2536] trunk/bp-blogs:
Major performance improvements to blog listings.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>2536</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-02-02 15:31:49 +0000 (Tue, 02 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Major performance improvements to blog listings.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpblogsbpblogsclassesphp">trunk/bp-blogs/bp-blogs-classes.php</a></li>
<li><a href="#trunkbpblogsbpblogstemplatetagsphp">trunk/bp-blogs/bp-blogs-templatetags.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpblogsbpblogsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-classes.php (2535 => 2536)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-classes.php        2010-02-02 14:11:08 UTC (rev 2535)
+++ trunk/bp-blogs/bp-blogs-classes.php        2010-02-02 15:31:49 UTC (rev 2536)
</span><span class="lines">@@ -69,6 +69,52 @@
</span><span class="cx">
</span><span class="cx">         /* Static Functions */
</span><span class="cx">
</span><ins>+        function get( $type, $limit = false, $page = false, $user_id = false, $search_terms = false ) {
+                global $bp, $wpdb;
+
+                if ( !$bp->blogs )
+                        bp_blogs_setup_globals();
+
+                if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
+                        $hidden_sql = "AND wb.public = 1";
+
+                if ( $limit && $page )
+                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
+
+                if ( $user_id )
+                        $user_sql = $wpdb->prepare( " AND b.user_id = %d", $user_id );
+
+                switch ( $type ) {
+                        case 'active': default:
+                                $order_sql = "ORDER BY CONVERT(bm.meta_value, SIGNED) DESC";
+                                break;
+                        case 'alphabetical':
+                                $order_sql = "ORDER BY bm2.meta_value ASC";
+                                break;
+                        case 'newest':
+                                $order_sql = "ORDER BY wb.registered DESC";
+                                break;
+                        case 'random':
+                                $order_sql = "ORDER BY RAND()";
+                                break;
+                }
+
+                if ( !empty( $search_terms ) ) {
+                        $filter = like_escape( $wpdb->escape( $search_terms ) );
+                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm2.meta_value as name, bm3.meta_value as description FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$bp->blogs->table_name_blogmeta} bm3, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND b.blog_id = bm3.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND bm3.meta_key = 'description' AND ( bm2.meta_value LIKE '%%$filter%%' || bm3.meta_value LIKE '%%$filter%%' ) {$user_sql} {$order_sql} {$pag_sql}" ) );
+                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2 WHERE b.blog_id = wb.blog_id AND bm.blog_id = b.blog_id AND bm2.blog_id = b.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'name' AND bm2.meta_key = 'description' AND ( bm.meta_value LIKE '%%$filter%%' || bm2.meta_value LIKE '%%$filter%%' ) {$user_sql}" ) );
+                } else {
+                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm2.meta_value as name, bm3.meta_value as description FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$bp->blogs->table_name_blogmeta} bm3, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND b.blog_id = bm3.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND bm3.meta_key = 'description' {$order_sql} {$pag_sql}" ) );
+                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql}" ) );
+                }
+
+                foreach ( $paged_blogs as $blog ) $blog_ids[] = $blog->blog_id;
+                $blog_ids = $wpdb->escape( join( ',', (array)$blog_ids ) );
+                $paged_blogs = BP_Blogs_Blog::get_blog_extras( &$paged_blogs, $blog_ids, $type );
+
+                return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
+        }
+
</ins><span class="cx">         function delete_blog_for_all( $blog_id ) {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><span class="lines">@@ -169,44 +215,6 @@
</span><span class="cx">                         return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id) );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function get_all( $limit = null, $page = null ) {
-                global $bp, $wpdb;
-
-                if ( !$bp->blogs )
-                        bp_blogs_setup_globals();
-
-                if ( !is_site_admin() )
-                        $hidden_sql = "AND wb.public = 1";
-
-                if ( $limit && $page )
-                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
-
-                $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$pag_sql}" ) );
-                $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql}" ) );
-
-                return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
-        }
-
-        function get_by_letter( $letter, $limit = null, $page = null ) {
-                global $bp, $wpdb;
-
-                if ( !$bp->blogs )
-                        bp_blogs_setup_globals();
-
-                $letter = like_escape( $wpdb->escape( $letter ) );
-
-                if ( !is_site_admin() )
-                        $hidden_sql = "AND wb.public = 1";
-
-                if ( $limit && $page )
-                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
-
-                $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" ) );
-                $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" ) );
-
-                return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
-        }
-
</del><span class="cx">         function search_blogs( $filter, $limit = null, $page = null ) {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><span class="lines">@@ -227,126 +235,62 @@
</span><span class="cx">                 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function get_random( $limit = null, $page = null, $user_id = false, $search_terms = false ) {
</del><ins>+        function get_all( $limit = null, $page = null ) {
</ins><span class="cx">                 global $bp, $wpdb;
</span><span class="cx">
</span><span class="cx">                 if ( !$bp->blogs )
</span><span class="cx">                         bp_blogs_setup_globals();
</span><span class="cx">
</span><del>-                if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
</del><ins>+                if ( !is_site_admin() )
</ins><span class="cx">                         $hidden_sql = "AND wb.public = 1";
</span><span class="cx">
</span><span class="cx">                 if ( $limit && $page )
</span><span class="cx">                         $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
</span><span class="cx">
</span><del>-                if ( $user_id ) {
-                        $blog_ids = $wpdb->escape( implode( ',', (array)BP_Blogs_Blog::get_blog_ids_for_user( $user_id) ) );
-                        $user_sql = $wpdb->prepare( " AND b.blog_id IN ( {$blog_ids} ) ");
-                }
</del><ins>+                $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$pag_sql}" ) );
+                $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql}" ) );
</ins><span class="cx">
</span><del>-                $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$user_sql} ORDER BY rand() {$pag_sql}" ) );
-                $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$user_sql} ORDER BY rand()" ) );
-
</del><span class="cx">                 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function get_active( $limit = null, $page = null, $user_id = false, $search_terms = false ) {
</del><ins>+        function get_by_letter( $letter, $limit = null, $page = null ) {
</ins><span class="cx">                 global $bp, $wpdb;
</span><span class="cx">
</span><span class="cx">                 if ( !$bp->blogs )
</span><span class="cx">                         bp_blogs_setup_globals();
</span><span class="cx">
</span><del>-                if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
-                        $hidden_sql = "AND wb.public = 1";
</del><ins>+                $letter = like_escape( $wpdb->escape( $letter ) );
</ins><span class="cx">
</span><del>-                if ( $limit && $page )
-                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
-
-                if ( $user_id ) {
-                        $blog_ids = $wpdb->escape( implode( ',', (array)BP_Blogs_Blog::get_blog_ids_for_user( $user_id ) ) );
-
-                        if ( empty( $blog_ids ) )
-                                return false;
-                        else
-                                $user_sql = $wpdb->prepare( " AND b.blog_id IN ( {$blog_ids} ) " );
-                }
-
-                if ( !empty( $search_terms ) ) {
-                        $filter = like_escape( $wpdb->escape( $search_terms ) );
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND ( ( bm2.meta_key = 'name' OR bm2.meta_key = 'description' ) AND bm2.meta_value LIKE '%%$filter%%' ) {$user_sql} ORDER BY CONVERT(bm.meta_value, SIGNED) DESC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND ( ( bm2.meta_key = 'name' OR bm2.meta_key = 'description' ) AND bm2.meta_value LIKE '%%$filter%%' ) {$user_sql} ORDER BY CONVERT(bm.meta_value, SIGNED) DESC" ) );
-                } else {
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' {$user_sql} ORDER BY CONVERT(bm.meta_value, SIGNED) DESC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' {$user_sql} ORDER BY CONVERT(bm.meta_value, SIGNED) DESC" ) );
-                }
-
-                return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
-        }
-
-        function get_alphabetical( $limit = null, $page = null, $user_id = false, $search_terms = false ) {
-                global $bp, $wpdb;
-
-                if ( !$bp->blogs )
-                        bp_blogs_setup_globals();
-
-                if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
</del><ins>+                if ( !is_site_admin() )
</ins><span class="cx">                         $hidden_sql = "AND wb.public = 1";
</span><span class="cx">
</span><span class="cx">                 if ( $limit && $page )
</span><span class="cx">                         $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
</span><span class="cx">
</span><del>-                if ( $user_id ) {
-                        $blog_ids = $wpdb->escape( implode( ',', (array)BP_Blogs_Blog::get_blog_ids_for_user( $user_id ) ) );
</del><ins>+                $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" ) );
+                $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" ) );
</ins><span class="cx">
</span><del>-                        if ( empty( $blog_ids ) )
-                                return false;
-                        else
-                                $user_sql = $wpdb->prepare( " AND b.blog_id IN ( {$blog_ids} ) " );
-                }
-
-                if ( !empty( $search_terms ) ) {
-                        $filter = like_escape( $wpdb->escape( $search_terms ) );
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$bp->blogs->table_name_blogmeta} bm3, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND b.blog_id = bm3.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND ( ( bm3.meta_key = 'name' OR bm3.meta_key = 'description' ) AND bm3.meta_value LIKE '%%$filter%%' ) {$user_sql} ORDER BY bm2.meta_value ASC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$bp->blogs->table_name_blogmeta} bm3, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND b.blog_id = bm3.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND ( ( bm3.meta_key = 'name' OR bm3.meta_key = 'description' ) AND bm3.meta_value LIKE '%%$filter%%' ) {$user_sql}" ) );
-                } else {
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' {$user_sql} ORDER BY bm2.meta_value ASC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' {$user_sql}" ) );
-                }
-
</del><span class="cx">                 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function get_newest( $limit = null, $page = null, $user_id = false, $search_terms = false ) {
</del><ins>+        function get_blog_extras( $paged_blogs, $blog_ids, $type = false ) {
</ins><span class="cx">                 global $bp, $wpdb;
</span><span class="cx">
</span><del>-                if ( !$bp->blogs )
-                        bp_blogs_setup_globals();
</del><ins>+                if ( empty( $blog_ids ) )
+                        return $paged_blogs;
</ins><span class="cx">
</span><del>-                if ( !is_user_logged_in() || ( !is_site_admin() && ( $user_id != $bp->loggedin_user->id ) ) )
-                        $hidden_sql = "AND wb.public = 1";
</del><ins>+                /* Fetch lastest post for each blog. */
+                $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM {$bp->blogs->table_name_blog_posts} p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.blog_id IN ( {$blog_ids} ) GROUP BY p.blog_id ORDER BY p.date_created DESC" ) );
</ins><span class="cx">
</span><del>-                if ( $limit && $page )
-                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
-
-                if ( $user_id ) {
-                        $blog_ids = $wpdb->escape( implode( ',', (array)BP_Blogs_Blog::get_blog_ids_for_user( $bp->loggedin_user->id ) ) );
-
-                        if ( empty( $blog_ids ) )
-                                return false;
-                        else
-                                $user_sql = $wpdb->prepare( " AND b.blog_id IN ( {$blog_ids} ) " );
</del><ins>+                for ( $i = 0; $i < count( $paged_blogs ); $i++ ) {
+                        foreach ( $post_ids as $post ) {
+                                if ( $post->blog_id == $paged_blogs[$i]->blog_id ) {
+                                        $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT post_title, guid FROM {$wpdb->base_prefix}" . $post->blog_id . "_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY id DESC LIMIT 1" );
+                                }
+                        }
</ins><span class="cx">                 }
</span><span class="cx">
</span><del>-                if ( !empty( $search_terms ) ) {
-                        $filter = like_escape( $wpdb->escape( $search_terms ) );
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND bm.meta_value LIKE '%%$filter%%' ) {$user_sql} ORDER BY wb.registered DESC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm WHERE b.blog_id = wb.blog_id AND b.blog_id = bm.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND bm.meta_value LIKE '%%$filter%%' ) {$user_sql} ORDER BY wb.registered DESC" ) );
-                } else {
-                        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT wb.blog_id FROM {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name} b WHERE wb.blog_id = b.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$user_sql} ORDER BY wb.registered DESC {$pag_sql}" ) );
-                        $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT wb.blog_id) FROM {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name} b WHERE wb.blog_id = b.blog_id {$hidden_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$user_sql} ORDER BY wb.registered DESC" ) );
-                }
-
-                return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
</del><ins>+                return $paged_blogs;
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         function is_hidden( $blog_id ) {
</span></span></pre></div>
<a id="trunkbpblogsbpblogstemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-templatetags.php (2535 => 2536)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-templatetags.php        2010-02-02 14:11:08 UTC (rev 2535)
+++ trunk/bp-blogs/bp-blogs-templatetags.php        2010-02-02 15:31:49 UTC (rev 2536)
</span><span class="lines">@@ -23,28 +23,11 @@
</span><span class="cx">                 $this->pag_page = isset( $_REQUEST['bpage'] ) ? intval( $_REQUEST['bpage'] ) : $page;
</span><span class="cx">                 $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
</span><span class="cx">
</span><del>-                if ( isset( $_REQUEST['letter'] ) && '' != $_REQUEST['letter'] ) {
</del><ins>+                if ( isset( $_REQUEST['letter'] ) && '' != $_REQUEST['letter'] )
</ins><span class="cx">                         $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
</span><del>-                } else {
-                        switch ( $type ) {
-                                case 'random':
-                                        $this->blogs = BP_Blogs_Blog::get_random( $this->pag_num, $this->pag_page, $user_id, $search_terms );
-                                        break;
</del><ins>+                else
+                        $this->blogs = BP_Blogs_Blog::get( $type, $this->pag_num, $this->pag_page, $user_id, $search_terms );
</ins><span class="cx">
</span><del>-                                case 'alphabetical':
-                                        $this->blogs = BP_Blogs_Blog::get_alphabetical( $this->pag_num, $this->pag_page, $user_id, $search_terms );
-                                        break;
-
-                                case 'newest':
-                                        $this->blogs = BP_Blogs_Blog::get_newest( $this->pag_num, $this->pag_page, $user_id, $search_terms );
-                                        break;
-
-                                case 'active': default:
-                                        $this->blogs = BP_Blogs_Blog::get_active( $this->pag_num, $this->pag_page, $user_id, $search_terms );
-                                        break;
-                        }
-                }
-
</del><span class="cx">                 if ( !$max || $max >= (int)$this->blogs['total'] )
</span><span class="cx">                         $this->total_blog_count = (int)$this->blogs['total'];
</span><span class="cx">                 else
</span><span class="lines">@@ -218,7 +201,8 @@
</span><span class="cx">                         'height' => false,
</span><span class="cx">                         'class' => 'avatar',
</span><span class="cx">                         'id' => false,
</span><del>-                        'alt' => __( 'Blog avatar', 'buddypress' )
</del><ins>+                        'alt' => __( 'Blog avatar', 'buddypress' ),
+                        'no_grav' => true
</ins><span class="cx">                 );
</span><span class="cx">
</span><span class="cx">                 $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -227,8 +211,9 @@
</span><span class="cx">                 /***
</span><span class="cx">                  * In future BuddyPress versions you will be able to set the avatar for a blog.
</span><span class="cx">                  * Right now you can use a filter with the ID of the blog to change it if you wish.
</span><ins>+                 * By default it will return the avatar for the primary blog admin.
</ins><span class="cx">                  */
</span><del>-                return apply_filters( 'bp_get_blog_avatar_' . $blogs_template->blog->blog_id, bp_core_fetch_avatar( array( 'item_id' => $blogs_template->blog->blog_id, 'object' => 'blog', 'type' => $type, 'avatar_dir' => 'blog-avatars', 'alt' => $alt, 'width' => $width, 'height' => $height, 'class' => $class, 'email' => get_blog_option( $blogs_template->blog->blog_id, 'admin_email' ) ) ) );
</del><ins>+                return apply_filters( 'bp_get_blog_avatar_' . $blogs_template->blog->blog_id, bp_core_fetch_avatar( array( 'item_id' => $blogs_template->blog->admin_user_id, 'type' => $type, 'alt' => $alt, 'width' => $width, 'height' => $height, 'class' => $class, 'email' => $blogs_template->blog->admin_user_email ) ) );
</ins><span class="cx">         }
</span><span class="cx">                 /* DEPRECATED */
</span><span class="cx">                 function bp_blog_avatar_thumb() { echo bp_get_blog_avatar('type=thumb'); }
</span><span class="lines">@@ -240,7 +225,17 @@
</span><span class="cx">         function bp_get_blog_permalink() {
</span><span class="cx">                 global $blogs_template;
</span><span class="cx">
</span><del>-                return apply_filters( 'bp_get_blog_permalink', get_blog_option( $blogs_template->blog->blog_id, 'siteurl' ) );
</del><ins>+                if ( empty( $blogs_template->blog->domain ) )
+                        $permalink = $bp->root_domain . $blogs_template->blog->path;
+                else {
+                        $protocol = 'http://';
+                        if ( is_ssl() )
+                                $protocol = 'https://';
+
+                        $permalink = $protocol . $blogs_template->blog->domain . $blogs_template->blog->path;
+                }
+
+                return apply_filters( 'bp_get_blog_permalink', $permalink );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_blog_name() {
</span><span class="lines">@@ -249,7 +244,7 @@
</span><span class="cx">         function bp_get_blog_name() {
</span><span class="cx">                 global $blogs_template;
</span><span class="cx">
</span><del>-                return apply_filters( 'bp_get_blog_name', get_blog_option( $blogs_template->blog->blog_id, 'blogname' ) );
</del><ins>+                return apply_filters( 'bp_get_blog_name', $blogs_template->blog->name );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_blog_description() {
</span><span class="lines">@@ -258,7 +253,7 @@
</span><span class="cx">         function bp_get_blog_description() {
</span><span class="cx">                 global $blogs_template;
</span><span class="cx">
</span><del>-                return apply_filters( 'bp_get_blog_description', get_blog_option( $blogs_template->blog->blog_id, 'blogdescription' ) );
</del><ins>+                return apply_filters( 'bp_get_blog_description', $blogs_template->blog->description );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_blog_last_active() {
</span><span class="lines">@@ -267,7 +262,7 @@
</span><span class="cx">         function bp_get_blog_last_active() {
</span><span class="cx">                 global $blogs_template;
</span><span class="cx">
</span><del>-                return apply_filters( 'bp_blog_last_active', bp_core_get_last_activity( bp_blogs_get_blogmeta( $blogs_template->blog->blog_id, 'last_activity' ), __( 'active %s ago', 'buddypress' ) ) );
</del><ins>+                return apply_filters( 'bp_blog_last_active', bp_core_get_last_activity( $blogs_template->blog->last_activity, __( 'active %s ago', 'buddypress' ) ) );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_blog_latest_post() {
</span><span class="lines">@@ -276,9 +271,10 @@
</span><span class="cx">         function bp_get_blog_latest_post() {
</span><span class="cx">                 global $blogs_template;
</span><span class="cx">
</span><del>-                if ( $post = bp_blogs_get_latest_posts( $blogs_template->blog->blog_id, 1 ) ) {
-                        return apply_filters( 'bp_get_blog_latest_post', sprintf( __( 'Latest Post: %s', 'buddypress' ), '<a href="' . bp_post_get_permalink( $post[0], $blogs_template->blog->blog_id ) . '">' . apply_filters( 'the_title', $post[0]->post_title ) . '</a>' ) );
-                }
</del><ins>+                if ( null == $blogs_template->blog->latest_post )
+                        return false;
+
+                return apply_filters( 'bp_get_blog_latest_post', sprintf( __( 'Latest Post: %s', 'buddypress' ), '<a href="' . $blogs_template->blog->latest_post->guid . '">' . apply_filters( 'the_title', $blogs_template->blog->latest_post->post_title ) . '</a>' ) );
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx"> function bp_blog_hidden_fields() {
</span></span></pre>
</div>
</div>
</body>
</html>