/** * Step #1: Choose File */ public function index() { PMXI_Plugin::$session->clean_session(); $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(); $default = array('type' => 'upload', 'wizard_type' => 'new', 'custom_type' => 'post', 'show_hidden_cpt' => 0, 'feed_type' => '', 'url' => '', 'ftp' => array('url' => 'ftp://'), 'file' => '', 'reimport' => '', 'is_update_previous' => $id ? 1 : 0, 'update_previous' => $id, 'xpath' => '/', 'filepath' => '', 'root_element' => '', 'downloaded' => '', 'auto_generate' => 0, 'template' => false); if ($parent_import and !$parent_import_record->getById($parent_import)->isEmpty()) { $default['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 $_GET['deligate'] == 'wpallexport') { wp_redirect(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(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 { $default['custom_type'] = $import->options['custom_type']; } } $this->data['post'] = $post = $this->input->post($default); if (!class_exists('DOMDocument') or !class_exists('XMLReader')) { $this->errors->add('form-validation', __('Required PHP components are missing.<br/><br/>WP All Import requires DOMDocument, XMLReader, and XMLWriter PHP modules to be installed.<br/>These are standard features of PHP, and are necessary for WP All Import to read the files you are trying to import.<br/>Please contact your web hosting provider and ask them to install and activate the DOMDocument, XMLReader, and XMLWriter PHP modules.', 'wp_all_import_plugin')); } 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')) { 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 ('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')); } $this->data['detection_feed_extension'] = false; $elements_cloud = array(); @set_time_limit(0); $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'imports'; $deligate = $this->input->get('deligate', false); $redirect_to_template = false; $importRecord = new PMXI_Import_Record(); switch ($deligate) { case 'wpallexport': global $wpdb; $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; //$chunks = $importRecord->count; $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 User Import Add-On.<br/><a href="http://www.wpallimport.com/add-ons/user-import/?utm_source=wordpress.org&utm_medium=wpai-import-template&utm_campaign=free+wp+all+export+plugin" target="_blank">Purchase the User Import Add-On</a>', '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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\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) { $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'], '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(), 'csv_paths' => array(PMXI_Plugin::$csv_path), 'action' => 'import', 'elements_cloud' => !empty($elements_cloud) ? $elements_cloud : array(), 'pointer' => 1, 'deligate' => $deligate); // apply options from WP All Export bundle if (!empty($post['template'])) { $templates = json_decode($post['template'], true); $template_options = 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('form-validation', __('Please confirm you are importing a valid feed.<br/> Often, feed providers distribute feeds with invalid data, improperly wrapped HTML, line breaks where they should not be, faulty character encodings, syntax errors in the XML, and other issues.<br/><br/>WP All Import has checks in place to automatically fix some of the most common problems, but we can’t catch every single one.<br/><br/>It is also possible that there is a bug in WP All Import, and the problem is not with the feed.<br/><br/>If you need assistance, please contact support – <a href="mailto:support@wpallimport.com">support@wpallimport.com</a> – with your XML/CSV file. We will identify the problem and release a bug fix if necessary.', 'wp_all_import_plugin')); } elseif ($redirect_to_template) { wp_redirect(add_query_arg('action', 'template', $this->baseUrl)); die; } elseif ($post['auto_generate']) { wp_redirect(add_query_arg('action', 'options', $this->baseUrl)); die; } else { wp_redirect(add_query_arg('action', 'element', $this->baseUrl)); die; } } else { if ('url' == $this->input->post('type') and !empty($this->errors)) { $this->errors->add('form-validation', __('WP All Import unable to detect file type.<br/><br/>WP All Import not able to determine what type of file you are importing. Make sure your file extension is correct for the file type you are importing.<br/> Please choose the correct file type from the dropdown below, or try adding &type=xml or &type=csv to the end of the URL, for example http://example.com/export-products.php?&type=xml', 'wp_all_import_plugin')); $this->data['detection_feed_extension'] = true; } else { $this->errors->add('form-validation', __('Please confirm you are importing a valid feed.<br/> Often, feed providers distribute feeds with invalid data, improperly wrapped HTML, line breaks where they should not be, faulty character encodings, syntax errors in the XML, and other issues.<br/><br/>WP All Import has checks in place to automatically fix some of the most common problems, but we can’t catch every single one.<br/><br/>It is also possible that there is a bug in WP All Import, and the problem is not with the feed.<br/><br/>If you need assistance, please contact support – <a href="mailto:support@wpallimport.com">support@wpallimport.com</a> – with your XML/CSV file. We will identify the problem and release a bug fix if necessary.', 'wp_all_import_plugin')); } } do_action("pmxi_get_file", $filePath); } if ($this->input->post('is_submitted') and $this->errors->get_error_codes()) { PMXI_Plugin::$session->clean_session(); } $this->render(); }
function pmxi_wp_loaded() { @ini_set("max_input_time", PMXI_Plugin::getInstance()->getOption('max_input_time')); @ini_set("max_execution_time", PMXI_Plugin::getInstance()->getOption('max_execution_time')); $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'imports'; global $wpdb; $imports = $wpdb->get_results("SELECT `id`, `name`, `path` FROM {$table} WHERE `path` IS NULL", ARRAY_A); if (!empty($imports)) { $importRecord = new PMXI_Import_Record(); $importRecord->clear(); foreach ($imports as $imp) { $importRecord->getById($imp['id']); if (!$importRecord->isEmpty()) { $importRecord->delete(true); } $importRecord->clear(); } } /* Check if cron is manualy, then execute import */ $cron_job_key = PMXI_Plugin::getInstance()->getOption('cron_job_key'); if (!empty($cron_job_key) and !empty($_GET['import_id']) and !empty($_GET['import_key']) and $_GET['import_key'] == $cron_job_key and !empty($_GET['action']) and in_array($_GET['action'], array('processing', 'trigger', 'pipe'))) { $logger = create_function('$m', 'echo "<p>$m</p>\\n";'); $import = new PMXI_Import_Record(); $ids = explode(',', $_GET['import_id']); if (!empty($ids) and is_array($ids)) { foreach ($ids as $id) { if (empty($id)) { continue; } $import->getById($id); if (!$import->isEmpty()) { if (!in_array($import->type, array('url', 'ftp', 'file'))) { $logger and call_user_func($logger, sprintf(__('Scheduling update is not working with "upload" import type. Import #%s.', 'wp_all_import_plugin'), $id)); } switch ($_GET['action']) { case 'trigger': if ((int) $import->executing) { $logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id)); } elseif (!$import->processing and !$import->triggered) { $import->set(array('triggered' => 1, 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'queue_chunk_number' => 0, 'last_activity' => date('Y-m-d H:i:s')))->update(); $history_log = new PMXI_History_Record(); $history_log->set(array('import_id' => $import->id, 'date' => date('Y-m-d H:i:s'), 'type' => 'trigger', 'summary' => __("triggered by cron", "wp_all_import_plugin")))->save(); $logger and call_user_func($logger, sprintf(__('#%s Cron job triggered.', 'wp_all_import_plugin'), $id)); } elseif ($import->processing and !$import->triggered) { $logger and call_user_func($logger, sprintf(__('Import #%s currently in process. Request skipped.', 'wp_all_import_plugin'), $id)); } elseif (!$import->processing and $import->triggered) { $logger and call_user_func($logger, sprintf(__('Import #%s already triggered. Request skipped.', 'wp_all_import_plugin'), $id)); } break; case 'processing': if ($import->processing == 1 and time() - strtotime($import->registered_on) > (PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') ? PMXI_Plugin::getInstance()->getOption('cron_processing_time_limit') : 120)) { // it means processor crashed, so it will reset processing to false, and terminate. Then next run it will work normally. $import->set(array('processing' => 0))->update(); } // start execution imports that is in the cron process if (!(int) $import->triggered) { $logger and call_user_func($logger, sprintf(__('Import #%s is not triggered. Request skipped.', 'wp_all_import_plugin'), $id)); } elseif ((int) $import->executing) { $logger and call_user_func($logger, sprintf(__('Import #%s is currently in manually process. Request skipped.', 'wp_all_import_plugin'), $id)); } elseif ((int) $import->triggered and !(int) $import->processing) { $log_storage = (int) PMXI_Plugin::getInstance()->getOption('log_storage'); // unlink previous logs $by = array(); $by[] = array(array('import_id' => $id, 'type NOT LIKE' => 'trigger'), 'AND'); $historyLogs = new PMXI_History_List(); $historyLogs->setColumns('id', 'import_id', 'type', 'date')->getBy($by, 'id ASC'); if ($historyLogs->count() and $historyLogs->count() >= $log_storage) { $logsToRemove = $historyLogs->count() - $log_storage; foreach ($historyLogs as $i => $file) { $historyRecord = new PMXI_History_Record(); $historyRecord->getBy('id', $file['id']); if (!$historyRecord->isEmpty()) { $historyRecord->delete(); } // unlink history file only if ($i == $logsToRemove) { break; } } } $history_log = new PMXI_History_Record(); $history_log->set(array('import_id' => $import->id, 'date' => date('Y-m-d H:i:s'), 'type' => 'processing', 'summary' => __("cron processing", "wp_all_import_plugin")))->save(); if ($log_storage) { $wp_uploads = wp_upload_dir(); $log_file = wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::LOGS_DIRECTORY, $history_log->id) . DIRECTORY_SEPARATOR . $history_log->id . '.html'; if (@file_exists($log_file)) { wp_all_import_remove_source($log_file, false); } } ob_start(); $import->set(array('canceled' => 0, 'failed' => 0))->execute($logger, true, $history_log->id); $log_data = ob_get_clean(); if ($log_storage) { $log = @fopen($log_file, 'a+'); @fwrite($log, $log_data); @fclose($log); } if (!(int) $import->queue_chunk_number) { $logger and call_user_func($logger, sprintf(__('Import #%s complete', 'wp_all_import_plugin'), $import->id)); } else { $logger and call_user_func($logger, sprintf(__('Records Count %s', 'wp_all_import_plugin'), (int) $import->count)); $logger and call_user_func($logger, sprintf(__('Records Processed %s', 'wp_all_import_plugin'), (int) $import->queue_chunk_number)); } } else { $logger and call_user_func($logger, sprintf(__('Import #%s already processing. Request skipped.', 'wp_all_import_plugin'), $id)); } break; case 'pipe': $import->execute($logger); break; } } } } } }
public function cleanup() { $removedFiles = 0; $wp_uploads = wp_upload_dir(); $dir = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY; $cacheDir = PMXI_Plugin::ROOT_DIR . '/libraries/cache'; $files = array_diff(@scandir($dir), array('.', '..')); $cacheFiles = @array_diff(@scandir($cacheDir), array('.', '..')); $msg = __('Files not found', 'wp_all_import_plugin'); if (count($files) or count($cacheFiles)) { wp_all_import_clear_directory($dir); wp_all_import_clear_directory($cacheDir); $msg = __('Clean Up has been successfully completed.', 'wp_all_import_plugin'); } // clean logs files $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'history'; global $wpdb; $histories = $wpdb->get_results("SELECT * FROM {$table}", ARRAY_A); if (!empty($histories)) { $importRecord = new PMXI_Import_Record(); $importRecord->clear(); foreach ($histories as $history) { $importRecord->getById($history['import_id']); if ($importRecord->isEmpty()) { $historyRecord = new PMXI_History_Record(); $historyRecord->getById($history['id']); if (!$historyRecord->isEmpty()) { $historyRecord->delete(); } } $importRecord->clear(); } } // clean uploads folder $table = PMXI_Plugin::getInstance()->getTablePrefix() . 'files'; $files = $wpdb->get_results("SELECT * FROM {$table}", ARRAY_A); $required_dirs = array(); if (!empty($files)) { $importRecord = new PMXI_Import_Record(); $importRecord->clear(); foreach ($files as $file) { $importRecord->getById($file['import_id']); if ($importRecord->isEmpty()) { $fileRecord = new PMXI_File_Record(); $fileRecord->getById($file['id']); if (!$fileRecord->isEmpty()) { $fileRecord->delete(); } } else { $path_parts = pathinfo(wp_all_import_get_absolute_path($file['path'])); if (!empty($path_parts['dirname'])) { $path_all_parts = explode('/', $path_parts['dirname']); $dirname = array_pop($path_all_parts); if (wp_all_import_isValidMd5($dirname)) { $required_dirs[] = $path_parts['dirname']; } } } $importRecord->clear(); } } $uploads_dir = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY; if (($dir = @opendir($uploads_dir . DIRECTORY_SEPARATOR)) !== false or ($dir = @opendir($uploads_dir)) !== false) { while (($file = @readdir($dir)) !== false) { $filePath = $uploads_dir . DIRECTORY_SEPARATOR . $file; if (is_dir($filePath) and !in_array($filePath, $required_dirs) and !in_array($file, array('.', '..'))) { wp_all_import_rmdir($filePath); } } } wp_redirect(add_query_arg('pmxi_nt', urlencode($msg), $this->baseUrl)); die; }