Ejemplo n.º 1
0
 /**
  * Step #1: Choose File
  */
 public function index($post_type = 'post')
 {
     $default = PMAI_Plugin::get_default_import_options();
     $this->data['id'] = $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
         $post = $this->input->post((isset(PMXI_Plugin::$session->options) ? PMXI_Plugin::$session->options : array()) + $default);
     } else {
         $post = $this->input->post($this->data['import']->options + $default);
     }
     $this->data['is_loaded_template'] = !empty(PMXI_Plugin::$session->is_loaded_template) ? PMXI_Plugin::$session->is_loaded_template : false;
     $load_options = $this->input->post('load_template');
     if ($load_options) {
         // init form with template selected
         $template = new PMXI_Template_Record();
         if (!$template->getById($this->data['is_loaded_template'])->isEmpty()) {
             $post = (!empty($template->options) ? $template->options : array()) + $default;
         }
     } elseif ($load_options == -1) {
         $post = $default;
     }
     $this->data['post_type'] = $post_type;
     $this->data['post'] =& $post;
     $this->render();
 }
 function helper_current_field_values($default = array())
 {
     if (empty($default)) {
         $options = array('mapping' => array(), 'xpaths' => array());
         foreach ($this->fields as $field_slug => $field_params) {
             $options[$field_slug] = '';
             if (!empty($field_params['enum_values'])) {
                 foreach ($field_params['enum_values'] as $key => $value) {
                     $options[$field_slug] = is_numeric($key) ? (int) $key : $key;
                     break;
                 }
             }
         }
         $default = array($this->slug => $options);
     }
     $input = new PMXI_Input();
     $id = $input->get('id');
     $import = new PMXI_Import_Record();
     if (!$id or $import->getById($id)->isEmpty()) {
         // specified import is not found
         $post = $input->post($default);
     } else {
         $post = $input->post($import->options + $default);
     }
     $is_loaded_template = !empty(PMXI_Plugin::$session->is_loaded_template) ? PMXI_Plugin::$session->is_loaded_template : false;
     $load_options = $input->post('load_template');
     if ($load_options) {
         // init form with template selected
         $template = new PMXI_Template_Record();
         if (!$template->getById($is_loaded_template)->isEmpty()) {
             $post = (!empty($template->options) ? $template->options : array()) + $default;
         }
     } elseif ($load_options == -1) {
         $post = $default;
     }
     return $post;
 }
