<?php
/*
Plugin name: Separate Blog User Tables
Plugin URI: http://core.trac.wordpress.org/ticket/15467
Description: Configure and implements separate User Tables for each blog in the same Multisite Network.
Author: lightningspirit
Author URI: http://vcarvalho.com/
Version: 0.1
*/

/**
 * This file actually do all do work on creating user tables for the
 * current blog if they are not already created.
 * 
 * 
 * 
 * NOTE: Don't use this file in a production environment.
 * Not stable. Use only for test purposes.
 * 
 * 
 * 
 * Instalation Procedure: 
 * ----------------------
 * 1. Put inside wp-contents/mu-plugins/
 * 
 * 
 * @package WordPress
 * @since 3.4
 */



/**
 * Fire on Must use plugins loading process
 */
add_action( 'muplugins_loaded', '_separate_blog_user_tables_create' );


/**
 * _separate_blog_user_tables_create
 * 
 * This code will create new user tables if they are not present in
 * the DB for the current blog request.
 * TODO: replicate the administrator of the blog in those custom tables
 * or at least hook the authentication process to load the users 
 * from the global users table too
 * 
 * @since 0.1
 * @uses dbDelta
 */
function _separate_blog_user_tables_create() {
	global $wpdb, $blog_id;
	
	/** Lets set the name of the custom Users tables */
	$users_table 	 = $wpdb->prefix . 'users';
	$user_meta_table = $wpdb->prefix . 'usermeta';
	
	
	/** Check if the tables were already created, if so return and have fun! :) */
	if ( $wpdb->get_var( "SHOW TABLES LIKE '$users_table'" ) == $users_table )
		return;
		
	
	/** 
	 * Hum... it seems that it is the first time that you are running
	 * this code in the current blog...
	 * We have to create the custom tables, lets do it!
	 * 
	 * Require the dbDelta() function
	 */	
	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	
	/** Create user meta table */
	dbDelta( "CREATE TABLE IF NOT EXISTS `$user_meta_table` (
	  `umeta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	  `user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
	  `meta_key` varchar(255) DEFAULT NULL,
	  `meta_value` longtext,
	  PRIMARY KEY (`umeta_id`),
	  KEY `user_id` (`user_id`),
	  KEY `meta_key` (`meta_key`)
	);" );
	
	/** Create users table **/
	dbDelta( "CREATE TABLE IF NOT EXISTS `$users_table` (
	  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
	  `user_login` varchar(60) NOT NULL DEFAULT '',
	  `user_pass` varchar(64) NOT NULL DEFAULT '',
	  `user_nicename` varchar(50) NOT NULL DEFAULT '',
	  `user_email` varchar(100) NOT NULL DEFAULT '',
	  `user_url` varchar(100) NOT NULL DEFAULT '',
	  `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
	  `user_activation_key` varchar(60) NOT NULL DEFAULT '',
	  `user_status` int(11) NOT NULL DEFAULT '0',
	  `display_name` varchar(250) NOT NULL DEFAULT '',
	  `spam` tinyint(2) NOT NULL DEFAULT '0',
	  `deleted` tinyint(2) NOT NULL DEFAULT '0',
	  PRIMARY KEY (`ID`),
	  KEY `user_login_key` (`user_login`),
	  KEY `user_nicename` (`user_nicename`)
	);" );
	
	/** TODO: Replicate current user on those tables */
	
}
