Skip to:
Content

BuddyPress.org

Changeset 2533 for trunk/bp-forums.php


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

Removing database hits inside BP loops to improve overall performance.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.