Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/17/2010 07:42:57 PM (15 years ago)
Author:
apeatling
Message:

Improved profile page performance.

File:
1 edited

Legend:

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

    r2277 r2320  
    2626            $this->description = $group->description;
    2727            $this->can_delete = $group->can_delete;
    28 
    29             // get the fields for this group.
    30             $this->fields = $this->get_fields();
    3128        }
    3229
     
    7875    }
    7976
    80     function get_fields() {
    81         global $wpdb, $bp;
    82 
    83         /* Find the max value for field_order, if it is zero, order by field_id instead -- provides backwards compat ordering */
    84         if ( !(int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $this->id ) ) )
    85             $order_sql = "ORDER BY id";
     77    /** Static Functions **/
     78
     79    function get( $args = '' ) {
     80        global $wpdb, $bp;
     81
     82        $defaults = array(
     83            'profile_group_id' => false,
     84            'user_id' => $bp->displayed_user->id,
     85            'hide_empty_groups' => false,
     86            'fetch_fields' => false,
     87            'fetch_field_data' => false
     88        );
     89
     90        $r = wp_parse_args( $args, $defaults );
     91        extract( $r, EXTR_SKIP );
     92
     93        if ( $profile_group_id )
     94            $group_id_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id );
     95
     96        if ( $hide_empty_groups )
     97            $groups = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$group_id_sql} ORDER BY g.id ASC" ) );
    8698        else
    87             $order_sql = "ORDER BY field_order";
    88 
    89         // Get field ids for the current group.
    90         if ( !$fields = $wpdb->get_results( $wpdb->prepare("SELECT id, type FROM {$bp->profile->table_name_fields} WHERE group_id = %d AND parent_id = 0 {$order_sql}", $this->id ) ) )
    91             return false;
    92 
    93         return $fields;
    94     }
    95 
    96     /** Static Functions **/
    97 
    98     function get_all( $hide_empty = false ) {
    99         global $wpdb, $bp;
    100 
    101         if ( $hide_empty ) {
    102             $sql = $wpdb->prepare( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id ORDER BY g.id ASC" );
    103         } else {
    104             $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_groups} ORDER BY id ASC" );
    105         }
    106 
    107         if ( !$groups_temp = $wpdb->get_results($sql) )
    108             return false;
    109 
    110         for ( $i = 0; $i < count($groups_temp); $i++ ) {
    111             $group = new BP_XProfile_Group($groups_temp[$i]->id);
    112             $groups[] = $group;
     99            $groups = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g {$group_id_sql} ORDER BY g.id ASC" ) );
     100
     101        if ( !$fetch_fields )
     102            return $groups;
     103
     104        /* Get the group ids */
     105        foreach( (array)$groups as $group )
     106            $group_ids[] = $group->id;
     107
     108        $group_ids = implode( ',', (array) $group_ids );
     109
     110        if ( empty( $group_ids ) )
     111            return $groups;
     112
     113        /* Fetch the fields */
     114        $fields = $wpdb->get_results( $wpdb->prepare( "SELECT id, name, type, group_id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 ORDER BY field_order" ) );
     115
     116        if ( empty( $fields ) )
     117            return $groups;
     118
     119        if ( $fetch_field_data ) {
     120            /* Fetch the field data for the user. */
     121            foreach( (array)$fields as $field )
     122                $field_ids[] = $field->id;
     123
     124            $field_ids = implode( ',', (array) $field_ids );
     125
     126            if ( !empty( $field_ids ) )
     127                $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids} ) AND user_id = %d", $user_id ) );
     128
     129            if ( !empty( $field_data ) ) {
     130                foreach( (array)$fields as $field_key => $field ) {
     131                    foreach( (array)$field_data as $data ) {
     132                        if ( $field->id == $data->field_id )
     133                            $fields[$field_key]->data->value = $data->value;
     134                    }
     135                }
     136            }
     137        }
     138
     139        /* Merge the field array back in with the group array */
     140        foreach( (array)$groups as $group_key => $group ) {
     141            foreach( (array)$fields as $field ) {
     142                if ( $group->id == $field->group_id )
     143                    $groups[$group_key]->fields[] = $field;
     144            }
    113145        }
    114146
     
    727759        global $wpdb, $bp;
    728760
    729         $results = $wpdb->get_results( $wpdb->prepare( "SELECT g.name as field_group_name, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email FROM {$bp->profile->table_name_groups} g LEFT JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp->profile->table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb->users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''", $user_id ) );
     761        $results = $wpdb->get_results( $wpdb->prepare( "SELECT g.id as field_group_id, g.name as field_group_name, f.id as field_id, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email FROM {$bp->profile->table_name_groups} g LEFT JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp->profile->table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb->users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''", $user_id ) );
    730762
    731763        if ( $results ) {
     
    735767
    736768            foreach( (array) $results as $field ) {
    737                 $profile_data[$field->field_name] = array( 'field_group' => $field->field_group_name, 'field_type' => $field->field_type, 'field_data' => $field->field_data );
     769                $profile_data[$field->field_name] = array(
     770                    'field_group_id' => $field->field_group_id,
     771                    'field_group_name' => $field->field_group_name,
     772                    'field_id' => $field->field_id,
     773                    'field_type' => $field->field_type,
     774                    'field_data' => $field->field_data
     775                );
    738776            }
    739777        }
Note: See TracChangeset for help on using the changeset viewer.