bp_xprofile_get_fields_by_visibility_levels

Fetch an array of the xprofile fields that a given user has marked with certain visibility levels.

Description

(array) bp_xprofile_get_fields_by_visibility_levels( (int) $user_id, (array) $levels = array() ); 

Returns (array)

$field_ids The fields that match the requested visibility levels for the given user.

Parameters (2)

0. $user_id (int)
The id of the profile owner.
1. $levels — Optional. (array) => array()
An array of visibility levels ('public', friends,, loggedin , adminsonly etc) to be checked against.

Usage

  1. if ( !function_exists( 'bp_xprofile_get_fields_by_visibility_levels' ) ) { 
  2. require_once '/bp-xprofile/bp-xprofile-functions.php'; 
  3.  
  4. // The id of the profile owner. 
  5. $user_id = -1; 
  6.  
  7. // An array of visibility levels ('public', 'friends', 'loggedin', 'adminsonly' etc) to be 
  8. // checked against. 
  9. $levels = array(); 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bp_xprofile_get_fields_by_visibility_levels($user_id, $levels); 
  13.  

Defined (1)

The function is defined in the following location(s).

/bp-xprofile/bp-xprofile-functions.php  
  1. function bp_xprofile_get_fields_by_visibility_levels( $user_id, $levels = array() ) { 
  2. if ( !is_array( $levels ) ) { 
  3. $levels = (array)$levels; 
  4.  
  5. $user_visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); 
  6.  
  7. // Parse the user-provided visibility levels with the default levels, which may take 
  8. // precedence. 
  9. $default_visibility_levels = BP_XProfile_Group::fetch_default_visibility_levels(); 
  10.  
  11. foreach( (array) $default_visibility_levels as $d_field_id => $defaults ) { 
  12. // If the admin has forbidden custom visibility levels for this field, replace 
  13. // the user-provided setting with the default specified by the admin. 
  14. if ( isset( $defaults['allow_custom'] ) && isset( $defaults['default'] ) && 'disabled' == $defaults['allow_custom'] ) { 
  15. $user_visibility_levels[$d_field_id] = $defaults['default']; 
  16.  
  17. $field_ids = array(); 
  18. foreach( (array) $user_visibility_levels as $field_id => $field_visibility ) { 
  19. if ( in_array( $field_visibility, $levels ) ) { 
  20. $field_ids[] = $field_id; 
  21.  
  22. // Never allow the fullname field to be excluded. 
  23. if ( in_array( 1, $field_ids ) ) { 
  24. $key = array_search( 1, $field_ids ); 
  25. unset( $field_ids[$key] ); 
  26.  
  27. return $field_ids;