user_email; // prepare/get data based on single / multi input if ($_POST['acswpmu_multiple'] == 'on'){ // get post and trim to remove whitespace $new_sites = trim($_POST['acswpmu_new_sites']); // explode the post to lines $the_array = explode( "\n", $new_sites ); // explode each line for ( $i = 0; $i < sizeof( $the_array ); $i++ ) { $the_array[$i] = explode( ",", $the_array[$i] ); $the_array[$i][3] = $user_id; $the_array[$i][4] = $admin_email; $the_array[$i][5] = $template_id; } } else { // get data for single site $siteurl = $_POST['acswpmu_siteurl']; $blogname = $_POST['acswpmu_blogname']; $blogdescription = $_POST['acswpmu_blogdescription']; $the_array[0][0] = $siteurl; $the_array[0][1] = $blogdescription; $the_array[0][2] = $blogname; $the_array[0][3] = $user_id; $the_array[0][4] = $admin_email; $the_array[0][5] = $template_id; } // trim each value in the array from whitespaces and left comma's for ( $i = 0; $i < sizeof( $the_array ); $i++ ) { array_walk($the_array[$i], 'trim_value'); } // And now: here the loop starts if (sizeof($the_array) > 0) { echo "

" . __( 'Adding cloned sites ' . $siteurl, 'acswpmu_trdom' ) . "

"; // create tabs ?>
'; foreach($the_array as $line) { //get the data from the array into strings $siteurl = $line[0]; $blogdescription = $line[1]; $blogname = $line[2]; $user_id = $line[3]; $admin_email = $line[4]; $template_id = $line[5]; // Prepare some data $dashedsiteurl = str_replace('.', '-', $siteurl); $domain = $dashedsiteurl . "." . get_blog_details(1)->domain; echo "

" . __( 'Start with creating site for ' . $siteurl, 'acswpmu_trdom' ) . "

"; //check for correct and non empty siteurl if (!valid_url($siteurl)){ _e("The url '$siteurl' is not a valid url.", 'acswpmu_trdom' ); $error = TRUE; } // create new empty blog, stops if domain already exists if(!$error) { $new_blog_id = create_empty_blog($domain, '/', $blogname); //$new_blog_id = wpmu_create_blog($domain, '/', $blogname, $user_id); if(is_integer($new_blog_id)) { _e("New site created with id: $new_blog_id
", 'acswpmu_trdom' ); } else { _e("The URL $domain already exist, we skipped it!", 'acswpmu_trdom' ); $error = TRUE; } } // Then add user to the new blog if(!$error) { $role = "administrator"; if ( add_user_to_blog( $new_blog_id, $user_id, $role ) ) { _e( 'Added user '.$user_id.' as '.$role.' to site '.$new_blog_id.'.
', 'acswpmu_trdom' ); } else { _e( 'Failed to add user '.$user_id.' as '.$role.' to site '.$new_blog_id.'.
', 'acswpmu_trdom' ); $error = TRUE; } } // Copy all data to the new tables if(!$error) { // get all database table names $template_like = $wpdb->prefix . $template_id . "_"; $template_new = $wpdb->prefix . $new_blog_id . "_"; $temp_like = str_replace('_', '\_', $template_like); //escape the _ for correct sql!! $template_tables = $wpdb->get_results( "SHOW TABLES LIKE '$temp_like%'", ARRAY_N ); foreach ($template_tables as $old_table) { $new_table = str_replace($template_like, $template_new, $old_table[0]); if (!strstr($old_table[0], 'options')){ //for all but options table $result = $wpdb->query( "REPLACE INTO $new_table SELECT * FROM $old_table[0]" ); } else { $result = $wpdb->query( "TRUNCATE TABLE $new_table" ); $result = $wpdb->query( "INSERT INTO $new_table SELECT * FROM $old_table[0]" ); } // feedback on result if ($result) { _e("Copied $result items from $old_table[0] to $new_table.
", 'acswpmu_trdom' ); } else { _e("The copy of $old_table[0] to $new_table failed or there was nothing to copy.", 'acswpmu_trdom' ); //$error = TRUE; } } } // Add custom data to newly duplicated blog if(!$error) { if(!$blogname) { $blogname = $siteurl; } $home = "http://" . $domain . "/"; $site_url = "http://" . $siteurl . "/"; $fileupload_url = $site_url . "files"; // update the cloned table with the new data and blog_id update_blog_option ($new_blog_id, 'siteurl', $site_url); update_blog_option ($new_blog_id, 'blogname', $blogname); update_blog_option ($new_blog_id, 'blogdescription', $blogdescription); update_blog_option ($new_blog_id, 'admin_email', $admin_email); update_blog_option ($new_blog_id, 'home', $home); update_blog_option ($new_blog_id, 'fileupload_url', $fileupload_url); update_blog_option ($new_blog_id, 'upload_path', 'wp-content/blogs.dir/' . $new_blog_id . '/files'); $new_options_table = $wpdb->prefix . $new_blog_id . '_options'; $old_name = $wpdb->prefix . $template_id . '_user_roles'; $new_name = $wpdb->prefix . $new_blog_id . '_user_roles'; $result = $wpdb->update( $new_options_table, array('option_name' => $new_name), array('option_name' => $old_name)); // 'check' if it went ok - NOTE: is just a basic check could give an error anyway... if(get_blog_option($new_blog_id, 'blogdescription') != $blogdescription) { //$error = TRUE; _e("Maybe we had an error updating the options table with the new data.", 'acswpmu_trdom' ); } else { _e("Updated the options table with cloned data
", 'acswpmu_trdom' ); } } /* // copy all Posts to new site if(!$error && $copy_posts) { $old_posts = $wpdb->prefix . $template_id . "_posts"; $new_posts = $wpdb->prefix . $new_blog_id . "_posts"; $result = $wpdb->query( "INSERT INTO $new_posts SELECT * FROM $old_posts WHERE post_type = 'post'" ); if ($result) { _e("Copied $result Posts to the new site.
", 'acswpmu_trdom' ); } else { _e("The copy of the Posts failed or there were no Posts to copy from.", 'acswpmu_trdom' ); //$error = TRUE; } } // copy all Pages to new site if(!$error && $copy_pages) { $old_posts = $wpdb->prefix . $template_id . "_posts"; $new_posts = $wpdb->prefix . $new_blog_id . "_posts"; $result = $wpdb->query( "INSERT INTO $new_posts SELECT * FROM $old_posts WHERE post_type = 'page'" ); if ($result) { _e("Copied $result Pages to the new site.
", 'acswpmu_trdom' ); } else { _e("The copy of the Pages failed or there were no Pages to copy from.", 'acswpmu_trdom' ); //$error = TRUE; } } // copy all Menuitems to new site if(!$error) { $old_posts = $wpdb->prefix . $template_id . "_posts"; $new_posts = $wpdb->prefix . $new_blog_id . "_posts"; $result = $wpdb->query( "INSERT INTO $new_posts SELECT * FROM $old_posts WHERE post_type = 'nav_menu_item'" ); if ($result) { _e("Copied $result Menu Items to the new site.
", 'acswpmu_trdom' ); } else { //_e("The copy of the Pages failed or there were no Pages to copy from.", 'acswpmu_trdom' ); //$error = TRUE; } } // Start with adding the new blog to the blogs table if (!$error){ _e("

