Example #1
0
        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);
            }
        }
Example #2
0
    public function member_history_rows()
    {
        $wc2_db_member = WC2_DB_Member::get_instance();
        $wc2_order = WC2_DB_Order::get_instance();
        $wc2_item = WC2_DB_Item::get_instance();
        if (is_admin()) {
            $member_id = $wc2_db_member->get_member_id();
        } else {
            if (wc2_is_login() && 0 != $_SESSION[WC2]['member']['ID']) {
                $member_id = $_SESSION[WC2]['member']['ID'];
            } else {
                return false;
            }
        }
        $member_history = $wc2_order->get_member_history($member_id);
        $colspan = wc2_is_membersystem_point() ? 9 : 7;
        $html = '<div class="history-area">
					<table>';
        if (!count($member_history)) {
            $html .= '<tr>
			<td>' . __('There is no your purchase history.', 'wc2') . '</td>
			</tr>';
        }
        foreach ($member_history as $umhs) {
            $cart = $umhs['cart'];
            $history_member_head = '<tr>
				<th class="historyrow">' . __('Order number', 'wc2') . '</th>
				<th class="historyrow">' . __('Order status', 'wc2') . '</th>
				<th class="historyrow">' . __('Purchase date', 'wc2') . '</th>
				<th class="historyrow">' . __('Purchase amount', 'wc2') . '</th>';
            if (wc2_is_membersystem_point()) {
                $history_member_head .= '<th class="historyrow">' . __('Used points', 'wc2') . '</th>';
            }
            $history_member_head .= '<th class="historyrow">' . apply_filters('wc2_filter_discount_label', __('Discount', 'wc2'), $umhs) . '</th>
				<th class="historyrow">' . __('Shipping charges', 'wc2') . '</th>
				<th class="historyrow">' . apply_filters('wc2_filter_cod_label', __('COD fee', 'wc2')) . '</th>
				<th class="historyrow">' . __('Consumption tax', 'wc2') . '</th>';
            if (wc2_is_membersystem_point()) {
                $history_member_head .= '<th class="historyrow">' . __('Acquired points', 'wc2') . '</th>';
            }
            $history_member_head .= '</tr>
				<tr>
				<td class="rightnum"><a href="' . WC2_ADMIN_URL . '?page=wc2_order&action=edit&target=' . $umhs['ID'] . '">' . $umhs['dec_order_id'] . '</a></td>
				<td class="aleft">' . wc2_get_order_status_name($umhs['order_status']) . '</td>
				<td class="date">' . $umhs['order_date'] . '</td>
				<td class="rightnum">' . wc2_crform($umhs['item_total_price'] - $umhs['usedpoint'] + $umhs['discount'] + $umhs['shipping_charge'] + $umhs['cod_fee'] + $umhs['tax'], true, false) . '</td>';
            if (wc2_is_membersystem_point()) {
                $history_member_head .= '<td class="rightnum">' . number_format($umhs['usedpoint']) . '</td>';
            }
            $history_member_head .= '<td class="rightnum">' . wc2_crform($umhs['discount'], true, false) . '</td>
				<td class="rightnum">' . wc2_crform($umhs['shipping_charge'], true, false) . '</td>
				<td class="rightnum">' . wc2_crform($umhs['cod_fee'], true, false) . '</td>
				<td class="rightnum">' . wc2_crform($umhs['tax'], true, false) . '</td>';
            if (wc2_is_membersystem_point()) {
                $history_member_head .= '<td class="rightnum">' . number_format($umhs['getpoint']) . '</td>';
            }
            $history_member_head .= '</tr>';
            $html .= apply_filters('wc2_filter_history_member_head', $history_member_head, $umhs);
            $html .= apply_filters('wc2_filter_member_history_header', NULL, $umhs);
            $html .= '<tr>
				<td class="retail" colspan="' . $colspan . '">
					<table id="retail_table_' . $umhs['ID'] . '" class="retail">';
            $history_cart_head = '<tr>
					<th scope="row" class="num">No.</th>
					<th class="thumbnail">&nbsp;</th>
					<th>' . __('Items', 'wc2') . '</th>
					<th class="price ">' . __('Unit price', 'wc2') . '(' . wc2_crcode() . ')' . '</th>
					<th class="quantity">' . __('Quantity', 'wc2') . '</th>
					<th class="subtotal">' . __('Amount', 'wc2') . '(' . wc2_crcode() . ')' . '</th>
					</tr>';
            $html .= apply_filters('wc2_filter_history_cart_head', $history_cart_head, $umhs);
            $i = 1;
            foreach ($cart as $cart_row) {
                $ordercart_id = $cart_row['cart_id'];
                $post_id = $cart_row['post_id'];
                $item_id = $cart_row['item_id'];
                $sku_id = $cart_row['sku_id'];
                $quantity = $cart_row['quantity'];
                $options = isset($cart_row['options']) ? $cart_row['options'] : '';
                //$options = wc2_get_ordercart_meta_value( 'option', $ordercart_id );
                //$options = wc2_get_ordercart_meta( 'option', $ordercart_id );
                $item_name = $cart_row['item_name'];
                $item_code = $cart_row['item_code'];
                $sku_name = $cart_row['sku_name'];
                $sku_code = $cart_row['sku_code'];
                $cart_item_name = wc2_get_cart_item_name($item_name, $item_code, $sku_name, $sku_code);
                $skuPrice = $cart_row['price'];
                $pictid = (int) $wc2_item->get_mainpictid($item_code);
                $optstr = '';
                if (is_array($options) && count($options) > 0) {
                    $optstr = '';
                    foreach ($options as $key => $value) {
                        if (!empty($key)) {
                            $key = urldecode($key);
                            $value = maybe_unserialize($value);
                            if (is_array($value)) {
                                $c = '';
                                $optstr .= esc_html($key) . ' : ';
                                foreach ($value as $v) {
                                    $optstr .= $c . nl2br(esc_html(urldecode($v)));
                                    $c = ', ';
                                }
                                $optstr .= "<br />\n";
                            } else {
                                $optstr .= esc_html($key) . ' : ' . nl2br(esc_html(urldecode($value))) . "<br />\n";
                            }
                        }
                    }
                    $optstr = apply_filters('wc2_filter_option_history', $optstr, $options);
                }
                $optstr = apply_filters('wc2_filter_option_info_history', $optstr, $umhs, $cart_row, $i);
                $permalink = apply_filters('wc2_filter_link_item_history', get_permalink($post_id), $cart_row);
                $history_cart_row = '<tr>
					<td>' . $i . '</td>
					<td>';
                if ($pictid) {
                    $cart_thumbnail = '<a href="' . esc_url($permalink) . '">' . wp_get_attachment_image($pictid, array(60, 60), true) . '</a>';
                } else {
                    $cart_thumbnail = '<p>' . wc2_no_image() . '</p>';
                }
                $history_cart_row .= apply_filters('wc2_filter_cart_thumbnail', $cart_thumbnail, $post_id, $pictid, $i, $cart_row);
                $history_cart_row .= '</td>
					<td class="aleft"><a href="' . esc_url($permalink) . '">' . esc_html($cart_item_name) . '<br />' . $optstr . '</a>' . apply_filters('wc2_filter_history_item_name', NULL, $umhs, $cart_row, $i) . '</td>
					<td class="rightnum">' . wc2_crform($skuPrice, true, false) . '</td>
					<td class="rightnum">' . number_format($cart_row['quantity']) . '</td>
					<td class="rightnum">' . wc2_crform($skuPrice * $cart_row['quantity'], true, false) . '</td>
					</tr>';
                $materials = compact('cart_thumbnail', 'post_id', 'pictid', 'cart_item_name', 'optstr');
                $html .= apply_filters('wc2_filter_history_cart_row', $history_cart_row, $umhs, $cart_row, $i, $materials);
                $i++;
            }
            $html .= '</table>
				</td>
				</tr>';
        }
        $html .= '</table>
		</div>';
        $html = apply_filters('wc2_filter_member_history_rows', $html, $member_id, $member_history, $colspan);
        return $html;
    }
