<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[BuddyPress] [2320] trunk/bp-xprofile: Improved profile page performance.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>2320</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-01-17 19:42:57 +0000 (Sun, 17 Jan 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Improved profile page performance.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpxprofilebpxprofileadminphp">trunk/bp-xprofile/bp-xprofile-admin.php</a></li>
<li><a href="#trunkbpxprofilebpxprofileclassesphp">trunk/bp-xprofile/bp-xprofile-classes.php</a></li>
<li><a href="#trunkbpxprofilebpxprofilefiltersphp">trunk/bp-xprofile/bp-xprofile-filters.php</a></li>
<li><a href="#trunkbpxprofilebpxprofiletemplatetagsphp">trunk/bp-xprofile/bp-xprofile-templatetags.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpxprofilebpxprofileadminphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-admin.php (2319 => 2320)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-admin.php        2010-01-17 16:08:01 UTC (rev 2319)
+++ trunk/bp-xprofile/bp-xprofile-admin.php        2010-01-17 19:42:57 UTC (rev 2320)
</span><span class="lines">@@ -13,7 +13,9 @@
</span><span class="cx">
</span><span class="cx">         $type = preg_replace( '|[^a-z]|i', '', $type );
</span><span class="cx">
</span><del>-        $groups = BP_XProfile_Group::get_all();
</del><ins>+        $groups = BP_XProfile_Group::get( array(
+                'fetch_fields' => true
+        ) );
</ins><span class="cx">
</span><span class="cx">         if ( isset($_GET['mode']) && isset($_GET['group_id']) && 'add_field' == $_GET['mode'] ) {
</span><span class="cx">                 xprofile_admin_manage_field($_GET['group_id']);
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofileclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-classes.php (2319 => 2320)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-classes.php        2010-01-17 16:08:01 UTC (rev 2319)
+++ trunk/bp-xprofile/bp-xprofile-classes.php        2010-01-17 19:42:57 UTC (rev 2320)
</span><span class="lines">@@ -25,9 +25,6 @@
</span><span class="cx">                         $this->name = $group->name;
</span><span class="cx">                         $this->description = $group->description;
</span><span class="cx">                         $this->can_delete = $group->can_delete;
</span><del>-
-                        // get the fields for this group.
-                        $this->fields = $this->get_fields();
</del><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">         }
</span><span class="lines">@@ -77,41 +74,76 @@
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        function get_fields() {
</del><ins>+        /** Static Functions **/
+
+        function get( $args = '' ) {
</ins><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><del>-                /* Find the max value for field_order, if it is zero, order by field_id instead -- provides backwards compat ordering */
-                if ( !(int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $this->id ) ) )
-                        $order_sql = "ORDER BY id";
</del><ins>+                $defaults = array(
+                        'profile_group_id' => false,
+                        'user_id' => $bp->displayed_user->id,
+                        'hide_empty_groups' => false,
+                        'fetch_fields' => false,
+                        'fetch_field_data' => false
+                );
+
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r, EXTR_SKIP );
+
+                if ( $profile_group_id )
+                        $group_id_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id );
+
+                if ( $hide_empty_groups )
+                        $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" ) );
</ins><span class="cx">                 else
</span><del>-                        $order_sql = "ORDER BY field_order";
</del><ins>+                        $groups = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g {$group_id_sql} ORDER BY g.id ASC" ) );
</ins><span class="cx">
</span><del>-                // Get field ids for the current group.
-                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 ) ) )
-                        return false;
</del><ins>+                if ( !$fetch_fields )
+                        return $groups;
</ins><span class="cx">
</span><del>-                return $fields;
-        }
</del><ins>+                /* Get the group ids */
+                foreach( (array)$groups as $group )
+                        $group_ids[] = $group->id;
</ins><span class="cx">
</span><del>-        /** Static Functions **/
</del><ins>+                $group_ids = implode( ',', (array) $group_ids );
</ins><span class="cx">
</span><del>-        function get_all( $hide_empty = false ) {
-                global $wpdb, $bp;
</del><ins>+                if ( empty( $group_ids ) )
+                        return $groups;
</ins><span class="cx">
</span><del>-                if ( $hide_empty ) {
-                        $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" );
-                } else {
-                        $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_groups} ORDER BY id ASC" );
-                }
</del><ins>+                /* Fetch the fields */
+                $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" ) );
</ins><span class="cx">
</span><del>-                if ( !$groups_temp = $wpdb->get_results($sql) )
-                        return false;
</del><ins>+                if ( empty( $fields ) )
+                        return $groups;
</ins><span class="cx">
</span><del>-                for ( $i = 0; $i < count($groups_temp); $i++ ) {
-                        $group = new BP_XProfile_Group($groups_temp[$i]->id);
-                        $groups[] = $group;
</del><ins>+                if ( $fetch_field_data ) {
+                        /* Fetch the field data for the user. */
+                        foreach( (array)$fields as $field )
+                                $field_ids[] = $field->id;
+
+                        $field_ids = implode( ',', (array) $field_ids );
+
+                        if ( !empty( $field_ids ) )
+                                $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 ) );
+
+                        if ( !empty( $field_data ) ) {
+                                foreach( (array)$fields as $field_key => $field ) {
+                                        foreach( (array)$field_data as $data ) {
+                                                if ( $field->id == $data->field_id )
+                                                        $fields[$field_key]->data->value = $data->value;
+                                        }
+                                }
+                        }
</ins><span class="cx">                 }
</span><span class="cx">
</span><ins>+                /* Merge the field array back in with the group array */
+                foreach( (array)$groups as $group_key => $group ) {
+                        foreach( (array)$fields as $field ) {
+                                if ( $group->id == $field->group_id )
+                                        $groups[$group_key]->fields[] = $field;
+                        }
+                }
+
</ins><span class="cx">                 return $groups;
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -726,7 +758,7 @@
</span><span class="cx">         function get_all_for_user( $user_id ) {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span><del>-                $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 ) );
</del><ins>+                $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 ) );
</ins><span class="cx">
</span><span class="cx">                 if ( $results ) {
</span><span class="cx">                         $profile_data['user_login'] = $results[0]->user_login;
</span><span class="lines">@@ -734,7 +766,13 @@
</span><span class="cx">                         $profile_data['user_email'] = $results[0]->user_email;
</span><span class="cx">
</span><span class="cx">                         foreach( (array) $results as $field ) {
</span><del>-                                $profile_data[$field->field_name] = array( 'field_group' => $field->field_group_name, 'field_type' => $field->field_type, 'field_data' => $field->field_data );
</del><ins>+                                $profile_data[$field->field_name] = array(
+                                        'field_group_id' => $field->field_group_id,
+                                        'field_group_name' => $field->field_group_name,
+                                        'field_id' => $field->field_id,
+                                        'field_type' => $field->field_type,
+                                        'field_data' => $field->field_data
+                                );
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofilefiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-filters.php (2319 => 2320)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-filters.php        2010-01-17 16:08:01 UTC (rev 2319)
+++ trunk/bp-xprofile/bp-xprofile-filters.php        2010-01-17 19:42:57 UTC (rev 2320)
</span><span class="lines">@@ -2,6 +2,8 @@
</span><span class="cx">
</span><span class="cx"> /* Apply WordPress defined filters */
</span><span class="cx"> add_filter( 'bp_get_the_profile_field_value', 'wp_filter_kses', 1 );
</span><ins>+add_filter( 'bp_get_the_profile_field_name', 'wp_filter_kses', 1 );
+
</ins><span class="cx"> add_filter( 'bp_get_the_site_member_profile_data', 'wp_filter_kses', 1 );
</span><span class="cx"> add_filter( 'xprofile_get_field_data', 'wp_filter_kses', 1 );
</span><span class="cx"> add_filter( 'xprofile_field_name_before_save', 'wp_filter_kses', 1 );
</span><span class="lines">@@ -32,6 +34,7 @@
</span><span class="cx">
</span><span class="cx"> add_filter( 'bp_get_the_profile_field_edit_value', 'stripslashes' );
</span><span class="cx"> add_filter( 'bp_get_the_profile_field_value', 'stripslashes' );
</span><ins>+add_filter( 'bp_get_the_profile_field_name', 'stripslashes' );
</ins><span class="cx"> add_filter( 'xprofile_get_field_data', 'stripslashes' );
</span><span class="cx"> add_filter( 'bp_get_the_profile_field_description', 'stripslashes' );
</span><span class="cx"> add_filter( 'bp_get_the_site_member_profile_data', 'stripslashes' );
</span></span></pre></div>
<a id="trunkbpxprofilebpxprofiletemplatetagsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-xprofile/bp-xprofile-templatetags.php (2319 => 2320)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-xprofile/bp-xprofile-templatetags.php        2010-01-17 16:08:01 UTC (rev 2319)
+++ trunk/bp-xprofile/bp-xprofile-templatetags.php        2010-01-17 19:42:57 UTC (rev 2320)
</span><span class="lines">@@ -19,22 +19,14 @@
</span><span class="cx">         var $user_id;
</span><span class="cx">
</span><span class="cx">         function bp_xprofile_data_template( $user_id, $profile_group_id ) {
</span><ins>+                $this->groups = BP_XProfile_Group::get( array(
+                        'profile_group_id' => $profile_group_id,
+                        'user_id' => $user_id,
+                        'hide_empty_groups' => true,
+                        'fetch_fields' => true,
+                        'fetch_field_data' => true
+                ) );
</ins><span class="cx">
</span><del>-                if ( !$profile_group_id ) {
-                        if ( !$this->groups = wp_cache_get( 'xprofile_groups', 'bp' ) ) {
-                                $this->groups = BP_XProfile_Group::get_all(true);
-                                wp_cache_set( 'xprofile_groups', $this->groups, 'bp' );
-                        }
-                } else {
-                        if ( !$this->groups = wp_cache_get( 'xprofile_group_' . $profile_group_id, 'bp' ) ) {
-                                $this->groups = new BP_XProfile_Group( $profile_group_id );
-                                wp_cache_set( 'xprofile_group_' . $profile_group_id, 'bp' );
-                        }
-
-                        /* We need to put this single group into the same format as multiple group (an array) */
-                        $this->groups = array( $this->groups );
-                }
-
</del><span class="cx">                 $this->group_count = count($this->groups);
</span><span class="cx">                 $this->user_id = $user_id;
</span><span class="cx">         }
</span><span class="lines">@@ -50,20 +42,7 @@
</span><span class="cx">                 $this->current_group++;
</span><span class="cx">
</span><span class="cx">                 $this->group = $this->groups[$this->current_group];
</span><del>-
-                if ( !$fields = wp_cache_get( 'xprofile_fields_' . $this->group->id . '_' . $this->user_id, 'bp' ) ) {
-                        for ( $i = 0; $i < count($this->group->fields); $i++ ) {
-                                /* Don't try and fetch any existing profile data if we are using this loop on the registration page */
-                                $get_data = ( !bp_is_register_page() ) ? true : false;
-
-                                $field = new BP_XProfile_Field( $this->group->fields[$i]->id, $this->user_id, $get_data );
-                                $fields[$i] = $field;
-                        }
-
-                        wp_cache_set( 'xprofile_fields_' . $this->group->id . '_' . $this->user_id, $fields, 'bp' );
-                }
-
-                $this->group->fields = apply_filters( 'xprofile_group_fields', $fields, $this->group->id );
</del><ins>+                $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id );
</ins><span class="cx">                 $this->field_count = count( $this->group->fields );
</span><span class="cx">
</span><span class="cx">                 return $this->group;
</span><span class="lines">@@ -386,6 +365,9 @@
</span><span class="cx">                 $r = wp_parse_args( $args, $defaults );
</span><span class="cx">                 extract( $r, EXTR_SKIP );
</span><span class="cx">
</span><ins>+                if ( !method_exists( $field, 'get_children' ) )
+                        $field = new BP_XProfile_Field( $field->id );
+
</ins><span class="cx">                 $options = $field->get_children();
</span><span class="cx">
</span><span class="cx">                 switch ( $field->type ) {
</span><span class="lines">@@ -583,7 +565,7 @@
</span><span class="cx">         global $bp, $group_name;
</span><span class="cx">
</span><span class="cx">         if ( !$groups = wp_cache_get( 'xprofile_groups_inc_empty', 'bp' ) ) {
</span><del>-                $groups = BP_XProfile_Group::get_all();
</del><ins>+                $groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
</ins><span class="cx">                 wp_cache_set( 'xprofile_groups_inc_empty', $groups, 'bp' );
</span><span class="cx">         }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>