BP_XProfile_ProfileData::get_data_for_user( int $user_id, array $field_ids )
Get a user’s profile data for a set of fields.
Description
Parameters
- $user_id
-
(Required) ID of user whose data is being queried.
- $field_ids
-
(Required) Array of field IDs to query for.
Return
(array)
Source
File: bp-xprofile/classes/class-bp-xprofile-profiledata.php
public static function get_data_for_user( $user_id, $field_ids ) {
global $wpdb;
$data = array();
$uncached_field_ids = bp_xprofile_get_non_cached_field_ids( $user_id, $field_ids );
// Prime the cache.
if ( ! empty( $uncached_field_ids ) ) {
$bp = buddypress();
$uncached_field_ids_sql = implode( ',', wp_parse_id_list( $uncached_field_ids ) );
$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 ) );
// Rekey.
$queried_data = array();
foreach ( $uncached_data as $ud ) {
$d = new stdClass;
$d->id = $ud->id;
$d->user_id = $ud->user_id;
$d->field_id = $ud->field_id;
$d->value = $ud->value;
$d->last_updated = $ud->last_updated;
$queried_data[ $ud->field_id ] = $d;
}
// Set caches.
foreach ( $uncached_field_ids as $field_id ) {
$cache_key = "{$user_id}:{$field_id}";
// If a value was found, cache it.
if ( isset( $queried_data[ $field_id ] ) ) {
wp_cache_set( $cache_key, $queried_data[ $field_id ], 'bp_xprofile_data' );
// If no value was found, cache an empty item
// to avoid future cache misses.
} else {
$d = new stdClass;
$d->id = '';
$d->user_id = $user_id;
$d->field_id = $field_id;
$d->value = '';
$d->last_updated = '';
wp_cache_set( $cache_key, $d, 'bp_xprofile_data' );
}
}
}
// Now that all items are cached, fetch them.
foreach ( $field_ids as $field_id ) {
$cache_key = "{$user_id}:{$field_id}";
$data[] = wp_cache_get( $cache_key, 'bp_xprofile_data' );
}
// Integer casting.
foreach ( (array) $data as $key => $d ) {
if ( isset( $data[ $key ]->id ) ) {
$data[ $key ]->id = (int) $data[ $key ]->id;
}
if ( isset( $data[ $key ]->user_id ) ) {
$data[ $key ]->user_id = (int) $data[ $key ]->user_id;
}
$data[ $key ]->field_id = (int) $data[ $key ]->field_id;
}
return $data;
}
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.