function changeWidgetDir($newDir)
 {
     $defaultDir = str_replace('//', '/', str_replace('\\', '/', dirname(plugin_dir_path(__FILE__)))) . '/custom-widgets/';
     if (empty($newDir) || get_option('preset-cdwd') == TRUE || get_option('widgetdir') == '/') {
         $newDir = $defaultDir;
         $dirchange = TRUE;
     }
     $newDir = str_replace('//', '/', str_replace('\\', '/', $newDir));
     $wpdir = str_replace('//', '/', str_replace('\\', '/', wp_upload_dir()));
     $plugindir = str_replace('//', '/', str_replace('\\', '/', dirname(plugin_dir_path(__FILE__))));
     if (WPWM_DEBUG == 1) {
         $error = TRUE;
         $errmsg = "Debug Mode enabled, unrestricted  directory changes permitted";
     } else {
         if (strstr($newDir, $plugindir) == FALSE) {
             $error = TRUE;
             $errmsg = " ERROR-Custom Widget Directory must be within Wordpress manager plugin directory. The default had been set instead of " . $newDir . '<br/>';
             $newDir = dirname(plugin_dir_path(__FILE__)) . '/custom-widgets/';
             $dirchange = TRUE;
         }
     }
     $dirchange = TRUE;
     $newDir = str_replace('//', '/', str_replace('\\', '/', $newDir));
     if (file_exists($newDir) == FALSE) {
         $dirDiff = true;
         mkdir($newDir, 0755);
         $user = exec(whoami);
         chown($newDir, $user);
     }
     $sourceDir = get_option('widgetdir');
     if (file_exists($sourceDir)) {
         $sourceDir = str_replace('//', '/', str_replace('\\', '/', $sourceDir));
         if (strcmp($sourceDir, $wpdir['basedir']) != 0) {
             $contents = scandir($sourceDir);
             if (SUBSTR($newDir, -1) != '/') {
                 $newDir .= '/';
             }
             foreach ($contents as $widgets) {
                 if ($widgets != "." && $widgets != "..") {
                     recurse_copy($sourceDir, $newDir);
                 }
             }
             if ($sourceDir != $newDir) {
                 if ($sourceDir != $defaultDir) {
                     $check = recursiveRemove($sourceDir);
                 }
             }
         }
     }
     update_option('widgetdir', $newDir);
     msgDisplay($error, $errmsg, $dirchange, $dirDiff);
 }
function recursiveRemove($path)
{
    // Ensure trailing / so we can append
    if (!preg_match('/\\/$/', $path)) {
        $path .= '/';
    }
    $dir = opendir($path);
    if (!$dir) {
        return false;
    }
    while (($item = readdir($dir)) !== false) {
        if ($item === '.' || $item === '..') {
            continue;
        }
        $itemPath = $path . $item;
        if (is_dir($itemPath)) {
            recursiveRemove($itemPath);
        } else {
            if (substr($itemPath, 0, 2) !== 's3') {
                test(false, true, "Paths being unlinked should start with s3, something scary is wrong, bailing out");
                exit(1);
            }
            if (!unlink($itemPath)) {
                return false;
            }
        }
    }
    closedir($dir);
    rmdir($path);
    return true;
}