Last active
August 29, 2015 14:24
-
-
Save bappi-d-great/c9ab5f4be98152c9f53a to your computer and use it in GitHub Desktop.
Restrict group creation based on S2Member capabilities
This file contains hidden or 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 | |
// Define the number of groups can be created by paid users | |
if( ! defined( 'PAID_USER_GROUP_LIMIT' ) ) define( 'PAID_USER_GROUP_LIMIT', 5 ); | |
// Define the error message | |
if( ! defined( 'PAID_USER_GROUP_LIMIT_PROTECTION_MSG' ) ) define( 'PAID_USER_GROUP_LIMIT_PROTECTION_MSG', "Either You have exceeded the no. of groups you can create or you don't have permission to create group." ); | |
function is_custom_free_member() { | |
if( is_user_logged_in() ){ | |
if( current_user_is( 's2member_level0' ) ){ | |
return true; | |
} | |
}else{ | |
return false; | |
} | |
} | |
function can_current_user_create_group() { | |
$groups = get_user_groups( get_current_user_id() ); | |
if( intval( $groups['total'] ) >= PAID_USER_GROUP_LIMIT ) { | |
return false; | |
} | |
return true; | |
} | |
add_filter( 'bp_user_can_create_groups', 'show_hide_create_btn' ); | |
add_action( 'wp', 'check_group_create', 2 ); | |
function show_hide_create_btn( $can_create ) { | |
if( is_custom_free_member() ){ | |
return false; | |
}else{ | |
if( ! can_current_user_create_group() ) { | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
return $can_create; | |
} | |
function check_group_create() { | |
if( ! function_exists( 'bp_is_active' ) || ! bp_is_active( 'groups' ) ) | |
return; //do not cause headache | |
restrict_creation(); | |
} | |
function restrict_creation() { | |
//if we are here,It is group creation step | |
if( can_current_user_create_group() ) { | |
bp_core_add_message( apply_filters( 'restrict_group_message', PAID_USER_GROUP_LIMIT_PROTECTION_MSG ), 'error' ); | |
remove_action( 'bp_actions', 'groups_action_create_group' ); //priority changed from 3 to 10 in bp 1.9 | |
} | |
} | |
function get_user_groups( $user_id = false ) { | |
if( ! $user_id ) | |
$user_id = get_current_user_id(); | |
return BP_Groups_Member::get_is_admin_of( $user_id );// $wpdb->get_var( $wpdb->prepare( "SELECT count(group_id) as count FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_admin = 1 AND is_banned = 0", $user_id) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment