function set_up_target_vars($target_id, $vars = array('id', 'prefix', 'subd', 'title', 'upload_dir', 'upload_url', 'url'))
 {
     //db
     if (is_null($this->target_db)) {
         global $wpdb;
         $default_db_creds = array('host' => DB_HOST, 'name' => DB_NAME, 'user' => DB_USER, 'password' => DB_PASSWORD);
         $target_db_creds = apply_filters('ns_cloner_target_db_credentials', $default_db_creds, $this);
         if ($target_db_creds !== $default_db_creds) {
             $this->target_db = @new ns_wpdb($target_db_creds['user'], $target_db_creds['password'], $target_db_creds['name'], $target_db_creds['host']);
             if (!empty($this->target_db->last_error)) {
                 $this->dlog("Could not connect to and select the target database. Error:" . $this->target_db->last_error);
                 if (is_network_admin()) {
                     ns_add_admin_notice(__("Could not connect to and select the target database", "ns-cloner"), "error", $this->menu_slug, true);
                     wp_redirect(wp_get_referer());
                     exit;
                 }
             }
         } else {
             $this->target_db = $wpdb;
         }
     }
     //ids
     if (in_array('id', $vars)) {
         $this->target_id = $this->report[__("New Site ID", "ns-cloner")] = $target_id;
         $this->dlog("Setting target id: " . $this->target_id);
     }
     //db prefixes
     if (in_array('prefix', $vars)) {
         $this->target_prefix = $target_id == 1 ? $this->target_db->base_prefix : $this->target_db->base_prefix . $target_id . '_';
         $this->dlog("Setting target prefix: " . $this->target_prefix);
     }
     //subdomains/dirs
     if (in_array('subd', $vars)) {
         $this->target_subd = untrailingslashit(get_blog_details($this->target_id)->domain . get_blog_details($this->target_id)->path);
         $this->dlog("Setting target subdomain/subdirectory: " . $this->target_subd);
     }
     //titles
     if (in_array('title', $vars)) {
         $this->target_title = get_blog_details($this->target_id)->blogname;
         $this->dlog("Setting target site title: " . $this->target_title);
     }
     //upload dirs
     if (in_array('upload_dir', $vars)) {
         $this->target_upload_dir = ns_get_upload_dir($this->target_id, NS_CLONER_LOG_FILE_DETAILED);
         $this->target_upload_dir_relative = str_replace(ns_norm_winpath(ABSPATH), '', $this->target_upload_dir);
         $this->dlog("Setting target full upload dir path: " . $this->target_upload_dir . " and shorter relative path: " . $this->target_upload_dir_relative);
     }
     //upload urls
     if (in_array('upload_url', $vars)) {
         $this->target_upload_url = ns_get_upload_url($this->target_id, NS_CLONER_LOG_FILE_DETAILED);
         $this->target_upload_url_relative = str_replace(get_site_url($this->target_id) . '/', '', $this->target_upload_url);
         $this->dlog("Setting target full upload url: " . $this->target_upload_url . " and shorter relative url: " . $this->target_upload_url_relative);
     }
     //urls
     if (in_array('url', $vars)) {
         $this->target_url = $this->report[__('New Site', 'ns-cloner')] = get_blog_details($this->target_id, true)->siteurl;
         $this->dlog("Setting target url " . $this->target_url);
     }
 }
