BP_XProfile_ProfileData::get_data_for_user( int $user_id, array $field_ids, array $field_type_objects = array() )

Get a user’s profile data for a set of fields.


Description Description


Parameters Parameters

$user_id

(Required) ID of user whose data is being queried.

$field_ids

(Required) Array of field IDs to query for.

$field_type_objects

(Optional) Array of field type objects keyed by the queried filed IDs.

Default value: array()


Top ↑

Return Return

(array)


Top ↑

Source 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;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
8.0.0 Checks if a null field data is an xProfile WP Field. Adds a new parameter $field_type_objects to pass the list of field type objects.
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.