Skip to:
Content

BuddyPress.org

Changeset 2533


Ignore:
Timestamp:
02/02/2010 12:40:49 PM (10 years ago)
Author:
apeatling
Message:

Removing database hits inside BP loops to improve overall performance.

Location:
trunk
Files:
8 edited

Legend:

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

    r2457 r2533  
    340340        }
    341341
     342        /* Fetch whether or not the user is a friend */
     343        if ( function_exists( 'friends_install' ) ) {
     344            $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->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 )", $bp->loggedin_user->id, $bp->loggedin_user->id ) );
     345            for ( $i = 0; $i < count( $paged_users ); $i++ ) {
     346                foreach ( $friend_status as $status ) {
     347                    if ( $status->initiator_user_id == $paged_users[$i]->id || $status->friend_user_id == $paged_users[$i]->id )
     348                        $paged_users[$i]->is_friend = $status->is_confirmed;
     349                }
     350            }
     351        }
     352
     353        if ( 'active' != $type ) {
     354            $user_activity = $wpdb->get_results( "SELECT user_id as id, meta_value as last_activity FROM " . CUSTOM_USER_META_TABLE . " WHERE meta_key = 'last_activity' AND user_id IN ( {$user_ids} )" );
     355            for ( $i = 0; $i < count( $paged_users ); $i++ ) {
     356                foreach ( $user_activity as $activity ) {
     357                    if ( $activity->id == $paged_users[$i]->id )
     358                        $paged_users[$i]->last_activity = $activity->last_activity;
     359                }
     360            }
     361        }
     362
    342363        /* Fetch the user's last_activity */
    343364        if ( 'active' != $type ) {
  • trunk/bp-core/bp-core-templatetags.php

    r2516 r2533  
    342342
    343343    if ( function_exists( 'bp_add_friend_button' ) ) {
    344         echo bp_add_friend_button( $members_template->member->id );
     344        if ( null === $members_template->member->is_friend )
     345            $friend_status = 'not_friends';
     346        else
     347            $friend_status = ( 0 == $members_template->member->is_friend ) ? 'pending' : 'is_friend';
     348
     349        echo bp_add_friend_button( $members_template->member->id, $friend_status );
    345350    }
    346351}
  • trunk/bp-forums.php

    r2431 r2533  
    334334}
    335335
     336function bp_forums_get_topic_extras( $topics ) {
     337    global $bp, $wpdb, $bbdb;
     338
     339    if ( empty( $topics ) )
     340        return $topics;
     341
     342    /* Get the topic ids */
     343    foreach ( $topics as $topic ) $topic_ids[] = $topic->topic_id;
     344    $topic_ids = $wpdb->escape( join( ',', (array)$topic_ids ) );
     345
     346    /* Fetch the topic's last poster details */
     347    $poster_details = $wpdb->get_results( $wpdb->prepare( "SELECT t.topic_id, t.topic_last_poster, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u, {$bbdb->topics} t WHERE u.ID = t.topic_last_poster AND t.topic_id IN ( {$topic_ids} )" ) );
     348    for ( $i = 0; $i < count( $topics ); $i++ ) {
     349        foreach ( $poster_details as $poster ) {
     350            if ( $poster->topic_id == $topics[$i]->topic_id ) {
     351                $topics[$i]->topic_last_poster_email = $poster->user_email;
     352                $topics[$i]->topic_last_poster_nicename = $poster->user_nicename;
     353                $topics[$i]->topic_last_poster_login = $poster->user_login;
     354                $topics[$i]->topic_last_poster_displayname = $poster->display_name;
     355            }
     356        }
     357    }
     358
     359    /* Fetch fullname for the topic's last poster */
     360    if ( function_exists( 'xprofile_install' ) ) {
     361        $poster_names = $wpdb->get_results( $wpdb->prepare( "SELECT t.topic_id, pd.value FROM {$bp->profile->table_name_data} pd, {$bbdb->topics} t WHERE pd.user_id = t.topic_last_poster AND pd.field_id = 1 AND t.topic_id IN ( {$topic_ids} )" ) );
     362        for ( $i = 0; $i < count( $topics ); $i++ ) {
     363            foreach ( $poster_names as $name ) {
     364                if ( $name->topic_id == $topics[$i]->topic_id )
     365                    $topics[$i]->topic_last_poster_displayname = $name->value;
     366            }
     367        }
     368    }
     369
     370    return $topics;
     371}
     372
    336373/* Post Functions */
    337374
  • trunk/bp-forums/bp-forums-templatetags.php

    r2488 r2533  
    9393        }
    9494
     95        /* Fetch extra information for topics, so we don't have to query inside the loop */
     96        $this->topics = bp_forums_get_topic_extras( &$this->topics );
     97
    9598        $this->pag_links = paginate_links( array(
    9699            'base' => add_query_arg( array( 'p' => '%#%', 'n' => $this->pag_num ) ),
     
    348351        global $forum_template;
    349352
    350         if ( !$name = bp_core_get_userlink( $forum_template->topic->topic_last_poster ) )
     353        if ( !$domain = bp_core_get_user_domain( $forum_template->topic->topic_last_poster, $forum_template->topic->topic_last_poster_nicename, $forum_template->topic->topic_last_poster_login ) )
    351354            return __( 'Deleted User', 'buddypress' );
    352355
    353         return apply_filters( 'bp_get_the_topic_last_poster_name', $name );
     356        return apply_filters( 'bp_get_the_topic_last_poster_name', '<a href="' . $domain . '">' . $forum_template->topic->topic_last_poster_displayname . '</a>' );
    354357    }
    355358
     
    387390        extract( $r, EXTR_SKIP );
    388391
    389         return apply_filters( 'bp_get_the_topic_last_poster_avatar', bp_core_fetch_avatar( array( 'item_id' => $forum_template->topic->topic_last_poster, 'type' => $type, 'width' => $width, 'height' => $height ) ) );
     392        return apply_filters( 'bp_get_the_topic_last_poster_avatar', bp_core_fetch_avatar( array( 'email' => $forum_template->topic->topic_last_poster_email, 'item_id' => $forum_template->topic->topic_last_poster, 'type' => $type, 'width' => $width, 'height' => $height ) ) );
    390393    }
    391394
  • trunk/bp-friends/bp-friends-templatetags.php

    r2445 r2533  
    115115}
    116116
    117 function bp_add_friend_button( $potential_friend_id = false ) {
    118     echo bp_get_add_friend_button( $potential_friend_id );
    119 }
    120     function bp_get_add_friend_button( $potential_friend_id = false ) {
     117function bp_add_friend_button( $potential_friend_id = false, $friend_status = false ) {
     118    echo bp_get_add_friend_button( $potential_friend_id, $friend_status );
     119}
     120    function bp_get_add_friend_button( $potential_friend_id = false, $friend_status = false ) {
    121121        global $bp, $friends_template;
    122122
     
    132132            return false;
    133133
    134         $friend_status = friends_check_friendship_status( $bp->loggedin_user->id, $potential_friend_id );
     134        if ( empty( $friend_status ) )
     135            $friend_status = friends_check_friendship_status( $bp->loggedin_user->id, $potential_friend_id );
    135136
    136137        $button = '<div class="generic-button friendship-button ' . $friend_status . '" id="friendship-button-' . $potential_friend_id . '">';
  • trunk/bp-groups/bp-groups-classes.php

    r2431 r2533  
    4848                $this->user_dataset = $this->get_user_dataset();
    4949
    50                 //if ( !$this->total_member_count ) {
    5150                $this->total_member_count = count( $this->user_dataset );
    5251                groups_update_groupmeta( $this->id, 'total_member_count', $this->total_member_count );
    53                 //}
    5452            }
     53
     54            /* Get group extras */
     55            $this->is_member = BP_Groups_Member::check_is_member( $bp->loggedin_user->id, $this->id );
    5556        }
    5657    }
     
    336337        }
    337338
     339        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     340        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'newest' );
     341
    338342        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    339343    }
     
    361365        }
    362366
     367        foreach ( $paged_groups as $group ) $group_ids[] = $group->id;
     368        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     369        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'active' );
     370
    363371        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    364372    }
     
    387395        }
    388396
     397        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     398        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'popular' );
     399
    389400        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    390401    }
     
    412423        }
    413424
     425        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     426        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'alphabetical' );
     427
    414428        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    415429    }
     
    441455        }
    442456
     457        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     458        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'most_forum_topics' );
     459
    443460        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    444461    }
     
    469486            $total_groups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->table_name_groupmeta} gm3, {$bbdb->forums} f, {$bp->groups->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}" ) );
    470487        }
     488
     489        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     490        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'most_forum_posts' );
    471491
    472492        return array( 'groups' => $paged_groups, 'total' => $total_groups );
     
    535555        $paged_groups = $wpdb->get_results( $wpdb->prepare( "SELECT g.*, gm1.meta_value as total_member_count, gm2.meta_value as last_activity FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2, {$bp->groups->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}"  ) );
    536556
     557        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     558        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'letter' );
     559
    537560        return array( 'groups' => $paged_groups, 'total' => $total_groups );
    538561    }
     
    560583        }
    561584
    562         return array( 'groups' => $paged_groups, 'total' => $total_groups );
     585        $group_ids = $wpdb->escape( join( ',', (array)$group_ids ) );
     586        $paged_groups = BP_Groups_Group::get_group_extras( &$paged_groups, $group_ids, 'random' );
     587
     588        return array( 'groups' => $paged_groups, 'total' => $total_groups );
     589    }
     590
     591    function get_group_extras( $paged_groups, $group_ids, $type = false ) {
     592        global $bp, $wpdb;
     593
     594        if ( empty( $group_ids ) )
     595            return $paged_groups;
     596
     597        /* Fetch the logged in users status within each group */
     598        $user_status = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id IN ( {$group_ids} ) AND is_confirmed = 1 AND is_banned = 0", $bp->loggedin_user->id ) );
     599        for ( $i = 0; $i < count( $paged_groups ); $i++ ) {
     600            foreach ( $user_status as $group_id ) {
     601                if ( $group_id == $paged_groups[$i]->id )
     602                    $paged_groups[$i]->is_member = true;
     603            }
     604        }
     605
     606        $user_banned = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_members} WHERE is_banned = 1 AND user_id = %d AND group_id IN ( {$group_ids} )", $bp->loggedin_user->id ) );
     607        for ( $i = 0; $i < count( $paged_groups ); $i++ ) {
     608            foreach ( $user_banned as $group_id ) {
     609                if ( $group_id == $paged_groups[$i]->id )
     610                    $paged_groups[$i]->is_banned = true;
     611            }
     612        }
     613
     614        return $paged_groups;
    563615    }
    564616
  • trunk/bp-groups/bp-groups-templatetags.php

    r2496 r2533  
    10801080        $group =& $groups_template->group;
    10811081
    1082     if ( groups_is_user_member( $bp->loggedin_user->id, $group->id ) )
    1083         return true;
    1084 
    1085     return false;
     1082    if ( null == $group->is_member )
     1083        return false;
     1084
     1085    return true;
    10861086}
    10871087
     
    11681168
    11691169    // If they're not logged in or are banned from the group, no join button.
    1170     if ( !is_user_logged_in() || groups_is_user_banned( $bp->loggedin_user->id, $group->id ) )
     1170    if ( !is_user_logged_in() || $group->is_banned )
    11711171        return false;
    11721172
     
    11781178    switch ( $group->status ) {
    11791179        case 'public':
    1180             if ( BP_Groups_Member::check_is_member( $bp->loggedin_user->id, $group->id ) )
     1180            if ( $group->is_member )
    11811181                echo '<a class="leave-group" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
    11821182            else
     
    11851185
    11861186        case 'private':
    1187             if ( BP_Groups_Member::check_is_member( $bp->loggedin_user->id, $group->id ) ) {
     1187            if ( $group->is_member ) {
    11881188                echo '<a class="leave-group" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
    11891189            } else {
  • trunk/bp-themes/bp-default/_inc/global.js

    r2519 r2533  
    797797    /** Alternate Highlighting ******************************************/
    798798
    799     j('table tr').each( function(i) {
    800         if ( i % 2 == 1 )
    801             j(this).addClass('alt');
    802     });
    803 
    804     j('div.message-box, ul#topic-post-list li').each( function(i) {
     799    j('table tr, div.message-box, ul#topic-post-list li').each( function(i) {
    805800        if ( i % 2 != 1 )
    806801            j(this).addClass('alt');
Note: See TracChangeset for help on using the changeset viewer.