*/
class PMXI_Admin_Import extends PMXI_Controller_Admin {
protected $isWizard = true; // indicates whether controller is in wizard mode (otherwize it called to be deligated an edit action)
protected $isTemplateEdit = false; // indicates whether controlled is deligated by manage imports controller
protected function init() {
parent::init();
if ('PMXI_Admin_Manage' == PMXI_Plugin::getInstance()->getAdminCurrentScreen()->base) { // prereqisites are not checked when flow control is deligated
$id = $this->input->get('id');
$this->data['import'] = $import = new PMXI_Import_Record();
if ( ! $id or $import->getById($id)->isEmpty()) { // specified import is not found
wp_redirect(esc_url_raw(add_query_arg('page', 'pmxi-admin-manage', admin_url('admin.php')))); die();
}
$this->isWizard = false;
} else {
$action = PMXI_Plugin::getInstance()->getAdminCurrentScreen()->action;
$this->_step_ready($action);
$this->isInline = 'process' == $action;
}
XmlImportConfig::getInstance()->setCacheDirectory(sys_get_temp_dir());
// preserve id parameter as part of baseUrl
$id = $this->input->get('id') and $this->baseUrl = add_query_arg('id', $id, $this->baseUrl);
$this->baseUrl = apply_filters('pmxi_base_url', $this->baseUrl);
}
public function set($var, $val)
{
$this->{$var} = $val;
}
public function get($var)
{
return $this->{$var};
}
/**
* Checks whether corresponding step of wizard is complete
* @param string $action
*/
protected function _step_ready($action) {
// step #1: xml selction - has no prerequisites
if ('index' == $action) return true;
// step #2: element selection
$this->data['dom'] = $dom = new DOMDocument('1.0', PMXI_Plugin::$session->encoding);
$this->data['update_previous'] = $update_previous = new PMXI_Import_Record();
$old = libxml_use_internal_errors(true);
$xml = $this->get_xml();
if (empty($xml) and in_array($action, array('process')) ){
! empty( PMXI_Plugin::$session->update_previous ) and $update_previous->getById(PMXI_Plugin::$session->update_previous);
return true;
}
if ( ! PMXI_Plugin::$session->has_session()
or ! empty( PMXI_Plugin::$session->update_previous ) and $update_previous->getById(PMXI_Plugin::$session->update_previous)->isEmpty()
or empty($xml)
or ! @$dom->loadXML($xml)// FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load
) {
if ( ! PMXI_Plugin::is_ajax() ){
$this->errors->add('form-validation', __('WP All Import lost track of where you are.
Maybe you cleared your cookies or maybe it is just a temporary issue on your or your web host\'s end.', 'wp_all_import_plugin'));
wp_redirect_or_javascript(esc_url_raw($this->baseUrl)); die();
}
}
libxml_use_internal_errors($old);
if ('element' == $action) return true;
if ('evaluate' == $action) return true;
if ('evaluate_variations' == $action) return true;
// step #3: template
$xpath = new DOMXPath($dom);
$this->data['elements'] = $elements = @$xpath->query(PMXI_Plugin::$session->xpath);
if ('preview' == $action or 'tag' == $action or 'preview_images' == $action or 'preview_taxonomies' == $action or 'preview_images' == $action) return true;
if ( ! PMXI_Plugin::$session->get('xpath', false) or empty($elements) or ! $elements->length) {
$this->errors->add('form-validation', __('There are no elements to import based on your XPath.
If you are in Step 2, you probably specified filtering options that don’t match any elements present in your file.
If you are seeing this error elsewhere, it means that while the XPath expression for your initial import matched some elements in your file previously, there are now zero elements in the file that match this expression.
You can edit the XPath for your import by going to the Manage Imports -> Import Settings page.', 'wp_all_import_plugin'));
wp_redirect_or_javascript(esc_url_raw(add_query_arg('action', 'element', $this->baseUrl))); die();
}
if ('template' == $action or 'preview' == $action or 'tag' == $action) return true;
// step #4: options
if ( empty( PMXI_Plugin::$session->options ) ) {
wp_redirect_or_javascript(esc_url_raw(add_query_arg('action', 'template', $this->baseUrl))); die();
}
if ('options' == $action) return true;
if ( empty( PMXI_Plugin::$session->options ) ) {
wp_redirect(esc_url_raw(add_query_arg('action', 'options', $this->baseUrl))); die();
}
}
/**
* Step #1: Choose File
*/
public function index() {
$action = $this->input->get('action');
$this->data['reimported_import'] = $import = new PMXI_Import_Record();
$this->data['id'] = $id = $this->input->get('id');
$this->data['parent_import'] = $parent_import = $this->input->get('parent_import', 0);
$parent_import_record = new PMXI_Import_Record();
$DefaultOptions = array(
'type' => '',
'wizard_type' => 'new',
'custom_type' => 'post',
'show_hidden_cpt' => 0,
'feed_type' => '',
'url' => '',
'ftp_host' => '',
'ftp_path' => '',
'ftp_root' => '/',
'ftp_port' => '21',
'ftp_username' => '',
'ftp_password' => '',
'ftp_private_key' => '',
'file' => '',
'reimport' => '',
'is_update_previous' => $id ? 1 : 0,
'update_previous' => $id,
'xpath' => '/',
'filepath' => '',
'root_element' => '',
'downloaded' => '',
'auto_generate' => 0,
'template' => false ,
'taxonomy_type' => ''
);
$DefaultOptions = apply_filters('wp_all_import_default_options', $DefaultOptions);
if ($parent_import and ! $parent_import_record->getById($parent_import)->isEmpty()){
$DefaultOptions['custom_type'] = $parent_import_record->options['custom_type'];
}
if ( $id ) { // update requested but corresponding import is not found
if ( $import->getById($id)->isEmpty() ) {
if ( ! empty($_GET['deligate']) and sanitize_key($_GET['deligate']) == 'wpallexport' ) {
wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', array('error' => urlencode(__('The import associated with this export has been deleted.', 'wp_all_import_plugin')), 'updated' => urlencode(__('Please re-run your export by clicking Run Export on the All Export -> Manage Exports page. Then try your import again.', 'wp_all_import_plugin'))), remove_query_arg('id', $this->baseUrl)))); die();
} else {
wp_redirect(esc_url_raw(add_query_arg('pmxi_nt', array('error' => urlencode(__('This import has been deleted.', 'wp_all_import_plugin'))), remove_query_arg('id', $this->baseUrl)))); die();
}
} else {
$DefaultOptions['custom_type'] = $import->options['custom_type'];
}
}
if ( ! in_array($action, array('index'))) {
PMXI_Plugin::$session->clean_session();
} else {
$DefaultOptions = (PMXI_Plugin::$session->has_session() && !empty(PMXI_Plugin::$session->first_step) ? PMXI_Plugin::$session->first_step : array()) + $DefaultOptions;
}
$this->data['post'] = $post = $this->input->post( $DefaultOptions );
if ( ! class_exists('DOMDocument') or ! class_exists('XMLReader') ) {
$this->errors->add('form-validation', __('Required PHP components are missing.
WP All Import requires DOMDocument, XMLReader, and XMLWriter PHP modules to be installed.
These are standard features of PHP, and are necessary for WP All Import to read the files you are trying to import.
Please contact your web hosting provider and ask them to install and activate the DOMDocument, XMLReader, and XMLWriter PHP modules.', 'wp_all_import_plugin'));
}
$this->data['upload_validation'] = false;
if ($this->input->post('is_submitted') and ! $this->errors->get_error_codes()) {
check_admin_referer('choose-file', '_wpnonce_choose-file');
if ('upload' == $this->input->post('type')) {
$uploader = new PMXI_Upload($post['filepath'], $this->errors, rtrim(str_replace(basename($post['filepath']), '', $post['filepath']), '/'));
$upload_result = $uploader->upload();
if ($upload_result instanceof WP_Error) {
$this->errors = $upload_result;
} else {
$source = $upload_result['source'];
$filePath = $upload_result['filePath'];
$post['template'] = $upload_result['template'];
PMXI_Plugin::$is_csv = $upload_result['is_csv'];
if ( ! empty($upload_result['root_element']))
$post['root_element'] = $upload_result['root_element'];
}
} elseif ('url' == $this->input->post('type')) {
$post['url'] = trim($post['url']);
if ( ! empty($post['downloaded']) ){
$downloaded = json_decode($post['downloaded'], true);
$source = $downloaded['source'];
$filePath = $downloaded['filePath'];
$post['template'] = $downloaded['template'];
PMXI_Plugin::$csv_path = $downloaded['csv_path'];
PMXI_Plugin::$is_csv = $downloaded['is_csv'];
if ( ! empty($downloaded['root_element']))
$post['root_element'] = $downloaded['root_element'];
$post['feed_type'] = $downloaded['feed_type'];
} else {
$uploader = new PMXI_Upload($post['url'], $this->errors);
$upload_result = $uploader->url($post['feed_type']);
if ($upload_result instanceof WP_Error) {
$this->errors = $upload_result;
} else {
$source = $upload_result['source'];
$filePath = $upload_result['filePath'];
$post['template'] = $upload_result['template'];
PMXI_Plugin::$csv_path = $upload_result['csv_path'];
PMXI_Plugin::$is_csv = $upload_result['is_csv'];
if ( ! empty($upload_result['root_element']))
$post['root_element'] = $upload_result['root_element'];
$post['feed_type'] = $upload_result['feed_type'];
}
}
} elseif ( 'ftp' == $this->input->post('type')) {
if ( ! empty($post['downloaded']) ){
$downloaded = json_decode($post['downloaded'], true);
$source = $downloaded['source'];
$filePath = $downloaded['filePath'];
$post['template'] = $downloaded['template'];
PMXI_Plugin::$csv_path = $downloaded['csv_path'];
PMXI_Plugin::$is_csv = $downloaded['is_csv'];
if ( ! empty($downloaded['root_element']))
$post['root_element'] = $downloaded['root_element'];
$post['feed_type'] = $downloaded['feed_type'];
} else {
try {
$files = PMXI_FTPFetcher::fetch($post);
$uploader = new PMXI_Upload($files[0], $this->errors, rtrim(str_replace(basename($files[0]), '', $files[0]), '/'));
$upload_result = $uploader->upload();
if (!$this->errors->get_error_codes()) {
$source = $upload_result['source'];
$filePath = $upload_result['filePath'];
$post['template'] = $upload_result['template'];
PMXI_Plugin::$csv_path = $upload_result['csv_path'];
PMXI_Plugin::$is_csv = $upload_result['is_csv'];
if ( ! empty($upload_result['root_element']))
$post['root_element'] = $upload_result['root_element'];
$post['feed_type'] = $upload_result['feed_type'] ?? '';
}
} catch (Exception $e) {
$this->errors->add('form-validation', $e->getMessage());
}
}
$source['type'] = 'ftp';
} elseif ('file' == $this->input->post('type')) {
$uploader = new PMXI_Upload($post['file'], $this->errors);
$upload_result = $uploader->file();
if ($upload_result instanceof WP_Error) {
$this->errors = $upload_result;
} else {
$source = $upload_result['source'];
$filePath = $upload_result['filePath'];
$post['template'] = $upload_result['template'];
PMXI_Plugin::$is_csv = $upload_result['is_csv'];
if ( ! empty($upload_result['root_element']))
$post['root_element'] = $upload_result['root_element'];
}
}
if ($this->input->post('is_submitted') and '' == $this->input->post('custom_type')) {
$this->errors->add('form-validation', __('Select an item type to import the data', 'wp_all_import_plugin'));
}
if ($post['is_update_previous'] and empty($post['update_previous'])) {
$this->errors->add('form-validation', __('Previous import for update must be selected to proceed with a new one', 'wp_all_import_plugin'));
}
if ( 'taxonomies' == $this->input->post('custom_type') and '' == $this->input->post('taxonomy_type')){
$this->errors->add('form-validation', __('Select a taxonomy to import the data', 'wp_all_import_plugin'));
}
$this->data['detection_feed_extension'] = false;
$elements_cloud = array();
@set_time_limit(0);
$deligate = $this->input->get('deligate', false);
$redirect_to_template = false;
$importRecord = new PMXI_Import_Record();
switch ( $deligate ) {
case 'wpallexport':
$import_id = $this->input->get('id', 0);
$importRecord->clear();
$importRecord->getById($import_id);
if ( ! $importRecord->isEmpty() and ! empty($importRecord->options['unique_key'])) {
$importRecord->set(array(
'path' => wp_all_import_get_relative_path($filePath),
'parent_import_id' => 0
))->save();
$post['is_update_previous'] = 1;
$post['update_previous'] = $importRecord->id;
$redirect_to_template = true;
}
if ( $importRecord->isEmpty() ){
$this->errors->add('form-validation', __('File is no longer in the correct format', 'wp_all_import_plugin'));
} elseif (empty($importRecord->options['unique_key'])) {
$this->errors->add('form-validation', __('Certain columns are required to be present in your file to enable it to be re-imported with WP All Import. These columns are missing. Re-export your file using WP All Export, and don\'t delete any of the columns when editing it. Then, re-import will work correctly.', 'wp_all_import_plugin'));
} elseif($importRecord->options['custom_type'] == 'import_users' && ! class_exists('PMUI_Plugin')){
$this->errors->add('form-validation', __('
The import template you are using requires the User Add-On.
Purchase the User Add-On', 'wp_all_import_plugin')); } elseif($importRecord->options['custom_type'] == 'shop_customer' && ! class_exists('PMUI_Plugin')){ $this->errors->add('form-validation', __('The import template you are using requires the User Add-On.
Purchase the User Add-On', 'wp_all_import_plugin')); } break; default: # code... break; } $local_paths = !empty($local_paths) ? $local_paths : array($filePath); foreach ($local_paths as $key => $path) { if ( @file_exists($path) ){ $file = new PMXI_Chunk($path, array('element' => $post['root_element'], 'get_cloud' => true)); if ( ! empty($file->options['element']) ) { $xpath = "/" . $file->options['element']; $elements_cloud = $file->cloud; if ( ! empty($elements_cloud) and class_exists('PMXE_Plugin') and ! $importRecord->isEmpty() ) { $is_file_valid = apply_filters('wp_all_import_is_exported_file_valid', true, $importRecord->options['export_id'], $elements_cloud); if ( ! $is_file_valid ) { $this->errors->add('form-validation', __('Certain columns are required to be present in your file to enable it to be re-imported with WP All Import. These columns are missing. Re-export your file using WP All Export, and don\'t delete any of the columns when editing it. Then, re-import will work correctly.', 'wp_all_import_plugin')); } } if ( ($redirect_to_template or $post['auto_generate']) and ! $this->errors->get_error_codes() ) { // loop through the file until all lines are read while ($xml = $file->read()) { if ( ! empty($xml) ) { //PMXI_Import_Record::preprocessXml($xml); $xml = "" . "\n" . $xml; $dom = new DOMDocument('1.0', 'UTF-8'); $old = libxml_use_internal_errors(true); $dom->loadXML($xml); libxml_use_internal_errors($old); $dxpath = new DOMXPath($dom); if (($elements = @$dxpath->query($xpath)) and $elements->length) { if ( empty($chunks) ) { $chunks = 0; } $chunks += $elements->length; unset($dom, $dxpath, $elements); } } } //unset($file); } break; } } else { $this->errors->add('form-validation', __('Unable to download feed resource.', 'wp_all_import_plugin')); } } if ( ! $this->errors->get_error_codes() ) { // xml is valid $source['root_element'] = $file->options['element']; $source['first_import'] = date("Y-m-d H:i:s"); PMXI_Plugin::$session->clean_session(); $session_data = array( 'filePath' => $filePath, 'parent_import_id' => $parent_import, 'xpath' => (!empty($xpath)) ? $xpath : '', 'feed_type' => $post['feed_type'], 'wizard_type' => $post['wizard_type'], 'custom_type' => $post['custom_type'], 'taxonomy_type' => $post['taxonomy_type'], 'ftp_host' => $post['ftp_host'], 'ftp_path' => $post['ftp_path'], 'ftp_root' => $post['ftp_root'], 'ftp_port' => $post['ftp_port'], 'ftp_username' => $post['ftp_username'], 'ftp_password' => $post['ftp_password'], 'ftp_private_key' => $post['ftp_private_key'], 'source' => $source, 'encoding' => 'UTF-8', 'is_csv' => PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'chunk_number' => 1, 'log' => '', 'processing' => 0, 'queue_chunk_number' => 0, 'count' => (isset($chunks)) ? $chunks : 0, 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'local_paths' => (!empty($local_paths)) ? $local_paths : array(), // ftp import local copies of remote files 'csv_paths' => array(PMXI_Plugin::$csv_path), // ftp import local copies of remote CSV files 'action' => 'import', 'elements_cloud' => (!empty($elements_cloud)) ? $elements_cloud : array(), 'pointer' => 1, 'deligate' => $deligate, 'first_step' => $post ); // apply options from WP All Export bundle if ( ! empty($post['template'])) { $templates = json_decode($post['template'], true); $template_options = pmxi_maybe_unserialize($templates[0]['options']); $template_options['type'] = ($post['custom_type'] == 'page') ? 'page' : 'post'; $template_options['custom_type'] = $post['custom_type']; $template_options['wizard_type'] = $post['wizard_type']; if ($post['wizard_type'] == 'new') { $template_options['create_new_records'] = 1; } $this->data['post'] = $template_options; PMXI_Plugin::$session->set('options', $template_options); } foreach ($session_data as $key => $value) { PMXI_Plugin::$session->set( $key, $value ); } $update_previous = new PMXI_Import_Record(); if ($post['is_update_previous'] and ! $update_previous->getById($post['update_previous'])->isEmpty()) { PMXI_Plugin::$session->set('update_previous', $update_previous->id); PMXI_Plugin::$session->set('xpath', $update_previous->xpath); PMXI_Plugin::$session->set('options', $update_previous->options); } else { PMXI_Plugin::$session->set('update_previous', ''); } PMXI_Plugin::$session->save_data(); $xml = $this->get_xml(); if ( empty($xml) ) { $this->errors->add('upload-validation', __('Please confirm you are importing a valid feed.