Changeset 10163 for trunk/src/bp-xprofile/bp-xprofile-cache.php
- Timestamp:
- 10/01/2015 04:18:13 AM (10 years ago)
- File:
-
- 1 edited
-
trunk/src/bp-xprofile/bp-xprofile-cache.php (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/bp-xprofile-cache.php
r10140 r10163 18 18 * @since 2.2.0 19 19 * 20 * @param int $user_id User ID to check 20 * @param int $user_id User ID to check. 21 21 * @param array $field_ids XProfile field IDs. 22 * 22 23 * @return array 23 24 */ … … 51 52 * 52 53 * @param array $object_ids Multi-dimensional array of object_ids, keyed by 53 * object type ('group', 'field', 'data') 54 * object type ('group', 'field', 'data'). 55 * 56 * @return bool 54 57 */ 55 58 function bp_xprofile_update_meta_cache( $object_ids = array() ) { 56 59 global $wpdb; 57 60 58 // Bail if no objects 61 // Bail if no objects. 59 62 if ( empty( $object_ids ) ) { 60 63 return false; … … 63 66 $bp = buddypress(); 64 67 65 // Define the array where uncached object IDs will be stored 68 // Define the array where uncached object IDs will be stored. 66 69 $uncached_object_ids = array( 67 70 'group', … … 70 73 ); 71 74 72 // Define the cache groups for the 3 types of XProfile metadata 75 // Define the cache groups for the 3 types of XProfile metadata. 73 76 $cache_groups = array( 74 77 'group' => 'xprofile_group_meta', … … 77 80 ); 78 81 79 // No reason to query yet 82 // No reason to query yet. 80 83 $do_query = false; 81 84 82 // Loop through object types and look for uncached data 85 // Loop through object types and look for uncached data. 83 86 foreach ( $uncached_object_ids as $object_type ) { 84 87 85 // Skip if empty object type 88 // Skip if empty object type. 86 89 if ( empty( $object_ids[ $object_type ] ) ) { 87 90 continue; 88 91 } 89 92 90 // Sanitize $object_ids passed to the function 93 // Sanitize $object_ids passed to the function. 91 94 $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] ); 92 95 93 // Get non-cached IDs for each object type 96 // Get non-cached IDs for each object type. 94 97 $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] ); 95 98 96 // Set the flag to do the meta query 99 // Set the flag to do the meta query. 97 100 if ( ! empty( $uncached_object_ids[ $object_type ] ) && ( false === $do_query ) ) { 98 101 $do_query = true; … … 100 103 } 101 104 102 // Bail if no uncached items 105 // Bail if no uncached items. 103 106 if ( false === $do_query ) { 104 107 return; 105 108 } 106 109 107 // Setup where conditions for query 110 // Setup where conditions for query. 108 111 $where_sql = ''; 109 112 $where_conditions = array(); 110 113 111 // Loop through uncached objects and prepare to query for them 114 // Loop through uncached objects and prepare to query for them. 112 115 foreach ( $uncached_object_ids as $otype => $oids ) { 113 116 114 // Skip empty object IDs 117 // Skip empty object IDs. 115 118 if ( empty( $oids ) ) { 116 119 continue; 117 120 } 118 121 119 // Compile WHERE query conditions for uncached metadata 122 // Compile WHERE query conditions for uncached metadata. 120 123 $oids_sql = implode( ',', wp_parse_id_list( $oids ) ); 121 124 $where_conditions[] = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype ); 122 125 } 123 126 124 // Bail if no where conditions 127 // Bail if no where conditions. 125 128 if ( empty( $where_conditions ) ) { 126 129 return; 127 130 } 128 131 129 // Setup the WHERE query part 132 // Setup the WHERE query part. 130 133 $where_sql = implode( " OR ", $where_conditions ); 131 134 132 // Attempt to query meta values 135 // Attempt to query meta values. 133 136 $meta_list = $wpdb->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" ); 134 137 135 // Bail if no results found 138 // Bail if no results found. 136 139 if ( empty( $meta_list ) || is_wp_error( $meta_list ) ) { 137 140 return; 138 141 } 139 142 140 // Setup empty cache array 143 // Setup empty cache array. 141 144 $cache = array(); 142 145 143 // Loop through metas 146 // Loop through metas. 144 147 foreach ( $meta_list as $meta ) { 145 148 $oid = $meta->object_id; … … 148 151 $ovalue = $meta->meta_value; 149 152 150 // Force subkeys to be array type 153 // Force subkeys to be array type. 151 154 if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) { 152 155 $cache[ $otype ][ $oid ] = array(); … … 157 160 } 158 161 159 // Add to the cache array 162 // Add to the cache array. 160 163 $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue ); 161 164 } 162 165 163 // Loop through data and cache to the appropriate object 166 // Loop through data and cache to the appropriate object. 164 167 foreach ( $cache as $object_type => $object_caches ) { 165 168 166 // Determine the cache group for this data 169 // Determine the cache group for this data. 167 170 $cache_group = $cache_groups[ $object_type ]; 168 171 169 // Loop through objects and cache appropriately 172 // Loop through objects and cache appropriately. 170 173 foreach ( $object_caches as $object_id => $object_cache ) { 171 174 wp_cache_set( $object_id, $object_cache, $cache_group ); … … 175 178 176 179 /** 177 * Clear cached XProfile field group data 180 * Clear cached XProfile field group data. 178 181 * 179 182 * @since 2.1.0 180 183 * 181 * @param object $group_obj 184 * @param object $group_obj Groub object to clear. 182 185 */ 183 186 function xprofile_clear_profile_groups_object_cache( $group_obj ) { … … 189 192 190 193 /** 191 * Clear cached XProfile fullname data for user 194 * Clear cached XProfile fullname data for user. 192 195 * 193 196 * @since 2.1.0 194 197 * 195 * @param int $user_id ID of user whose fullname cache to delete 198 * @param int $user_id ID of user whose fullname cache to delete. 196 199 */ 197 200 function xprofile_clear_profile_data_object_cache( $user_id = 0 ) { … … 203 206 * Clear the fullname cache when field 1 is updated. 204 207 * 205 * xprofile_clear_profile_data_object_cache() will make this redundant in most 206 * cases, except where the field is updated directly with xprofile_set_field_data() 207 * 208 * @since 2.0.0 208 * The xprofile_clear_profile_data_object_cache() will make this redundant in most 209 * cases, except where the field is updated directly with xprofile_set_field_data(). 210 * 211 * @since 2.0.0 212 * 213 * @param object $data Data object to clear. 209 214 */ 210 215 function xprofile_clear_fullname_cache_on_profile_field_edit( $data ) { … … 220 225 * @since 2.0.0 221 226 * 222 * @param BP_XProfile_Field 227 * @param BP_XProfile_Field $field_obj Field object cache to delete. 223 228 */ 224 229 function xprofile_clear_profile_field_object_cache( $field_obj ) { 225 230 226 // Clear default visibility level cache 231 // Clear default visibility level cache. 227 232 wp_cache_delete( 'default_visibility_levels', 'bp_xprofile' ); 228 233 229 234 // Modified fields can alter parent group status, in particular when 230 235 // the group goes from empty to non-empty. Bust its cache, as well as 231 // the global 'all' cache 236 // the global 'all' cache. 232 237 wp_cache_delete( 'all', 'bp_xprofile_groups' ); 233 238 wp_cache_delete( $field_obj->group_id, 'bp_xprofile_groups' ); … … 251 256 * @since 2.0.0 252 257 * 253 * @param BP_XProfile_ProfileData $data_obj 258 * @param BP_XProfile_ProfileData $data_obj Field data object to delete. 254 259 */ 255 260 function xprofile_clear_profiledata_object_cache( $data_obj ) { … … 279 284 add_action( 'update_option_bp-xprofile-fullname-field-name', 'xprofile_clear_fullname_field_id_cache' ); 280 285 281 // List actions to clear super cached pages on, if super cache is installed 286 // List actions to clear super cached pages on, if super cache is installed. 282 287 add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
Note: See TracChangeset
for help on using the changeset viewer.