user1735856
user1735856

Reputation: 281

check if member is of a group

I use buddypress with multiple groups. Every user can also join multiple groups as a member. I use custom profile fields for e.g. the address. Every member can select, who is able to view his profile. This try is based on the plugin 'User Profile Visibility Manager'. Now I need an additional option: View the profile to "Group Members Only"

My plan is:

  1. Get the user_id of the 'visitor' - works perfect with:

    $user_id_visitor = bp_loggedin_user_id();

  2. Get the user_id of the 'profile owner' - works perfect with:

    $user_id_profile_owner = bp_displayed_user_id();

  3. Get the group(s) by the profile owner´s user_id:

Here, I´ve tried a lot. With this function, I´m able to print all groups in which the 'profile owner' is a member. But I don´t need to print it, it is only for a test:

function bp_groups_profileowner( $user_id_profile_owner ) {
global $wpdb;
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT group_id FROM wp_bp_groups_members WHERE user_id = %d", $user_id_profile_owner ) );
 if ( $groups ) {
  foreach ( $groups as $group ) {
  echo  '<li>' . $group->group_id . '</li>';
  }
 }
  1. check all members of the profile_owner´s group(s) and check, if the visitor is also member the group(s).

My new option in the selectbox:

<option value="groupmembers" <?php echo selected('groupmembers',bp_profile_visibility_get_settings($user_id,'bp_profile_visibility' ));?>><?php _e('Group Members Only','bp-profile-visibility');?></option>    

This is the code snippet, coming from the plugin, which checks and protects user account visibility:

 /**
 * Checks and protects user account visibility
 * @return type 
 */

function check_profile_access(){


    if(!bp_is_user() ||  is_super_admin())
        return;
    //if we are on user profile page
    $privacy = bp_profile_visibility_get_settings(bp_displayed_user_id(), 'bp_profile_visibility');

    //if privacy is public, everyone can see
    if( 'public' == $privacy )
        return;

    $referrer=wp_get_referer();
    if($referrer)
        $referrer=bp_core_get_root_domain ();
    //in all other cases, user must be logged in
    if(!is_user_logged_in()){
        bp_core_add_message(__('Please login to view profile','bp-profile-visibility'),'error');
        wp_safe_redirect($referrer);
        exit(0);

        return ;


    }
    //if we are here, the person is logged in, let us see if the visibility is set to logged in
    if( 'loggedin' == $privacy )
        return ;

    //if this is my profile, do not prevent user
    if(bp_is_my_profile())
        return ;

    //now, since we have already tested for login , we just need to test for the friends only and me 
     if( 'friends' == $privacy && function_exists('friends_check_friendship') && friends_check_friendship(bp_displayed_user_id(), get_current_user_id()) )
                return;  


    //now, we just need to test for the group members 
    if( 'groupmembers' ... )



    //if we are here, don't show the profile


        bp_core_add_message(__('This User\'s privacy settings does not allow you to view the profile.','bp-profile-visibility'),'error');
        wp_safe_redirect($referrer);
        exit(0);

        return ;

}

Upvotes: 0

Views: 2357

Answers (1)

user1735856
user1735856

Reputation: 281

I found the solution by myself:

//now, we just need to test for the group members only and me
    if( 'groupmembers' == $privacy )
        /**
         * Check if visitor is a member of one of the profile owners groups mm
         * 
         */         
        $user_id_visitor = bp_loggedin_user_id();
        $user_id_profile_owner = bp_displayed_user_id();
        $all_groups = BP_Groups_Member::get_group_ids( $user_id_profile_owner);             
          if ( $all_groups  ) {
            foreach($all_groups[groups] AS $profile_owner_groups_id)
            {
            if (groups_is_user_member( $user_id_visitor, $profile_owner_groups_id ))
             // break if visitor is member of one of the profie owners group //
             return;                                                                                    
            }
        }

Upvotes: 0

Related Questions