Example #3
0
 public function delete_all_item_data()
 {
     global $wpdb;
     $table = $wpdb->prefix . 'wc2_item';
     $query = "SELECT * FROM {$table}";
     $all_item = $wpdb->get_results($query, ARRAY_A);
     $wc2_item = WC2_DB_Item::get_instance();
     foreach ($all_item as $key => $val) {
         $wc2_item->set_the_post_id($val['item_post_id']);
         $wc2_item->delete_item_data();
     }
     $args = array('post_type' => 'item', 'numberposts' => -1, 'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'));
     $item_posts = get_posts($args);
     foreach ($item_posts as $key => $val) {
         wp_delete_post($val->ID);
     }
 }
Example #4
0
function wc2_get_cart_item_name($item_name, $item_code, $sku_name, $sku_code)
{
    $name_arr = array();
    $name_str = '';
    $wc2_item = WC2_DB_Item::get_instance();
    $general = wc2_get_option('general');
    foreach ($general['indi_item_name'] as $key => $value) {
        if ($value) {
            $pos = (int) $general['pos_item_name'][$key];
            $ind = $pos === 0 ? 'A' : $pos;
            switch ($key) {
                case 'item_name':
                    $name_arr[$ind][$key] = $item_name;
                    break;
                case 'item_code':
                    $name_arr[$ind][$key] = $item_code;
                    break;
                case 'sku_name':
                    $name_arr[$ind][$key] = $sku_name;
                    break;
                case 'sku_code':
                    $name_arr[$ind][$key] = $sku_code;
                    break;
            }
        }
    }
    ksort($name_arr);
    foreach ($name_arr as $vals) {
        foreach ($vals as $key => $value) {
            $name_str .= $value . ' ';
        }
    }
    $name_str = apply_filters('wc2_admin_order_item_name_filter', $name_str, $item_name, $item_code, $sku_name, $sku_code);
    return trim($name_str);
}
Example #5
0
function wc2_item_search_where($where)
{
    global $post_type, $pagenow, $wpdb;
    if (ITEM_POST_TYPE == $post_type and $pagenow == 'edit.php') {
        $wc2_item = WC2_DB_Item::get_instance();
        if (array_key_exists('search_condition', $_REQUEST)) {
            switch ($_REQUEST['search_condition']) {
                case 'item_code':
                    if (array_key_exists('search_word', $_REQUEST)) {
                        $search_word = trim($_REQUEST['search_word']);
                        if (0 < strlen($search_word)) {
                            $where .= " AND {$wc2_item->item_table}.item_code LIKE '%{$search_word}%' ";
                        }
                    }
                    break;
                case 'item_name':
                    if (array_key_exists('search_word', $_REQUEST)) {
                        $search_word = trim($_REQUEST['search_word']);
                        if (0 < strlen($search_word)) {
                            $where .= " AND {$wc2_item->item_table}.item_name LIKE '%{$search_word}%' ";
                        }
                    }
                    break;
                case 'stock0':
                    $where .= " AND {$wc2_item->item_sku_table}.sku_stock = 0 ";
                    break;
                case 'stock_status':
                    if (array_key_exists('search_word', $_REQUEST)) {
                        $search_word = trim($_REQUEST['search_word']);
                        $where .= " AND {$wc2_item->item_sku_table}.sku_status = {$search_word} ";
                    }
                    break;
                case 'display_status':
                    if (array_key_exists('search_word', $_REQUEST)) {
                        $search_word = trim($_REQUEST['search_word']);
                        $where .= " AND {$wpdb->posts}.post_status = '{$search_word}' ";
                    }
                    break;
            }
        }
    }
    return $where;
}
Example #6
0
function wc2_admin_order_additem_field($key, $column, $sku, $sku_id, $data)
{
    $wc2_item = WC2_DB_Item::get_instance();
    $html = '';
    if ($column['display'] == 'hidden') {
    } elseif ($column['display'] == '') {
        switch ($column['type']) {
            case TYPE_TEXT:
            case TYPE_TEXT_Z:
            case TYPE_TEXT_ZK:
            case TYPE_TEXT_A:
            case TYPE_TEXT_I:
            case TYPE_TEXT_F:
            case TYPE_TEXTAREA:
                $html .= $column['label_pre'] . esc_attr($data) . $column['label_post'];
                break;
            case TYPE_TEXT_P:
                $html .= $column['label_pre'] . esc_attr(floor($data)) . $column['label_post'];
                break;
            case TYPE_SELECT:
            case TYPE_RADIO:
                $select = explode(';', $column['value']);
                foreach ($select as $option) {
                    list($value, $name) = explode(':', $option);
                    if ($data == $value) {
                        $html .= $column['label_pre'] . esc_attr($name) . $column['label_post'];
                        break;
                    }
                }
                break;
            case TYPE_SELECT_MULTIPLE:
            case TYPE_CHECK:
                $select = explode(';', $column['value']);
                foreach ($select as $option) {
                    list($value, $name) = explode(':', $option);
                    if (is_array($data)) {
                        if (array_key_exists($value, $data)) {
                            $html .= $column['label_pre'] . esc_attr($name) . $column['label_post'] . ', ';
                        }
                    } else {
                        if ($data == $value) {
                            $html .= $column['label_pre'] . esc_attr($name) . $column['label_post'] . ', ';
                        }
                    }
                }
                $html = rtrim($html, ', ');
                break;
        }
    }
    return $html;
}