Changeset 7914
- Timestamp:
- 02/17/2014 07:42:03 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-xprofile/bp-xprofile-cache.php
r7812 r7914 2 2 3 3 /** 4 * BuddyPress XProfile Template Tags4 * BuddyPress XProfile Caching Functions 5 5 * 6 6 * Caching functions handle the clearing of cached objects and pages on specific … … 8 8 * 9 9 * @package BuddyPress 10 * @subpackage XProfileTemplate11 10 */ 12 11 13 12 // Exit if accessed directly 14 13 if ( !defined( 'ABSPATH' ) ) exit; 14 15 /** 16 * Slurp up xprofilemeta for a specified set of profile objects. 17 * 18 * We do not use bp_update_meta_cache() for the xprofile component. This is 19 * because the xprofile component has three separate object types (group, 20 * field, and data) and three corresponding cache groups. Using the technique 21 * in bp_update_meta_cache(), pre-fetching would take three separate database 22 * queries. By grouping them together, we can reduce the required queries to 23 * one. 24 * 25 * This function is called within a bp_has_profile() loop. 26 * 27 * @since BuddyPress (2.0.0) 28 * 29 * @param array $object_ids Multi-dimensional array of object_ids, keyed by 30 * object type ('group', 'field', 'data') 31 */ 32 function bp_xprofile_update_meta_cache( $object_ids = array(), $user_id = 0 ) { 33 global $wpdb; 34 35 if ( empty( $object_ids ) ) { 36 return false; 37 } 38 39 // $object_ids is a multi-dimensional array 40 $uncached_object_ids = array( 41 'group' => array(), 42 'field' => array(), 43 'data' => array(), 44 ); 45 46 $cache_groups = array( 47 'group' => 'xprofile_group_meta', 48 'field' => 'xprofile_field_meta', 49 'data' => 'xprofile_data_meta', 50 ); 51 52 $do_query = false; 53 foreach ( $uncached_object_ids as $object_type => $uncached_object_type_ids ) { 54 if ( ! empty( $object_ids[ $object_type ] ) ) { 55 // Sanitize $object_ids passed to the function 56 $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] ); 57 58 // Get non-cached IDs for each object type 59 $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] ); 60 61 // Set the flag to do the meta query 62 if ( ! empty( $uncached_object_ids[ $object_type ] ) && ! $do_query ) { 63 $do_query = true; 64 } 65 } 66 } 67 68 // If there are uncached items, go ahead with the query 69 if ( $do_query ) { 70 $where = array(); 71 foreach ( $uncached_object_ids as $otype => $oids ) { 72 if ( empty( $oids ) ) { 73 continue; 74 } 75 76 $oids_sql = implode( ',', wp_parse_id_list( $oids ) ); 77 $where[] = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype ); 78 } 79 $where_sql = implode( " OR ", $where ); 80 } 81 82 83 $bp = buddypress(); 84 $meta_list = $wpdb->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" ); 85 86 if ( ! empty( $meta_list ) ) { 87 $object_type_caches = array( 88 'group' => array(), 89 'field' => array(), 90 'data' => array(), 91 ); 92 93 foreach ( $meta_list as $meta ) { 94 $oid = $meta->object_id; 95 $otype = $meta->object_type; 96 $okey = $meta->meta_key; 97 $ovalue = $meta->meta_value; 98 99 // Force subkeys to be array type 100 if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) { 101 $cache[ $otype ][ $oid ] = array(); 102 } 103 104 if ( ! isset( $cache[ $otype ][ $oid ][ $okey ] ) || ! is_array( $cache[ $otype ][ $oid ][ $okey ] ) ) { 105 $cache[ $otype ][ $oid ][ $okey ] = array(); 106 } 107 108 // Add to the cache array 109 $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue ); 110 } 111 112 foreach ( $cache as $object_type => $object_caches ) { 113 $cache_group = $cache_groups[ $object_type ]; 114 foreach ( $object_caches as $object_id => $object_cache ) { 115 wp_cache_set( $object_id, $object_cache, $cache_group ); 116 } 117 } 118 } 119 120 return; 121 } 15 122 16 123 function xprofile_clear_profile_groups_object_cache( $group_obj ) { -
trunk/bp-xprofile/bp-xprofile-classes.php
r7820 r7914 118 118 * 'exclude_groups' - Comma-separated list of groups to exclude 119 119 * '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 120 122 * 121 123 * @return array $groups … … 133 135 'fetch_visibility_level' => false, 134 136 'exclude_groups' => false, 135 'exclude_fields' => false 137 'exclude_fields' => false, 138 'update_meta_cache' => true, 136 139 ); 137 140 138 141 $r = wp_parse_args( $args, $defaults ); 139 142 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 140 151 $where_sql = ''; 141 152 … … 162 173 } 163 174 175 // Store for meta cache priming 176 $object_ids['group'] = $group_ids; 177 164 178 $group_ids = implode( ',', (array) $group_ids ); 165 179 … … 183 197 // Fetch the fields 184 198 $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' ); 185 202 186 203 if ( empty( $fields ) ) … … 241 258 $fields[$field_key]->data->id = $data->id; 242 259 } 260 261 // Store for meta cache priming 262 $object_ids['data'][] = $data->id; 243 263 } 244 264 } … … 272 292 // Reset indexes 273 293 $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 ); 274 299 } 275 300 -
trunk/bp-xprofile/bp-xprofile-template.php
r7888 r7914 25 25 var $user_id; 26 26 27 function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false ) {27 function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false, $update_meta_cache = true ) { 28 28 $this->groups = BP_XProfile_Group::get( array( 29 29 'profile_group_id' => $profile_group_id, … … 35 35 'fetch_visibility_level' => $fetch_visibility_level, 36 36 'exclude_groups' => $exclude_groups, 37 'exclude_fields' => $exclude_fields 37 'exclude_fields' => $exclude_fields, 38 'update_meta_cache' => $update_meta_cache, 38 39 ) ); 39 40 … … 175 176 'fetch_visibility_level' => $fetch_visibility_level_default, 176 177 'exclude_groups' => false, // Comma-separated list of profile field group IDs to exclude 177 'exclude_fields' => false // Comma-separated list of profile field IDs to exclude 178 'exclude_fields' => false, // Comma-separated list of profile field IDs to exclude 179 'update_meta_cache' => true, 178 180 ); 179 181 … … 181 183 extract( $r, EXTR_SKIP ); 182 184 183 $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields, $fetch_visibility_level );185 $profile_template = new BP_XProfile_Data_Template( $user_id, $profile_group_id, $hide_empty_groups, $fetch_fields, $fetch_field_data, $exclude_groups, $exclude_fields, $hide_empty_fields, $fetch_visibility_level, $update_meta_cache ); 184 186 return apply_filters( 'bp_has_profile', $profile_template->has_groups(), $profile_template ); 185 187 }
Note: See TracChangeset
for help on using the changeset viewer.