Skip to:
Content

BuddyPress.org

Changeset 2083


Ignore:
Timestamp:
11/06/2009 10:34:10 AM (15 years ago)
Author:
apeatling
Message:

Removing the use of get_user_blogs() from the admin bar as it uses switch_to_blog() and is very slow. Using bp_blogs_get_blogs_for_user() instead. This takes 10-20 queries off each page load.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs/bp-blogs-classes.php

    r2055 r2083  
    55    var $user_id;
    66    var $blog_id;
    7    
     7
    88    function bp_blogs_blog( $id = null ) {
    99        global $bp, $wpdb;
    10        
     10
    1111        if ( !$user_id )
    1212            $user_id = $bp->displayed_user->id;
     
    1717        }
    1818    }
    19    
     19
    2020    function populate() {
    2121        global $wpdb, $bp;
    22        
     22
    2323        $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id ) );
    2424
     
    2626        $this->blog_id = $blog->blog_id;
    2727    }
    28    
     28
    2929    function save() {
    3030        global $wpdb, $bp;
    31        
     31
    3232        $this->user_id = apply_filters( 'bp_blogs_blog_user_id_before_save', $this->user_id, $this->id );
    3333        $this->blog_id = apply_filters( 'bp_blogs_blog_id_before_save', $this->blog_id, $this->id );
    34        
     34
    3535        do_action( 'bp_blogs_blog_before_save', $this );
    36        
     36
    3737        // Don't try and save if there is no user ID or blog ID set.
    3838        if ( !$this->user_id || !$this->blog_id )
    3939            return false;
    40            
     40
    4141        // Don't save if this blog has already been recorded for the user.
    4242        if ( !$this->id && $this->exists() )
    4343            return false;
    44        
     44
    4545        if ( $this->id ) {
    4646            // Update
     
    5050            $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id );
    5151        }
    52        
     52
    5353        if ( !$wpdb->query($sql) )
    5454            return false;
    55            
     55
    5656        do_action( 'bp_blogs_blog_after_save', $this );
    57        
     57
    5858        if ( $this->id )
    5959            return $this->id;
     
    6161            return $wpdb->insert_id;
    6262    }
    63    
     63
    6464    function exists() {
    6565        global $bp, $wpdb;
    66        
     66
    6767        return $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $this->user_id, $this->blog_id ) );
    6868    }
    69    
     69
    7070    /* Static Functions */
    71    
     71
    7272    function delete_blog_for_all( $blog_id ) {
    7373        global $wpdb, $bp;
    74        
    75         if ( !$bp->blogs )
    76             bp_blogs_setup_globals();
    77        
     74
     75        if ( !$bp->blogs )
     76            bp_blogs_setup_globals();
     77
    7878        bp_blogs_delete_blogmeta( $blog_id );
    7979
    8080        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) );
    8181    }
    82    
     82
    8383    function delete_blog_for_user( $blog_id, $user_id = null ) {
    8484        global $wpdb, $bp;
    85        
    86         if ( !$bp->blogs )
    87             bp_blogs_setup_globals();
    88        
     85
     86        if ( !$bp->blogs )
     87            bp_blogs_setup_globals();
     88
    8989        if ( !$user_id )
    9090            $user_id = $bp->loggedin_user->id;
     
    9292        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) );
    9393    }
    94    
     94
    9595    function delete_blogs_for_user( $user_id = null ) {
    9696        global $wpdb, $bp;
    97        
     97
    9898        if ( !$bp->blogs )
    9999            bp_blogs_setup_globals();
     
    104104        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) );
    105105    }
    106    
     106
    107107    function get_blogs_for_user( $user_id = null ) {
    108108        global $bp, $wpdb;
    109        
    110         if ( !$bp->blogs )
    111             bp_blogs_setup_globals();
    112        
     109
     110        if ( !$bp->blogs )
     111            bp_blogs_setup_globals();
     112
    113113        if ( !$user_id )
    114114            $user_id = $bp->displayed_user->id;
    115        
     115
    116116        // Show logged in users their hidden blogs.
    117117        if ( !bp_is_home() )
    118             $blog_ids = $wpdb->get_col( $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.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) );
     118            $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.id, bm1.meta_value as name, bm2.meta_value as description, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1, {$bp->blogs->table_name_blogmeta} bm2 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND b.blog_id = bm2.blog_id AND bm1.meta_key = 'name' AND bm2.meta_key = 'description' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) );
    119119        else
    120             $blog_ids = $wpdb->get_col( $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.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) );
    121            
     120            $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.id, bm1.meta_value as name, bm2.meta_value as description, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1, {$bp->blogs->table_name_blogmeta} bm2 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND b.blog_id = bm2.blog_id AND bm1.meta_key = 'name' AND bm2.meta_key = 'description' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) );
     121
    122122        $total_blog_count = BP_Blogs_Blog::total_blog_count( $user_id );
    123        
    124         for ( $i = 0; $i < count($blog_ids); $i++ ) {
    125             $blogs[] = array(
    126                 'id' => $blog_ids[$i],
    127                 'siteurl' => get_blog_option($blog_ids[$i], 'siteurl'),
    128                 'title' => get_blog_option($blog_ids[$i], 'blogname'),
    129                 'description' => get_blog_option($blog_ids[$i], 'blogdescription')
    130             );
    131         }
    132 
    133         return array( 'blogs' => $blogs, 'count' => $total_blog_count );
    134     }
    135    
     123
     124        foreach ( (array)$blogs as $blog ) {
     125            $user_blogs[$blog->id] = new stdClass;
     126            $user_blogs[$blog->id]->id = $blog->id;
     127            $user_blogs[$blog->id]->siteurl = ( is_ssl() ) ? 'https://' . $blog->domain . $blog->path : 'http://' . $blog->domain . $blog->path;
     128            $user_blogs[$blog->id]->name = $blog->name;
     129            $user_blogs[$blog->id]->description = $blog->description;
     130        }
     131
     132        return array( 'blogs' => $user_blogs, 'count' => $total_blog_count );
     133    }
     134
    136135    function is_recorded( $blog_id ) {
    137136        global $bp, $wpdb;
    138        
    139         if ( !$bp->blogs )
    140             bp_blogs_setup_globals();
    141        
     137
     138        if ( !$bp->blogs )
     139            bp_blogs_setup_globals();
     140
    142141        return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) );
    143142    }
    144    
     143
    145144    function total_blog_count( $user_id = null ) {
    146145        global $bp, $wpdb;
    147        
    148         if ( !$bp->blogs )
    149             bp_blogs_setup_globals();
    150        
     146
     147        if ( !$bp->blogs )
     148            bp_blogs_setup_globals();
     149
    151150        if ( !$user_id )
    152151            $user_id = $bp->displayed_user->id;
     
    156155            return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id) );
    157156        else
    158             return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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) );         
    159     }
    160    
     157            return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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) );
     158    }
     159
    161160    function get_all( $limit = null, $page = null ) {
    162161        global $bp, $wpdb;
    163        
    164         if ( !$bp->blogs )
    165             bp_blogs_setup_globals();
    166        
     162
     163        if ( !$bp->blogs )
     164            bp_blogs_setup_globals();
     165
    167166        if ( $limit && $page ) {
    168167            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    169168            $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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 wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC" ) );
    170169        }
    171            
     170
    172171        $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 wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC {$pag_sql}" ) );
    173172
    174173        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
    175174    }
    176    
     175
    177176    function get_by_letter( $letter, $limit = null, $page = null ) {
    178177        global $bp, $wpdb;
    179        
    180         if ( !$bp->blogs )
    181             bp_blogs_setup_globals();
    182        
     178
     179        if ( !$bp->blogs )
     180            bp_blogs_setup_globals();
     181
    183182        $letter = like_escape( $wpdb->escape( $letter ) );
    184                
     183
    185184        if ( $limit && $page ) {
    186185            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    187186            $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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%%' AND wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" ) );
    188187        }
    189    
     188
    190189        $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%%' AND wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" ) );
    191        
    192         return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );       
    193     }
    194    
     190
     191        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
     192    }
     193
    195194    function search_blogs( $filter, $limit = null, $page = null ) {
    196195        global $wpdb, $bp;
    197        
    198         if ( !$bp->blogs )
    199             bp_blogs_setup_globals();
    200        
     196
     197        if ( !$bp->blogs )
     198            bp_blogs_setup_globals();
     199
    201200        $filter = like_escape( $wpdb->escape( $filter ) );
    202        
     201
    203202        if ( $limit && $page ) {
    204203            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
     
    207206
    208207        $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' OR bm.meta_key = 'description' ) AND bm.meta_value LIKE '%%$filter%%' ) AND wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC{$pag_sql}" ) );
    209        
    210         return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );       
    211     }
    212    
     208
     209        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
     210    }
     211
    213212    function get_random( $limit = null, $page = null ) {
    214213        global $bp, $wpdb;
    215        
    216         if ( !$bp->blogs )
    217             bp_blogs_setup_globals();
    218        
     214
     215        if ( !$bp->blogs )
     216            bp_blogs_setup_globals();
     217
    219218        if ( $limit && $page ) {
    220219            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    221220            $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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 ORDER BY rand()" ) );
    222221        }
    223        
    224         $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  ORDER BY rand() {$pag_sql}" ) );         
    225        
     222
     223        $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  ORDER BY rand() {$pag_sql}" ) );
     224
    226225        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
    227226    }
    228    
     227
    229228    function get_active( $limit = null, $page = null ) {
    230229        global $bp, $wpdb;
    231        
    232         if ( !$bp->blogs )
    233             bp_blogs_setup_globals();
    234        
     230
     231        if ( !$bp->blogs )
     232            bp_blogs_setup_globals();
     233
    235234        if ( $limit && $page ) {
    236235            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    237236            $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(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 wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC" ) );
    238237        }
    239            
     238
    240239        $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 wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC {$pag_sql}" ) );
    241240
    242241        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
    243242    }
    244    
     243
    245244    function get_newest( $limit = null, $page = null ) {
    246245        global $bp, $wpdb;
    247        
    248         if ( !$bp->blogs )
    249             bp_blogs_setup_globals();
    250        
     246
     247        if ( !$bp->blogs )
     248            bp_blogs_setup_globals();
     249
    251250        if ( $limit && $page ) {
    252251            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    253252            $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(wb.blog_id) FROM {$wpdb->base_prefix}blogs wb WHERE wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 ORDER BY wb.registered DESC" ) );
    254253        }
    255            
     254
    256255        $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT wb.blog_id FROM {$wpdb->base_prefix}blogs wb WHERE wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 ORDER BY wb.registered DESC {$pag_sql}" ) );
    257256
    258257        return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
    259258    }
    260    
     259
    261260    function is_hidden( $blog_id ) {
    262261        global $wpdb;
     
    264263        if ( !$bp->blogs )
    265264            bp_blogs_setup_globals();
    266        
     265
    267266        if ( !(int)$wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT public FROM {$wpdb->base_prefix}blogs WHERE blog_id = %d", $blog_id ) ) )
    268267            return true;
    269        
     268
    270269        return false;
    271270    }
     
    278277    var $post_id;
    279278    var $date_created;
    280    
     279
    281280    function bp_blogs_post( $id = null, $blog_id = null, $post_id = null ) {
    282281        global $bp, $wpdb;
     
    297296        else
    298297            $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d AND post_id = %d", $this->blog_id, $this->post_id ) );
    299        
    300         $this->id = $post->id; 
     298
     299        $this->id = $post->id;
    301300        $this->user_id = $post->user_id;
    302301        $this->blog_id = $post->blog_id;
     
    304303        $this->date_created = $post->date_created;
    305304    }
    306    
     305
    307306    function save() {
    308307        global $wpdb, $bp;
    309        
     308
    310309        $this->post_id = apply_filters( 'bp_blogs_post_id_before_save', $this->post_id, $this->id );
    311310        $this->blog_id = apply_filters( 'bp_blogs_post_blog_id_before_save', $this->blog_id, $this->id );
     
    322321            $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name_blog_posts} ( post_id, blog_id, user_id, date_created ) VALUES ( %d, %d, %d, FROM_UNIXTIME(%d) )", $this->post_id, $this->blog_id, $this->user_id, $this->date_created );
    323322        }
    324        
     323
    325324        if ( !$wpdb->query($sql) )
    326325            return false;
    327326
    328327        do_action( 'bp_blogs_post_after_save', $this );
    329        
     328
    330329        if ( $this->id )
    331330            return $this->id;
    332331        else
    333             return $wpdb->insert_id;   
    334     }
    335    
     332            return $wpdb->insert_id;
     333    }
     334
    336335    /* Static Functions */
    337    
     336
    338337    function delete( $post_id, $blog_id ) {
    339338        global $wpdb, $bp, $current_user;
    340        
     339
    341340        if ( !$bp->blogs )
    342341            bp_blogs_setup_globals();
     
    344343        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d AND post_id = %d", $blog_id, $post_id ) );
    345344    }
    346    
     345
    347346    function delete_oldest( $user_id = null ) {
    348347        global $wpdb, $bp;
    349        
    350         if ( !$bp->blogs )
    351             bp_blogs_setup_globals();
    352        
     348
     349        if ( !$bp->blogs )
     350            bp_blogs_setup_globals();
     351
    353352        if ( !$user_id )
    354353            $user_id = $current_user->ID;
    355            
    356         return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );     
    357     }
    358    
     354
     355        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );
     356    }
     357
    359358    function delete_posts_for_user( $user_id = null ) {
    360359        global $wpdb, $bp;
    361        
     360
    362361        if ( !$bp->blogs )
    363362            bp_blogs_setup_globals();
     
    368367        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d", $user_id ) );
    369368    }
    370    
     369
    371370    function delete_posts_for_blog( $blog_id ) {
    372371        global $wpdb, $bp;
    373        
    374         if ( !$bp->blogs )
    375             bp_blogs_setup_globals();
    376        
     372
     373        if ( !$bp->blogs )
     374            bp_blogs_setup_globals();
     375
    377376        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d", $blog_id ) );
    378377    }
    379    
     378
    380379    function get_latest_posts( $blog_id = null, $limit = 5 ) {
    381380        global $wpdb, $bp;
    382        
    383         if ( !$bp->blogs )
    384             bp_blogs_setup_globals();
    385        
     381
     382        if ( !$bp->blogs )
     383            bp_blogs_setup_globals();
     384
    386385        if ( $blog_id )
    387386            $blog_sql = $wpdb->prepare( " AND p.blog_id = %d", $blog_id );
    388        
     387
    389388        $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT 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.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 $blog_sql ORDER BY p.date_created DESC LIMIT $limit" ) );
    390389
     
    392391            $posts[$i] = BP_Blogs_Post::fetch_post_content($post_ids[$i]);
    393392        }
    394        
     393
    395394        return $posts;
    396395    }
    397    
     396
    398397    function get_posts_for_user( $user_id = null ) {
    399398        global $bp, $wpdb;
    400        
    401         if ( !$bp->blogs )
    402             bp_blogs_setup_globals();
    403        
     399
     400        if ( !$bp->blogs )
     401            bp_blogs_setup_globals();
     402
    404403        if ( !$user_id )
    405404            $user_id = $bp->displayed_user->id;
    406        
     405
    407406        // Show a logged in user their posts on private blogs, but not anyone else.
    408407        if ( !bp_is_home() ) {
     
    411410        } else {
    412411            $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.user_id = %d ORDER BY p.date_created DESC", $user_id) );
    413             $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(p.post_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.user_id = %d", $user_id) );           
     412            $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(p.post_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.user_id = %d", $user_id) );
    414413        }
    415414
     
    420419        return array( 'posts' => $posts, 'count' => $total_post_count );
    421420    }
    422    
     421
    423422    function fetch_post_content( $post_object ) {
    424423        // TODO: switch_to_blog() calls are expensive and this needs to be changed.
     
    430429        return $post;
    431430    }
    432    
     431
    433432    function get_total_recorded_for_user( $user_id = null ) {
    434433        global $bp, $wpdb;
    435        
    436         if ( !$bp->blogs )
    437             bp_blogs_setup_globals();
    438        
     434
     435        if ( !$bp->blogs )
     436            bp_blogs_setup_globals();
     437
    439438        if ( !$user_id )
    440439            $user_id = $current_user->ID;
     
    442441        return $wpdb->get_var( $wpdb->prepare( "SELECT count(post_id) FROM {$bp->blogs->table_name_blog_posts} WHERE user_id = %d", $user_id ) );
    443442    }
    444    
     443
    445444    function is_recorded( $post_id, $blog_id, $user_id = null ) {
    446445        global $bp, $wpdb, $current_user;
    447        
    448         if ( !$bp->blogs )
    449             bp_blogs_setup_globals();
    450        
     446
     447        if ( !$bp->blogs )
     448            bp_blogs_setup_globals();
     449
    451450        if ( !$user_id )
    452451            $user_id = $current_user->ID;
    453        
     452
    454453        return $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$bp->blogs->table_name_blog_posts} WHERE post_id = %d AND blog_id = %d AND user_id = %d", $post_id, $blog_id, $user_id ) );
    455454    }
    456    
     455
    457456    function total_post_count( $blog_id ) {
    458457        global $bp, $wpdb;
    459        
    460         if ( !$bp->blogs )
    461             bp_blogs_setup_globals();
    462        
     458
     459        if ( !$bp->blogs )
     460            bp_blogs_setup_globals();
     461
    463462        if ( !$blog_id )
    464463            return false;
    465            
    466         return $wpdb->get_var( $wpdb->prepare( "SELECT count(post_id) FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d", $blog_id ) );       
    467     }
    468    
     464
     465        return $wpdb->get_var( $wpdb->prepare( "SELECT count(post_id) FROM {$bp->blogs->table_name_blog_posts} WHERE blog_id = %d", $blog_id ) );
     466    }
     467
    469468    function get_all() {
    470469        global $bp, $wpdb;
    471        
    472         if ( !$bp->blogs )
    473             bp_blogs_setup_globals();
    474        
     470
     471        if ( !$bp->blogs )
     472            bp_blogs_setup_globals();
     473
    475474        return $wpdb->get_col( $wpdb->prepare( "SELECT post_id, blog_id FROM " . $bp->blogs->table_name_blog_posts ) );
    476475    }
    477    
     476
    478477}
    479478
     
    485484    var $comment_post_id;
    486485    var $date_created;
    487    
     486
    488487    function bp_blogs_comment( $id = false, $blog_id = false, $comment_id = false ) {
    489488        global $bp, $wpdb;
     
    491490        if ( !$user_id )
    492491            $user_id = $bp->displayed_user->id;
    493            
     492
    494493        if ( $id || ( !$id && $blog_id && $comment_id ) ) {
    495494            $this->id = $id;
     
    502501    function populate() {
    503502        global $wpdb, $bp;
    504        
     503
    505504        if ( $this->id )
    506505            $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE id = %d", $this->id ) );
    507506        else
    508507            $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE blog_id = %d AND comment_id = %d", (int)$this->blog_id, (int)$this->comment_id ) );
    509        
     508
    510509        $this->comment_id = $comment->comment_id;
    511510        $this->user_id = $comment->user_id;
     
    514513        $this->date_created = $comment->date_created;
    515514    }
    516    
     515
    517516    function save() {
    518517        global $wpdb, $bp;
     
    525524
    526525        do_action( 'bp_blogs_comment_before_save', $this );
    527        
     526
    528527        if ( $this->id ) {
    529528            // Update
     
    536535        if ( !$wpdb->query($sql) )
    537536            return false;
    538            
     537
    539538        do_action( 'bp_blogs_comment_after_save', $this );
    540        
     539
    541540        if ( $this->id )
    542541            return $this->id;
    543542        else
    544             return $wpdb->insert_id;   
     543            return $wpdb->insert_id;
    545544    }
    546545
    547546    /* Static Functions */
    548    
     547
    549548    function delete( $comment_id, $blog_id ) {
    550549        global $wpdb, $bp, $current_user;
    551        
    552         if ( !$bp->blogs )
    553             bp_blogs_setup_globals();
    554            
     550
     551        if ( !$bp->blogs )
     552            bp_blogs_setup_globals();
     553
    555554        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE comment_id = %d AND blog_id = %d", $comment_id, $blog_id ) );
    556555    }
    557    
     556
    558557    function delete_oldest( $user_id = null ) {
    559558        global $wpdb, $bp, $current_user;
    560        
    561         if ( !$bp->blogs )
    562             bp_blogs_setup_globals();
    563        
     559
     560        if ( !$bp->blogs )
     561            bp_blogs_setup_globals();
     562
    564563        if ( !$user_id )
    565564            $user_id = $current_user->ID;
    566            
    567         return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );       
    568     }
    569    
     565
     566        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) );
     567    }
     568
    570569    function delete_comments_for_user( $user_id = null ) {
    571570        global $wpdb, $bp;
    572        
     571
    573572        if ( !$bp->blogs )
    574573            bp_blogs_setup_globals();
     
    579578        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d", $user_id ) );
    580579    }
    581    
     580
    582581    function delete_comments_for_blog( $blog_id ) {
    583582        global $wpdb, $bp;
    584        
    585         if ( !$bp->blogs )
    586             bp_blogs_setup_globals();
    587        
     583
     584        if ( !$bp->blogs )
     585            bp_blogs_setup_globals();
     586
    588587        return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name_blog_comments} WHERE blog_id = %d", $blog_id ) );
    589588    }
    590    
     589
    591590    function get_comments_for_user( $user_id = null ) {
    592591        global $bp, $wpdb;
    593        
     592
    594593        if ( !$bp->blogs )
    595594            bp_blogs_setup_globals();
     
    597596        if ( !$user_id )
    598597            $user_id = $bp->displayed_user->id;
    599            
     598
    600599        // Show the logged in user their comments on hidden blogs, but not to anyone else.
    601600        if ( !bp_is_home() ) {
     
    604603        } else {
    605604            $comment_ids = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_id, c.blog_id FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d ORDER BY c.date_created ASC", $user_id) );
    606             $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(c.comment_id) FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) );         
    607         }
    608        
     605            $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(c.comment_id) FROM {$bp->blogs->table_name_blog_comments} c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) );
     606        }
     607
    609608        for ( $i = 0; $i < count($comment_ids); $i++ ) {
    610609            $comments[$i] = BP_Blogs_Comment::fetch_comment_content($comment_ids[$i]);
     
    613612        return array( 'comments' => $comments, 'count' => $total_comment_count );
    614613    }
    615    
    616     function fetch_comment_content( $comment_object ) { 
     614
     615    function fetch_comment_content( $comment_object ) {
    617616        switch_to_blog($comment_object->blog_id);
    618617        $comment = get_comment($comment_object->comment_id);
     
    620619        $comment->post = &get_post( $comment->comment_post_ID );
    621620        restore_current_blog();
    622        
     621
    623622        return $comment;
    624623    }
    625    
     624
    626625    function get_total_recorded_for_user( $user_id = null ) {
    627626        global $bp, $wpdb, $current_user;
    628        
    629         if ( !$bp->blogs )
    630             bp_blogs_setup_globals();
    631        
     627
     628        if ( !$bp->blogs )
     629            bp_blogs_setup_globals();
     630
    632631        if ( !$user_id )
    633632            $user_id = $current_user->ID;
     
    635634        return $wpdb->get_var( $wpdb->prepare( "SELECT count(comment_id) FROM {$bp->blogs->table_name_blog_comments} WHERE user_id = %d", $user_id ) );
    636635    }
    637    
     636
    638637    function total_comment_count( $blog_id, $post_id ) {
    639638        global $bp, $wpdb;
    640        
    641         if ( !$bp->blogs )
    642             bp_blogs_setup_globals();
    643            
     639
     640        if ( !$bp->blogs )
     641            bp_blogs_setup_globals();
     642
    644643        if ( $post_id )
    645644            $post_sql = $wpdb->prepare( " AND comment_post_id = %d", $post_id );
     
    647646        return $wpdb->get_var( $wpdb->prepare( "SELECT count(comment_id) WHERE blog_id = %d{$post_sql}", $blog_id ) );
    648647    }
    649    
    650    
     648
     649
    651650    function is_recorded( $comment_id, $comment_post_id, $blog_id, $user_id = null ) {
    652651        global $bp, $wpdb, $current_user;
    653        
    654         if ( !$bp->blogs )
    655             bp_blogs_setup_globals();
    656            
     652
     653        if ( !$bp->blogs )
     654            bp_blogs_setup_globals();
     655
    657656        if ( !$user_id )
    658657            $user_id = $current_user->ID;
    659        
     658
    660659        return $wpdb->get_var( $wpdb->prepare( "SELECT comment_id FROM {$bp->blogs->table_name_blog_comments} WHERE comment_id = %d AND blog_id = %d AND comment_post_id = %d AND user_id = %d", $comment_id, $blog_id, $comment_post_id, $user_id ) );
    661660    }
    662    
     661
    663662}
    664663
  • trunk/bp-core/bp-core-adminbar.php

    r2077 r2083  
    132132
    133133            if ( !$blogs = wp_cache_get( 'bp_blogs_of_user_' . $bp->loggedin_user->id, 'bp' ) ) {
    134                 $blogs = get_blogs_of_user( $bp->loggedin_user->id );
     134                $blogs = bp_blogs_get_blogs_for_user( $bp->loggedin_user->id );
    135135                wp_cache_set( 'bp_blogs_of_user_' . $bp->loggedin_user->id, $blogs, 'bp' );
    136136            }
     
    143143
    144144            echo '<ul>';
    145             if ( is_array( $blogs )) {
     145
     146            if ( is_array( $blogs['blogs'] ) && (int)$blogs['count'] ) {
    146147
    147148                $counter = 0;
    148                 foreach( $blogs as $blog ) {
    149                     $role = get_blog_role_for_user( $bp->loggedin_user->id, $blog->userblog_id );
     149                foreach ( $blogs['blogs'] as $blog ) {
     150                    $role = get_blog_role_for_user( $bp->loggedin_user->id, $blog->id );
    150151
    151152                    $alt = ( 0 == $counter % 2 ) ? ' class="alt"' : '';
    152153                    echo '<li' . $alt . '>';
    153                     echo '<a href="' . $blog->siteurl . '">' . $blog->blogname . ' (' . $role . ')</a>';
     154                    echo '<a href="' . $blog->siteurl . '">' . $blog->name . ' (' . $role . ')</a>';
    154155                    if ( !( 'Subscriber' == $role ) ) { // then they have something to display on the flyout menu
    155156                        echo '<ul>';
    156                         echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/">' . __('Dashboard', 'buddypress') . '</a></li>';
    157                         echo '<li><a href="' . $blog->siteurl  . '/wp-admin/post-new.php">' . __('New Post', 'buddypress') . '</a></li>';
    158                         echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/edit.php">' . __('Manage Posts', 'buddypress') . '</a></li>';
    159                         echo '<li><a href="' . $blog->siteurl  . '/wp-admin/edit-comments.php">' . __('Manage Comments', 'buddypress') . '</a></li>';
     157                        echo '<li class="alt"><a href="' . $blog->siteurl  . 'wp-admin/">' . __('Dashboard', 'buddypress') . '</a></li>';
     158                        echo '<li><a href="' . $blog->siteurl  . 'wp-admin/post-new.php">' . __('New Post', 'buddypress') . '</a></li>';
     159                        echo '<li class="alt"><a href="' . $blog->siteurl  . 'wp-admin/edit.php">' . __('Manage Posts', 'buddypress') . '</a></li>';
     160                        echo '<li><a href="' . $blog->siteurl  . 'wp-admin/edit-comments.php">' . __('Manage Comments', 'buddypress') . '</a></li>';
    160161                        if ( 'Admin' == $role ) {
    161                             echo '<li class="alt"><a href="' . $blog->siteurl  . '/wp-admin/themes.php">' . __('Switch Theme', 'buddypress') . '</a></li>';
     162                            echo '<li class="alt"><a href="' . $blog->siteurl  . 'wp-admin/themes.php">' . __('Switch Theme', 'buddypress') . '</a></li>';
    162163                        }
    163164                        echo '</ul>';
Note: See TracChangeset for help on using the changeset viewer.