Ejemplo n.º 3
0
 /**
  * Step #4: Options
  */
 public function options()
 {
     $default = PMXI_Plugin::get_default_import_options();
     if ($this->isWizard) {
         $this->data['source_type'] = PMXI_Plugin::$session->source['type'];
         $DefaultOptions = (isset(PMXI_Plugin::$session->options) ? PMXI_Plugin::$session->options : array()) + $default;
         foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
             if (class_exists($class)) {
                 $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
             }
         }
         if (PMXI_Plugin::$session->options['custom_type'] != 'import_users') {
             if (empty(PMXI_Plugin::$session->options['title'])) {
                 $this->warnings->add('form-validation', __('<strong>Warning:</strong> your title is blank.'));
             }
             if (empty(PMXI_Plugin::$session->options['content'])) {
                 $this->warnings->add('form-validation', __('<strong>Warning:</strong> your content is blank.'));
             }
         }
         if (empty(PMXI_Plugin::$session->options['unique_key'])) {
             $keys_black_list = array('programurl');
             if (empty(PMXI_Plugin::$session->deligate)) {
                 $DefaultOptions['unique_key'] = PMXI_Plugin::$session->options['title'];
             }
             // auto searching ID element
             if (!empty($this->data['dom']) and empty(PMXI_Plugin::$session->deligate)) {
                 $this->find_unique_key($this->data['dom']->documentElement);
                 if (!empty($this->_unique_key)) {
                     foreach ($keys_black_list as $key => $value) {
                         $DefaultOptions['unique_key'] = str_replace('{' . $value . '[1]}', "", $DefaultOptions['unique_key']);
                     }
                     foreach ($this->_unique_key as $key) {
                         if (stripos($key, 'id') !== false) {
                             $DefaultOptions['unique_key'] .= ' - {' . $key . '[1]}';
                             break;
                         }
                     }
                     foreach ($this->_unique_key as $key) {
                         if (stripos($key, 'url') !== false or stripos($key, 'sku') !== false or stripos($key, 'ref') !== false) {
                             if (!in_array($key, $keys_black_list)) {
                                 $DefaultOptions['unique_key'] .= ' - {' . $key . '[1]}';
                                 break;
                             }
                         }
                     }
                 }
                 $DefaultOptions['unique_key'] = apply_filters('pmxi_unique_key', $DefaultOptions['unique_key'], PMXI_Plugin::$session->options);
             }
         } else {
             $DefaultOptions['unique_key'] = PMXI_Plugin::$session->options['unique_key'];
         }
         if ($DefaultOptions['custom_type'] == "product" and class_exists('PMWI_Plugin') and $DefaultOptions['wizard_type'] != 'new') {
             $DefaultOptions['duplicate_indicator'] = 'custom field';
             $DefaultOptions['custom_duplicate_name'] = '_sku';
         }
         $post = $this->input->post($DefaultOptions);
     } else {
         $this->data['source_type'] = $this->data['import']->type;
         $DefaultOptions = is_array($this->data['import']->options) ? $this->data['import']->options + $default : $default;
         foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
             if (class_exists($class)) {
                 $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
             }
         }
         $source = array('name' => $this->data['import']->name, 'type' => $this->data['import']->type, 'path' => wp_all_import_get_relative_path($this->data['import']->path), 'root_element' => $this->data['import']->root_element);
         PMXI_Plugin::$session->set('source', $source);
         $post = $this->input->post($DefaultOptions);
     }
     $this->data['post'] =& $post;
     PMXI_Plugin::$session->set('options', $post);
     if ($this->input->post('is_submitted')) {
         check_admin_referer('options', '_wpnonce_options');
         if ($post['is_import_specified']) {
             if (empty($post['import_specified'])) {
                 $this->errors->add('form-validation', __('Records to import must be specified or uncheck `Import only specified records` option to process all records', 'wp_all_import_plugin'));
             } else {
                 $chanks = preg_split('% *, *%', $post['import_specified']);
                 foreach ($chanks as $chank) {
                     if (!preg_match('%^([1-9]\\d*)( *- *([1-9]\\d*))?$%', $chank, $mtch)) {
                         $this->errors->add('form-validation', __('Wrong format of `Import only specified records` value', 'wp_all_import_plugin'));
                         break;
                     } elseif (isset($mtch[3]) and intval($mtch[3]) > PMXI_Plugin::$session->count) {
                         $this->errors->add('form-validation', __('One of the numbers in `Import only specified records` value exceeds record quantity in XML file', 'wp_all_import_plugin'));
                         break;
                     }
                 }
             }
         }
         if ('manual' != $post['duplicate_matching'] and '' == $post['unique_key']) {
             $this->errors->add('form-validation', __('Expression for `Post Unique Key` must be set, use the same expression as specified for post title if you are not sure what to put there', 'wp_all_import_plugin'));
         } elseif ('manual' != $post['duplicate_matching']) {
             $this->_validate_template($post['unique_key'], __('Post Unique Key', 'wp_all_import_plugin'));
         }
         if ('manual' == $post['duplicate_matching'] and 'custom field' == $post['duplicate_indicator']) {
             if ('' == $post['custom_duplicate_name']) {
                 $this->errors->add('form-validation', __('Custom field name must be specified.', 'wp_all_import_plugin'));
             }
             if ('' == $post['custom_duplicate_value']) {
                 $this->errors->add('form-validation', __('Custom field value must be specified.', 'wp_all_import_plugin'));
             }
         }
         // Categories/taxonomies logic
         if ($post['update_categories_logic'] == 'only' and !empty($post['taxonomies_only_list'])) {
             $post['taxonomies_list'] = explode(",", $post['taxonomies_only_list']);
         } elseif ($post['update_categories_logic'] == 'all_except' and !empty($post['taxonomies_except_list'])) {
             $post['taxonomies_list'] = explode(",", $post['taxonomies_except_list']);
         }
         // Custom fields logic
         if ($post['update_custom_fields_logic'] == 'only' and !empty($post['custom_fields_only_list'])) {
             $post['custom_fields_list'] = explode(",", $post['custom_fields_only_list']);
         } elseif ($post['update_custom_fields_logic'] == 'all_except' and !empty($post['custom_fields_except_list'])) {
             $post['custom_fields_list'] = explode(",", $post['custom_fields_except_list']);
         }
         $upload_result = false;
         if (!$this->isWizard) {
             // File Path validation
             switch ($this->input->post('new_type')) {
                 case 'upload':
                     $filePath = $this->input->post('filepath');
                     if ($this->data['import']['path'] != $filePath) {
                         $uploader = new PMXI_Upload($filePath, $this->errors);
                         $upload_result = $uploader->upload();
                     }
                     break;
                 case 'url':
                     $filePath = $this->input->post('url');
                     if ($this->data['import']['path'] != $filePath) {
                         $filesXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<data><node></node></data>";
                         $filePaths = XmlImportParser::factory($filesXML, '/data/node', $filePath, $file)->parse();
                         $tmp_files[] = $file;
                         foreach ($tmp_files as $tmp_file) {
                             // remove all temporary files created
                             @unlink($tmp_file);
                         }
                         $file_to_import = $filePath;
                         if (!empty($filePaths) and is_array($filePaths)) {
                             $file_to_import = array_shift($filePaths);
                         }
                         $uploader = new PMXI_Upload($file_to_import, $this->errors);
                         $upload_result = $uploader->url($this->data['import']->feed_type, $filePath);
                     }
                     break;
                 case 'file':
                     $filePath = $this->input->post('file');
                     if ($this->data['import']['path'] != $filePath) {
                         $uploader = new PMXI_Upload($filePath, $this->errors);
                         $upload_result = $uploader->file();
                     }
                     break;
                 default:
                     $this->errors->add('form-validation', __('WP All Import doesn\'t support this import type.', 'wp_all_import_plugin'));
                     break;
             }
             if ($upload_result instanceof WP_Error) {
                 $this->errors = $upload_result;
             } elseif ($upload_result !== false and $this->data['import']['path'] != $filePath) {
                 $file = new PMXI_Chunk($upload_result['filePath'], array('element' => !empty($this->data['import']->root_element) ? $this->data['import']->root_element : ''));
                 $root_element = '';
                 if (!empty($file->options['element'])) {
                     $root_element = $file->options['element'];
                     $baseXpath = $this->data['import']->xpath;
                     $loop = 0;
                     // 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=\"" . $this->data['import']['options']['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', $this->data['import']['options']['encoding']);
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = @$xpath->query($baseXpath) and $elements->length) {
                                 $loop += $elements->length;
                             }
                             unset($dom, $xpath, $elements);
                         }
                     }
                     unset($file);
                     if ((int) $loop === 0) {
                         $this->warnings->add('form-validation', __('<strong>Warning:</strong> this file does not have the same structure as the last file associated with this import. WP All Import won\'t be able to import this file with your current settings. Probably you\'ll need to adjust your XPath in the "Configure Advanced Settings" box below, and reconfigure your import by clicking "Edit" on the Manage Imports page.', 'wp_all_import_plugin'));
                         $file = new PMXI_Chunk($upload_result['filePath'], array('element' => !empty($upload_result['root_element']) ? $upload_result['root_element'] : ''));
                         if (!empty($file->options['element'])) {
                             $root_element = $file->options['element'];
                             $baseXpath = '/' . $upload_result['root_element'];
                             $loop = 0;
                             // 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=\"" . $this->data['import']['options']['encoding'] . "\"?>" . "\n" . $xml;
                                     $dom = new DOMDocument('1.0', $this->data['import']['options']['encoding']);
                                     $old = libxml_use_internal_errors(true);
                                     $dom->loadXML($xml);
                                     libxml_use_internal_errors($old);
                                     $xpath = new DOMXPath($dom);
                                     if ($elements = @$xpath->query($baseXpath) and $elements->length) {
                                         $loop += $elements->length;
                                     }
                                     unset($dom, $xpath, $elements);
                                 }
                             }
                             unset($file);
                             if ($loop) {
                                 $this->data['import']->set(array('count' => $loop))->save();
                             }
                         }
                     }
                     $upload_result['root_element'] = $root_element;
                     $post['delimiter'] = !empty($upload_result['is_csv']) ? $upload_result['is_csv'] : '';
                 } else {
                     $this->errors->add('form-validation', __('Root element not found for uploaded feed.', 'wp_all_import_plugin'));
                 }
             }
         }
         $this->errors = apply_filters('pmxi_options_validation', $this->errors, $post, $this->data['import']);
         if (!$this->errors->get_error_codes()) {
             // no validation errors found
             // Attributes fields logic
             $post = apply_filters('pmxi_save_options', $post);
             if ($this->isWizard) {
                 PMXI_Plugin::$session->set('options', $post);
                 PMXI_Plugin::$session->save_data();
                 // update import template with final settings
                 if (PMXI_Plugin::$session->saved_template) {
                     $template = new PMXI_Template_Record();
                     $template->getById(PMXI_Plugin::$session->saved_template)->set(array('options' => $post))->save();
                 }
                 if (!$this->input->post('save_only')) {
                     wp_redirect(add_query_arg('action', 'confirm', $this->baseUrl));
                     die;
                 } else {
                     $import = $this->data['update_previous'];
                     $is_update = !$import->isEmpty();
                     $import->set(PMXI_Plugin::$session->source + array('xpath' => PMXI_Plugin::$session->xpath, 'options' => PMXI_Plugin::$session->options, 'count' => PMXI_Plugin::$session->count, 'friendly_name' => $this->data['post']['friendly_name']))->save();
                     $history_file = new PMXI_File_Record();
                     $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => wp_all_import_get_relative_path(PMXI_Plugin::$session->filePath), 'registered_on' => date('Y-m-d H:i:s')))->save();
                     wp_redirect(add_query_arg(array('page' => 'pmxi-admin-manage', 'pmxi_nt' => urlencode($is_update ? __('Import updated', 'wp_all_import_plugin') : __('Import created', 'wp_all_import_plugin'))), admin_url('admin.php')));
                     die;
                 }
             } else {
                 $xpath = $this->input->post('xpath');
                 $toUpdate = array('friendly_name' => $this->data['post']['friendly_name'], 'xpath' => $this->input->post('xpath'), 'settings_update_on' => date('Y-m-d H:i:s'));
                 // detecting root element
                 if ($xpath != $this->data['import']->xpath) {
                     $xpath_elements = explode('[', $xpath);
                     $xpath_parts = explode('/', $xpath_elements[0]);
                     $toUpdate['root_element'] = $xpath_parts[1];
                 }
                 $this->data['import']->set('options', $post)->set($toUpdate)->save();
                 // set new import file
                 switch ($this->input->post('new_type')) {
                     case 'upload':
                         $filePath = $this->input->post('filepath');
                         $source = array('name' => basename($filePath), 'type' => 'upload', 'path' => $filePath);
                         break;
                     case 'url':
                         $filePath = $this->input->post('url');
                         $source = array('name' => basename(parse_url($filePath, PHP_URL_PATH)), 'type' => 'url', 'path' => $filePath);
                         break;
                     case 'file':
                         $wp_uploads = wp_upload_dir();
                         $filePath = $this->input->post('file');
                         $source = array('name' => basename($filePath), 'type' => 'file', 'path' => $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR . $filePath);
                         break;
                         /*case 'ftp':
                         		$ftp = $this->input->post('ftp');
                         		$filePath = preg_replace('%://([^@/]*@)?%', '://' . rawurlencode($ftp['user']) . ':' . rawurlencode($ftp['pass']) . '@', $ftp['url'], 1);	
                         		$source = array(
                         			'name' => basename($filePath),
                         			'type' => 'ftp',
                         			'path' => $filePath,							
                         		);
                         		break;*/
                 }
                 $source['path'] = wp_all_import_get_relative_path($source['path']);
                 // if new file is successfully uploaded
                 if (!empty($upload_result['filePath'])) {
                     // unlink previous files
                     $history = new PMXI_File_List();
                     $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $this->data['import']->id), 'id DESC');
                     if ($history->count()) {
                         foreach ($history as $file) {
                             $history_file_path = wp_all_import_get_absolute_path($file['path']);
                             if (@file_exists($history_file_path) and $history_file_path != $upload_result['filePath']) {
                                 if (in_array($this->data['import']->type, array('upload'))) {
                                     wp_all_import_remove_source($history_file_path, false);
                                 } else {
                                     wp_all_import_remove_source($history_file_path);
                                 }
                             }
                             $history_file = new PMXI_File_Record();
                             $history_file->getBy('id', $file['id']);
                             if (!$history_file->isEmpty()) {
                                 $history_file->delete();
                             }
                         }
                     }
                     $history_file = new PMXI_File_Record();
                     $history_file->set(array('name' => $this->data['import']->name, 'import_id' => $this->data['import']->id, 'path' => wp_all_import_get_relative_path($upload_result['filePath']), 'registered_on' => date('Y-m-d H:i:s')))->save();
                 }
                 if (!$this->warnings->get_error_codes()) {
                     $this->data['import']->set($source)->save();
                     wp_redirect(add_query_arg(array('page' => 'pmxi-admin-manage', 'pmxi_nt' => urlencode(__('Configuration updated', 'wp_all_import_plugin'))) + array_intersect_key($_GET, array_flip($this->baseUrlParamNames)), admin_url('admin.php')));
                     die;
                 } else {
                     $source['root_element'] = $upload_result['root_element'];
                     PMXI_Plugin::$session->set('source', $source);
                     $this->data['import']->set(array_merge($source, array('xpath' => '/' . $upload_result['root_element'])))->save();
                 }
             }
         }
     }
     PMXI_Plugin::$session->save_data();
     $this->data['existing_meta_keys'] = array();
     if (!in_array($post['custom_type'], array('import_users'))) {
         global $wpdb;
         // Get all meta keys for requested post type
         $hide_fields = array('_wp_page_template', '_edit_lock', '_edit_last', '_wp_trash_meta_status', '_wp_trash_meta_time');
         $records = get_posts(array('post_type' => $post['custom_type']));
         if (!empty($records)) {
             foreach ($records as $record) {
                 $record_meta = get_post_meta($record->ID, '');
                 if (!empty($record_meta)) {
                     foreach ($record_meta as $record_meta_key => $record_meta_value) {
                         if (!in_array($record_meta_key, $this->data['existing_meta_keys']) and !in_array($record_meta_key, $hide_fields)) {
                             $this->data['existing_meta_keys'][] = $record_meta_key;
                         }
                     }
                 }
             }
         }
         // Get existing product attributes
         $existing_attributes = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' LIMIT 0 , 50");
         $this->data['existing_attributes'] = array();
         if (!empty($existing_attributes)) {
             foreach ($existing_attributes as $key => $existing_attribute) {
                 $existing_attribute = maybe_unserialize($existing_attribute->meta_value);
                 if (!empty($existing_attribute) and is_array($existing_attribute)) {
                     foreach ($existing_attribute as $key => $value) {
                         if (strpos($key, "pa_") === false and !in_array($key, $this->data['existing_attributes'])) {
                             $this->data['existing_attributes'][] = $key;
                         }
                     }
                 }
             }
         }
     }
     $this->render();
 }
