BP_XProfile_Group::get( array $args = array() )

Populates the BP_XProfile_Group object with profile field groups, fields, and field data.


Parameters Parameters

$args

(array) (Optional) Array of optional arguments:

  • 'profile_group_id'
    (int) Limit results to a single profile group.
  • 'user_id'
    (int) Required if you want to load a specific user's data. Default: displayed user's ID.
  • 'member_type'
    (array|string) Limit fields by those restricted to a given member type, or array of member types. If $user_id is provided, the value of $member_type will be overridden by the member types of the provided user. The special value of 'any' will return only those fields that are unrestricted by member type
    • i.e., those applicable to any type.
  • 'hide_empty_groups'
    (bool) True to hide groups that don't have any fields. Default: false.
  • 'hide_empty_fields'
    (bool) True to hide fields where the user has not provided data. Default: false.
  • 'fetch_fields'
    (bool) Whether to fetch each group's fields. Default: false.
  • 'fetch_field_data'
    (bool) Whether to fetch data for each field. Requires a $user_id. Default: false.
  • 'exclude_groups'
    (int[]|bool) Comma-separated list or array of group IDs to exclude.
  • 'exclude_fields'
    (int[]|bool) Comma-separated list or array of field IDs to exclude.
  • 'hide_field_types'
    (string[]) List of field types to hide form loop. Default: empty array.
  • 'signup_fields_only'
    (bool) Whether to only return signup fields. Default: false.
  • 'update_meta_cache'
    (bool) Whether to pre-fetch xprofilemeta for all retrieved groups, fields, and data. Default: true.

Default value: array()


Top ↑

Return Return

(array) $groups


Source Source

File: bp-xprofile/classes/class-bp-xprofile-group.php

	public static function get( $args = array() ) {
		global $wpdb;

		// Parse arguments.
		$r = wp_parse_args( $args, array(
			'profile_group_id'       => false,
			'user_id'                => bp_displayed_user_id(),
			'member_type'            => false,
			'hide_empty_groups'      => false,
			'hide_empty_fields'      => false,
			'fetch_fields'           => false,
			'fetch_field_data'       => false,
			'fetch_visibility_level' => false,
			'exclude_groups'         => false,
			'exclude_fields'         => false,
			'hide_field_types'       => array(),
			'update_meta_cache'      => true,
			'signup_fields_only'     => false,
		) );

		// Keep track of object IDs for cache-priming.
		$object_ids = array(
			'group' => array(),
			'field' => array(),
			'data'  => array(),
		);

		$bp = buddypress();

		$group_ids = self::get_group_ids( $r );

		// Get all group data.
		$groups = self::get_group_data( $group_ids );

		// Bail if not also getting fields.
		if ( empty( $r['fetch_fields'] ) ) {
			return $groups;
		}

		// Get the group ids from the groups we found.
		$group_ids = wp_list_pluck( $groups, 'id' );

		// Store for meta cache priming.
		$object_ids['group'] = $group_ids;

		// Bail if no groups found.
		if ( empty( $group_ids ) ) {
			return $groups;
		}

		$field_ids = self::get_group_field_ids( $group_ids, $r );

		foreach( $groups as $group ) {
			$group->fields = array();
		}

		// Bail if no fields.
		if ( empty( $field_ids ) ) {
			return $groups;
		}

		$field_ids = array_map( 'intval', $field_ids );

		// Prime the field cache.
		$uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
		if ( ! empty( $uncached_field_ids ) ) {
			$_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
			$uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
			foreach ( $uncached_fields as $uncached_field ) {
				$fid = intval( $uncached_field->id );
				wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
			}
		}

		// Pull field objects from the cache.
		$fields = array();
		foreach ( $field_ids as $field_id ) {
			if ( true === $r['signup_fields_only'] && ! in_array( $field_id, bp_xprofile_get_signup_field_ids(), true ) ) {
				continue;
			}

			$_field = xprofile_get_field( $field_id, null, false );

			if ( in_array( $_field->type, $r['hide_field_types'], true ) ) {
				continue;
			}

			$fields[] = $_field;
		}

		// Store field IDs for meta cache priming.
		$object_ids['field'] = $field_ids;

		// Maybe fetch field data.
		if ( ! empty( $r['fetch_field_data'] ) ) {
			$field_type_objects = wp_list_pluck( $fields, 'type_obj', 'id' );

			// Get field data for user ID.
			if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
				$field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids, $field_type_objects );
			}

			// Remove data-less fields, if necessary.
			if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {

				// Loop through the results and find the fields that have data.
				foreach( (array) $field_data as $data ) {

					// Empty fields may contain a serialized empty array.
					$maybe_value = maybe_unserialize( $data->value );

					// Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
					if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {

						// Fields that have data get removed from the list.
						unset( $field_ids[ $key ] );
					}
				}

				// The remaining members of $field_ids are empty. Remove them.
				foreach( $fields as $field_key => $field ) {
					if ( in_array( $field->id, $field_ids ) ) {
						unset( $fields[ $field_key ] );
					}
				}

				// Reset indexes.
				$fields = array_values( $fields );
			}

			// Field data was found.
			if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {

				// Loop through fields.
				foreach( (array) $fields as $field_key => $field ) {

					// Loop through the data in each field.
					foreach( (array) $field_data as $data ) {

						// Assign correct data value to the field.
						if ( $field->id == $data->field_id ) {
							$fields[ $field_key ]->data        = new stdClass;
							$fields[ $field_key ]->data->value = $data->value;
							$fields[ $field_key ]->data->id    = $data->id;
						}

						// Store for meta cache priming.
						$object_ids['data'][] = $data->id;
					}
				}
			}
		}

		// Prime the meta cache, if necessary.
		if ( ! empty( $r['update_meta_cache'] ) ) {
			bp_xprofile_update_meta_cache( $object_ids );
		}

		// Maybe fetch visibility levels.
		if ( ! empty( $r['fetch_visibility_level'] ) ) {
			$fields = self::fetch_visibility_level( $r['user_id'], $fields );
		}

		// Merge the field array back in with the group array.
		foreach( (array) $groups as $group ) {
			// Indexes may have been shifted after previous deletions, so we get a
			// fresh one each time through the loop.
			$index = array_search( $group, $groups );

			foreach( (array) $fields as $field ) {
				if ( $group->id === $field->group_id ) {
					$groups[ $index ]->fields[] = $field;
				}
			}

			// When we unset fields above, we may have created empty groups.
			// Remove them, if necessary.
			if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) {
				unset( $groups[ $index ] );
			}

			// Reset indexes.
			$groups = array_values( $groups );
		}

		return $groups;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
8.0.0 Introduced $hide_field_types & $signup_fields_only arguments.
2.4.0 Introduced $member_type argument.
1.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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