<?php
/*
Plugin Name: Test insert_blog() bug
Description: Activate this plugin to test the insert_blog() bug here http://core.trac.wordpress.org/ticket/23400
Plugin URI: http://core.trac.wordpress.org/ticket/23400
Author: David M&aring;rtensson
Version: 1.0
Author URI: http://www.feedmeastraycat.net/
*/


add_action('init', '_init');
add_action('refresh_blog_details', '_refresh_blog_details', 10, 1);

function _init() {
	
	if ( MULTISITE && isset($_GET['action']) && $_GET['action'] == "testbug" ) {
	
		global $current_site;
		
		$domain = "test-".date("YmdHis");
		$name = "Test ".date("Y-m-d H:i:s");
		
		if ( is_subdomain_install() ) {
			$newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
			$path      = $current_site->path;
		} else {
			$newdomain = $current_site->domain;
			$path      = $current_site->path . $domain . '/';
		}
		
		$blog_id = wpmu_create_blog( $newdomain, $path, $name, 0 );
		
		if ( is_int($blog_id) ) {
			echo "Created blog id: ".$blog_id."<br>";
		}
		else {
			echo "Error:<br>";
			var_dump($blog_id);
		}
		die;
		
	}
	
}

function _refresh_blog_details( $blog_id ) {

	$current_blog_id = get_current_blog_id();
	if ($current_blog_id != 1) {
		switch_to_blog(1);
	}
	
	global $wpdb;
	$truncate = $wpdb->query( $wpdb->prepare('TRUNCATE TABLE `'.$wpdb->posts.'`') );
	if ($truncate) {
		$set_autoincrement = $wpdb->query( $wpdb->prepare('ALTER TABLE  `'.$wpdb->posts.'` AUTO_INCREMENT = 100') );
		if ($set_autoincrement) {
			wp_insert_post(array(
				'post_title' => 'Hello World '.date('H:i'),
				'post_content' => 'Hello World!'
			));	
		}
	}
	
	if ($current_blog_id != 1) {
		switch_to_blog($current_blog_id);
	}
	
}