Ejemplo n.º 4
0
 public function index()
 {
     $this->data['post'] = $post = $this->input->post(PMXI_Plugin::getInstance()->getOption());
     /*$addons = new PMXI_Admin_Addons();
     
     		$this->data['addons'] = $addons->get_premium_addons();*/
     $this->data['addons']['PMXI_Plugin'] = array('title' => __('WP All Import', 'wp_all_import_plugin'), 'active' => class_exists('PMXI_Plugin') and defined('PMXI_EDITION') and PMXI_EDITION == 'paid');
     $this->data['addons'] = array_reverse($this->data['addons']);
     if ($this->input->post('is_settings_submitted')) {
         // save settings form
         check_admin_referer('edit-settings', '_wpnonce_edit-settings');
         if (!preg_match('%^\\d+$%', $post['history_file_count'])) {
             $this->errors->add('form-validation', __('History File Count must be a non-negative integer', 'wp_all_import_plugin'));
         }
         if (!preg_match('%^\\d+$%', $post['history_file_age'])) {
             $this->errors->add('form-validation', __('History Age must be a non-negative integer', 'wp_all_import_plugin'));
         }
         if (empty($post['html_entities'])) {
             $post['html_entities'] = 0;
         }
         if (empty($post['utf8_decode'])) {
             $post['utf8_decode'] = 0;
         }
         if (!$this->errors->get_error_codes()) {
             // no validation errors detected
             PMXI_Plugin::getInstance()->updateOption($post);
             if (empty($_POST['pmxi_license_activate']) and empty($_POST['pmxi_license_deactivate'])) {
                 foreach ($this->data['addons'] as $class => $addon) {
                     $post['statuses'][$class] = $this->check_license($class);
                 }
                 PMXI_Plugin::getInstance()->updateOption($post);
             }
             isset($_POST['pmxi_license_activate']) and $this->activate_licenses();
             $files = new PMXI_File_List();
             $files->sweepHistory();
             // adjust file history to new settings specified
             wp_redirect(add_query_arg('pmxi_nt', urlencode(__('Settings saved', 'wp_all_import_plugin')), $this->baseUrl));
             die;
         }
     }
     /*else{			
     
     			foreach ($this->data['addons'] as $class => $addon) {
     				$post['statuses'][$class] = $this->check_license($class);
     			}								
     
     			PMXI_Plugin::getInstance()->updateOption($post);	
     		}*/
     if ($this->input->post('is_templates_submitted')) {
         // delete templates form
         check_admin_referer('delete-templates', '_wpnonce_delete-templates');
         if ($this->input->post('import_templates')) {
             if (!empty($_FILES)) {
                 $file_name = $_FILES['template_file']['name'];
                 $file_size = $_FILES['template_file']['size'];
                 $tmp_name = $_FILES['template_file']['tmp_name'];
                 if (isset($file_name)) {
                     $filename = stripslashes($file_name);
                     $extension = strtolower(pmxi_getExtension($filename));
                     if ($extension != "txt") {
                         $this->errors->add('form-validation', __('Unknown File extension. Only txt files are permitted', 'wp_all_import_plugin'));
                     } else {
                         $import_data = @file_get_contents($tmp_name);
                         if (!empty($import_data)) {
                             $templates_data = json_decode($import_data, true);
                             if (!empty($templates_data)) {
                                 $template = new PMXI_Template_Record();
                                 foreach ($templates_data as $template_data) {
                                     unset($template_data['id']);
                                     $template->clear()->set($template_data)->insert();
                                 }
                                 wp_redirect(add_query_arg('pmxi_nt', urlencode(sprintf(_n('%d template imported', '%d templates imported', count($templates_data), 'wp_all_import_plugin'), count($templates_data))), $this->baseUrl));
                                 die;
                             } else {
                                 $this->errors->add('form-validation', __('Wrong imported data format', 'wp_all_import_plugin'));
                             }
                         } else {
                             $this->errors->add('form-validation', __('File is empty or doesn\'t exests', 'wp_all_import_plugin'));
                         }
                     }
                 } else {
                     $this->errors->add('form-validation', __('Undefined entry!', 'wp_all_import_plugin'));
                 }
             } else {
                 $this->errors->add('form-validation', __('Please select file.', 'wp_all_import_plugin'));
             }
         } else {
             $templates_ids = $this->input->post('templates', array());
             if (empty($templates_ids)) {
                 $this->errors->add('form-validation', __('Templates must be selected', 'wp_all_import_plugin'));
             }
             if (!$this->errors->get_error_codes()) {
                 // no validation errors detected
                 if ($this->input->post('delete_templates')) {
                     $template = new PMXI_Template_Record();
                     foreach ($templates_ids as $template_id) {
                         $template->clear()->set('id', $template_id)->delete();
                     }
                     wp_redirect(add_query_arg('pmxi_nt', urlencode(sprintf(_n('%d template deleted', '%d templates deleted', count($templates_ids), 'wp_all_import_plugin'), count($templates_ids))), $this->baseUrl));
                     die;
                 }
                 if ($this->input->post('export_templates')) {
                     $export_data = array();
                     $template = new PMXI_Template_Record();
                     foreach ($templates_ids as $template_id) {
                         $export_data[] = $template->clear()->getBy('id', $template_id)->toArray(TRUE);
                     }
                     $uploads = wp_upload_dir();
                     $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY;
                     $export_file_name = "templates_" . uniqid() . ".txt";
                     file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($export_data));
                     PMXI_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name);
                 }
             }
         }
     }
     $this->render();
 }
