Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/17/2014 07:42:03 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce metadata pre-fetching for xprofile objects

XProfile objects (groups, fields, and data) are generally fetched in a nested
way, as within the context of a bp_has_profile() loop. Thus, it makes sense to
pre-fetch xprofile metadata within a bp_has_profile() loop in a nested way as
well. This changeset introduces bp_xprofile_update_meta_cache(), which is able
to fetch all uncached metadata for nested meta types with a single database
query.

This changeset also introduces an 'update_meta_cache' parameter to the
bp_has_profile() stack. This param allows developers to disable the pre-
fetching introduced here. Defaults to true.

See #5398

File:
1 edited

Legend:

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

    r7820 r7914  
    118118     *      'exclude_groups' - Comma-separated list of groups to exclude
    119119     *      'exclude_fields' - Comma-separated list of fields to exclude
     120     *      'update_meta_cache' - Whether to pre-fetch xprofilemeta
     121     *         for all retrieved groups, fields, and data
    120122     *
    121123     * @return array $groups
     
    133135            'fetch_visibility_level' => false,
    134136            'exclude_groups'         => false,
    135             'exclude_fields'         => false
     137            'exclude_fields'         => false,
     138            'update_meta_cache'      => true,
    136139        );
    137140
    138141        $r = wp_parse_args( $args, $defaults );
    139142        extract( $r, EXTR_SKIP );
     143
     144        // Keep track of object IDs for cache-priming
     145        $object_ids = array(
     146            'group' => array(),
     147            'field' => array(),
     148            'data'  => array(),
     149        );
     150
    140151        $where_sql = '';
    141152
     
    162173        }
    163174
     175        // Store for meta cache priming
     176        $object_ids['group'] = $group_ids;
     177
    164178        $group_ids = implode( ',', (array) $group_ids );
    165179
     
    183197        // Fetch the fields
    184198        $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" );
     199
     200        // Store field IDs for meta cache priming
     201        $object_ids['field'] = wp_list_pluck( $fields, 'id' );
    185202
    186203        if ( empty( $fields ) )
     
    241258                            $fields[$field_key]->data->id    = $data->id;
    242259                        }
     260
     261                        // Store for meta cache priming
     262                        $object_ids['data'][] = $data->id;
    243263                    }
    244264                }
     
    272292            // Reset indexes
    273293            $groups = array_values( $groups );
     294        }
     295
     296        // Prime the meta cache, if necessary
     297        if ( $update_meta_cache ) {
     298            bp_xprofile_update_meta_cache( $object_ids );
    274299        }
    275300
Note: See TracChangeset for help on using the changeset viewer.