function process_posts() { if (!($fp = fopen($this->file, "r"))) { echo '<p><strong>' . __('Failed to open file.', 'wc2') . '</strong></p>'; wp_import_cleanup($this->id); return false; } global $wpdb; $wc2_item = WC2_DB_Item::get_instance(); //all delete //$wc2_item->delete_all_item_data(); //die(); $err = new WP_Error(); $sp = ","; $lines = array(); $buf = ''; while (!feof($fp)) { $temp = fgets($fp, 10240); if (0 == strlen($temp)) { continue; } $num = substr_count($temp, '"'); if (0 == $num % 2 && '' == $buf) { $lines[] = $temp; } elseif (1 == $num % 2 && '' == $buf) { $buf .= $temp; } elseif (0 == $num % 2 && '' != $buf) { $buf .= $temp; } elseif (1 == $num % 2 && '' != $buf) { $buf .= $temp; $lines[] = $buf; $buf = ''; } } fclose($fp); //Post data - fixed define('COL_POST_ID', 0); define('COL_POST_AUTHOR', 1); define('COL_POST_CONTENT', 2); define('COL_POST_TITLE', 3); define('COL_POST_EXCERPT', 4); define('COL_POST_STATUS', 5); define('COL_POST_COMMENT_STATUS', 6); define('COL_POST_PASSWORD', 7); define('COL_POST_NAME', 8); define('COL_POST_MODIFIED', 9); define('COL_POST_CATEGORY', 10); define('COL_POST_TAG', 11); define('COL_POST_CUSTOM_FIELD', 12); define('COL_ITEM_CODE', 13); define('COL_ITEM_NAME', 14); $item_base_column = $wc2_item->get_item_base_column(); $item_meta_column = $wc2_item->get_item_meta_column(); $item_sku_column = $wc2_item->get_item_sku_column(); $item_sku_meta_column = $wc2_item->get_item_sku_meta_column(); $system = wc2_get_option('system'); $this->encode_type = isset($system['csv_encode_type']) ? $system['csv_encode_type'] : 0; $start_col = 13; $sku_start_col = $start_col; foreach ((array) $item_base_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $sku_start_col++; } } foreach ((array) $item_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $sku_start_col++; } } $post_status = array('publish', 'future', 'draft', 'pending', 'private'); $pre_item_code = ''; $item_id = 0; $sku_id = 1; $this->data_rows = count($lines); $this->success = 0; $this->false = 0; //Progressbar 処理件数SET echo '<script type="text/javascript">PG_Set_Max(' . $this->data_rows . ');</script>' . "\r\n"; ob_flush(); flush(); foreach ($lines as $row => $line) { $datas = array(); $datas = explode($sp, trim($line)); $this->values = array(); $buf = ''; foreach ($datas as $data) { $num = substr_count($data, '"'); if (0 == $num % 2 && '' == $buf) { if ('"' == substr($data, 0, 1)) { $data = substr($data, 1); } if ('"' == substr($data, -1)) { $data = substr($data, 0, -1); } $data = str_replace(array('""'), '"', $data); $this->values[] = false !== $data ? $data : ''; } elseif (1 == $num % 2 && '' == $buf) { $buf .= $data; } elseif (0 == $num % 2 && '' != $buf) { $buf .= $sp . $data; } elseif (1 == $num % 2 && '' != $buf) { $buf .= $sp . $data; if ('"' == substr($buf, 0, 1)) { $buf = substr($buf, 1); } if ('"' == substr($buf, -1)) { $buf = substr($buf, 0, -1); } $buf = str_replace(array('""'), '"', $buf); $this->values[] = false !== $buf ? $buf : ''; $buf = ''; } } if ('Post ID' == $this->values[COL_POST_ID]) { $this->data_rows -= 1; $this->progress_add(); continue; } if ($pre_item_code == $this->values[COL_ITEM_CODE] && WC2_Utils::is_blank($this->values[COL_POST_ID])) { $mode = 'add'; $post_id = ''; } else { $post_id = !WC2_Utils::is_blank($this->values[COL_POST_ID]) ? (int) $this->values[COL_POST_ID] : ''; if ($post_id) { $post_ob = get_post($post_id); if (!isset($post_ob->ID) || empty($post_ob)) { $this->error_log($row, __("Post-ID {$post_id} does not exist.", 'wc2')); $this->create_log(); $this->progress_add(); continue; } $mode = 'upd'; } else { $mode = 'add'; } } $wc2_item->set_the_post_id($post_id); //data_check foreach ($this->values as $key => $val) { $value = trim($val); switch ($key) { case COL_POST_ID: if (!preg_match("/^[0-9]+\$/", $value) && 0 != strlen($value)) { $this->error_log($row, __('A value of the Post-ID is abnormal.', 'wc2')); } break; case COL_POST_AUTHOR: case COL_POST_COMMENT_STATUS: case COL_POST_PASSWORD: case COL_POST_NAME: case COL_POST_TITLE: case COL_POST_CONTENT: case COL_POST_EXCERPT: break; case COL_POST_STATUS: if (0 == strlen($value) || !in_array($value, $post_status)) { $this->error_log($row, __('A value of the display status is abnormal.', 'wc2')); } break; case COL_POST_MODIFIED: if ('future' == $this->values[COL_POST_STATUS] && (0 == strlen($value) || '0000-00-00 00:00:00' == $value)) { if (preg_match($date_pattern, $value, $match)) { if (checkdate($match[2], $match[3], $match[1]) && (0 < $match[4] && 24 > $match[4]) && (0 < $match[5] && 60 > $match[5]) && (0 < $match[6] && 60 > $match[6])) { } else { $this->error_log($row, __('A value of the schedule is abnormal.', 'wc2')); } } else { $this->error_log($row, __('A value of the schedule is abnormal.', 'wc2')); } } else { if (0 != strlen($value) && '0000-00-00 00:00:00' != $value) { if (preg_match("/^[0-9]+\$/", substr($value, 0, 4))) { if (strtotime($value) === false) { $this->error_log($row, __('A value of the schedule is abnormal.', 'wc2')); } } else { $datetime = explode(' ', $value); $date_str = $this->dates_interconv($datetime[0]) . ' ' . $datetime[1]; if (strtotime($date_str) === false) { $this->error_log($row, __('A value of the schedule is abnormal.', 'wc2')); } } } } break; case COL_POST_CATEGORY: if (0 == strlen($value)) { $this->error_log($row, __('A category is non-input.', 'wc2')); } break; case COL_POST_TAG: case COL_POST_CUSTOM_FIELD: break; case COL_ITEM_CODE: if (0 == strlen($value)) { $this->error_log($row, __('An item code is non-input.', 'wc2')); } else { $post_ids = $wc2_item->get_some_post_ids_by_item_code($value); if ('upd' == $mode) { if (1 < count($post_ids)) { $this->error_log($row, __('This Item-Code has been duplicated.', 'wc2')); foreach ($post_ids as $res_val) { $this->error_log($row, "item_code=" . $value . ", post_id=" . $res_val['item_post_id']); } } elseif (1 === count($post_ids)) { if ($post_ids[0]['item_post_id'] != $post_id) { $this->error_log($row, __('This Item-Code has already been used.', 'wc2')); $this->error_log($row, "item_code=" . $value . ", post_id=" . $post_ids[0]['item_post_id']); } } } else { if ('add' == $mode) { if ($value != $pre_item_code) { if (0 < count($post_ids)) { $this->error_log($row, __('This Item-Code has already been used.', 'wc2')); foreach ($post_ids as $res_val) { $this->error_log($row, "item_code=" . $value . ", post_id=" . $res_val['item_post_id']); } } } } } } break; case COL_ITEM_NAME: if (0 == strlen($value)) { $this->error_log($row, __('An item name is non-input.', 'wc2')); } break; } } //表示する Item column をエラーチェック $check_num = $start_col; foreach ($item_base_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $check_column[$key] = $check_num; $check_num++; } } foreach ($item_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $check_column[$key] = $check_num; $check_num++; } } foreach ($item_sku_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $check_column[$key] = $check_num; $check_num++; } } foreach ($item_sku_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $check_column[$key] = $check_num; $check_num++; } } foreach ($check_column as $column_key => $data_num) { switch ($column_key) { case ITEM_PRODUCT_TYPE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Product type is abnormal.', 'wc2')); } break; case ITEM_CHARGES_TYPE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Billing type is abnormal.', 'wc2')); } break; case ITEM_PURCHASE_LIMIT_LOWEST: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Purchase limit(lowest) is abnormal.', 'wc2')); } break; case ITEM_PURCHASE_LIMIT_HIGHEST: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Purchase limit(highest) is abnormal.', 'wc2')); } break; case ITEM_POINT_RATE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the point rate is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_NUM1: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount1(number) is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_RATE1: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount1(rate) is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_NUM2: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount2(number) is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_RATE2: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount2(rate) is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_NUM3: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount3(number) is abnormal.', 'wc2')); } break; case ITEM_QUANTITY_DISCOUNT_RATE3: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount3(rate) is abnormal.', 'wc2')); } break; case ITEM_SKU_CODE: if (0 == strlen($this->values[$data_num])) { $this->error_log($row, __('A SKU code is non-input.', 'wc2')); } break; case ITEM_SKU_NAME: case ITEM_SKU_UNIT: break; case ITEM_SKU_STOCK: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the SKU stock is abnormal.', 'wc2')); } break; case ITEM_SKU_STATUS: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the SKU status is abnormal.', 'wc2')); } break; case ITEM_SKU_PRICE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the SKU price is abnormal.', 'wc2')); } break; case ITEM_SKU_COSTPRICE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the SKU cost price is abnormal.', 'wc2')); } break; case ITEM_SKU_LISTPRICE: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the SKU list price is abnormal.', 'wc2')); } break; case ITEM_SKU_SET_QUANTITY_DISCOUNT: if (!preg_match("/^[0-9]+\$/", $this->values[$data_num])) { $this->error_log($row, __('A value of the Quantity discount applies is abnormal.', 'wc2')); } break; } } if ('' != $this->get_log_line()) { $this->create_log(); $this->progress_add(); continue; } $post = array(); if ($pre_item_code != $this->values[COL_ITEM_CODE]) { $sku_id = 1; if ($this->values[COL_POST_MODIFIED] == '' || $this->values[COL_POST_MODIFIED] == '0000-00-00 00:00:00') { $post['post_date'] = current_time('mysql'); $post['post_date_gmt'] = current_time('mysql', 1); $post['post_modified'] = current_time('mysql'); $post['post_modified_gmt'] = current_time('mysql', 1); } else { if (preg_match("/^[0-9]+\$/", substr($this->values[COL_POST_MODIFIED], 0, 4))) { $time_data = strtotime($this->values[COL_POST_MODIFIED]); } else { $datetime = explode(' ', $this->values[COL_POST_MODIFIED]); $date_str = $this->dates_interconv($datetime[0]) . ' ' . $datetime[1]; $time_data = strtotime($date_str); } $post['post_date'] = date('Y-m-d H:i:s', $time_data); $post['post_date_gmt'] = gmdate('Y-m-d H:i:s', $time_data); $post['post_modified'] = date('Y-m-d H:i:s', $time_data); $post['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $time_data); } if ('publish' == $this->values[COL_POST_STATUS]) { $now = current_time('mysql'); if (mysql2date('U', $post['post_modified'], false) > mysql2date('U', $now, false)) { $this->values[COL_POST_STATUS] = 'future'; } } elseif ('future' == $this->values[COL_POST_STATUS]) { $now = current_time('mysql'); if (mysql2date('U', $post['post_modified'], false) <= mysql2date('U', $now, false)) { $this->values[COL_POST_STATUS] = 'publish'; } } $post['ID'] = $post_id; $post['post_author'] = !WC2_Utils::is_blank($this->values[COL_POST_AUTHOR]) ? $this->values[COL_POST_AUTHOR] : 1; $post['post_content'] = $this->convert_encoding($this->values[COL_POST_CONTENT]); $post['post_title'] = $this->convert_encoding($this->values[COL_POST_TITLE]); $post['post_excerpt'] = $this->convert_encoding($this->values[COL_POST_EXCERPT]); $post['post_status'] = $this->values[COL_POST_STATUS]; $post['comment_status'] = !WC2_Utils::is_blank($this->values[COL_POST_COMMENT_STATUS]) ? $this->values[COL_POST_COMMENT_STATUS] : 'close'; $post['ping_status'] = 'close'; $post['post_password'] = '******' == $post['post_status'] ? '' : $this->values[COL_POST_PASSWORD]; $post['post_type'] = ITEM_POST_TYPE; $post['post_parent'] = 0; $post_name = sanitize_title($this->convert_encoding($this->values[COL_POST_NAME])); $post['post_name'] = wp_unique_post_slug($post_name, $post_id, $post['post_status'], $post['post_type'], $post['post_parent']); $post['to_ping'] = ''; $post['pinged'] = ''; $post['menu_order'] = 0; $post['post_content_filtered'] = ''; if (empty($post['post_name']) && !in_array($post['post_status'], array('draft', 'pending', 'auto-draft'))) { $post['post_name'] = sanitize_title($post['post_title'], $post_id); } if ($mode == 'add') { $post['guid'] = ''; if (false === $wpdb->insert($wpdb->posts, $post)) { $this->error_log($row, __('This data was not registered in the database.', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; $this->create_log(); $this->progress_add(); continue; } $post_id = $wpdb->insert_id; $wc2_item->set_the_post_id($post_id); $where = array('ID' => $post_id); $wpdb->update($wpdb->posts, array('guid' => get_permalink($post_id)), $where); } elseif ($mode == 'upd') { $where = array('ID' => $post_id); if (false === $wpdb->update($wpdb->posts, $post, $where)) { $this->error_log($row, __('The data were not registered with a database.', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; $this->create_log(); $this->progress_add(); continue; } //delete all metas of Item $meta_key_table = array(); $cfrows = explode(';', trim($this->values[COL_POST_CUSTOM_FIELD])); //if( !(1 === count($cfrows) && '' == reset($cfrows)) ) { if ('' != reset($cfrows)) { foreach ($cfrows as $cf) { list($meta_key, $meta_value) = explode('=', $cf, 2); if (!WC2_Utils::is_blank($meta_key)) { array_push($meta_key_table, $this->convert_encoding($meta_key)); } } } $meta_key_table = apply_filters('wc2_filter_importitem_delete_postmeta', $meta_key_table); $res = $wc2_item->delete_custome_field_key($meta_key_table); if (false === $res) { $this->error_log($row, __('Error : delete postmeta', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; $this->create_log(); $this->progress_add(); continue; } //delete Item revisions $res = $wc2_item->delete_item_revision(); if (false === $res) { $this->error_log($row, __('Error : delete revisions', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; $this->create_log(); $this->progress_add(); continue; } //delete relationships of category $res = $wc2_item->delete_term_relationship(); if (false === $res) { $this->error_log($row, __('Error : delete term_relationships(category)', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; $this->create_log(); $this->progress_add(); continue; } //delete relationships of tag //$query = "SELECT term_taxonomy_id, COUNT(*) AS ct FROM {$wpdb->term_relationships} GROUP BY term_taxonomy_id"; //$relation_data = $wpdb->get_results( $query, ARRAY_A ); $relation_data = $wc2_item->get_count_term_taxonomy(); foreach ((array) $relation_data as $relation_rows) { $term_taxonomy_where['term_taxonomy_id'] = $relation_rows['term_taxonomy_id']; // $term_taxonomy_id['term_taxonomy_id'] = $relation_rows['term_taxonomy_id']; $term_taxonomy_updatas['count'] = $relation_rows['ct']; if (false === $wpdb->update($wpdb->term_taxonomy, $term_taxonomy_updatas, $term_taxonomy_where)) { $this->error_log($row, __('Error : delete term_relationships(tag)', 'wc2')); $pre_item_code = $this->values[COL_ITEM_CODE]; continue; } } } //add term_relationships, edit term_taxonomy //category $categories = explode(';', $this->values[COL_POST_CATEGORY]); $category_ids = array(); foreach ((array) $categories as $category) { $cat = get_term_by('slug', $category, 'item'); if ($cat == false) { $category = (string) $category; $this->error_log($row, __(sprintf('Since the category slug "%s" does not exist or could not be category registration.', $this->convert_encoding($category)), 'wc2')); continue; } $category_ids[] = $cat->term_id; } $term_taxonomy_ids = wp_set_post_terms($post_id, $category_ids, 'item'); foreach ($term_taxonomy_ids as $term_taxonomy_id) { $wc2_item->term_taxonomy_count_post($term_taxonomy_id); } //tag $tags_concat = str_replace(';', ',', $this->convert_encoding($this->values[COL_POST_TAG])); $term_taxonomy_ids = wp_set_post_terms($post_id, $tags_concat, 'item-tag'); foreach ($term_taxonomy_ids as $term_taxonomy_id) { $wc2_item->term_taxonomy_count_post($term_taxonomy_id); } //add custom field $cfrows = explode(';', trim($this->values[COL_POST_CUSTOM_FIELD])); //if( !(1 === count($cfrows) && '' == reset($cfrows)) ) { if ('' != reset($cfrows)) { $valstr = ''; foreach ($cfrows as $cf) { list($meta_key, $meta_value) = explode('=', $cf, 2); if (!WC2_Utils::is_blank($meta_key)) { update_post_meta($post_id, $this->convert_encoding($meta_key), $this->convert_encoding($meta_value)); } } } $wc2_item->clear_column(); //Item data set $col = $start_col; foreach ($item_base_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_value($key, $this->set_value($col, $column['type'])); $col++; } } foreach ($item_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_value($key, $this->set_value($col, $column['type'])); $col++; } } //SKU data set $col = $sku_start_col; foreach ($item_sku_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_sku_value($key, $sku_id, $this->set_value($col, $column['type'])); $col++; } } foreach ($item_sku_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_sku_value($key, $sku_id, $this->set_value($col, $column['type'])); $col++; } } if ($mode == 'add') { $wc2_item->add_item_data(); } elseif ($mode == 'upd') { $wc2_item->update_item_data(); } } else { //sku登録のみの行 $sku_id++; //SKU data set $col = $sku_start_col; foreach ($item_sku_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_sku_value($key, $sku_id, $this->set_value($col, $column['type'])); $col++; } } foreach ($item_sku_meta_column as $key => $column) { if ($column['display'] != 'none' and $column['type'] != TYPE_PARENT) { $wc2_item->set_the_item_sku_value($key, $sku_id, $this->set_value($col, $column['type'])); $col++; } } $item_id = $wc2_item->get_item_id_by_post_id($post_id); $sku_data = $wc2_item->get_item_sku_data($item_id, $sku_id); if (0 == $wc2_item->count_sku_data($sku_id)) { if (0 < $sku_id and '' != $wc2_item->get_the_item_sku_code($sku_id)) { $res = $wc2_item->add_item_sku_data($sku_id, $wc2_item->get_item_sku_data($item_id, $sku_id)); if (false === $res) { break; } } } else { $res = $wc2_item->update_item_sku_data($sku_id, $wc2_item->get_item_sku_data($item_id, $sku_id)); if (false === $res) { break; } } } if ('' != $this->get_log_line()) { $this->create_log(); } //登録成功数加算 $this->success += 1; $pre_item_code = $this->values[COL_ITEM_CODE]; clean_post_cache($post_id); wp_cache_delete($post_id, 'posts'); wp_cache_delete($post_id, 'post_meta'); clean_object_term_cache($post_id, 'post'); //進捗加算 $this->progress_add(); } wp_import_cleanup($this->id); $this->false = $this->data_rows - $this->success; echo '<h3>' . __('All Done.', 'wc2') . '</h3> <h3>' . sprintf(__('Success %d failure %d', 'wc2'), $this->success, $this->false) . '</h3>'; if (0 < strlen($this->log)) { WC2_Utils::wc2_log($this->log, "import_item.log"); echo str_replace("\n", "<br />", $this->log); } }
function wc2_log($log, $file, $place = ''){ WC2_Utils::wc2_log($log, $file, $place); }