Ejemplo n.º 5
0
 /**
  * Step #4: Options
  */
 public function options()
 {
     $default = PMXI_Plugin::get_default_import_options();
     if ($this->isWizard) {
         $this->data['source_type'] = PMXI_Plugin::$session->data['pmxi_import']['source']['type'];
         $default['unique_key'] = PMXI_Plugin::$session->data['pmxi_import']['template']['title'];
         $keys_black_list = array('programurl');
         // auto searching ID element
         if (!empty($this->data['dom'])) {
             $this->find_unique_key($this->data['dom']->documentElement);
             if (!empty($this->_unique_key)) {
                 foreach ($keys_black_list as $key => $value) {
                     $default['unique_key'] = str_replace('{' . $value . '[1]}', "", $default['unique_key']);
                 }
                 foreach ($this->_unique_key as $key) {
                     if (stripos($key, 'id') !== false) {
                         $default['unique_key'] .= ' - {' . $key . '[1]}';
                         break;
                     }
                 }
                 foreach ($this->_unique_key as $key) {
                     if (stripos($key, 'url') !== false or stripos($key, 'sku') !== false or stripos($key, 'ref') !== false) {
                         if (!in_array($key, $keys_black_list)) {
                             $default['unique_key'] .= ' - {' . $key . '[1]}';
                             break;
                         }
                     }
                 }
             }
         }
         $DefaultOptions = (isset(PMXI_Plugin::$session->data['pmxi_import']['options']) ? PMXI_Plugin::$session->data['pmxi_import']['options'] : array()) + $default;
         foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
             if (class_exists($class)) {
                 $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
             }
         }
         $post = $this->input->post(apply_filters('pmxi_options_options', $DefaultOptions, $this->isWizard));
     } else {
         $this->data['source_type'] = $this->data['import']->type;
         $DefaultOptions = $this->data['import']->options + $default;
         foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
             if (class_exists($class)) {
                 $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
             }
         }
         $post = $this->input->post(apply_filters('pmxi_options_options', $DefaultOptions, $this->isWizard));
     }
     $this->data['post'] =& $post;
     // Get All meta keys in the system
     $this->data['meta_keys'] = $keys = new PMXI_Model_List();
     $keys->setTable(PMXI_Plugin::getInstance()->getWPPrefix() . 'postmeta');
     $keys->setColumns('meta_id', 'meta_key')->getBy(NULL, "meta_id", NULL, NULL, "meta_key");
     global $wpdb;
     $existing_attributes = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes'");
     $this->data['existing_attributes'] = array();
     if (!empty($existing_attributes)) {
         foreach ($existing_attributes as $key => $existing_attribute) {
             $existing_attribute = maybe_unserialize($existing_attribute->meta_value);
             if (!empty($existing_attribute) and is_array($existing_attribute)) {
                 foreach ($existing_attribute as $key => $value) {
                     if (strpos($key, "pa_") === false and !in_array($key, $this->data['existing_attributes'])) {
                         $this->data['existing_attributes'][] = $key;
                     }
                 }
             }
         }
     }
     $load_template = $this->input->post('load_template');
     if ($load_template) {
         // init form with template selected
         PMXI_Plugin::$session['pmxi_import']['is_loaded_template'] = $load_template;
         $template = new PMXI_Template_Record();
         if (!$template->getById($load_template)->isEmpty()) {
             $post = (!empty($template->options) ? $template->options : array()) + $default;
         }
     }
     $this->data['get'] = $this->input->get(array('pmxi-cpt' => !empty($post['custom_type']) ? $post['custom_type'] : $post['type']));
     if ($load_template == -1) {
         PMXI_Plugin::$session['pmxi_import']['is_loaded_template'] = 0;
         $post = $default;
     } elseif ($this->input->post('is_submitted')) {
         check_admin_referer('options', '_wpnonce_options');
         // Categories/taxonomies logic
         if ($post['update_categories_logic'] == 'only') {
             $post['taxonomies_list'] = explode(",", $post['taxonomies_only_list']);
         } elseif ($post['update_categories_logic'] == 'all_except') {
             $post['taxonomies_list'] = explode(",", $post['taxonomies_except_list']);
         }
         // Custom fields logic
         if ($post['update_custom_fields_logic'] == 'only') {
             $post['custom_fields_list'] = explode(",", $post['custom_fields_only_list']);
         } elseif ($post['update_custom_fields_logic'] == 'all_except') {
             $post['custom_fields_list'] = explode(",", $post['custom_fields_except_list']);
         }
         // Attributes fields logic
         $post = apply_filters('pmxi_save_options', $post);
         // remove entires where both custom_name and custom_value are empty
         $not_empty = array_flip(array_values(array_merge(array_keys(array_filter($post['custom_name'], 'strlen')), array_keys(array_filter($post['custom_value'], 'strlen')))));
         $post['custom_name'] = array_intersect_key($post['custom_name'], $not_empty);
         $post['custom_value'] = array_intersect_key($post['custom_value'], $not_empty);
         // validate
         if (array_keys(array_filter($post['custom_name'], 'strlen')) != array_keys(array_filter($post['custom_value'], 'strlen')) and !count(array_filter($post['custom_format']))) {
             $this->errors->add('form-validation', __('Both name and value must be set for all custom parameters', 'pmxi_plugin'));
         } else {
             foreach ($post['custom_name'] as $custom_name) {
                 $this->_validate_template($custom_name, __('Custom Field Name', 'pmxi_plugin'));
             }
             foreach ($post['custom_value'] as $key => $custom_value) {
                 if (empty($post['custom_format'][$key])) {
                     $this->_validate_template($custom_value, __('Custom Field Value', 'pmxi_plugin'));
                 }
             }
         }
         if ($post['type'] == "post" and $post['custom_type'] == "product" and class_exists('PMWI_Plugin')) {
             // remove entires where both custom_name and custom_value are empty
             $not_empty = array_flip(array_values(array_merge(array_keys(array_filter($post['attribute_name'], 'strlen')), array_keys(array_filter($post['attribute_value'], 'strlen')))));
             $post['attribute_name'] = array_intersect_key($post['attribute_name'], $not_empty);
             $post['attribute_value'] = array_intersect_key($post['attribute_value'], $not_empty);
             // validate
             if (array_keys(array_filter($post['attribute_name'], 'strlen')) != array_keys(array_filter($post['attribute_value'], 'strlen'))) {
                 $this->errors->add('form-validation', __('Both name and value must be set for all woocommerce attributes', 'pmxi_plugin'));
             } else {
                 foreach ($post['attribute_name'] as $attribute_name) {
                     $this->_validate_template($attribute_name, __('Attribute Field Name', 'pmxi_plugin'));
                 }
                 foreach ($post['attribute_value'] as $custom_value) {
                     $this->_validate_template($custom_value, __('Attribute Field Value', 'pmxi_plugin'));
                 }
             }
         }
         if ('page' == $post['type'] and !preg_match('%^(-?\\d+)?$%', $post['order'])) {
             $this->errors->add('form-validation', __('Order must be an integer number', 'pmxi_plugin'));
         }
         if ('post' == $post['type']) {
             /*'' == $post['categories'] or $this->_validate_template($post['categories'], __('Categories', 'pmxi_plugin'));*/
             '' == $post['tags'] or $this->_validate_template($post['tags'], __('Tags', 'pmxi_plugin'));
         }
         if ('specific' == $post['date_type']) {
             '' == $post['date'] or $this->_validate_template($post['date'], __('Date', 'pmxi_plugin'));
         } else {
             '' == $post['date_start'] or $this->_validate_template($post['date_start'], __('Start Date', 'pmxi_plugin'));
             '' == $post['date_end'] or $this->_validate_template($post['date_end'], __('Start Date', 'pmxi_plugin'));
         }
         if ('' == $post['tags_delim']) {
             $this->errors->add('form-validation', __('Tag list delimiter must cannot be empty', 'pmxi_plugin'));
         }
         if ($post['is_import_specified']) {
             if (empty($post['import_specified'])) {
                 $this->errors->add('form-validation', __('Records to import must be specified or uncheck `Import only specified records` option to process all records', 'pmxi_plugin'));
             } else {
                 $chanks = preg_split('% *, *%', $post['import_specified']);
                 foreach ($chanks as $chank) {
                     if (!preg_match('%^([1-9]\\d*)( *- *([1-9]\\d*))?$%', $chank, $mtch)) {
                         $this->errors->add('form-validation', __('Wrong format of `Import only specified records` value', 'pmxi_plugin'));
                         break;
                     } elseif (isset($mtch[3]) and intval($mtch[3]) > PMXI_Plugin::$session->data['pmxi_import']['count']) {
                         $this->errors->add('form-validation', __('One of the numbers in `Import only specified records` value exceeds record quantity in XML file', 'pmxi_plugin'));
                         break;
                     }
                 }
             }
         }
         if ('' == $post['unique_key']) {
             $this->errors->add('form-validation', __('Expression for `Post Unique Key` must be set, use the same expression as specified for post title if you are not sure what to put there', 'pmxi_plugin'));
         } else {
             $this->_validate_template($post['unique_key'], __('Post Unique Key', 'pmxi_plugin'));
         }
         if ('manual' == $post['duplicate_matching'] and 'custom field' == $post['duplicate_indicator']) {
             if ('' == $post['custom_duplicate_name']) {
                 $this->errors->add('form-validation', __('Custom field name must be specified.', 'pmxi_plugin'));
             }
             if ('' == $post['custom_duplicate_value']) {
                 $this->errors->add('form-validation', __('Custom field value must be specified.', 'pmxi_plugin'));
             }
         }
         $this->errors = apply_filters('pmxi_options_validation', $this->errors, $post, $this->data['import']);
         if (!$this->errors->get_error_codes()) {
             // no validation errors found
             // assign some defaults
             '' !== $post['date'] or $post['date'] = 'now';
             '' !== $post['date_start'] or $post['date_start'] = 'now';
             '' !== $post['date_end'] or $post['date_end'] = 'now';
             if ($this->input->post('name')) {
                 // save template in database
                 $template = new PMXI_Template_Record();
                 $template->getByName($this->input->post('name'))->set(array('name' => $this->input->post('name'), 'options' => $post))->save();
             }
             if ($this->isWizard) {
                 PMXI_Plugin::$session['pmxi_import']['options'] = $post;
                 pmxi_session_commit();
                 if (!$this->input->post('save_only')) {
                     wp_redirect(add_query_arg('action', 'process', $this->baseUrl));
                     die;
                 } else {
                     $import = $this->data['update_previous'];
                     $is_update = !$import->isEmpty();
                     $import->set(PMXI_Plugin::$session->data['pmxi_import']['source'] + array('xpath' => PMXI_Plugin::$session->data['pmxi_import']['xpath'], 'template' => PMXI_Plugin::$session->data['pmxi_import']['template'], 'options' => PMXI_Plugin::$session->data['pmxi_import']['options'], 'count' => PMXI_Plugin::$session->data['pmxi_import']['count'], 'friendly_name' => $this->data['post']['friendly_name']))->save();
                     $history_file = new PMXI_File_Record();
                     $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => PMXI_Plugin::$session->data['pmxi_import']['filePath'], 'contents' => $this->get_xml(), 'registered_on' => date('Y-m-d H:i:s')))->save();
                     pmxi_session_unset();
                     wp_redirect(add_query_arg(array('page' => 'pmxi-admin-manage', 'pmlc_nt' => urlencode($is_update ? __('Import updated', 'pmxi_plugin') : __('Import created', 'pmxi_plugin'))), admin_url('admin.php')));
                     die;
                 }
             } else {
                 $this->data['import']->set('options', $post)->set(array('scheduled' => '', 'friendly_name' => $this->data['post']['friendly_name']))->save();
                 wp_redirect(add_query_arg(array('page' => 'pmxi-admin-manage', 'pmlc_nt' => urlencode(__('Options updated', 'pmxi_plugin'))) + array_intersect_key($_GET, array_flip($this->baseUrlParamNames)), admin_url('admin.php')));
                 die;
             }
         }
     }
     !empty($post['custom_name']) or $post['custom_name'] = array('') and $post['custom_value'] = array('');
     if ($post['type'] == "product" and class_exists('PMWI_Plugin')) {
         !empty($post['attribute_name']) or $post['attribute_name'] = array('') and $post['attribute_value'] = array('');
     }
     pmxi_session_commit();
     $this->render();
 }
