Created
April 22, 2022 15:59
-
-
Save KaineLabs/a17876d2cd1ae49f826e55d8e8522f40 to your computer and use it in GitHub Desktop.
Youzify - Restrict BuddyPress Groups Access by Group Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Youzify - Restrict BuddyPress Groups Access by Group Type | |
* */ | |
function yzc_set_restriction_by_group_type() { | |
$restriction_roles = array( | |
'subscriber' => array( | |
'nature' => 5, | |
'sport' => 1 | |
), | |
'administrator' => array( | |
'nature' => 5, | |
'sport' => 1 | |
), | |
); | |
$group_id = isset( $_POST['gid'] ) ? $_POST['gid'] : ''; | |
if ( empty( $group_id ) ) { | |
$group_id = isset( $_POST['item_id'] ) ? $_POST['item_id'] : ''; | |
} | |
$group_types = bp_groups_get_group_type( $group_id, false ); | |
if ( empty( $group_types ) ) { | |
return; | |
} | |
$current_user_id = bp_loggedin_user_id(); | |
// Get User Data. | |
$user = get_userdata( $current_user_id ); | |
foreach ( $user->roles as $role ) { | |
if ( isset( $restriction_roles[ $role ] ) ) { | |
foreach ($group_types as $group_type ) { | |
if ( isset( $restriction_roles[ $role ][ $group_type ] ) ) { | |
$groups = groups_get_groups( | |
array( | |
'user_id' => $current_user_id, | |
'group_type' => $group_type, | |
'fields' => 'ids' | |
) | |
); | |
if ( $groups['total'] > $restriction_roles[ $role ][ $group_type ] ) { | |
$tax = bp_groups_get_group_type_object( 'nature' ); | |
return sprintf( __( 'Sorry, you cannot access more than %d %s groups' ), $restriction_roles[ $role ][ $group_type ], $tax->labels['singular_name'] ); | |
} | |
} | |
} | |
} | |
} | |
return; | |
} | |
add_filter( 'yzmr_groups_join_group', 'yzc_set_restriction_by_group_type' ); | |
add_filter( 'yzmr_groups_request_membership', 'yzc_set_restriction_by_group_type' ); | |
add_filter( 'yzmr_groups_accept_invite', 'yzc_set_restriction_by_group_type' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment