BP_XProfile_Field_Type_WordPress::get_field_value( integer $user_id, integer $field_id )

Gets the WordPress field value during an xProfile fields loop.


Description Description

This function is used inside BP_XProfile_ProfileData::get_data_for_user() to include the WordPress field value into the xProfile fields loop.


Top ↑

Parameters Parameters

$user_id

(integer) (Required) The user ID.

$field_id

(integer) (Required) The xProfile field ID.


Top ↑

Return Return

(array) An array containing the metadata id, value and table_name.


Source Source

File: bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php

	public function get_field_value( $user_id, $field_id = 0 ) {
		global $wpdb;
		$wp_field = array(
			'id'         => 0,
			'value'      => '',
			'table_name' => $wpdb->usermeta,
		);

		if ( 'user_url' === $this->wp_user_key ) {
			if ( bp_displayed_user_id() ) {
				$wp_field['value'] = bp_get_displayed_user()->userdata->{$this->wp_user_key};
			} elseif ( $user_id ) {
				$user = get_user_by( 'id', $user_id );
				$wp_field['value'] = $user->{$this->wp_user_key};
			}

			$wp_field['id']         = $user_id;
			$wp_field['table_name'] = $wpdb->users;
		} else {
			$umeta_key = $this->wp_user_key;
			$user_mid  = wp_cache_get( $user_id, 'bp_user_mid' );
			if ( ! $user_mid ) {
				$user_mid = array();
			}

			if ( ! $user_mid ) {
				$list_values = bp_get_user_meta( $user_id, $umeta_key );

				if ( is_array( $list_values ) ) {
					$wp_field['value'] = reset( $list_values );
					$wp_field['id']    = key( $list_values );

					if ( 0 === $wp_field['id'] ) {
						/*
						* We can't just update the WP User Meta cache to key again meta values with meta_ids because of
						* `return maybe_unserialize( $meta_cache[ $meta_key ][0] );` in `get_metadata_raw()`.
						*/
						$user_meta_cache = wp_cache_get( $user_id, 'user_meta' );

						if ( $user_meta_cache ) {
							$metas = $wpdb->get_results( $wpdb->prepare( "SELECT umeta_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE user_id = %d ORDER BY umeta_id ASC", $user_id ) );

							if ( $metas ) {
								foreach ( $user_meta_cache as $meta_key => $meta_values ) {
									if ( ! in_array( $meta_key, $this->supported_keys, true ) ) {
										continue;
									}

									foreach ( $meta_values as $meta_value ) {
										$meta_object = wp_list_filter( $metas, array( 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );

										if ( 1 === count( $meta_object ) ) {
											$meta_object = reset( $meta_object );
											$user_mid[ $meta_key ][ $meta_object->umeta_id ] = $meta_value;

											// Set the meta_id for the requested field.
											if ( $umeta_key === $meta_key ) {
												$wp_field['id'] = $meta_object->umeta_id;
											}
										}
									}
								}
							}

							// Set the User mid cache.
							wp_cache_set( $user_id, $user_mid, 'bp_user_mid' );
						}
					}
				}
			}

			if ( isset( $user_mid[ $umeta_key ] ) ) {
				$wp_field['value'] = reset( $user_mid[ $umeta_key ] );
				$wp_field['id']    = key( $user_mid[ $umeta_key ] );
			}
		}

		return $wp_field;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
8.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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