Adding $domain to the database.

", 'acswpmu_trdom' ); $new_blog_id = insert_blog( $domain, '/', '1'); //Next duplicate all tables from the template _e("

Starting duplicating the template

", 'acswpmu_trdom' ); $template_like = $wpdb->prefix . $template_id . "_"; $template_new = $wpdb->prefix . $new_blog_id . "_"; $temp_like = str_replace('_', '\_', $template_like); //escape the _ for correct sql!! $template_tables = $wpdb->get_results( "SHOW TABLES LIKE '$temp_like%'", ARRAY_N ); foreach ($template_tables as $old_table) { $new_table = str_replace($template_like, $template_new, $old_table[0]); _e("Duplicate: $old_table[0] to $new_table : ", 'acswpmu_trdom' ); // check if table already exists if($wpdb->get_var("SHOW TABLES LIKE '$new_table'") != $new_table) { $result = $wpdb->query( "CREATE TABLE $new_table SELECT * FROM $old_table[0]"); if($result === FALSE) { _e("Failed.
", 'acswpmu_trdom' ); $error['Duplicating Tables'][] = "Failed to create $new_table"; } else { _e("OK.
", 'acswpmu_trdom' ); } } else { _e("Failed.
", 'acswpmu_trdom' ); $error['Duplicating Tables'][] = "The table $new_table already existed"; } } */ // Last but not least domainmap de new cloned site if(!$error) { $tbl_domain_map = $wpdb->prefix . "domain_mapping"; // Check if domainmapping exists if($wpdb->get_var("SHOW TABLES LIKE '$tbl_domain_map'") == $tbl_domain_map) { // Map the domain $result = $wpdb->insert( $tbl_domain_map, array('blog_id' => $new_blog_id, 'domain' => $siteurl, 'active' => '1')); if($result) { _e("New site mapped to domain: $siteurl
", 'acswpmu_trdom' ); } else { _e("Domain mapping for $siteurl failed", 'acswpmu_trdom' ); $error = TRUE; } } else { _e("Domainmapping failed because the domainmap plugin is not activated!", 'acswpmu_trdom' ); $error = TRUE; } } // count succesfull and failed sites if(!$error) { // count succesfull cloned sites $cloned[] = $siteurl; } else { // count failed sites $failed[] = $siteurl; $error = NULL; } } echo '
'; } ?>
' . __( 'Job done, here are the results:', 'acswpmu_trdom' ) . ''; if ($nr_sites > 0) { _e("

It took only $time seconds to create and clone $nr_sites sites!

", 'acswpmu_trdom' ); } if ($failed) { _e("

Hey $nr_failed sites could not be cloned due to errors, you might want to check the log why they failed.

", 'acswpmu_trdom' ); } // show donation options if ($nr_sites > -1) { echo '

' . __( 'You must be pleased..', 'acswpmu_trdom' ) . '

'; echo __("

Normally this would have taken you at least $normaltime minutes to do this all manually.
You could thank me by buying a tea, coffee, cappuchino, or if you are very pleased with the time this plugin saved you, a coffee with cake!

", 'acswpmu_trdom'); echo __("

You can buy me one of these items by clicking on one of the buttons below.
Come on its only a coffee, give it a try!

", 'acswpmu_trdom'); ?>