0

我在多个组中使用 buddypress。每个用户还可以作为成员加入多个组。我使用自定义配置文件字段,例如地址。每个成员都可以选择,谁可以查看他的个人资料。此尝试基于插件“用户配置文件可见性管理器”。现在我需要一个额外的选项:查看“仅限组成员”的个人资料

我的计划是:

  1. 获取“访客”的 user_id - 适用于:

    $user_id_visitor = bp_loggedin_user_id();

  2. 获取“个人资料所有者”的 user_id - 适用于:

    $user_id_profile_owner = bp_displayed_user_id();

  3. 通过配置文件所有者的 user_id 获取组:

在这里,我尝试了很多。使用此功能,我可以打印“个人资料所有者”所属的所有组。但我不需要打印它,它只是为了测试:

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. 检查 profile_owner 组的所有成员,并检查访问者是否也是该组的成员。

我在选择框中的新选项:

<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>    

这是来自插件的代码片段,用于检查和保护用户帐户的可见性:

 /**
 * 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 ;

}
4

1 回答 1

0

我自己找到了解决方案:

//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;                                                                                    
            }
        }
于 2013-08-26T19:07:48.737 回答