Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/15/2012 08:33:09 PM (14 years ago)
Author:
boonebgorges
Message:

First pass at per-field visibility/privacy for XProfile:

  • Allows admins to set default levels for specific fields
  • Allows users to set visibility/privacy on a field-by-field basis
  • Modifies the profile templates to show markup for editing visibility status

See #3695

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r5729 r5789  
    251251}
    252252
     253/**
     254 * Set the privacy level for this field
     255 *
     256 * @param int $field_id The ID of the xprofile field
     257 * @param int $user_id The ID of the user to whom the data belongs
     258 * @param string $privacy_level
     259 * @return bool True on success
     260 */
     261function xprofile_set_field_privacy_level( $field_id = 0, $user_id = 0, $privacy_level = '' ) {
     262    if ( empty( $field_id ) || empty( $user_id ) || empty( $privacy_level ) ) {
     263        return false;
     264    }
     265   
     266    // Get the fielddata id
     267    $fielddata_id = BP_XProfile_ProfileData::get_fielddataid_byid( $field_id, $user_id );
     268   
     269    if ( empty( $fielddata_id ) ) {
     270        return false;
     271    }
     272   
     273    // Check against a whitelist
     274    $allowed_values = bp_xprofile_get_privacy_levels();
     275    if ( !array_key_exists( $privacy_level, $allowed_values ) ) {
     276        return false;
     277    }
     278   
     279    // Stored in an array in usermeta
     280    $current_privacy_levels = get_user_meta( $user_id, 'bp_xprofile_privacy_levels', true );
     281   
     282    if ( !$current_privacy_levels ) {
     283        $current_privacy_levels = array();
     284    }
     285   
     286    $current_privacy_levels[$field_id] = $privacy_level;
     287   
     288    return update_user_meta( $user_id, 'bp_xprofile_privacy_levels', $current_privacy_levels );
     289}
     290
    253291function xprofile_delete_field_data( $field, $user_id ) {
    254292    if ( is_numeric( $field ) )
     
    606644}
    607645
     646/**
     647 * Get privacy levels out of the $bp global
     648 *
     649 * @return array
     650 */
     651function bp_xprofile_get_privacy_levels() {
     652    global $bp;
     653   
     654    return apply_filters( 'bp_xprofile_get_privacy_levels', $bp->profile->privacy_levels );
     655}
     656
     657/**
     658 * Get the ids of fields that are hidden for this displayed/loggedin user pair
     659 *
     660 * This is the function primarily responsible for profile field privacy. It works by determining
     661 * the relationship between the displayed_user (ie the profile owner) and the current_user (ie the
     662 * profile viewer). Then, based on that relationship, we query for the set of fields that should
     663 * be excluded from the profile loop.
     664 *
     665 * @since 1.6
     666 * @see BP_XProfile_Group::get()
     667 * @uses apply_filters() Filter bp_xprofile_get_hidden_fields_for_user to modify privacy levels,
     668 *   or if you have added your own custom levels
     669 *
     670 * @param int $displayed_user_id The id of the user the profile fields belong to
     671 * @param int $current_user_id The id of the user viewing the profile
     672 * @return array An array of field ids that should be excluded from the profile query
     673 */
     674function bp_xprofile_get_hidden_fields_for_user( $displayed_user_id = 0, $current_user_id = 0 ) {
     675    if ( !$displayed_user_id ) {
     676        $displayed_user_id = bp_displayed_user_id();
     677    }
     678   
     679    if ( !$displayed_user_id ) {
     680        return array();
     681    }
     682   
     683    if ( !$current_user_id ) {
     684        $current_user_id = bp_loggedin_user_id();
     685    }
     686   
     687    // @todo - This is where you'd swap out for current_user_can() checks
     688   
     689    if ( $current_user_id ) {
     690        // Current user is logged in
     691        if ( $displayed_user_id == $current_user_id ) {
     692            // If you're viewing your own profile, nothing's private
     693            $hidden_fields = array();   
     694           
     695        } else if ( bp_is_active( 'friends' ) && friends_check_friendship( $displayed_user_id, $current_user_id ) ) {
     696            // If the current user and displayed user are friends, show all
     697            $hidden_fields = array();
     698           
     699        } else {
     700            // current user is logged-in but not friends, so exclude friends-only   
     701            $hidden_levels = array( 'friends' );           
     702            $hidden_fields = bp_xprofile_get_fields_by_privacy_levels( $displayed_user_id, $hidden_levels );
     703        }
     704       
     705    } else {
     706        // Current user is not logged in, so exclude friends-only and loggedin
     707        $hidden_levels = array( 'friends', 'loggedin' );
     708        $hidden_fields = bp_xprofile_get_fields_by_privacy_levels( $displayed_user_id, $hidden_levels );
     709    }
     710   
     711    return apply_filters( 'bp_xprofile_get_hidden_fields_for_user', $hidden_fields, $displayed_user_id, $current_user_id );
     712}
     713
     714/**
     715 * Fetch an array of the xprofile fields that a given user has marked with certain privacy levels
     716 *
     717 * @since 1.6
     718 * @see bp_xprofile_get_hidden_fields_for_user()
     719 *
     720 * @param int $user_id The id of the profile owner
     721 * @param array $levels An array of privacy levels ('public', 'friends', 'loggedin', etc) to be
     722 *    checked against
     723 * @return array $field_ids The fields that match the requested privacy levels for the given user
     724 */
     725function bp_xprofile_get_fields_by_privacy_levels( $user_id, $levels = array() ) {
     726    if ( !is_array( $levels ) ) {
     727        $levels = (array)$levels;
     728    }
     729   
     730    $user_privacy_levels = get_user_meta( $user_id, 'bp_xprofile_privacy_levels', true );
     731   
     732    $field_ids = array();
     733    foreach( (array)$user_privacy_levels as $field_id => $field_privacy ) {
     734        if ( in_array( $field_privacy, $levels ) ) {
     735            $field_ids[] = $field_id;
     736        }
     737    }
     738   
     739    // Never allow the fullname field to be excluded
     740    if ( in_array( 1, $field_ids ) ) {
     741        $key = array_search( 1, $field_ids );
     742        unset( $field_ids[$key] );
     743    }
     744   
     745    return $field_ids;
     746}
     747
     748
    608749?>
Note: See TracChangeset for help on using the changeset viewer.