Ejemplo n.º 6
0
 public function get_template()
 {
     $nonce = !empty($_REQUEST['_wpnonce']) ? $_REQUEST['_wpnonce'] : '';
     if (!wp_verify_nonce($nonce, '_wpnonce-download_template')) {
         die(__('Security check', 'wp_all_export_plugin'));
     } else {
         $id = $this->input->get('id');
         $export = new PMXE_Export_Record();
         $filepath = '';
         $export_data = array();
         if (!$export->getById($id)->isEmpty()) {
             $template = new PMXI_Template_Record();
             if (!empty($export->options['template_name'])) {
                 $template->getByName($export->options['template_name']);
                 if (!$template->isEmpty()) {
                     $export_data[] = $template->toArray(TRUE);
                     $uploads = wp_upload_dir();
                     $targetDir = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::TEMP_DIRECTORY;
                     $export_file_name = "templates_" . uniqid() . ".txt";
                     file_put_contents($targetDir . DIRECTORY_SEPARATOR . $export_file_name, json_encode($export_data));
                     PMXI_download::csv($targetDir . DIRECTORY_SEPARATOR . $export_file_name);
                 }
             }
         }
     }
 }
Ejemplo n.º 7
0
 /**
  * Import all files matched by path
  * @param callback[optional] $logger Method where progress messages are submmitted
  * @return PMXI_Import_Record
  * @chainable
  */
 public function execute($logger = NULL, $cron = false)
 {
     $this->set('registered_on', date('Y-m-d H:i:s'))->save();
     // update registered_on to indicated that job has been exectured even if no files are going to be imported by the rest of the method
     $wp_uploads = wp_upload_dir();
     $this->set(array('processing' => 1))->update();
     // lock cron requests
     wp_reset_postdata();
     XmlExportEngine::$exportOptions = $this->options;
     XmlExportEngine::$is_user_export = $this->options['is_user_export'];
     if ('advanced' == $this->options['export_type']) {
         if (XmlExportEngine::$is_user_export) {
             $exportQuery = eval('return new WP_User_Query(array(' . $this->options['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $this->exported . ', \'number\' => ' . $this->options['records_per_iteration'] . '));');
         } else {
             $exportQuery = eval('return new WP_Query(array(' . $this->options['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $this->exported . ', \'posts_per_page\' => ' . $this->options['records_per_iteration'] . '));');
         }
     } else {
         XmlExportEngine::$post_types = $this->options['cpt'];
         if (!in_array('users', $this->options['cpt'])) {
             add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
             add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
             $exportQuery = new WP_Query(array('post_type' => $this->options['cpt'], 'post_status' => 'any', 'orderby' => 'ID', 'order' => 'ASC', 'offset' => $this->exported, 'posts_per_page' => $this->options['records_per_iteration']));
             remove_filter('posts_join', 'wp_all_export_posts_join');
             remove_filter('posts_where', 'wp_all_export_posts_where');
         } else {
             add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1);
             $exportQuery = new WP_User_Query(array('orderby' => 'ID', 'order' => 'ASC', 'number' => $this->options['records_per_iteration'], 'offset' => $this->exported));
             remove_action('pre_user_query', 'wp_all_export_pre_user_query');
         }
     }
     XmlExportEngine::$exportQuery = $exportQuery;
     $file_path = false;
     $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
     if ($this->exported == 0) {
         $import = new PMXI_Import_Record();
         $import->getById($this->options['import_id']);
         if ($import->isEmpty()) {
             $import->set(array('parent_import_id' => 99999, 'xpath' => '/', 'type' => 'upload', 'options' => array('empty'), 'root_element' => 'root', 'path' => 'path', 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'iteration' => 1))->save();
             $exportOptions = $this->options;
             $exportOptions['import_id'] = $import->id;
             $this->set(array('options' => $exportOptions))->save();
         } else {
             if ($import->parent_import_id != 99999) {
                 $newImport = new PMXI_Import_Record();
                 $newImport->set(array('parent_import_id' => 99999, 'xpath' => '/', 'type' => 'upload', 'options' => array('empty'), 'root_element' => 'root', 'path' => 'path', 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'iteration' => 1))->save();
                 $exportOptions = $this->options;
                 $exportOptions['import_id'] = $newImport->id;
                 $this->set(array('options' => $exportOptions))->save();
             }
         }
         if (!empty($this->attch_id)) {
             wp_delete_attachment($this->attch_id, true);
         }
         $target = $is_secure_import ? wp_all_export_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY) : $wp_uploads['path'];
         $file_path = $target . DIRECTORY_SEPARATOR . time() . '.' . $this->options['export_to'];
         if (!$is_secure_import) {
             $wp_filetype = wp_check_filetype(basename($file_path), null);
             $attachment_data = array('guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path($file_path), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file_path)), 'post_content' => '', 'post_status' => 'inherit');
             $attach_id = wp_insert_attachment($attachment_data, $file_path);
             $this->set(array('attch_id' => $attach_id))->save();
         } else {
             wp_all_export_remove_source(wp_all_export_get_absolute_path($this->options['filepath']));
             $exportOptions = $this->options;
             $exportOptions['filepath'] = $file_path;
             $this->set(array('options' => $exportOptions))->save();
         }
     } else {
         if (!$is_secure_import) {
             $file_path = str_replace($wp_uploads['baseurl'], $wp_uploads['basedir'], wp_get_attachment_url($this->attch_id));
         } else {
             $file_path = wp_all_export_get_absolute_path($this->options['filepath']);
         }
     }
     $foundPosts = !XmlExportEngine::$is_user_export ? $exportQuery->found_posts : $exportQuery->get_total();
     $postCount = !XmlExportEngine::$is_user_export ? $exportQuery->post_count : count($exportQuery->get_results());
     // if posts still exists then export them
     if ($postCount) {
         switch ($this->options['export_to']) {
             case 'xml':
                 if (!XmlExportEngine::$is_user_export) {
                     $exported_to_file = pmxe_export_xml($exportQuery, $this->options, false, $cron, $file_path);
                 } else {
                     $exported_to_file = pmxe_export_users_xml($exportQuery, $this->options, false, $cron, $file_path);
                 }
                 break;
             case 'csv':
                 if (!XmlExportEngine::$is_user_export) {
                     $exported_to_file = pmxe_export_csv($exportQuery, $this->options, false, $cron, $file_path, $this->exported);
                 } else {
                     $exported_to_file = pmxe_export_users_csv($exportQuery, $this->options, false, $cron, $file_path, $this->exported);
                 }
                 break;
             default:
                 # code...
                 break;
         }
         wp_reset_postdata();
         $this->set(array('exported' => $this->exported + $postCount, 'last_activity' => date('Y-m-d H:i:s'), 'processing' => 0))->save();
     } else {
         wp_reset_postdata();
         if (file_exists($file_path)) {
             if ($this->options['export_to'] == 'xml') {
                 file_put_contents($file_path, '</data>', FILE_APPEND);
             }
             if (wp_all_export_is_compatible() and ($this->options['is_generate_templates'] or $this->options['is_generate_import'])) {
                 $custom_type = empty($exportOptions['cpt']) ? 'post' : $exportOptions['cpt'][0];
                 $templateOptions = array('type' => (!empty($exportOptions['cpt']) and $exportOptions['cpt'][0] == 'page') ? 'page' : 'post', 'wizard_type' => 'new', 'deligate' => 'wpallexport', 'custom_type' => XmlExportEngine::$is_user_export ? 'import_users' : $custom_type, 'status' => 'xpath', 'is_multiple_page_parent' => 'no', 'unique_key' => '', 'acf' => array(), 'fields' => array(), 'is_multiple_field_value' => array(), 'multiple_value' => array(), 'fields_delimiter' => array(), 'update_all_data' => 'no', 'is_update_status' => 0, 'is_update_title' => 0, 'is_update_author' => 0, 'is_update_slug' => 0, 'is_update_content' => 0, 'is_update_excerpt' => 0, 'is_update_dates' => 0, 'is_update_menu_order' => 0, 'is_update_parent' => 0, 'is_update_attachments' => 0, 'is_update_acf' => 0, 'update_acf_logic' => 'only', 'acf_list' => '', 'is_update_product_type' => 1, 'is_update_attributes' => 0, 'update_attributes_logic' => 'only', 'attributes_list' => '', 'is_update_images' => 0, 'is_update_custom_fields' => 0, 'update_custom_fields_logic' => 'only', 'custom_fields_list' => '', 'is_update_categories' => 0, 'update_categories_logic' => 'only', 'taxonomies_list' => '', 'export_id' => $this->id);
                 if (in_array('product', $this->options['cpt'])) {
                     $templateOptions['_virtual'] = 1;
                     $templateOptions['_downloadable'] = 1;
                 }
                 if (XmlExportEngine::$is_user_export) {
                     $templateOptions['is_update_first_name'] = 0;
                     $templateOptions['is_update_last_name'] = 0;
                     $templateOptions['is_update_role'] = 0;
                     $templateOptions['is_update_nickname'] = 0;
                     $templateOptions['is_update_description'] = 0;
                     $templateOptions['is_update_login'] = 0;
                     $templateOptions['is_update_password'] = 0;
                     $templateOptions['is_update_nicename'] = 0;
                     $templateOptions['is_update_email'] = 0;
                     $templateOptions['is_update_registered'] = 0;
                     $templateOptions['is_update_display_name'] = 0;
                     $templateOptions['is_update_url'] = 0;
                 }
                 if ('xml' == $this->options['export_to']) {
                     wp_all_export_prepare_template_xml($this->options, $templateOptions);
                 } else {
                     wp_all_export_prepare_template_csv($this->options, $templateOptions);
                 }
                 $options = $templateOptions + PMXI_Plugin::get_default_import_options();
                 if ($this->options['is_generate_templates']) {
                     $template = new PMXI_Template_Record();
                     $tpl_data = array('name' => $this->options['template_name'], 'is_keep_linebreaks' => 0, 'is_leave_html' => 0, 'fix_characters' => 0, 'options' => $options);
                     if (!empty($this->options['template_name'])) {
                         // save template in database
                         $template->getByName($this->options['template_name'])->set($tpl_data)->save();
                     }
                 }
                 if ($this->options['is_generate_import']) {
                     $import = new PMXI_Import_Record();
                     $import->getById($this->options['import_id']);
                     if (!$import->isEmpty() and $import->parent_import_id == 99999) {
                         $xmlPath = $file_path;
                         $root_element = '';
                         if ('csv' == $this->options['export_to']) {
                             $options['delimiter'] = $this->options['delimiter'];
                             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                             $path_parts = pathinfo($xmlPath);
                             $path_parts_arr = explode(DIRECTORY_SEPARATOR, $path_parts['dirname']);
                             $target = $is_secure_import ? $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . array_pop($path_parts_arr) : $wp_uploads['path'];
                             $csv = new PMXI_CsvParser(array('filename' => $xmlPath, 'targetDir' => $target));
                             $xmlPath = $csv->xml_path;
                             $root_element = 'node';
                         } else {
                             $root_element = 'post';
                         }
                         $import->set(array('xpath' => '/' . $root_element, 'type' => 'upload', 'options' => $options, 'root_element' => $root_element, 'path' => $xmlPath, 'name' => basename($xmlPath), 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'count' => $exportQuery->found_posts))->save();
                         $history_file = new PMXI_File_Record();
                         $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => $xmlPath, 'registered_on' => date('Y-m-d H:i:s')))->save();
                         $exportOptions = $this->options;
                         $exportOptions['import_id'] = $import->id;
                         $this->set(array('options' => $exportOptions))->save();
                     }
                 }
             }
             // update export file for remove access
             # 1 meg at a time, you can adjust this.
             $to_dirname = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::CRON_DIRECTORY . DIRECTORY_SEPARATOR . md5(PMXE_Plugin::getInstance()->getOption('cron_job_key') . $this->id);
             if (!@is_dir($to_dirname)) {
                 wp_mkdir_p($to_dirname);
             }
             if (!@file_exists($to_dirname . DIRECTORY_SEPARATOR . 'index.php')) {
                 @touch($to_dirname . DIRECTORY_SEPARATOR . 'index.php');
             }
             $to = $to_dirname . DIRECTORY_SEPARATOR . (!empty($this->friendly_name) ? sanitize_file_name($this->friendly_name) : 'feed') . '.' . $this->options['export_to'];
             $buffer_size = 1048576;
             $fin = @fopen($file_path, "rb");
             $fout = @fopen($to, "w");
             while (!@feof($fin)) {
                 @fwrite($fout, @fread($fin, $buffer_size));
             }
             @fclose($fin);
             @fclose($fout);
             if ($this->options['is_scheduled'] and "" != $this->options['scheduled_email']) {
                 add_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
                 $headers = 'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>' . "\r\n";
                 $message = '<p>Export ' . $this->options['friendly_name'] . ' has been completed. You can find exported file in attachments.</p>';
                 wp_mail($this->options['scheduled_email'], __("WP All Export", "pmxe_plugin"), $message, $headers, array($file_path));
                 remove_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
             }
         }
         $this->set(array('processing' => 0, 'triggered' => 0, 'canceled' => 0, 'registered_on' => date('Y-m-d H:i:s')))->update();
     }
     return $this;
 }
