/**
  * Hooks into the remove_user_from_blog action to remove the user
  * from groups that belong to that blog.
  * 
  *  Note that this is preemptive as there is no
  *  removed_user_from_blog action.
  * 
  * @param int $user_id
  * @param int $blog_id
  */
 public static function remove_user_from_blog($user_id, $blog_id)
 {
     if (is_multisite()) {
         Groups_Controller::switch_to_blog($blog_id);
     }
     global $wpdb;
     $group_table = _groups_get_tablename("group");
     $user_group_table = _groups_get_tablename("user_group");
     // We can end up here while a blog is being deleted, in that case,
     // the tables have already been deleted.
     if ($wpdb->get_var("SHOW TABLES LIKE '" . $group_table . "'") == $group_table && $wpdb->get_var("SHOW TABLES LIKE '" . $user_group_table . "'") == $user_group_table) {
         $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$user_group_table}\n\t\t\t\tLEFT JOIN {$group_table} ON {$user_group_table}.group_id = {$group_table}.group_id\n\t\t\t\tWHERE {$user_group_table}.user_id = %d\n\t\t\t\t", Groups_Utility::id($user_id)));
         if ($rows) {
             foreach ($rows as $row) {
                 // don't optimize that, favour standard deletion
                 self::delete($row->user_id, $row->group_id);
             }
         }
     }
     if (is_multisite()) {
         Groups_Controller::restore_current_blog();
     }
 }
 /**
  * Assign a user to its "Registered" group for the given blog.
  * 
  * @param int $user_id
  * @param WP_string $role
  */
 function add_user_to_blog($user_id, $role, $blog_id)
 {
     if (is_multisite()) {
         Groups_Controller::switch_to_blog($blog_id);
     }
     global $wpdb;
     // Check if the group table exists, if it does not exist, we are
     // probably here because the action has been triggered in the middle
     // of wpmu_create_blog() before the wpmu_new_blog action has been
     // triggered. In that case, just skip this as the user will be added
     // later when wpmu_new_blog is triggered, the activation sequence has
     // created the tables and all users of the new blog are added to
     // that blog's "Registered" group.
     $group_table = _groups_get_tablename('group');
     if ($wpdb->get_var("SHOW TABLES LIKE '" . $group_table . "'") == $group_table) {
         $registered_group = Groups_Group::read_by_name(self::REGISTERED_GROUP_NAME);
         if (!$registered_group) {
             $registered_group_id = Groups_Group::create(array("name" => self::REGISTERED_GROUP_NAME));
         } else {
             $registered_group_id = $registered_group->group_id;
         }
         if ($registered_group_id) {
             Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $registered_group_id));
         }
     }
     if (is_multisite()) {
         Groups_Controller::restore_current_blog();
     }
 }