Skip to:
Content

BuddyPress.org

Changeset 2320


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

Improved profile page performance.

Location:
trunk/bp-xprofile
Files:
4 edited

Legend:

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

    r2077 r2320  
    1414        $type = preg_replace( '|[^a-z]|i', '', $type );
    1515
    16         $groups = BP_XProfile_Group::get_all();
     16        $groups = BP_XProfile_Group::get( array(
     17                'fetch_fields' => true
     18        ) );
    1719
    1820        if ( isset($_GET['mode']) && isset($_GET['group_id']) && 'add_field' == $_GET['mode'] ) {
  • 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                }
  • trunk/bp-xprofile/bp-xprofile-filters.php

    r2088 r2320  
    33/* Apply WordPress defined filters */
    44add_filter( 'bp_get_the_profile_field_value', 'wp_filter_kses', 1 );
     5add_filter( 'bp_get_the_profile_field_name', 'wp_filter_kses', 1 );
     6
    57add_filter( 'bp_get_the_site_member_profile_data', 'wp_filter_kses', 1 );
    68add_filter( 'xprofile_get_field_data', 'wp_filter_kses', 1 );
     
    3335add_filter( 'bp_get_the_profile_field_edit_value', 'stripslashes' );
    3436add_filter( 'bp_get_the_profile_field_value', 'stripslashes' );
     37add_filter( 'bp_get_the_profile_field_name', 'stripslashes' );
    3538add_filter( 'xprofile_get_field_data', 'stripslashes' );
    3639add_filter( 'bp_get_the_profile_field_description', 'stripslashes' );
  • trunk/bp-xprofile/bp-xprofile-templatetags.php

    r2168 r2320  
    2020
    2121        function bp_xprofile_data_template( $user_id, $profile_group_id ) {
    22 
    23                 if ( !$profile_group_id ) {
    24                         if ( !$this->groups = wp_cache_get( 'xprofile_groups', 'bp' ) ) {
    25                                 $this->groups = BP_XProfile_Group::get_all(true);
    26                                 wp_cache_set( 'xprofile_groups', $this->groups, 'bp' );
    27                         }
    28                 } else {
    29                         if ( !$this->groups = wp_cache_get( 'xprofile_group_' . $profile_group_id, 'bp' ) ) {
    30                                 $this->groups = new BP_XProfile_Group( $profile_group_id );
    31                                 wp_cache_set( 'xprofile_group_' . $profile_group_id, 'bp' );
    32                         }
    33 
    34                         /* We need to put this single group into the same format as multiple group (an array) */
    35                         $this->groups = array( $this->groups );
    36                 }
     22                $this->groups = BP_XProfile_Group::get( array(
     23                        'profile_group_id' => $profile_group_id,
     24                        'user_id' => $user_id,
     25                        'hide_empty_groups' => true,
     26                        'fetch_fields' => true,
     27                        'fetch_field_data' => true
     28                ) );
    3729
    3830                $this->group_count = count($this->groups);
     
    5143
    5244                $this->group = $this->groups[$this->current_group];
    53 
    54                 if ( !$fields = wp_cache_get( 'xprofile_fields_' . $this->group->id . '_' . $this->user_id, 'bp' ) ) {
    55                         for ( $i = 0; $i < count($this->group->fields); $i++ ) {
    56                                 /* Don't try and fetch any existing profile data if we are using this loop on the registration page */
    57                                 $get_data = ( !bp_is_register_page() ) ? true : false;
    58 
    59                                 $field = new BP_XProfile_Field( $this->group->fields[$i]->id, $this->user_id, $get_data );
    60                                 $fields[$i] = $field;
    61                         }
    62 
    63                         wp_cache_set( 'xprofile_fields_' . $this->group->id . '_' . $this->user_id, $fields, 'bp' );
    64                 }
    65 
    66                 $this->group->fields = apply_filters( 'xprofile_group_fields', $fields, $this->group->id );
     45                $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id );
    6746                $this->field_count = count( $this->group->fields );
    6847
     
    386365                $r = wp_parse_args( $args, $defaults );
    387366                extract( $r, EXTR_SKIP );
     367
     368                if ( !method_exists( $field, 'get_children' ) )
     369                        $field = new BP_XProfile_Field( $field->id );
    388370
    389371                $options = $field->get_children();
     
    584566
    585567        if ( !$groups = wp_cache_get( 'xprofile_groups_inc_empty', 'bp' ) ) {
    586                 $groups = BP_XProfile_Group::get_all();
     568                $groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
    587569                wp_cache_set( 'xprofile_groups_inc_empty', $groups, 'bp' );
    588570        }
Note: See TracChangeset for help on using the changeset viewer.