function ns_get_upload_dir($id, $logfile = false)
{
    // ---------------------------------------------------------------------------------------------------------------------
    //										T E S T I N G  	 N E W    C O D E
    // ---------------------------------------------------------------------------------------------------------------------
    // initialize
    switch_to_blog($id);
    $wp_upload_dir_a = wp_upload_dir();
    restore_current_blog();
    $wp_upload_dir = ns_norm_winpath($wp_upload_dir_a['basedir']);
    $upload_dir = '';
    // eventual return value
    // handle difference between ID = 1 and everything else
    // in ID = 1 test_1 and test_2 are identical
    if ($id == 1) {
        $test_1_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads');
        $test_1_upload_dir = ns_norm_winpath($test_1_base);
        $test_2_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads');
        $test_2_upload_dir = ns_norm_winpath($test_2_base);
    } else {
        $test_1_base = ns_norm_winpath(WP_CONTENT_DIR . '/blogs.dir');
        $test_1_upload_dir = ns_norm_winpath($test_1_base . '/' . $id . '/files');
        $test_2_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads/sites');
        $test_2_upload_dir = ns_norm_winpath($test_2_base . '/' . $id);
    }
    // compare and set conditions for determining confidence level
    $test_1_base_exists = file_exists($test_1_base) ? true : false;
    $test_1_upload_dir_exists = file_exists($test_1_upload_dir) ? true : false;
    $test_1_matches_wp = $test_1_upload_dir == $wp_upload_dir ? true : false;
    $test_2_base_exists = file_exists($test_2_base) ? true : false;
    $test_2_upload_dir_exists = file_exists($test_2_upload_dir) ? true : false;
    $test_2_matches_wp = $test_2_upload_dir == $wp_upload_dir ? true : false;
    $wp_upload_dir_exists = file_exists($wp_upload_dir) ? true : false;
    // cascade in order of confidence
    // HIGH CONFIDENCE ----------------------------------------------------------------------------------
    // WP Origin < 3.5
    if ($test_1_base_exists && $test_1_upload_dir_exists && $test_1_matches_wp) {
        $upload_dir = $test_1_upload_dir;
        $confidence = 'HIGH CONFIDENCE';
    }
    // WP Origin > 3.5 (fall through if $upload_dir already set)
    if ($upload_dir == '' && $test_2_base_exists && $test_2_upload_dir_exists && $test_2_matches_wp) {
        $upload_dir = $test_2_upload_dir;
        $confidence = 'HIGH CONFIDENCE';
    }
    // MEDIUM CONFIDENCE --------------------------------------------------------------------------------
    // WP Origin < 3.5 (fall through if $upload_dir already set)
    if ($upload_dir == '' && $test_1_base_exists && $test_1_matches_wp) {
        $upload_dir = $test_1_upload_dir;
        $confidence = 'MEDIUM CONFIDENCE';
    }
    // WP Origin > 3.5 (fall through if $upload_dir already set)
    if ($upload_dir == '' && $test_2_base_exists && $test_2_matches_wp) {
        $upload_dir = $test_2_upload_dir;
        $confidence = 'MEDIUM CONFIDENCE';
    }
    // LOW CONFIDENCE -----------------------------------------------------------------------------------
    // WP Origin < 3.5 (fall through if $upload_dir already set)
    if ($upload_dir == '' && $test_1_base_exists) {
        $upload_dir = $test_1_upload_dir;
        $confidence = 'LOW CONFIDENCE';
    }
    // WP Origin > 3.5 (fall through if $upload_dir already set)
    if ($upload_dir == '' && $test_2_base_exists) {
        $upload_dir = $test_2_upload_dir;
        $confidence = 'LOW CONFIDENCE';
    }
    // FAIL SAFE ----------------------------------------------------------------------------------------
    if ($upload_dir == '') {
        $upload_dir = $wp_upload_dir;
        $confidence = 'FAIL SAFE';
    }
    // log results for debugging
    ns_log_section_break($logfile);
    ns_log_write('TESTING UPLOAD LOCATION for ID = ' . $id, $logfile);
    ns_log_section_break($logfile);
    ns_log_write('wp_upload_dir	     = ' . $wp_upload_dir . ns_t2e($wp_upload_dir_exists), $logfile);
    ns_log_write('test_1_base        = ' . $test_1_base . ns_t2e($test_1_base_exists), $logfile);
    ns_log_write('test_1_upload_dir  = ' . $test_1_upload_dir . ns_t2e($test_1_upload_dir_exists), $logfile);
    ns_log_write('test_1_matches_wp  = ' . ns_t2e($test_1_matches_wp), $logfile);
    ns_log_write('test_2_base        = ' . $test_2_base . ns_t2e($test_2_base_exists), $logfile);
    ns_log_write('test_2_upload_dir  = ' . $test_2_upload_dir . ns_t2e($test_2_upload_dir_exists), $logfile);
    ns_log_write('test_2_matches_wp  = ' . ns_t2e($test_2_matches_wp), $logfile);
    ns_log_write('<b>upload_dir = ' . $upload_dir . '</b> with ' . $confidence, $logfile);
    ns_log_section_break($logfile);
    return $upload_dir;
}