function _import_file($file) { if ($this->had_error()) { return false; } if (!is_file($file)) { $this->add_error(new WP_Error('builder-import-export-no-file', __('Unable to find requested export file', 'it-l10n-Builder-Cohen'))); return false; } it_classes_load('it-file-utility.php'); it_classes_load('it-zip.php'); $path = ITFileUtility::create_writable_directory(array('name' => 'deleteme-builder-import-export-upload-temp', 'random' => true)); if (is_wp_error($path)) { $this->add_error($path); return false; } $result = ITZip::unzip($file, $path); if (is_wp_error($result) || !is_file("{$path}/info.txt") || !is_dir("{$path}/data-sources")) { ITFileUtility::delete_directory($path); $this->add_error(new WP_Error('builder-import-export-invalid-export-format', __('The uploaded file is not a valid Builder export file. It cannot be imported.', 'it-l10n-Builder-Cohen'))); return false; } $info = $this->_get_file_content("{$path}/info.txt"); $zip = new ITZip(); $export_path = $this->_get_export_path($info['guid'], $info['name']); $new_guid = false; if (is_wp_error($export_path)) { $info['guid'] = $this->_generate_guid(); if (false === $info['guid']) { return false; } $export_path = $this->_get_export_path($info['guid'], $info['name']); $new_guid = true; } if (true === $new_guid) { $this->_add_zip_file_content($zip, 'info.txt', $info); } $files = ITFileUtility::get_flat_file_listing($path); foreach ((array) $files as $file) { if (true === $new_guid && 'info.txt' === $file) { continue; } $dir = dirname($file); $dir = '.' === $dir ? '' : "{$dir}/"; $zip->add_file("{$path}/{$file}", $dir); } $file = $zip->create_zip(array('file' => $export_path)); ITFileUtility::delete_directory($path); return $info['guid']; }
public static function get_flat_file_listing($path, $recurse = false) { if (!is_dir($path)) { return false; } $path = rtrim($path, '/'); $files = array_merge(glob("{$path}/*"), glob("{$path}/.*")); $contents = array(); foreach ((array) $files as $file) { if (in_array(basename($file), array('.', '..'))) { continue; } if (is_dir($file)) { $results = ITFileUtility::get_flat_file_listing($file, true); $contents = array_merge($contents, $results); } else { if (is_file($file)) { $contents[] = $file; } } } if (false === $recurse) { $contents = str_replace("{$path}/", '', $contents); } return $contents; }