Skip to:
Content

BuddyPress.org

Changeset 7780


Ignore:
Timestamp:
02/04/2014 02:05:24 AM (12 years ago)
Author:
boonebgorges
Message:

Split xprofile group query, and add caching support for individual xprofile groups.

See #1332

File:
1 edited

Legend:

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

    r7710 r7780  
    2727                global $wpdb, $bp;
    2828
    29                 $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id = %d", $id );
    30 
    31                 if ( !$group = $wpdb->get_row( $sql ) )
    32                         return false;
     29                $group = wp_cache_get( 'xprofile_group_' . $this->id, 'bp' );
     30
     31                if ( false === $group ) {
     32                        $group = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id = %d", $id ) );
     33                }
     34
     35                if ( empty( $group ) ) {
     36                        return false;
     37                }
    3338
    3439                $this->id          = $group->id;
     
    136141                        $where_sql = $wpdb->prepare( "WHERE g.id NOT IN ({$exclude_groups})");
    137142
    138                 if ( !empty( $hide_empty_groups ) )
    139                         $groups = $wpdb->get_results( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" );
    140                 else
    141                         $groups = $wpdb->get_results( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" );
     143                if ( ! empty( $hide_empty_groups ) ) {
     144                        $group_ids = $wpdb->get_col( "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 {$where_sql} ORDER BY g.group_order ASC" );
     145                } else {
     146                        $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" );
     147                }
     148
     149                $groups = self::get_group_data( $group_ids );
    142150
    143151                if ( empty( $fetch_fields ) )
     
    261269                        $groups = array_values( $groups );
    262270                }
     271
     272                return $groups;
     273        }
     274
     275        /**
     276         * Get data about a set of groups, based on IDs.
     277         *
     278         * @since BuddyPress (2.0.0)
     279         *
     280         * @param array $group_ids Array of IDs.
     281         * @return array
     282         */
     283        protected static function get_group_data( $group_ids ) {
     284                global $wpdb;
     285
     286                // Bail if no group IDs are passed
     287                if ( empty( $group_ids ) ) {
     288                        return array();
     289                }
     290
     291                $groups        = array();
     292                $uncached_gids = array();
     293
     294                foreach ( $group_ids as $group_id ) {
     295
     296                        // If cached data is found, use it
     297                        if ( $group_data = wp_cache_get( 'xprofile_group_' . $group_id, 'bp' ) ) {
     298                                $groups[ $group_id ] = $group_data;
     299
     300                        // Otherwise leave a placeholder so we don't lose the order
     301                        } else {
     302                                $groups[ $group_id ] = '';
     303
     304                                // Add to the list of items to be queried
     305                                $uncached_gids[] = $group_id;
     306                        }
     307                }
     308
     309                // Fetch uncached data from the DB if necessary
     310                if ( ! empty( $uncached_gids ) ) {
     311                        $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) );
     312
     313                        $bp = buddypress();
     314
     315                        // Fetch data, preserving order
     316                        $queried_gdata = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
     317
     318                        // Put queried data into the placeholders created earlier,
     319                        // and add it to the cache
     320                        foreach ( (array) $queried_gdata as $gdata ) {
     321                                $groups[ $gdata->id ] = $gdata;
     322                                wp_cache_set( 'xprofile_group_' . $gdata->id, $gdata, 'bp' );
     323                        }
     324                }
     325
     326                // Reset indexes
     327                $groups = array_values( $groups );
    263328
    264329                return $groups;
Note: See TracChangeset for help on using the changeset viewer.