Ejemplo n.º 8
0
/**
*	AJAX action export processing
*/
function pmxe_wp_ajax_export()
{
    if (!check_ajax_referer('wp_all_export_secure', 'security', false)) {
        exit(__('Security check', 'wp_all_export_plugin'));
    }
    if (!current_user_can('manage_options')) {
        exit(__('Security check', 'wp_all_export_plugin'));
    }
    $wp_uploads = wp_upload_dir();
    $export = new PMXE_Export_Record();
    $export->getById(PMXE_Plugin::$session->update_previous);
    $exportOptions = (PMXE_Plugin::$session->has_session() ? PMXE_Plugin::$session->get_clear_session_data() : array()) + PMXE_Plugin::get_default_import_options();
    wp_reset_postdata();
    XmlExportEngine::$exportOptions = $exportOptions;
    XmlExportEngine::$is_user_export = $exportOptions['is_user_export'];
    $posts_per_page = $exportOptions['records_per_iteration'];
    if ('advanced' == $exportOptions['export_type']) {
        if (XmlExportEngine::$is_user_export) {
            $exportQuery = eval('return new WP_User_Query(array(' . $exportOptions['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $export->exported . ', \'number\' => ' . $posts_per_page . ' ));');
        } else {
            $exportQuery = eval('return new WP_Query(array(' . $exportOptions['wp_query'] . ', \'orderby\' => \'ID\', \'order\' => \'ASC\', \'offset\' => ' . $export->exported . ', \'posts_per_page\' => ' . $posts_per_page . ' ));');
        }
    } else {
        XmlExportEngine::$post_types = $exportOptions['cpt'];
        if (!in_array('users', $exportOptions['cpt'])) {
            add_filter('posts_join', 'wp_all_export_posts_join', 10, 1);
            add_filter('posts_where', 'wp_all_export_posts_where', 10, 1);
            $exportQuery = new WP_Query(array('post_type' => $exportOptions['cpt'], 'post_status' => 'any', 'orderby' => 'ID', 'order' => 'ASC', 'offset' => $export->exported, 'posts_per_page' => $posts_per_page));
            remove_filter('posts_where', 'wp_all_export_posts_where');
            remove_filter('posts_join', 'wp_all_export_posts_join');
        } else {
            add_action('pre_user_query', 'wp_all_export_pre_user_query', 10, 1);
            $exportQuery = new WP_User_Query(array('orderby' => 'ID', 'order' => 'ASC', 'number' => $posts_per_page, 'offset' => $export->exported));
            remove_action('pre_user_query', 'wp_all_export_pre_user_query');
        }
    }
    XmlExportEngine::$exportQuery = $exportQuery;
    $foundPosts = !XmlExportEngine::$is_user_export ? $exportQuery->found_posts : $exportQuery->get_total();
    $postCount = !XmlExportEngine::$is_user_export ? $exportQuery->post_count : count($exportQuery->get_results());
    if (!$export->exported) {
        if (!empty($export->attch_id)) {
            wp_delete_attachment($export->attch_id, true);
        }
        $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
        if ($is_secure_import and !empty($exportOptions['filepath'])) {
            wp_all_export_remove_source(wp_all_export_get_absolute_path($exportOptions['filepath']));
            $exportOptions['filepath'] = '';
        }
        PMXE_Plugin::$session->set('count', $foundPosts);
        PMXE_Plugin::$session->save_data();
    }
    // if posts still exists then export them
    if ($postCount) {
        switch ($exportOptions['export_to']) {
            case 'xml':
                if (!XmlExportEngine::$is_user_export) {
                    pmxe_export_xml($exportQuery, $exportOptions);
                } else {
                    pmxe_export_users_xml($exportQuery, $exportOptions);
                }
                break;
            case 'csv':
                if (!XmlExportEngine::$is_user_export) {
                    pmxe_export_csv($exportQuery, $exportOptions);
                } else {
                    pmxe_export_users_csv($exportQuery, $exportOptions);
                }
                break;
            default:
                # code...
                break;
        }
        wp_reset_postdata();
    }
    if ($postCount) {
        $export->set(array('exported' => $export->exported + $postCount))->save();
    }
    if ($posts_per_page != -1 and $postCount) {
        wp_send_json(array('exported' => $export->exported, 'percentage' => ceil($export->exported / $foundPosts * 100), 'done' => false, 'records_per_request' => $exportOptions['records_per_iteration']));
    } else {
        wp_reset_postdata();
        if (file_exists(PMXE_Plugin::$session->file)) {
            if ($exportOptions['export_to'] == 'xml') {
                file_put_contents(PMXE_Plugin::$session->file, '</data>', FILE_APPEND);
            }
            $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure');
            if (!$is_secure_import) {
                $wp_filetype = wp_check_filetype(basename(PMXE_Plugin::$session->file), null);
                $attachment_data = array('guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path(PMXE_Plugin::$session->file), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename(PMXE_Plugin::$session->file)), 'post_content' => '', 'post_status' => 'inherit');
                $attach_id = wp_insert_attachment($attachment_data, PMXE_Plugin::$session->file);
                if (!$export->isEmpty()) {
                    $export->set(array('attch_id' => $attach_id))->save();
                }
            } else {
                $exportOptions['filepath'] = wp_all_export_get_relative_path(PMXE_Plugin::$session->file);
                if (!$export->isEmpty()) {
                    $export->set(array('options' => $exportOptions))->save();
                }
            }
            if (wp_all_export_is_compatible() and ($exportOptions['is_generate_templates'] or $exportOptions['is_generate_import'])) {
                $custom_type = empty($exportOptions['cpt']) ? 'post' : $exportOptions['cpt'][0];
                $templateOptions = array('type' => (!empty($exportOptions['cpt']) and $exportOptions['cpt'][0] == 'page') ? 'page' : 'post', 'wizard_type' => 'new', 'deligate' => 'wpallexport', 'custom_type' => XmlExportEngine::$is_user_export ? 'import_users' : $custom_type, 'status' => 'xpath', 'is_multiple_page_parent' => 'no', 'unique_key' => '', 'acf' => array(), 'fields' => array(), 'is_multiple_field_value' => array(), 'multiple_value' => array(), 'fields_delimiter' => array(), 'update_all_data' => 'no', 'is_update_status' => 0, 'is_update_title' => 0, 'is_update_author' => 0, 'is_update_slug' => 0, 'is_update_content' => 0, 'is_update_excerpt' => 0, 'is_update_dates' => 0, 'is_update_menu_order' => 0, 'is_update_parent' => 0, 'is_update_attachments' => 0, 'is_update_acf' => 0, 'update_acf_logic' => 'only', 'acf_list' => '', 'is_update_product_type' => 0, 'is_update_attributes' => 0, 'update_attributes_logic' => 'only', 'attributes_list' => '', 'is_update_images' => 0, 'is_update_custom_fields' => 0, 'update_custom_fields_logic' => 'only', 'custom_fields_list' => '', 'is_update_categories' => 0, 'update_categories_logic' => 'only', 'taxonomies_list' => '', 'export_id' => $export->id);
                if (in_array('product', $exportOptions['cpt'])) {
                    $templateOptions['_virtual'] = 1;
                    $templateOptions['_downloadable'] = 1;
                }
                if (XmlExportEngine::$is_user_export) {
                    $templateOptions['is_update_first_name'] = 0;
                    $templateOptions['is_update_last_name'] = 0;
                    $templateOptions['is_update_role'] = 0;
                    $templateOptions['is_update_nickname'] = 0;
                    $templateOptions['is_update_description'] = 0;
                    $templateOptions['is_update_login'] = 0;
                    $templateOptions['is_update_password'] = 0;
                    $templateOptions['is_update_nicename'] = 0;
                    $templateOptions['is_update_email'] = 0;
                    $templateOptions['is_update_registered'] = 0;
                    $templateOptions['is_update_display_name'] = 0;
                    $templateOptions['is_update_url'] = 0;
                }
                if ('xml' == $exportOptions['export_to']) {
                    wp_all_export_prepare_template_xml($exportOptions, $templateOptions);
                } else {
                    wp_all_export_prepare_template_csv($exportOptions, $templateOptions);
                }
                $options = $templateOptions + PMXI_Plugin::get_default_import_options();
                if ($exportOptions['is_generate_templates']) {
                    $template = new PMXI_Template_Record();
                    $tpl_options = $options;
                    if ('csv' == $exportOptions['export_to']) {
                        $tpl_options['delimiter'] = $exportOptions['delimiter'];
                    }
                    $tpl_options['update_all_data'] = 'yes';
                    $tpl_options['is_update_status'] = 1;
                    $tpl_options['is_update_title'] = 1;
                    $tpl_options['is_update_author'] = 1;
                    $tpl_options['is_update_slug'] = 1;
                    $tpl_options['is_update_content'] = 1;
                    $tpl_options['is_update_excerpt'] = 1;
                    $tpl_options['is_update_dates'] = 1;
                    $tpl_options['is_update_menu_order'] = 1;
                    $tpl_options['is_update_parent'] = 1;
                    $tpl_options['is_update_attachments'] = 1;
                    $tpl_options['is_update_acf'] = 1;
                    $tpl_options['update_acf_logic'] = 'full_update';
                    $tpl_options['acf_list'] = '';
                    $tpl_options['is_update_product_type'] = 1;
                    $tpl_options['is_update_attributes'] = 1;
                    $tpl_options['update_attributes_logic'] = 'full_update';
                    $tpl_options['attributes_list'] = '';
                    $tpl_options['is_update_images'] = 1;
                    $tpl_options['is_update_custom_fields'] = 1;
                    $tpl_options['update_custom_fields_logic'] = 'full_update';
                    $tpl_options['custom_fields_list'] = '';
                    $tpl_options['is_update_categories'] = 1;
                    $tpl_options['update_categories_logic'] = 'full_update';
                    $tpl_options['taxonomies_list'] = '';
                    $tpl_data = array('name' => $exportOptions['template_name'], 'is_keep_linebreaks' => 0, 'is_leave_html' => 0, 'fix_characters' => 0, 'options' => $tpl_options);
                    if (!empty($exportOptions['template_name'])) {
                        // save template in database
                        $template->getByName($exportOptions['template_name'])->set($tpl_data)->save();
                    }
                }
                // associate exported posts with new import
                if ($exportOptions['is_generate_import']) {
                    $import = new PMXI_Import_Record();
                    $import->getById($exportOptions['import_id']);
                    if (!$import->isEmpty() and $import->parent_import_id == 99999) {
                        $xmlPath = PMXE_Plugin::$session->file;
                        $root_element = '';
                        $historyPath = PMXE_Plugin::$session->file;
                        if ('csv' == $exportOptions['export_to']) {
                            $options['delimiter'] = $exportOptions['delimiter'];
                            include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                            $path_info = pathinfo($xmlPath);
                            $path_parts = explode(DIRECTORY_SEPARATOR, $path_info['dirname']);
                            $security_folder = array_pop($path_parts);
                            $target = $is_secure_import ? $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . $security_folder : $wp_uploads['path'];
                            $csv = new PMXI_CsvParser(array('filename' => $xmlPath, 'targetDir' => $target));
                            $historyPath = $csv->xml_path;
                            $root_element = 'node';
                        } else {
                            $root_element = 'post';
                        }
                        $import->set(array('xpath' => '/' . $root_element, 'type' => 'upload', 'options' => $options, 'root_element' => $root_element, 'path' => $xmlPath, 'name' => basename($xmlPath), 'imported' => 0, 'created' => 0, 'updated' => 0, 'skipped' => 0, 'deleted' => 0, 'iteration' => 1, 'count' => PMXE_Plugin::$session->count))->save();
                        $history_file = new PMXI_File_Record();
                        $history_file->set(array('name' => $import->name, 'import_id' => $import->id, 'path' => $historyPath, 'registered_on' => date('Y-m-d H:i:s')))->save();
                        $exportOptions['import_id'] = $import->id;
                        $export->set(array('options' => $exportOptions))->save();
                    }
                }
            }
        }
        $export->set(array('executing' => 0, 'canceled' => 0))->save();
        wp_send_json(array('exported' => $export->exported, 'percentage' => 100, 'done' => true, 'records_per_request' => $exportOptions['records_per_iteration'], 'file' => PMXE_Plugin::$session->file));
    }
}
Ejemplo n.º 9
0
function pmai_wp_ajax_get_acf()
{
    if (!check_ajax_referer('wp_all_import_secure', 'security', false)) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_import_plugin'))));
    }
    if (!current_user_can('manage_options')) {
        exit(json_encode(array('html' => __('Security check', 'wp_all_import_plugin'))));
    }
    global $acf;
    $version = $acf ? $acf->settings['version'] : false;
    ob_start();
    $acf_groups = PMXI_Plugin::$session->acf_groups;
    $acf_obj = false;
    if (!empty($acf_groups)) {
        foreach ($acf_groups as $key => $group) {
            if ($group['ID'] == $_GET['acf']) {
                $acf_obj = $group;
                break;
            }
        }
    }
    $import = new PMXI_Import_Record();
    if (!empty($_GET['id'])) {
        $import->getById($_GET['id']);
    }
    $is_loaded_template = !empty(PMXI_Plugin::$session->is_loaded_template) ? PMXI_Plugin::$session->is_loaded_template : false;
    if ($is_loaded_template) {
        $default = PMAI_Plugin::get_default_import_options();
        $template = new PMXI_Template_Record();
        if (!$template->getById($is_loaded_template)->isEmpty()) {
            $options = (!empty($template->options) ? $template->options : array()) + $default;
        }
    } else {
        if (!$import->isEmpty()) {
            $options = $import->options;
        } else {
            $options = PMXI_Plugin::$session->options;
        }
    }
    ?>
	<div class="postbox  acf_postbox default acf_signle_group rad4" rel="<?php 
    echo $acf_obj['ID'];
    ?>
">
		<h3 class="hndle" style="margin-top:0;"><span><?php 
    echo $acf_obj['title'];
    ?>
</span></h3>
		<div class="inside">
		<?php 
    if ($version and version_compare($version, '5.0.0') >= 0) {
        if (is_numeric($acf_obj['ID'])) {
            $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $_GET['acf'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC'));
            if (!empty($acf_fields)) {
                foreach ($acf_fields as $field) {
                    $fieldData = !empty($field->post_content) ? unserialize($field->post_content) : array();
                    $fieldData['ID'] = $field->ID;
                    $fieldData['id'] = $field->ID;
                    $fieldData['label'] = $field->post_title;
                    $fieldData['key'] = $field->post_name;
                    if (empty($fieldData['name'])) {
                        $fieldData['name'] = $field->post_excerpt;
                    }
                    echo pmai_render_field($fieldData, !empty($options) ? $options : array());
                }
            }
        } else {
            $fields = acf_local()->fields;
            if (!empty($fields)) {
                foreach ($fields as $key => $field) {
                    if ($field['parent'] == $acf_obj['key']) {
                        $fieldData = $field;
                        $fieldData['id'] = uniqid();
                        $fieldData['label'] = $field['label'];
                        $fieldData['key'] = $field['key'];
                        echo pmai_render_field($fieldData, !empty($options) ? $options : array());
                    }
                }
            }
        }
    } else {
        if (is_numeric($acf_obj['ID'])) {
            $fields = array();
            foreach (get_post_meta($acf_obj['ID'], '') as $cur_meta_key => $cur_meta_val) {
                if (strpos($cur_meta_key, 'field_') !== 0) {
                    continue;
                }
                $fields[] = !empty($cur_meta_val[0]) ? unserialize($cur_meta_val[0]) : array();
            }
            if (count($fields)) {
                $sortArray = array();
                foreach ($fields as $field) {
                    foreach ($field as $key => $value) {
                        if (!isset($sortArray[$key])) {
                            $sortArray[$key] = array();
                        }
                        $sortArray[$key][] = $value;
                    }
                }
                $orderby = "order_no";
                array_multisort($sortArray[$orderby], SORT_ASC, $fields);
                foreach ($fields as $field) {
                    echo pmai_render_field($field, !empty($options) ? $options : array());
                }
            }
        } else {
            global $acf_register_field_group;
            if (!empty($acf_register_field_group)) {
                foreach ($acf_register_field_group as $key => $group) {
                    if ($group['id'] == $acf_obj['ID']) {
                        foreach ($group['fields'] as $field) {
                            echo pmai_render_field($field, !empty($options) ? $options : array());
                        }
                    }
                }
            }
        }
    }
    ?>
								
		</div>
	</div>
	<?php 
    exit(json_encode(array('html' => ob_get_clean())));
    die;
}
Ejemplo n.º 10
0
                    if (wp_all_export_is_compatible() and !empty($item['options']['import_id']) and $is_re_import_allowed) {
                        ?>
										<a href="<?php 
                        echo add_query_arg(array('page' => 'pmxi-admin-import', 'id' => $item['options']['import_id'], 'deligate' => 'wpallexport'), remove_query_arg('page', $this->baseUrl));
                        ?>
"><?php 
                        _e("Import with WP All Import", "wp_all_export_plugin");
                        ?>
</a>
									<?php 
                    }
                    ?>
			
									<?php 
                    if (wp_all_export_is_compatible() and (empty($item['options']['cpt']) or !in_array('shop_order', $item['options']['cpt']))) {
                        $template = new PMXI_Template_Record();
                        if (!empty($item['options']['template_name'])) {
                            $template->getByName($item['options']['template_name']);
                            if (!$template->isEmpty()) {
                                ?>
													<br/>
													<a href="<?php 
                                echo add_query_arg(array('id' => $item['id'], 'action' => 'templates'), $this->baseUrl);
                                ?>
"><?php 
                                _e('Download Import Templates', 'wp_all_export_plugin');
                                ?>
</a>
													<?php 
                            }
                        }