/** * Pre-import settings * * Renders a directory listing */ public static function preImport(Main $main) { $root = trailingslashit(realpath(ABSPATH)); // Starting point $root_len = strlen($root); // Often used, string length of root // Check if the selected directory falls within the root, and if it does, continue to import (else display pre-import) if (isset($_REQUEST['directory']) && !empty($_REQUEST['directory'])) { $sel_dir = realpath($root . $_REQUEST['directory']); if (strpos($sel_dir, $root) === 0) { return; } } // Build directoy listing $iterator = new \RecursiveIteratorIterator(new \Pf4wp\Storage\IgnorantRecursiveDirectoryIterator($root, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); $last_ident = 0; // Tracks identation $directory = '<ul><li id="root"><a href="#">/</a>'; foreach ($iterator as $fileinfo) { if ($fileinfo->isDir()) { $cur_dir = str_replace('\\', '/', substr($fileinfo, $root_len)); $children = explode('/', $cur_dir); $child_count = count($children); if ($child_count > $last_ident) { // Increase identation $directory .= '<ul>'; $last_ident = $child_count; } else { if ($child_count < $last_ident) { // Reset identation for ($i = 0; $i < $last_ident - $child_count; $i++) { $directory .= '</li></ul></li>'; } $last_ident = $child_count; } else { $directory .= '</li>'; } } $directory .= '<li><a href="#" data-dir="' . $cur_dir . '">' . $children[$child_count - 1] . '</a>'; } } $directory .= '</li></ul>'; // Add jsTree list($js_url, $version, $debug) = $main->getResourceUrl(); wp_enqueue_script('jquery-jstree', $js_url . 'vendor/jsTree/jquery.jstree.min.js', array('jquery'), null); // Render template $vars = array('rtl' => is_rtl(), 'root' => $root, 'directory' => $directory); return $main->template->render('importer_local.html.twig', $vars); }