bp_xprofile_update_meta_cache( array $object_ids = array() )
Slurp up xprofilemeta for a specified set of profile objects.
Description
We do not use bp_update_meta_cache() for the xprofile component. This is because the xprofile component has three separate object types (group, field, and data) and three corresponding cache groups. Using the technique in bp_update_meta_cache(), pre-fetching would take three separate database queries. By grouping them together, we can reduce the required queries to one.
This function is called within a bp_has_profile() loop.
Parameters
- $object_ids
-
(Optional) Multi-dimensional array of object_ids, keyed by object type ('group', 'field', 'data').
Default value: array()
Return
(bool)
Source
File: bp-xprofile/bp-xprofile-cache.php
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | function bp_xprofile_update_meta_cache( $object_ids = array () ) { global $wpdb ; // Bail if no objects. if ( empty ( $object_ids ) ) { return false; } $bp = buddypress(); // Define the array where uncached object IDs will be stored. $uncached_object_ids = array ( 'group' , 'field' , 'data' ); // Define the cache groups for the 3 types of XProfile metadata. $cache_groups = array ( 'group' => 'xprofile_group_meta' , 'field' => 'xprofile_field_meta' , 'data' => 'xprofile_data_meta' , ); // No reason to query yet. $do_query = false; // Loop through object types and look for uncached data. foreach ( $uncached_object_ids as $object_type ) { // Skip if empty object type. if ( empty ( $object_ids [ $object_type ] ) ) { continue ; } // Sanitize $object_ids passed to the function. $object_type_ids = wp_parse_id_list( $object_ids [ $object_type ] ); // Get non-cached IDs for each object type. $uncached_object_ids [ $object_type ] = bp_get_non_cached_ids( $object_type_ids , $cache_groups [ $object_type ] ); // Set the flag to do the meta query. if ( ! empty ( $uncached_object_ids [ $object_type ] ) && ( false === $do_query ) ) { $do_query = true; } } // Bail if no uncached items. if ( false === $do_query ) { return ; } // Setup where conditions for query. $where_sql = '' ; $where_conditions = array (); // Loop through uncached objects and prepare to query for them. foreach ( $uncached_object_ids as $otype => $oids ) { // Skip empty object IDs. if ( empty ( $oids ) ) { continue ; } // Compile WHERE query conditions for uncached metadata. $oids_sql = implode( ',' , wp_parse_id_list( $oids ) ); $where_conditions [] = $wpdb ->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )" , $otype ); } // Bail if no where conditions. if ( empty ( $where_conditions ) ) { return ; } // Setup the WHERE query part. $where_sql = implode( " OR " , $where_conditions ); // Attempt to query meta values. $meta_list = $wpdb ->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" ); // Bail if no results found. if ( empty ( $meta_list ) || is_wp_error( $meta_list ) ) { return ; } // Setup empty cache array. $cache = array (); // Loop through metas. foreach ( $meta_list as $meta ) { $oid = $meta ->object_id; $otype = $meta ->object_type; $okey = $meta ->meta_key; $ovalue = $meta ->meta_value; // Force subkeys to be array type. if ( ! isset( $cache [ $otype ][ $oid ] ) || ! is_array ( $cache [ $otype ][ $oid ] ) ) { $cache [ $otype ][ $oid ] = array (); } if ( ! isset( $cache [ $otype ][ $oid ][ $okey ] ) || ! is_array ( $cache [ $otype ][ $oid ][ $okey ] ) ) { $cache [ $otype ][ $oid ][ $okey ] = array (); } // Add to the cache array. $cache [ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue ); } // Loop through data and cache to the appropriate object. foreach ( $cache as $object_type => $object_caches ) { // Determine the cache group for this data. $cache_group = $cache_groups [ $object_type ]; // Loop through objects and cache appropriately. foreach ( $object_caches as $object_id => $object_cache ) { wp_cache_set( $object_id , $object_cache , $cache_group ); } } } |
Changelog
Version | Description |
---|---|
BuddyPress 2.0.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.