<!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' =&gt; true
+        ) );
</ins><span class="cx"> 
</span><span class="cx">         if ( isset($_GET['mode']) &amp;&amp; isset($_GET['group_id']) &amp;&amp; '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-&gt;name = $group-&gt;name;
</span><span class="cx">                         $this-&gt;description = $group-&gt;description;
</span><span class="cx">                         $this-&gt;can_delete = $group-&gt;can_delete;
</span><del>-
-                        // get the fields for this group.
-                        $this-&gt;fields = $this-&gt;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-&gt;get_var( $wpdb-&gt;prepare( &quot;SELECT MAX(field_order) FROM {$bp-&gt;profile-&gt;table_name_fields} WHERE group_id = %d&quot;, $this-&gt;id ) ) )
-                        $order_sql = &quot;ORDER BY id&quot;;
</del><ins>+                $defaults = array(
+                        'profile_group_id' =&gt; false,
+                        'user_id' =&gt; $bp-&gt;displayed_user-&gt;id,
+                        'hide_empty_groups' =&gt; false,
+                        'fetch_fields' =&gt; false,
+                        'fetch_field_data' =&gt; false
+                );
+
+                $r = wp_parse_args( $args, $defaults );
+                extract( $r, EXTR_SKIP );
+
+                if ( $profile_group_id )
+                        $group_id_sql = $wpdb-&gt;prepare( 'WHERE g.id = %d', $profile_group_id );
+
+                if ( $hide_empty_groups )
+                        $groups = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT g.* FROM {$bp-&gt;profile-&gt;table_name_groups} g INNER JOIN {$bp-&gt;profile-&gt;table_name_fields} f ON g.id = f.group_id {$group_id_sql} ORDER BY g.id ASC&quot; ) );
</ins><span class="cx">                 else
</span><del>-                        $order_sql = &quot;ORDER BY field_order&quot;;
</del><ins>+                        $groups = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT DISTINCT g.* FROM {$bp-&gt;profile-&gt;table_name_groups} g {$group_id_sql} ORDER BY g.id ASC&quot; ) );
</ins><span class="cx"> 
</span><del>-                // Get field ids for the current group.
-                if ( !$fields = $wpdb-&gt;get_results( $wpdb-&gt;prepare(&quot;SELECT id, type FROM {$bp-&gt;profile-&gt;table_name_fields} WHERE group_id = %d AND parent_id = 0 {$order_sql}&quot;, $this-&gt;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-&gt;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-&gt;prepare( &quot;SELECT DISTINCT g.id FROM {$bp-&gt;profile-&gt;table_name_groups} g INNER JOIN {$bp-&gt;profile-&gt;table_name_fields} f ON g.id = f.group_id ORDER BY g.id ASC&quot; );
-                } else {
-                        $sql = $wpdb-&gt;prepare( &quot;SELECT id FROM {$bp-&gt;profile-&gt;table_name_groups} ORDER BY id ASC&quot; );
-                }
</del><ins>+                /* Fetch the fields */
+                $fields = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT id, name, type, group_id FROM {$bp-&gt;profile-&gt;table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 ORDER BY field_order&quot; ) );
</ins><span class="cx"> 
</span><del>-                if ( !$groups_temp = $wpdb-&gt;get_results($sql) )
-                        return false;
</del><ins>+                if ( empty( $fields ) )
+                        return $groups;
</ins><span class="cx"> 
</span><del>-                for ( $i = 0; $i &lt; count($groups_temp); $i++ ) {
-                        $group = new BP_XProfile_Group($groups_temp[$i]-&gt;id);
-                        $groups[] = $group;
</del><ins>+                if ( $fetch_field_data ) {
+                        /* Fetch the field data for the user. */
+                        foreach( (array)$fields as $field )
+                                $field_ids[] = $field-&gt;id;
+
+                        $field_ids = implode( ',', (array) $field_ids );
+
+                        if ( !empty( $field_ids ) )
+                                $field_data = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;SELECT field_id, value FROM {$bp-&gt;profile-&gt;table_name_data} WHERE field_id IN ( {$field_ids} ) AND user_id = %d&quot;, $user_id ) );
+
+                        if ( !empty( $field_data ) ) {
+                                foreach( (array)$fields as $field_key =&gt; $field ) {
+                                        foreach( (array)$field_data as $data ) {
+                                                if ( $field-&gt;id == $data-&gt;field_id )
+                                                        $fields[$field_key]-&gt;data-&gt;value = $data-&gt;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 =&gt; $group ) {
+                        foreach( (array)$fields as $field ) {
+                                if ( $group-&gt;id == $field-&gt;group_id )
+                                        $groups[$group_key]-&gt;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-&gt;get_results( $wpdb-&gt;prepare( &quot;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-&gt;profile-&gt;table_name_groups} g LEFT JOIN {$bp-&gt;profile-&gt;table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp-&gt;profile-&gt;table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb-&gt;users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''&quot;, $user_id ) );
</del><ins>+                $results = $wpdb-&gt;get_results( $wpdb-&gt;prepare( &quot;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-&gt;profile-&gt;table_name_groups} g LEFT JOIN {$bp-&gt;profile-&gt;table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp-&gt;profile-&gt;table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb-&gt;users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''&quot;, $user_id ) );
</ins><span class="cx"> 
</span><span class="cx">                 if ( $results ) {
</span><span class="cx">                         $profile_data['user_login'] = $results[0]-&gt;user_login;
</span><span class="lines">@@ -734,7 +766,13 @@
</span><span class="cx">                         $profile_data['user_email'] = $results[0]-&gt;user_email;
</span><span class="cx"> 
</span><span class="cx">                         foreach( (array) $results as $field ) {
</span><del>-                                $profile_data[$field-&gt;field_name] = array( 'field_group' =&gt; $field-&gt;field_group_name, 'field_type' =&gt; $field-&gt;field_type, 'field_data' =&gt; $field-&gt;field_data );
</del><ins>+                                $profile_data[$field-&gt;field_name] = array(
+                                        'field_group_id' =&gt; $field-&gt;field_group_id,
+                                        'field_group_name' =&gt; $field-&gt;field_group_name,
+                                        'field_id' =&gt; $field-&gt;field_id,
+                                        'field_type' =&gt; $field-&gt;field_type,
+                                        'field_data' =&gt; $field-&gt;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-&gt;groups = BP_XProfile_Group::get( array(
+                        'profile_group_id' =&gt; $profile_group_id,
+                        'user_id' =&gt; $user_id,
+                        'hide_empty_groups' =&gt; true,
+                        'fetch_fields' =&gt; true,
+                        'fetch_field_data' =&gt; true
+                ) );
</ins><span class="cx"> 
</span><del>-                if ( !$profile_group_id ) {
-                        if ( !$this-&gt;groups = wp_cache_get( 'xprofile_groups', 'bp' ) ) {
-                                $this-&gt;groups = BP_XProfile_Group::get_all(true);
-                                wp_cache_set( 'xprofile_groups', $this-&gt;groups, 'bp' );
-                        }
-                } else {
-                        if ( !$this-&gt;groups = wp_cache_get( 'xprofile_group_' . $profile_group_id, 'bp' ) ) {
-                                $this-&gt;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-&gt;groups = array( $this-&gt;groups );
-                }
-
</del><span class="cx">                 $this-&gt;group_count = count($this-&gt;groups);
</span><span class="cx">                 $this-&gt;user_id = $user_id;
</span><span class="cx">         }
</span><span class="lines">@@ -50,20 +42,7 @@
</span><span class="cx">                 $this-&gt;current_group++;
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;group = $this-&gt;groups[$this-&gt;current_group];
</span><del>-
-                if ( !$fields = wp_cache_get( 'xprofile_fields_' . $this-&gt;group-&gt;id . '_' . $this-&gt;user_id, 'bp' ) ) {
-                        for ( $i = 0; $i &lt; count($this-&gt;group-&gt;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-&gt;group-&gt;fields[$i]-&gt;id, $this-&gt;user_id, $get_data );
-                                $fields[$i] = $field;
-                        }
-
-                        wp_cache_set( 'xprofile_fields_' . $this-&gt;group-&gt;id . '_' . $this-&gt;user_id, $fields, 'bp' );
-                }
-
-                $this-&gt;group-&gt;fields = apply_filters( 'xprofile_group_fields', $fields, $this-&gt;group-&gt;id );
</del><ins>+                $this-&gt;group-&gt;fields = apply_filters( 'xprofile_group_fields', $this-&gt;group-&gt;fields, $this-&gt;group-&gt;id );
</ins><span class="cx">                 $this-&gt;field_count = count( $this-&gt;group-&gt;fields );
</span><span class="cx"> 
</span><span class="cx">                 return $this-&gt;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-&gt;id );
+
</ins><span class="cx">                 $options = $field-&gt;get_children();
</span><span class="cx"> 
</span><span class="cx">                 switch ( $field-&gt;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' =&gt; 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>