/** * Step #3: Choose template */ public function template() { $template = new PMXI_Template_Record(); $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")); } } $DefaultOptions['wizard_type'] = PMXI_Plugin::$session->wizard_type; $DefaultOptions['custom_type'] = PMXI_Plugin::$session->custom_type; $DefaultOptions['delimiter'] = PMXI_Plugin::$session->is_csv; $post = $this->input->post(apply_filters('pmxi_options_options', $DefaultOptions, $this->isWizard)); } 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(apply_filters('pmxi_options_options', $DefaultOptions, $this->isWizard)); } $this->data['post'] =& $post; PMXI_Plugin::$session->set('options', $post); PMXI_Plugin::$session->set('is_loaded_template', ''); if ($load_template = $this->input->post('load_template')) { // init form with template selected if (!$template->getById($load_template)->isEmpty()) { $template_options = $template->options; $template_options['type'] = $post['type']; $template_options['custom_type'] = $post['custom_type']; $template_options['wizard_type'] = $post['wizard_type']; $template_options['delimiter'] = $post['delimiter']; if ($this->isWizard and $post['wizard_type'] == 'new') { $template_options['create_new_records'] = 1; } $this->data['post'] = $template_options; PMXI_Plugin::$session->set('is_loaded_template', $load_template); PMXI_Plugin::$session->set('options', $template_options); } } elseif ($this->input->post('is_submitted')) { // save template submission check_admin_referer('template', '_wpnonce_template'); if (!empty($post['title'])) { $this->_validate_template($post['title'], 'Post title'); } else { $this->warnings->add('1', __('<strong>Warning:</strong> your title is blank.', 'wp_all_import_plugin')); } if (!empty($post['content'])) { $this->_validate_template($post['content'], 'Post content'); } else { $this->warnings->add('2', __('<strong>Warning:</strong> your content is blank.', 'wp_all_import_plugin')); } if (!$this->errors->get_error_codes()) { // Attributes fields logic $post = apply_filters('pmxi_save_options', $post); // validate post excerpt if (!empty($post['post_excerpt'])) { $this->_validate_template($post['post_excerpt'], __('Excerpt', 'wp_all_import_plugin')); } // validate images if ($post['download_images'] == 'yes') { if (!empty($post['download_featured_image'])) { $this->_validate_template($post['download_featured_image'], __('Images', 'wp_all_import_plugin')); } } else { if (!empty($post['featured_image'])) { $this->_validate_template($post['featured_image'], __('Images', 'wp_all_import_plugin')); } } // validate images meta data foreach (array('title', 'caption', 'alt', 'decription') as $section) { if ($post['set_image_meta_' . $section]) { if (!empty($post['image_meta_' . $section])) { $this->_validate_template($post['image_meta_' . $section], __('Images meta ' . $section, 'wp_all_import_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['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', 'wp_all_import_plugin')); } else { foreach ($post['custom_name'] as $custom_name) { $this->_validate_template($custom_name, __('Custom Field Name', 'wp_all_import_plugin')); } foreach ($post['custom_value'] as $key => $custom_value) { if (empty($post['custom_format'][$key])) { $this->_validate_template($custom_value, __('Custom Field Value', 'wp_all_import_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', 'wp_all_import_plugin')); } else { foreach ($post['attribute_name'] as $attribute_name) { $this->_validate_template($attribute_name, __('Attribute Field Name', 'wp_all_import_plugin')); } foreach ($post['attribute_value'] as $custom_value) { $this->_validate_template($custom_value, __('Attribute Field Value', 'wp_all_import_plugin')); } } } /*if ('page' == $post['type'] and ! preg_match('%^(-?\d+)?$%', $post['order'])) { $this->errors->add('form-validation', __('Order must be an integer number', 'wp_all_import_plugin')); }*/ if ('post' == $post['type']) { /*'' == $post['categories'] or $this->_validate_template($post['categories'], __('Categories', 'wp_all_import_plugin'));*/ '' == $post['tags'] or $this->_validate_template($post['tags'], __('Tags', 'wp_all_import_plugin')); } if ('specific' == $post['date_type']) { '' == $post['date'] or $this->_validate_template($post['date'], __('Date', 'wp_all_import_plugin')); } else { '' == $post['date_start'] or $this->_validate_template($post['date_start'], __('Start Date', 'wp_all_import_plugin')); '' == $post['date_end'] or $this->_validate_template($post['date_end'], __('Start Date', 'wp_all_import_plugin')); } /*if ('' == $post['tags_delim']) { $this->errors->add('form-validation', __('Tag list delimiter must cannot be empty', '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 // 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 (!empty($post['name']) and !empty($post['save_template_as'])) { // save template in database $template->getByName($post['name'])->set(array('name' => $post['name'], 'is_keep_linebreaks' => $post['is_keep_linebreaks'], 'is_leave_html' => $post['is_leave_html'], 'fix_characters' => $post['fix_characters'], 'options' => $post))->save(); PMXI_Plugin::$session->set('saved_template', $template->id); } if ($this->isWizard) { PMXI_Plugin::$session->set('options', $post); PMXI_Plugin::$session->save_data(); wp_redirect(add_query_arg('action', 'options', $this->baseUrl)); die; } else { $this->data['import']->set(array('options' => $post, 'settings_update_on' => date('Y-m-d H:i:s')))->update(); $args = array('page' => 'pmxi-admin-manage', 'pmxi_nt' => urlencode(__('Template updated', 'wp_all_import_plugin'))); if ($this->warnings->get_error_codes()) { $args['warnings'] = implode(',', $this->warnings->get_error_codes()); } wp_redirect(add_query_arg($args + array_intersect_key($_GET, array_flip($this->baseUrlParamNames)), admin_url('admin.php'))); die; } } } } PMXI_Plugin::$session->save_data(); if (!in_array($post['custom_type'], array('import_users'))) { global $wpdb; // Get all meta keys for requested post type $this->data['meta_keys'] = array(); $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['meta_keys']) and !in_array($record_meta_key, $hide_fields)) { $this->data['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; } } } } } } else { // Get All meta keys in the system $this->data['meta_keys'] = array(); $meta_keys = new PMXI_Model_List(); $meta_keys->setTable(PMXI_Plugin::getInstance()->getWPPrefix() . 'usermeta'); $meta_keys->setColumns('umeta_id', 'meta_key')->getBy(NULL, "umeta_id", NULL, NULL, "meta_key"); $hide_fields = array('first_name', 'last_name', 'nickname', 'description', PMXI_Plugin::getInstance()->getWPPrefix() . 'capabilities'); if (!empty($meta_keys) and $meta_keys->count()) { foreach ($meta_keys as $meta_key) { if (in_array($meta_key['meta_key'], $hide_fields) or strpos($meta_key['meta_key'], '_wp') === 0) { continue; } $this->data['meta_keys'][] = $meta_key['meta_key']; } } } if (user_can_richedit()) { wp_enqueue_script('editor'); } wp_enqueue_script('word-count'); add_thickbox(); wp_enqueue_script('media-upload'); wp_enqueue_script('quicktags'); $this->render(); }
/** * 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(); }