コード例 #1
0
ファイル: import.php プロジェクト: ryan2407/Vision
 /**
  * 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();
 }
コード例 #2
0
 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;
 }
コード例 #3
0
ファイル: import.php プロジェクト: hikaram/wee
 /**
  * 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();
 }
コード例 #4
0
ファイル: import.php プロジェクト: thabofletcher/tc-site
 /**
  * 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();
 }
コード例 #5
0
ファイル: wp_ajax_get_acf.php プロジェクト: ryan2407/Vision
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;
}