/**
  * Plugin activation work.
  */
 private static function setup()
 {
     global $wpdb, $wp_roles;
     // create WP capabilities
     Groups_Controller::set_default_capabilities();
     $charset_collate = '';
     if (!empty($wpdb->charset)) {
         $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
     }
     if (!empty($wpdb->collate)) {
         $charset_collate .= " COLLATE {$wpdb->collate}";
     }
     // create tables
     $group_table = _groups_get_tablename('group');
     if ($wpdb->get_var("SHOW TABLES LIKE '{$group_table}'") != $group_table) {
         $queries[] = "CREATE TABLE {$group_table} (\n\t\t\t\tgroup_id     BIGINT(20) UNSIGNED NOT NULL auto_increment,\n\t\t\t\tparent_id    BIGINT(20) DEFAULT NULL,\n\t\t\t\tcreator_id   BIGINT(20) DEFAULT NULL,\n\t\t\t\tdatetime     DATETIME DEFAULT NULL,\n\t\t\t\tname         VARCHAR(100) NOT NULL,\n\t\t\t\tdescription  LONGTEXT DEFAULT NULL,\n\t\t\t\tPRIMARY KEY  (group_id),\n\t\t\t\tUNIQUE INDEX group_n (name)\n\t\t\t) {$charset_collate};";
     }
     $capability_table = _groups_get_tablename('capability');
     if ($wpdb->get_var("SHOW TABLES LIKE '{$capability_table}'") != $capability_table) {
         $queries[] = "CREATE TABLE {$capability_table} (\n\t\t\t\tcapability_id BIGINT(20) UNSIGNED NOT NULL auto_increment,\n\t\t\t\tcapability    VARCHAR(255) NOT NULL,\n\t\t\t\tclass         VARCHAR(255) DEFAULT NULL,\n\t\t\t\tobject        VARCHAR(255) DEFAULT NULL,\n\t\t\t\tname          VARCHAR(100) DEFAULT NULL,\n\t\t\t\tdescription   LONGTEXT DEFAULT NULL,\n\t\t\t\tPRIMARY KEY   (capability_id),\n\t\t\t\tUNIQUE INDEX  capability (capability(100)),\n\t\t\t\tINDEX         capability_kco (capability(20),class(20),object(20))\n\t\t\t) {$charset_collate};";
     }
     $user_group_table = _groups_get_tablename('user_group');
     if ($wpdb->get_var("SHOW TABLES LIKE '{$user_group_table}'") != $user_group_table) {
         $queries[] = "CREATE TABLE {$user_group_table} (\n\t\t\t\tuser_id     bigint(20) unsigned NOT NULL,\n\t\t\t\tgroup_id    bigint(20) unsigned NOT NULL,\n\t\t\t\tPRIMARY KEY (user_id, group_id),\n\t\t\t\tINDEX       user_group_gu (group_id,user_id)\n\t\t\t) {$charset_collate};";
     }
     $user_capability_table = _groups_get_tablename('user_capability');
     if ($wpdb->get_var("SHOW TABLES LIKE '{$user_capability_table}'") != $user_capability_table) {
         $queries[] = "CREATE TABLE {$user_capability_table} (\n\t\t\t\tuser_id\t      bigint(20) unsigned NOT NULL,\n\t\t\t\tcapability_id bigint(20) unsigned NOT NULL,\n\t\t\t\tPRIMARY KEY   (user_id, capability_id),\n\t\t\t\tINDEX         user_capability_cu (capability_id,user_id)\n\t\t\t) {$charset_collate};";
     }
     $group_capability_table = _groups_get_tablename('group_capability');
     if ($wpdb->get_var("SHOW TABLES LIKE '{$group_capability_table}'") != $group_capability_table) {
         $queries[] = "CREATE TABLE {$group_capability_table} (\n\t\t\t\tgroup_id      bigint(20) unsigned NOT NULL,\n\t\t\t\tcapability_id bigint(20) unsigned NOT NULL,\n\t\t\t\tPRIMARY KEY   (group_id, capability_id),\n\t\t\t\tINDEX         group_capability_cg (capability_id,group_id)\n\t\t\t) {$charset_collate};";
     }
     if (!empty($queries)) {
         require_once ABSPATH . 'wp-admin/includes/upgrade.php';
         dbDelta($queries);
     }
     // needs to be called to create its capabilities
     Groups_Post_Access::activate();
     // same thing to created groups for registered users
     Groups_Registered::activate();
     // add WordPress capabilities
     Groups_WordPress::activate();
     // ... end of plugin activation work.
 }
    {
        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();
        }
    }
}
Groups_Registered::init();