Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/06/2014 02:23:51 AM (10 years ago)
Author:
boonebgorges
Message:

Refactor BP_XProfile_ProfileData::get_all_for_user() for performance

Method has been refactored to avoid joins, especially against global tables.
Direct SQL queries have been removed in favor of using existing query methods,
which has the added benefit of allowing existing caching strategies to be
inherited automatically from those methods.

Adds unit tests for the method.

See #1332

File:
1 edited

Legend:

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

    r7807 r7808  
    12791279
    12801280    /**
    1281      * BP_XProfile_ProfileData::get_all_for_user()
    1282      *
    12831281     * Get all of the profile information for a specific user.
     1282     *
     1283     * @param int $user_id ID of the user.
     1284     * @return array
    12841285     */
    12851286    public static function get_all_for_user( $user_id ) {
    12861287        global $wpdb, $bp;
    12871288
    1288         $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 ) );
     1289        $groups = BP_XProfile_Group::get( array(
     1290            'user_id'                => $user_id,
     1291            'hide_empty_groups'      => true,
     1292            'hide_empty_fields'      => true,
     1293            'fetch_fields'           => true,
     1294            'fetch_field_data'       => true,
     1295        ) );
     1296
    12891297        $profile_data = array();
    12901298
    1291         if ( !empty( $results ) ) {
    1292             $profile_data['user_login']    = $results[0]->user_login;
    1293             $profile_data['user_nicename'] = $results[0]->user_nicename;
    1294             $profile_data['user_email']    = $results[0]->user_email;
    1295 
    1296             foreach( (array) $results as $field ) {
    1297                 $profile_data[$field->field_name] = array(
    1298                     'field_group_id'   => $field->field_group_id,
    1299                     'field_group_name' => $field->field_group_name,
    1300                     'field_id'         => $field->field_id,
    1301                     'field_type'       => $field->field_type,
    1302                     'field_data'       => $field->field_data
    1303                 );
     1299        if ( ! empty( $groups ) ) {
     1300            $user = new WP_User( $user_id );
     1301
     1302            $profile_data['user_login']    = $user->user_login;
     1303            $profile_data['user_nicename'] = $user->user_nicename;
     1304            $profile_data['user_email']    = $user->user_email;
     1305
     1306            foreach ( (array) $groups as $group ) {
     1307                if ( empty( $group->fields ) ) {
     1308                    continue;
     1309                }
     1310
     1311                foreach ( (array) $group->fields as $field ) {
     1312                    $profile_data[ $field->name ] = array(
     1313                        'field_group_id'   => $group->id,
     1314                        'field_group_name' => $group->name,
     1315                        'field_id'         => $field->id,
     1316                        'field_type'       => $field->type,
     1317                        'field_data'       => $field->data->value,
     1318                    );
     1319                }
    13041320            }
    13051321        }
Note: See TracChangeset for help on using the changeset viewer.