- Timestamp:
- 10/01/2015 04:18:13 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/classes/class-bp-xprofile-profiledata.php
r10142 r10163 1 1 <?php 2 2 /** 3 * BuddyPress XProfile Classes 3 * BuddyPress XProfile Classes. 4 4 * 5 5 * @package BuddyPress … … 23 23 } 24 24 25 public function populate( $field_id, $user_id ) {25 public function populate( $field_id, $user_id ) { 26 26 global $wpdb; 27 27 … … 48 48 49 49 } else { 50 // When no row is found, we'll need to set these properties manually 50 // When no row is found, we'll need to set these properties manually. 51 51 $this->field_id = $field_id; 52 52 $this->user_id = $user_id; … … 64 64 global $wpdb; 65 65 66 // Check cache first 66 // Check cache first. 67 67 $cache_key = "{$this->user_id}:{$this->field_id}"; 68 68 $cached = wp_cache_get( $cache_key, 'bp_xprofile_data' ); … … 100 100 101 101 /** 102 * Filters whether or not data is for a valid field 102 * Filters whether or not data is for a valid field. 103 103 * 104 104 * @since 1.2.7 … … 164 164 165 165 /** 166 * Delete specific XProfile field data 166 * Delete specific XProfile field data. 167 167 * 168 168 * @global object $wpdb … … 205 205 * Get a user's profile data for a set of fields. 206 206 * 207 * @param int $user_id 208 * @param array $field_ids 207 * @param int $user_id ID of user whose data is being queried. 208 * @param array $field_ids Array of field IDs to query for. 209 * 209 210 * @return array 210 211 */ … … 216 217 $uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids ); 217 218 218 // Prime the cache 219 // Prime the cache. 219 220 if ( ! empty( $uncached_field_ids ) ) { 220 221 $bp = buddypress(); … … 222 223 $uncached_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id IN ({$uncached_field_ids_sql}) AND user_id = %d", $user_id ) ); 223 224 224 // Rekey 225 // Rekey. 225 226 $queried_data = array(); 226 227 foreach ( $uncached_data as $ud ) { … … 235 236 } 236 237 237 // Set caches 238 // Set caches. 238 239 foreach ( $uncached_field_ids as $field_id ) { 239 240 240 241 $cache_key = "{$user_id}:{$field_id}"; 241 242 242 // If a value was found, cache it 243 // If a value was found, cache it. 243 244 if ( isset( $queried_data[ $field_id ] ) ) { 244 245 wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' ); 245 246 246 247 // If no value was found, cache an empty item 247 // to avoid future cache misses 248 // to avoid future cache misses. 248 249 } else { 249 250 $d = new stdClass; … … 259 260 } 260 261 261 // Now that all items are cached, fetch them 262 // Now that all items are cached, fetch them. 262 263 foreach ( $field_ids as $field_id ) { 263 264 $cache_key = "{$user_id}:{$field_id}"; … … 272 273 * 273 274 * @param int $user_id ID of the user. 275 * 274 276 * @return array 275 277 */ … … 316 318 * Get the user's field data id by the id of the xprofile field. 317 319 * 318 * @param int $field_id 319 * @param int $user_id 320 * @param int $field_id Field ID being queried for. 321 * @param int $user_id User ID associated with field. 322 * 320 323 * @return int $fielddata_id 321 324 */ … … 328 331 $bp = buddypress(); 329 332 330 // Check cache first 333 // Check cache first. 331 334 $cache_key = "{$user_id}:{$field_id}"; 332 335 $fielddata = wp_cache_get( $cache_key, 'bp_xprofile_data' ); … … 346 349 * Supports multiple user IDs. 347 350 * 348 * @param int $field_id ID of the field. 349 * @param int|array $user_ids ID or IDs of user(s). 351 * @param int $field_id ID of the field. 352 * @param int|array|null $user_ids ID or IDs of user(s). 353 * 350 354 * @return string|array Single value if a single user is queried, 351 * otherwise an array of results.355 * otherwise an array of results. 352 356 */ 353 357 public static function get_value_byid( $field_id, $user_ids = null ) { … … 364 368 } 365 369 366 // Assemble uncached IDs 370 // Assemble uncached IDs. 367 371 $uncached_ids = array(); 368 372 foreach ( $user_ids as $user_id ) { … … 373 377 } 374 378 375 // Prime caches 379 // Prime caches. 376 380 if ( ! empty( $uncached_ids ) ) { 377 381 $bp = buddypress(); … … 379 383 $queried_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$uncached_ids_sql})", $field_id ) ); 380 384 381 // Rekey 385 // Rekey. 382 386 $qd = array(); 383 387 foreach ( $queried_data as $data ) { … … 386 390 387 391 foreach ( $uncached_ids as $id ) { 388 // The value was successfully fetched 392 // The value was successfully fetched. 389 393 if ( isset( $qd[ $id ] ) ) { 390 394 $d = $qd[ $id ]; 391 395 392 396 // No data found for the user, so we fake it to 393 // avoid cache misses and PHP notices 397 // avoid cache misses and PHP notices. 394 398 } else { 395 399 $d = new stdClass; … … 406 410 } 407 411 408 // Now that the cache is primed with all data, fetch it 412 // Now that the cache is primed with all data, fetch it. 409 413 $data = array(); 410 414 foreach ( $user_ids as $user_id ) { … … 413 417 } 414 418 415 // If a single ID was passed, just return the value 419 // If a single ID was passed, just return the value. 416 420 if ( $is_single ) { 417 421 return $data[0]->value; 418 422 419 // Otherwise return the whole array 423 // Otherwise return the whole array. 420 424 } else { 421 425 return $data;
Note: See TracChangeset
for help on using the changeset viewer.