SHARE
TWEET

Untitled

a guest Oct 14th, 2011 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. * Plugin name: Limit Groups per User
  4. *description: Limit the number of groups a user can create.
  5. *Author:Brajesh Singh
  6. *Author URI: http://buddydev.com
  7. *Plugin URI: http://buddydev.com/buddypress/limit-groups-per-user-plugin-for-buddypress/
  8. * Version: 1.0
  9. */
  10.  
  11.  
  12.  
  13. function bpdev_restrict_group_create($allowed_count=0,$current_group_count=0,$user_id=null){
  14.         global $bp,$wpdb;
  15.        
  16.        
  17.  
  18. //no restriction to site admin
  19. if ( $bp->current_component != $bp->groups->slug || 'create' != $bp->current_action ||is_super_admin())
  20.                 return false;
  21. //if we are here,It is group creation step
  22. if(!$allowed_count) //if zero, means no restriction
  23.         return false;
  24.  
  25. if(!$user_id)
  26.         $user_id=$bp->loggedin_user->id;
  27.  
  28. if($current_group_count>=$allowed_count){
  29.  
  30.                 bp_core_add_message(__("You have exceeded the no. of groups you can create."),"error");
  31.                 remove_action( 'wp', 'groups_action_create_group', 3 );
  32.                 bp_core_redirect(bp_get_root_domain()."/".bp_get_groups_root_slug());
  33. }
  34.  
  35.  
  36. }
  37.  
  38. function bpdev_check_group_create(){
  39.         global $bp;
  40.         if(!function_exists("bp_is_active")||!bp_is_active("groups"))
  41.                 return; //do not cause headache
  42.        
  43.                 $user_id=$bp->loggedin_user->id;//default for logged in user
  44.        
  45.         $allowed_count=get_site_option( 'limit-groups-creation-per-user',0 );//default 0, unlimited;
  46.         $user_has_admin_rights=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) );
  47.         $count=$user_has_admin_rights["total"];
  48.         bpdev_restrict_group_create(intval($allowed_count),intval($count),$user_id);
  49. }
  50.  
  51. add_action("wp","bpdev_check_group_create",2);
  52.  
  53.  
  54.  
  55. function bpdev_limit_groups_admin_screen(){
  56. ?>
  57. <table class="form-table">
  58. <tbody>
  59. <tr>
  60.         <th scope="row"><?php _e( 'Limit Groups Per User' ) ?></th>
  61.                 <td>
  62.                         <p><?php _e( 'How many Groups a user can create?') ?></p>
  63.  
  64.                                                 <label><input type="text" name="bp-admin[limit-groups-creation-per-user]" id="limit-groups-creation-per-user" value="<?php echo get_site_option( 'limit-groups-creation-per-user',0 );?>" /></label><br>
  65.         </td>
  66.                                 </tr>
  67. </tbody>
  68. </table>                               
  69. <?php
  70. }
  71. add_action("bp_core_admin_screen","bpdev_limit_groups_admin_screen");
  72.                 ?>
  73.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top