I finally managed to make it work. Here are my functions if it can help somebody else (it can be easily adapted for your own use) :
function hide_profile_template() {
global $bp;
/* Prevent access to the moderated users profiles from everyone except the moderated user themselves and the administrators (IDs "1" and "4") */
$pending_users = bp_registration_get_pending_users();
foreach( $pending_users as $pending_user ) {
$pending_user = $pending_user->user_id;
if((bp_is_profile && $bp->displayed_user->id == $pending_user && $bp->loggedin_user->id != $pending_user && $bp->loggedin_user->id !=1 && $bp->loggedin_user->id !=4)) :
wp_redirect( home_url() );
exit;
endif;
}
}
add_action( 'wp', 'hide_profile_template', 1 );
/* hiding moderated users (and admins) on the members listing */
function bpdev_exclude_users($qs=false,$object=false){
// Admins
$excluded_user='1,4';
// Moderated users
$pending_users = bp_registration_get_pending_users();
foreach( $pending_users as $pending_user ) {
$pending_user = $pending_user->user_id;
$excluded_user .= ',';
$excluded_user .= $pending_user;
}
if($object != 'members') //hiding from members
return $qs;
$args=wp_parse_args($qs);
//except for friends
if(!empty($args['user_id']))
return $qs;
if(!empty($args['exclude']))
$args['exclude'] = $args['exclude'].','.$excluded_user;
else
$args['exclude'] = $excluded_user;
$qs = build_query($args);
return $qs;
}
add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
/* We also need to recount the members */
function bpfr_hide_get_total_filter($count){
// Counting the number of moderated users
$pending_users = bp_registration_get_pending_users();
$nb_pending_users = 0;
foreach( $pending_users as $pending_user ) {
$nb_pending_users += 1;
}
// minus the two admins
return $count-2-$nb_pending_users;
}
add_filter('bp_get_total_member_count','bpfr_hide_get_total_filter');