检查成员是否属于某个组

时间:2013-08-25 20:47:07

标签: wordpress buddypress

我使用具有多个组的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获取群组:

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

    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组的所有成员,并检查访问者是否也是组的成员。
    2. 我在选择框中的新选项:

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

1 个答案:

答案 0 :(得分: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;                                                                                    
            }
        }