PluginProbe
Welcart e-Commerce / 2.12.4
Welcart e-Commerce v2.12.4
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
← All changes | functions/define_function.php +2852 -1295 1.3.42.12.4 View file →
@@ -1,1295 +1,2852 @@
1 -<?php
2 -function usces_define_functions(){
3 -
4 -//20101111ysk start
5 -if( !function_exists('usces_item_uploadcsv') ):
6 -function usces_item_uploadcsv(){
7 - global $wpdb, $usces, $user_ID;
8 -
9 - if( !current_user_can( 'import' ) ){
10 - $res['status'] = 'error';
11 - $res['message'] = __('You do not have permission to do that.');
12 - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
13 - wp_redirect($url);
14 - exit;
15 - }
16 -
17 - //check data SELECT id, title FROM table GROUP BY id HAVING COUNT(id) > 1
18 - $query = $wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta}
19 - LEFT JOIN {$wpdb->posts} ON ID = post_id
20 - WHERE meta_key = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')
21 - GROUP BY meta_value HAVING COUNT(meta_value) > 1",
22 - '_itemCode');
23 - $db_check = $wpdb->get_results( $query, ARRAY_A );
24 - if( $db_check ) {
25 - $res['status'] = 'error';
26 - $res['message'] .= __('The same product cord is registered.', 'usces');
27 - foreach($db_check as $d_item){
28 - $res['message'] .= ' , ' . $d_item['meta_value'];
29 - }
30 - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
31 - wp_redirect($url);
32 - exit;
33 - }
34 -
35 -
36 - $path = WP_CONTENT_DIR . '/uploads/';
37 -/*********************************************************************/
38 -// Upload
39 -/**********************************************************************/
40 - if( isset($_REQUEST['action']) && 'itemcsv' == $_REQUEST['action'] ){
41 - $workfile = $_FILES["usces_upcsv"]["tmp_name"];
42 -
43 - if ( !is_uploaded_file($workfile) ) {
44 - $res['status'] = 'error';
45 - $res['message'] = __('The file was not uploaded.', 'usces');
46 - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
47 - wp_redirect($url);
48 - exit;
49 - }
50 -
51 - //check ext
52 - list($fname, $fext) = explode('.', $_FILES["usces_upcsv"]["name"], 2);
53 - if( $fext != 'csv' ) {
54 - $res['status'] = 'error';
55 - $res['message'] = __('The file is not supported.', 'usces').$fname.'.'.$fext;
56 - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
57 - wp_redirect($url);
58 - exit;
59 - }
60 -
61 - $new_filename = base64_encode($fname . '_' . time() . '.' .$fext);
62 - if ( ! move_uploaded_file($_FILES['usces_upcsv']['tmp_name'], $path.$new_filename) ) {
63 - $res['status'] = 'error';
64 - $res['message'] = __('The file was not stored.', 'usces').$fname.'.'.$fext;
65 - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']);
66 - wp_redirect($url);
67 - exit;
68 - }
69 - return $new_filename;
70 - }
71 -
72 -
73 - $yn = "\r\n";
74 - $br = "<br />";
75 -/*********************************************************************/
76 -// Register
77 -/**********************************************************************/
78 - if( isset($_REQUEST['regfile']) && !WCUtils::is_blank($_REQUEST['regfile']) && isset($_REQUEST['action']) && 'upload_register' == $_REQUEST['action'] ){
79 - $file_name = $_REQUEST['regfile'];
80 - $decode_filename = base64_decode($file_name);
81 - if( ! file_exists($path.$file_name) ){
82 - $res['status'] = 'error';
83 - $res['message'] = __('CSV file does not exist.', 'usces').esc_html($decode_filename);
84 - //echo $res['status'] . ' : ' . $res['message'];
85 - //return;
86 - return( $res );
87 - }
88 - }
89 - /*////////////////////////////////////////*/
90 - // ready
91 - /*////////////////////////////////////////*/
92 - $start = microtime(true);
93 -
94 - //$wpdb->show_errors();
95 - $wpdb->query( 'SET SQL_BIG_SELECTS=1' );
96 - set_time_limit(3600);
97 -
98 - define('USCES_COL_POST_ID', 0);
99 - define('USCES_COL_POST_AUTHOR', 1);
100 - define('USCES_COL_POST_CONTENT', 2);
101 - define('USCES_COL_POST_TITLE', 3);
102 - define('USCES_COL_POST_EXCERPT', 4);
103 - define('USCES_COL_POST_STATUS', 5);
104 - define('USCES_COL_POST_COMMENT_STATUS', 6);
105 - define('USCES_COL_POST_PASSWORD', 7);
106 - define('USCES_COL_POST_NAME', 8);
107 - define('USCES_COL_POST_MODIFIED', 9);
108 -
109 - define('USCES_COL_ITEM_CODE', 10);
110 - define('USCES_COL_ITEM_NAME', 11);
111 - define('USCES_COL_ITEM_RESTRICTION', 12);
112 - define('USCES_COL_ITEM_POINTRATE', 13);
113 - define('USCES_COL_ITEM_GPNUM1', 14);
114 - define('USCES_COL_ITEM_GPDIS1', 15);
115 - define('USCES_COL_ITEM_GPNUM2', 16);
116 - define('USCES_COL_ITEM_GPDIS2', 17);
117 - define('USCES_COL_ITEM_GPNUM3', 18);
118 - define('USCES_COL_ITEM_GPDIS3', 19);
119 - define('USCES_COL_ITEM_SHIPPING', 20);
120 - define('USCES_COL_ITEM_DELIVERYMETHOD', 21);
121 - define('USCES_COL_ITEM_SHIPPINGCHARGE', 22);
122 - define('USCES_COL_ITEM_INDIVIDUALSCHARGE', 23);
123 -
124 - define('USCES_COL_CATEGORY', 24);
125 - define('USCES_COL_POST_TAG', 25);
126 - define('USCES_COL_CUSTOM_FIELD', 26);
127 -
128 - define('USCES_COL_SKU_CODE', 27);
129 - define('USCES_COL_SKU_NAME', 28);
130 - define('USCES_COL_SKU_CPRICE', 29);
131 - define('USCES_COL_SKU_PRICE', 30);
132 - define('USCES_COL_SKU_ZAIKONUM', 31);
133 - define('USCES_COL_SKU_ZAIKO', 32);
134 - define('USCES_COL_SKU_UNIT', 33);
135 - define('USCES_COL_SKU_GPTEKIYO', 34);
136 -// define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
137 -
138 - $lines = array();
139 - $total_num = 0;
140 - $comp_num = 0;
141 - $err_num = 0;
142 - $min_field_num = 35;//29
143 - $log = '';
144 - $pre_code = '';
145 - $sku_index = 0;
146 - $res = array();
147 - $date_pattern = "/(\d{4})-(\d{2}|\d)-(\d{2}|\d) (\d{2}):(\d{2}|\d):(\d{2}|\d)/";
148 - $linereadytime = 0;
149 - $checktime = 0;
150 - $insertposttime = 0;
151 - $metadeletetime = 0;
152 - $addmetatime = 0;
153 - $AddOptiontime = 0;
154 - $AddSKUtime = 0;
155 - $onelinetime = 0;
156 -
157 - //log
158 - if ( ! ($fpi = fopen (USCES_PLUGIN_DIR.'/logs/itemcsv_log.txt', "w"))) {
159 - $res['status'] = 'error';
160 - $res['message'] = __('The log file was not prepared for.', 'usces').esc_html($decode_filename);
161 - echo $res['status'] . ' : ' . $res['message'];
162 - return;
163 - }
164 - //read data
165 - if ( ! ($fpo = fopen ($path.$file_name, "r"))) {
166 - $res['status'] = 'error';
167 - $res['message'] = __('A file does not open.', 'usces').esc_html($decode_filename);
168 - echo $res['status'] . ' : ' . $res['message'];
169 - return;
170 - }
171 -
172 -
173 - $orglines = array();
174 - $sp = ",";
175 - $fname_parts = explode('.', $decode_filename);
176 - if('csv' !== end($fname_parts)) {
177 - $res['status'] = 'error';
178 - $res['message'] = __('This file is not in the CSV file.', 'usces').esc_html($decode_filename);
179 - echo $res['status'] . ' : ' . $res['message'];
180 - return;
181 -
182 - } else {
183 - $buf = '';
184 - while (! feof ($fpo)) {
185 - $temp = fgets ($fpo, 10240);
186 - if( 0 == strlen($temp) ) continue;
187 -
188 - $num = substr_count($temp, '"');
189 - if( 0 == $num % 2 && '' == $buf ){
190 - $orglines[] = $temp;
191 - }elseif( 1 == $num % 2 && '' == $buf ){
192 - $buf .= $temp;
193 - }elseif( 0 == $num % 2 && '' != $buf ){
194 - $buf .= $temp;
195 - }elseif( 1 == $num % 2 && '' != $buf ){
196 - $buf .= $temp;
197 - $orglines[] = $buf;
198 - $buf = '';
199 - }
200 - }
201 - }
202 - fclose($fpo);
203 -
204 - echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . '");</script>'.$yn;
205 - echo '<div class="error_log">'.$yn;
206 - ob_flush();
207 - flush();
208 -
209 - foreach($orglines as $line){
210 - $line = trim($line);
211 - if( 0 !== strpos( $line, 'Post ID' ) && !empty($line) )
212 - $lines[] = $line;
213 - }
214 - $total_num = count($lines);
215 -
216 - $readytime = microtime(true);
217 - //reg loop
218 - foreach($lines as $rows_num => $line){
219 -
220 - /*////////////////////////////////////////*/
221 - // lineReady
222 - /*////////////////////////////////////////*/
223 - $linestart = microtime(true);
224 - $datas = array();
225 -
226 - $logtemp = '';
227 - $mestemp = '';
228 - $line = trim($line);
229 - if( empty($line) ) continue;
230 -
231 - $d = explode($sp, $line);
232 - $buf = '';
233 - foreach($d as $data) {
234 - $num = substr_count($data, '"');
235 - if( 0 == $num % 2 && '' == $buf ){
236 - if( '"' == substr($data, 0, 1) )
237 - $data = substr($data, 1);
238 - if( '"' == substr($data, -1) )
239 - $data = substr($data, 0, -1);
240 - $data = str_replace(array('""'), '"', $data);
241 - $datas[] = ( false !== $data ) ? $data : '';
242 - }elseif( 1 == $num % 2 && '' == $buf ){
243 - $buf .= $data;
244 - }elseif( 0 == $num % 2 && '' != $buf ){
245 - $buf .= $sp.$data;
246 - }elseif( 1 == $num % 2 && '' != $buf ){
247 - $buf .= $sp.$data;
248 - if( '"' == substr($buf, 0, 1) )
249 - $buf = substr($buf, 1);
250 - if( '"' == substr($buf, -1) )
251 - $buf = substr($buf, 0, -1);
252 - $buf = str_replace(array('""'), '"', $buf);
253 - $datas[] = ( false !== $buf ) ? $buf : '';
254 - $buf = '';
255 - }
256 - }
257 -
258 - if( 'Post ID' == $datas[USCES_COL_POST_ID] )
259 - continue;
260 -
261 -
262 - if( $min_field_num > count($datas) ){
263 - $err_num++;
264 - $mes = "No." . ($rows_num+1)." ".count($datas) . "\t".__('The number of the columns is abnormal.', 'usces');
265 - $logtemp .= $mes.$yn;
266 - $log .= $logtemp;
267 - echo $mes.$br.$yn;
268 - continue;
269 - }
270 -
271 - if( $pre_code == $datas[USCES_COL_ITEM_CODE] && WCUtils::is_blank($datas[USCES_COL_POST_ID]) ){
272 - $mode = 'add';
273 -
274 - }else{
275 - $post_id = ( !WCUtils::is_blank($datas[USCES_COL_POST_ID]) ) ? (int)$datas[USCES_COL_POST_ID] : NULL;
276 - if( $post_id ){
277 - $db_res = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $post_id) );
278 - if( !$db_res ){
279 - $err_num++;
280 - $mes = "No." . ($rows_num+1) . "\t".__("Post-ID {$post_id} does not exist in the database.", 'usces');
281 - $logtemp .= $mes.$yn;
282 - $log .= $logtemp;
283 - echo $mes.$br.$yn;
284 - continue;
285 - }
286 - }
287 - if( $post_id ){
288 - $mode = 'upd';
289 - }else{
290 - $mode = 'add';
291 - }
292 - }
293 -
294 - $lineready = microtime(true);
295 - $linereadytime += $lineready-$linestart;
296 - /*////////////////////////////////////////*/
297 - // dataCheck
298 - /*////////////////////////////////////////*/
299 - //data check loop
300 - foreach($datas as $key => $data){
301 - $data = trim(mb_convert_encoding($data, 'UTF-8', 'SJIS'));
302 - switch($key){
303 - case USCES_COL_ITEM_CODE:
304 - if( 0 == strlen($data) ){
305 - $mes = "No." . ($rows_num+1) . "\t".__('An item cord is non-input.', 'usces');
306 - $logtemp .= $mes.$yn;
307 - $mestemp .= $mes.$br.$yn;
308 - }else{
309 - $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta}
310 - LEFT JOIN {$wpdb->posts} ON ID = post_id
311 - WHERE meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')",
312 - '_itemCode', $data);
313 - $db_res1 = $wpdb->get_results( $query, ARRAY_A );
314 - if( 'upd' == $mode ){
315 - if( 1 < count($db_res1) ){
316 - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has been duplicated.', 'usces');
317 - $logtemp .= $mes.$yn;
318 - $mestemp .= $mes.$br.$yn;
319 - $mes = '';
320 - foreach($db_res1 as $res_val){
321 - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
322 - }
323 - $logtemp .= $mes.$yn;
324 - $mestemp .= $mes.$br.$yn;
325 - }
326 - $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta}
327 - LEFT JOIN {$wpdb->posts} ON ID = post_id
328 - WHERE post_id <> %d AND meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')",
329 - $post_id, '_itemCode', $data);
330 - $db_res2 = $wpdb->get_results( $query, ARRAY_A );
331 - if( 0 < count($db_res2) ){
332 - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces');
333 - $logtemp .= $mes.$yn;
334 - $mestemp .= $mes.$br.$yn;
335 - $mes = '';
336 - foreach($db_res2 as $res_val){
337 - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
338 - }
339 - $logtemp .= $mes.$yn;
340 - $mestemp .= $mes.$br.$yn;
341 - }
342 - }else if( 'add' == $mode ){
343 - if( $data != $pre_code ) {
344 - if( 0 < count($db_res1) ){
345 - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces');
346 - $logtemp .= $mes.$yn;
347 - $mestemp .= $mes.$br.$yn;
348 - $mes = '';
349 - foreach($db_res1 as $res_val){
350 - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />";
351 - }
352 - $logtemp .= $mes.$yn;
353 - $mestemp .= $mes.$br.$yn;
354 - }
355 - }
356 - }
357 - }
358 - break;
359 - case USCES_COL_ITEM_NAME:
360 - if( 0 == strlen($data) ){
361 - $mes = "No." . ($rows_num+1) . "\t".__('An item name is non-input.', 'usces');
362 - $logtemp .= $mes.$yn;
363 - $mestemp .= $mes.$br.$yn;
364 - }
365 - break;
366 - case USCES_COL_ITEM_RESTRICTION:
367 - if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){
368 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the purchase limit number is abnormal.', 'usces');
369 - $logtemp .= $mes.$yn;
370 - $mestemp .= $mes.$br.$yn;
371 - }
372 - break;
373 - case USCES_COL_ITEM_POINTRATE:
374 - if( !preg_match("/^[0-9]+$/", $data) ){
375 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the point rate is abnormal.', 'usces');
376 - $logtemp .= $mes.$yn;
377 - $mestemp .= $mes.$br.$yn;
378 - }
379 - break;
380 - case USCES_COL_ITEM_GPNUM1:
381 - if( !preg_match("/^[0-9]+$/", $data) ){
382 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('umerical value is abnormality.', 'usces');
383 - $logtemp .= $mes.$yn;
384 - $mestemp .= $mes.$br.$yn;
385 - }
386 - break;
387 - case USCES_COL_ITEM_GPDIS1:
388 - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM1] && 1 > $data ) ){
389 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('rate is abnormal.', 'usces');
390 - $logtemp .= $mes.$yn;
391 - $mestemp .= $mes.$br.$yn;
392 - }
393 - break;
394 - case USCES_COL_ITEM_GPNUM2:
395 - if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM1] >= $data && 0 != $data) ){
396 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('umerical value is abnormality.', 'usces');
397 - $logtemp .= $mes.$yn;
398 - $mestemp .= $mes.$br.$yn;
399 - }
400 - break;
401 - case USCES_COL_ITEM_GPDIS2:
402 - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM2] && 1 > $data ) ){
403 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('rate is abnormal.', 'usces');
404 - $logtemp .= $mes.$yn;
405 - $mestemp .= $mes.$br.$yn;
406 - }
407 - break;
408 - case USCES_COL_ITEM_GPNUM3:
409 - if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM2] >= $data && 0 != $data) ){
410 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('umerical value is abnormality.', 'usces');
411 - $logtemp .= $mes.$yn;
412 - $mestemp .= $mes.$br.$yn;
413 - }
414 - break;
415 - case USCES_COL_ITEM_GPDIS3:
416 - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM3] && 1 > $data ) ){
417 - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('rate is abnormal.', 'usces');
418 - $logtemp .= $mes.$yn;
419 - $mestemp .= $mes.$br.$yn;
420 - }
421 - break;
422 - case USCES_COL_ITEM_SHIPPING:
423 - if( !preg_match("/^[0-9]+$/", $data) || 9 < $data ){
424 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the shipment day is abnormal.', 'usces');
425 - $logtemp .= $mes.$yn;
426 - $mestemp .= $mes.$br.$yn;
427 - }
428 - break;
429 - case USCES_COL_ITEM_DELIVERYMETHOD:
430 - if( 0 === strlen($data) || !preg_match("/^[0-9;]+$/", $data) ){
431 - $mes = "No." . ($rows_num+1) . "\t".__('Invalid value of Delivery method.', 'usces');
432 - $logtemp .= $mes.$yn;
433 - $mestemp .= $mes.$br.$yn;
434 - }
435 - break;
436 - case USCES_COL_ITEM_SHIPPINGCHARGE:
437 - if( 0 === strlen($data) || !preg_match("/^[0-9]+$/", $data) ){
438 - $mes = "No." . ($rows_num+1) . "\t".__('Invalid type of shipping charge.', 'usces');
439 - $logtemp .= $mes.$yn;
440 - $mestemp .= $mes.$br.$yn;
441 - }
442 - break;
443 - case USCES_COL_ITEM_INDIVIDUALSCHARGE:
444 - if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){
445 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the postage individual charging is abnormal.', 'usces');
446 - $logtemp .= $mes.$yn;
447 - $mestemp .= $mes.$br.$yn;
448 - }
449 - break;
450 - case USCES_COL_POST_ID:
451 - if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){
452 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the Post-ID is abnormal.', 'usces');
453 - $logtemp .= $mes.$yn;
454 - $mestemp .= $mes.$br.$yn;
455 - }
456 - break;
457 - case USCES_COL_POST_AUTHOR:
458 - case USCES_COL_POST_COMMENT_STATUS:
459 - case USCES_COL_POST_PASSWORD:
460 - case USCES_COL_POST_NAME:
461 - case USCES_COL_POST_TITLE:
462 - case USCES_COL_POST_CONTENT:
463 - case USCES_COL_POST_EXCERPT:
464 - break;
465 - case USCES_COL_POST_STATUS:
466 - $array17 = array('publish', 'future', 'draft', 'pending', 'private');
467 - if( !in_array($data, $array17) || WCUtils::is_blank($data) ){
468 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the display status is abnormal.', 'usces');
469 - $logtemp .= $mes.$yn;
470 - $mestemp .= $mes.$br.$yn;
471 - }
472 - break;
473 - case USCES_COL_POST_MODIFIED:
474 - if( 'future' == $datas[USCES_COL_POST_STATUS] && (WCUtils::is_blank($data) || '0000-00-00 00:00:00' === $data) ){
475 - if( preg_match($date_pattern, $data, $match) ){
476 - if( checkdate($match[2], $match[3], $match[1]) &&
477 - (0 < $match[4] && 24 > $match[4]) &&
478 - (0 < $match[5] && 60 > $match[5]) &&
479 - (0 < $match[6] && 60 > $match[6]) ){
480 - $mes = "";
481 - }else{
482 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
483 - $logtemp .= $mes.$yn;
484 - $mestemp .= $mes.$br.$yn;
485 - }
486 -
487 - }else{
488 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
489 - $logtemp .= $mes.$yn;
490 - $mestemp .= $mes.$br.$yn;
491 - }
492 - }else if( !WCUtils::is_blank($data) && '0000-00-00 00:00:00' !== $data ){
493 - if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//先頭4桁が数値のみ
494 - if(strtotime($data) === false){
495 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
496 - $logtemp .= $mes.$yn;
497 - $mestemp .= $mes.$br.$yn;
498 - }
499 - } else {
500 - $datetime = explode(' ', $data);
501 - $date_str = usces_dates_interconv($datetime[0]).' '.$datetime[1];
502 - if(strtotime($date_str) === false){
503 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces');
504 - $logtemp .= $mes.$yn;
505 - $mestemp .= $mes.$br.$yn;
506 - }
507 - }
508 - }
509 - break;
510 - case USCES_COL_CATEGORY:
511 - if( 0 == strlen($data) ){
512 - $mes = "No." . ($rows_num+1) . "\t".__('A category is non-input.', 'usces');
513 - $logtemp .= $mes.$yn;
514 - $mestemp .= $mes.$br.$yn;
515 - }
516 - break;
517 - case USCES_COL_POST_TAG:
518 - break;
519 - case USCES_COL_CUSTOM_FIELD:
520 - break;
521 - case USCES_COL_SKU_CODE:
522 - if( 0 == strlen($data) ){
523 - $mes = "No." . ($rows_num+1) . "\t".__('A SKU cord is non-input.', 'usces');
524 - $logtemp .= $mes.$yn;
525 - $mestemp .= $mes.$br.$yn;
526 - }
527 - break;
528 - case USCES_COL_SKU_NAME:
529 - break;
530 - case USCES_COL_SKU_CPRICE:
531 - if( 0 < strlen($data) and !preg_match("/^\d$|^\d+\.?\d+$/", $data) ){
532 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the price is abnormal.', 'usces');
533 - $logtemp .= $mes.$yn;
534 - $mestemp .= $mes.$br.$yn;
535 - }
536 - break;
537 - case USCES_COL_SKU_PRICE:
538 - if( !preg_match("/^\d$|^\d+\.?\d+$/", $data) || 0 == strlen($data) ){
539 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the normal price is abnormal.', 'usces');
540 - $logtemp .= $mes.$yn;
541 - $mestemp .= $mes.$br.$yn;
542 - }
543 - break;
544 - case USCES_COL_SKU_ZAIKONUM:
545 - if( 0 < strlen($data) and !preg_match("/^[0-9]+$/", $data) ){
546 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock amount is abnormal.', 'usces');
547 - $logtemp .= $mes.$yn;
548 - $mestemp .= $mes.$br.$yn;
549 - }
550 - break;
551 - case USCES_COL_SKU_ZAIKO:
552 - if( !preg_match("/^[0-9]+$/", $data) || 4 < $data ){
553 - $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock status is abnormal.', 'usces');
554 - $logtemp .= $mes.$yn;
555 - $mestemp .= $mes.$br.$yn;
556 - }
557 - break;
558 - case USCES_COL_SKU_UNIT:
559 - break;
560 - case USCES_COL_SKU_GPTEKIYO:
561 - if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){
562 - $mes = "No." . ($rows_num+1) . "\t".__('The value of the duties pack application is abnormal.', 'usces');
563 - $logtemp .= $mes.$yn;
564 - $mestemp .= $mes.$br.$yn;
565 - }
566 - break;
567 - }
568 - }
569 - $opnum = ceil((count($datas) - $min_field_num) / 4);
570 - for($i=0; $i<$opnum; $i++){
571 - $val = array();
572 - $oplogtemp = '';
573 - $opmestemp = '';
574 - for($o=1; $o<=4; $o++){
575 - $key = ($min_field_num-1)+$o+($i*4);
576 - if( isset($datas[$key]) ){
577 - $value = trim($datas[$key]);
578 - }else{
579 - $value = NULL;
580 - }
581 - switch($o){
582 - case 1:
583 - if( empty($value) ){
584 - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$yn;
585 - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn;
586 - }
587 - $val['name'] = $value;
588 - break;
589 - case 2:
590 - if( $value != NULL && ((0 != (int)$value) and (1 != (int)$value) and (2 != (int)$value) and (5 != (int)$value)) ){
591 - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$yn;
592 - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn;
593 - }
594 - $val['mean'] = $value;
595 - break;
596 - case 3:
597 - if( $value != NULL && (!preg_match("/^[0-9]+$/", $value) || 1 < (int)$value) ){
598 - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$yn;
599 - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn;
600 - }
601 - $val['essential'] = $value;
602 - break;
603 - case 4:
604 - if( ($value != NULL && $value == '') && (2 > $datas[($key-2)] && 0 < strlen($datas[($key-2)])) ){
605 - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$yn;
606 - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn;
607 - }
608 - $val['value'] = $value;
609 - break;
610 - }
611 - }
612 - if( !WCUtils::is_blank($val['name']) || !WCUtils::is_blank($val['mean']) || !WCUtils::is_blank($val['essential']) || !WCUtils::is_blank($val['value']) ){
613 - $logtemp .= $oplogtemp;
614 - $mestemp .= $opmestemp;
615 - }
616 - }
617 - if( 0 < strlen($logtemp) ){
618 - $err_num++;
619 - $log .= $logtemp;
620 - echo $mestemp;
621 - continue;
622 - }
623 -
624 -
625 - /******************/
626 - $checkend = microtime(true);
627 - $checktime += $checkend-$lineready;
628 - /*////////////////////////////////////////*/
629 - // insPost
630 - /*////////////////////////////////////////*/
631 - //wp_posts data reg;
632 - $cdatas = array();
633 - $post_fields = array();
634 - $sku = array();
635 - $opt = array();
636 - $valstr = '';
637 -
638 - if( $pre_code != $datas[USCES_COL_ITEM_CODE] ){
639 - $sku_index = 0;
640 -
641 - $cdatas['ID'] = $post_id;
642 -
643 - $data = $datas[USCES_COL_POST_MODIFIED];
644 - if( $data == '' || $data == '0000-00-00 00:00:00' ){
645 - $cdatas['post_date'] = current_time( 'mysql' );
646 - $cdatas['post_date_gmt'] = current_time( 'mysql', 1 );
647 - $cdatas['post_modified'] = current_time( 'mysql' );
648 - $cdatas['post_modified_gmt'] = current_time( 'mysql', 1 );
649 - }else{
650 - if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//先頭4桁が数値のみ
651 - $time_data =strtotime($data);
652 - } else {
653 - $datetime = explode(' ', $data);
654 - $date_str = usces_dates_interconv( $datetime[0] ).' '.$datetime[1];
655 - $time_data =strtotime($date_str);
656 - }
657 - $cdatas['post_date'] = date('Y-m-d H:i:s', $time_data);
658 - $cdatas['post_date_gmt'] = gmdate('Y-m-d H:i:s', $time_data);
659 - $cdatas['post_modified'] = date('Y-m-d H:i:s', $time_data);
660 - $cdatas['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $time_data);
661 - }
662 -//usces_log('mysql2date : '.mysql2date('U', $cdatas['post_modified'], false) . '/' . mysql2date('U', current_time( 'mysql' ), false), 'acting_transaction.log');
663 - if ( 'publish' == $datas[USCES_COL_POST_STATUS] ) {
664 - $now = current_time( 'mysql' );
665 - if ( mysql2date('U', $cdatas['post_modified'], false) > mysql2date('U', $now, false) )
666 - $datas[USCES_COL_POST_STATUS] = 'future';
667 - } elseif( 'future' == $datas[USCES_COL_POST_STATUS] ) {
668 - $now = current_time( 'mysql' );
669 - if ( mysql2date('U', $cdatas['post_modified'], false) <= mysql2date('U', $now, false) )
670 - $datas[USCES_COL_POST_STATUS] = 'publish';
671 - }
672 -
673 - $cdatas['ID'] = $post_id;
674 - $cdatas['post_author'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_AUTHOR]) ) ? $datas[USCES_COL_POST_AUTHOR] : 1;
675 - $cdatas['post_content'] = trim(mb_convert_encoding($datas[USCES_COL_POST_CONTENT], 'UTF-8', 'SJIS'));
676 - $cdatas['post_title'] = trim(mb_convert_encoding($datas[USCES_COL_POST_TITLE], 'UTF-8', 'SJIS'));
677 - $cdatas['post_excerpt'] = trim(mb_convert_encoding($datas[USCES_COL_POST_EXCERPT], 'UTF-8', 'SJIS'));
678 - $cdatas['post_status'] = $datas[USCES_COL_POST_STATUS];
679 - $cdatas['comment_status'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_COMMENT_STATUS]) ) ? $datas[USCES_COL_POST_COMMENT_STATUS] : 'close';
680 - $cdatas['ping_status'] = 'close';
681 - $cdatas['post_password'] = ( 'private' == $cdatas['post_status'] ) ? '' : $datas[USCES_COL_POST_PASSWORD];
682 - $cdatas['post_type'] = 'post';
683 - $cdatas['post_parent'] = 0;
684 - $spname = sanitize_title(trim(mb_convert_encoding($datas[USCES_COL_POST_NAME], 'UTF-8', 'SJIS')));
685 - $cdatas['post_name'] = urlencode(wp_unique_post_slug($spname, $cdatas['ID'], $cdatas['post_status'], $cdatas['post_type'], $cdatas['post_parent']));
686 - $cdatas['to_ping'] = '';
687 - $cdatas['pinged'] = '';
688 - $cdatas['menu_order'] = 0;
689 - $cdatas['post_mime_type'] = 'item';
690 - $cdatas['post_content_filtered'] = '';
691 -
692 -
693 - if ( empty($cdatas['post_name']) && !in_array( $cdatas['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
694 - $cdatas['post_name'] = sanitize_title($cdatas['post_title'], $post_id);
695 - }
696 -
697 - if($mode == 'add') {
698 -
699 - $cdatas['guid'] = '';
700 - if ( false === $wpdb->insert( $wpdb->posts, $cdatas ) ) {
701 - $err_num++;
702 - $mes = "No." . ($rows_num+1) . "\t".__('This data was not registered in the database.', 'usces');
703 - $log .= $mes.$yn;
704 - echo $mes.$br.$yn;
705 - $pre_code = $datas[USCES_COL_ITEM_CODE];
706 - continue;
707 - }
708 - $post_id = $wpdb->insert_id;
709 - $where = array( 'ID' => $post_id );
710 - $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where );
711 -
712 - /******************/
713 - $insert_post = microtime(true);
714 - $insertposttime += $insert_post-$checkend;
715 -
716 - }elseif($mode == 'upd') {
717 -
718 - $where = array( 'ID' => $post_id );
719 - if ( false === $wpdb->update( $wpdb->posts, $cdatas, $where ) ) {
720 - $err_num++;
721 - $mes = "No." . ($rows_num+1) . "\t".__('The data were not registered with a database.', 'usces');
722 - $log .= $mes.$yn;
723 - echo $mes.$br.$yn;
724 - $pre_code = $datas[USCES_COL_ITEM_CODE];
725 - continue;
726 - }
727 -
728 - /******************/
729 - $insert_post = microtime(true);
730 - $insertposttime += $insert_post-$checkend;
731 - //end of wp_insert_post
732 - /************************************************************************************************/
733 - /*////////////////////////////////////////*/
734 - // delMeta
735 - /*////////////////////////////////////////*/
736 -
737 - //delete all metas of Item
738 -//20130723ysk start 0000734
739 - $meta_key_table = array( '_itemCode', '_itemName', '_itemRestriction', '_itemPointrate', '_itemGpNum1', '_itemGpDis1', '_itemGpNum2', '_itemGpDis2', '_itemGpNum3', '_itemGpDis3', '_itemShipping', '_itemDeliveryMethod', '_itemShippingCharge', '_itemIndividualSCharge', '_iopt_', '_isku_' );
740 - $cfrows = explode( ';', trim(mb_convert_encoding($datas[USCES_COL_CUSTOM_FIELD], 'UTF-8', 'SJIS')) );
741 - if( !(1 === count($cfrows) && '' == reset($cfrows)) ) {
742 - foreach( $cfrows as $row ) {
743 - list( $meta_key, $meta_value ) = explode( '=', $row, 2 );
744 - if( !WCUtils::is_blank($meta_key) )
745 - array_push( $meta_key_table, trim($meta_key) );
746 - }
747 - }
748 - $meta_key_table = apply_filters( 'usces_filter_uploadcsv_delete_postmeta', $meta_key_table );
749 - $query = $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key IN ( %s ) AND post_id = %d", implode( "','", $meta_key_table ), $post_id );
750 - $query = stripslashes( $query );
751 - //$query = $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key <> '_thumbnail_id' AND post_id = %d", $post_id);
752 -//20130723ysk end
753 - $dbres = $wpdb->query( $query );
754 - if( $dbres === false ) {
755 - $err_num++;
756 - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete postmeta', 'usces');
757 - $log .= $mes.$yn;
758 - echo $mes.$br.$yn;
759 - $pre_code = $datas[USCES_COL_ITEM_CODE];
760 - continue;
761 - }
762 - // delete Item revisions
763 - $query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $post_id, 'revision');
764 - $dbres = $wpdb->query( $query );
765 - if( $dbres === false ) {
766 - $err_num++;
767 - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete revisions', 'usces');
768 - $log .= $mes.$yn;
769 - echo $mes.$br.$yn;
770 - $pre_code = $datas[USCES_COL_ITEM_CODE];
771 - continue;
772 - }
773 - // delete relationships of category
774 - $query = $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d", $post_id);
775 - $dbres = $wpdb->query( $query );
776 - if( $dbres === false ) {
777 - $err_num++;
778 - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(category)', 'usces');
779 - $log .= $mes.$yn;
780 - echo $mes.$br.$yn;
781 - $pre_code = $datas[USCES_COL_ITEM_CODE];
782 - continue;
783 - }
784 - // delete relationships of tag
785 - $query = "SELECT term_taxonomy_id, COUNT(*) AS ct FROM $wpdb->term_relationships GROUP BY term_taxonomy_id";
786 - $relation_data = $wpdb->get_results( $query, ARRAY_A );
787 - foreach((array)$relation_data as $relation_rows) {
788 - $term_taxonomy_ids['term_taxonomy_id'] = $relation_rows['term_taxonomy_id'];
789 - $term_taxonomy_updatas['count'] = $relation_rows['ct'];
790 - $dbres = $wpdb->update( $wpdb->term_taxonomy, $term_taxonomy_updatas, $term_taxonomy_ids );
791 - if( $dbres === false ) {
792 - $err_num++;
793 - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(tag)', 'usces');
794 - $log .= $mes.$yn;
795 - echo $mes.$br.$yn;
796 - $pre_code = $datas[USCES_COL_ITEM_CODE];
797 - continue;
798 - }
799 - }
800 - }
801 -
802 - /******************/
803 - $metadelete = microtime(true);
804 - $metadeletetime += $metadelete-$insert_post;
805 - /*////////////////////////////////////////*/
806 - // addMeta
807 - /*////////////////////////////////////////*/
808 - //add postmeta
809 - $itemDeliveryMethod = explode(';', $datas[USCES_COL_ITEM_DELIVERYMETHOD]);
810 - $valstr .= '(' . $post_id . ", '_itemCode','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_CODE], 'UTF-8', 'SJIS'))) . "'),";
811 - $valstr .= '(' . $post_id . ", '_itemName','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_NAME], 'UTF-8', 'SJIS'))) . "'),";
812 - $valstr .= '(' . $post_id . ", '_itemRestriction','" . $datas[USCES_COL_ITEM_RESTRICTION] . "'),";
813 - $valstr .= '(' . $post_id . ", '_itemPointrate','" . $datas[USCES_COL_ITEM_POINTRATE] . "'),";
814 - $valstr .= '(' . $post_id . ", '_itemGpNum1','" . $datas[USCES_COL_ITEM_GPNUM1] . "'),";
815 - $valstr .= '(' . $post_id . ", '_itemGpDis1','" . $datas[USCES_COL_ITEM_GPDIS1] . "'),";
816 - $valstr .= '(' . $post_id . ", '_itemGpNum2','" . $datas[USCES_COL_ITEM_GPNUM2] . "'),";
817 - $valstr .= '(' . $post_id . ", '_itemGpDis2','" . $datas[USCES_COL_ITEM_GPDIS2] . "'),";
818 - $valstr .= '(' . $post_id . ", '_itemGpNum3','" . $datas[USCES_COL_ITEM_GPNUM3] . "'),";
819 - $valstr .= '(' . $post_id . ", '_itemGpDis3','" . $datas[USCES_COL_ITEM_GPDIS3] . "'),";
820 - $valstr .= '(' . $post_id . ", '_itemShipping','" . $datas[USCES_COL_ITEM_SHIPPING] . "'),";
821 - $valstr .= '(' . $post_id . ", '_itemDeliveryMethod','" . mysql_real_escape_string(serialize($itemDeliveryMethod)) . "'),";
822 - $valstr .= '(' . $post_id . ", '_itemShippingCharge','" . $datas[USCES_COL_ITEM_SHIPPINGCHARGE] . "'),";
823 - $valstr .= '(' . $post_id . ", '_itemIndividualSCharge','" . $datas[USCES_COL_ITEM_INDIVIDUALSCHARGE] . "'),";
824 -
825 - //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($meta_key)."', '" . mysql_real_escape_string(serialize($sku)) . "'),";
826 -
827 -// print_r($valstr);
828 - $valstr = rtrim($valstr, ',');
829 - $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr");
830 -// $dbres = mysql_query($query) or die(mysql_error());
831 -
832 - //add term_relationships, edit term_taxonomy
833 - //category
834 - $categories = explode(';', $datas[USCES_COL_CATEGORY]);
835 - foreach((array)$categories as $category){
836 - $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
837 - WHERE term_id = %d", $category);
838 - $term_taxonomy_id = $wpdb->get_var( $query );
839 - if($term_taxonomy_id == NULL) continue;
840 -
841 - $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships
842 - (object_id, term_taxonomy_id, term_order) VALUES
843 - (%d, %d, 0)",
844 - $post_id, $term_taxonomy_id
845 - );
846 - $dbres = $wpdb->query($query);
847 - if( !$dbres ) continue;
848 -
849 - $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships
850 - WHERE term_taxonomy_id = %d", $term_taxonomy_id);
851 - $tct = $wpdb->get_var( $query );
852 -
853 - $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d
854 - WHERE term_taxonomy_id = %d",
855 - $tct, $term_taxonomy_id
856 - );
857 - $dbres = $wpdb->query($query);
858 - }
859 - //tag
860 - $tags = explode(';', trim(mb_convert_encoding($datas[USCES_COL_POST_TAG], 'UTF-8', 'SJIS')));
861 -// wp_set_object_terms($post_id, (array)$tags, 'post_tag');
862 - foreach((array)$tags as $tag){
863 - $tag = trim($tag, " \n\t\r\0\x0B,");
864 - if( empty($tag) )
865 - continue;
866 - $query = $wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $tag);
867 - $term_id = $wpdb->get_var( $query );
868 - //new tag
869 - if( empty($term_id) ){
870 - $query = $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES
871 - (%s, %s, 0)", $tag, urlencode($tag));
872 - $dbres = $wpdb->query( $query );
873 - if( !$dbres )
874 - continue;
875 - $term_id = $wpdb->insert_id;
876 - }
877 - $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy
878 - WHERE term_id = %d", $term_id);
879 - $term_taxonomy_id = $wpdb->get_var( $query );
880 - if( !$term_taxonomy_id ){
881 - $query = $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy
882 - (term_id, taxonomy, description, parent, count) VALUES
883 - (%d, %s, %s, 0, 0)", $term_id, 'post_tag', '');
884 - $dbres = $wpdb->query( $query );
885 - if( !$dbres )
886 - continue;
887 - $term_taxonomy_id = $wpdb->insert_id;
888 - }
889 - $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships
890 - (object_id, term_taxonomy_id, term_order) VALUES
891 - (%d, %d, 0)",
892 - $post_id, $term_taxonomy_id
893 - );
894 - $dbres = $wpdb->query($query);
895 - if( !$dbres ) continue;
896 -
897 - $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships
898 - WHERE term_taxonomy_id = %d", $term_taxonomy_id);
899 - $tct = $wpdb->get_var( $query );
900 -
901 - $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d
902 - WHERE term_taxonomy_id = %d",
903 - $tct, $term_taxonomy_id
904 - );
905 - $dbres = $wpdb->query($query);
906 - }
907 -
908 - /*////////////////////////////////////////*/
909 - // Add Custom Field
910 - /*////////////////////////////////////////*/
911 - $cfrows = explode(';', trim(mb_convert_encoding($datas[USCES_COL_CUSTOM_FIELD], 'UTF-8', 'SJIS')));
912 - if( !(1 === count($cfrows) && '' == reset($cfrows)) ){
913 - $valstr = '';
914 - foreach( $cfrows as $row ){
915 - list($meta_key, $meta_value) = explode( '=', $row, 2 );
916 - if( !WCUtils::is_blank($meta_key) )
917 - $valstr .= $wpdb->prepare("( %d, %s, %s),", $post_id, trim($meta_key), trim($meta_value));
918 - }
919 - if( !WCUtils::is_blank($valstr) ){
920 - $valstr = rtrim($valstr, ',');
921 - $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr");
922 - }
923 - }
924 -
925 - /******************/
926 - $add_postmeta = microtime(true);
927 - $addmetatime += $add_postmeta-$metadelete;
928 - /*////////////////////////////////////////*/
929 - // addOption
930 - /*////////////////////////////////////////*/
931 - // Add Item Option
932 - for($i=0; $i<$opnum; $i++){
933 - $opflg = true;
934 - $optvalue = array();
935 - for($o=1; $o<=4; $o++){
936 - $key = ($min_field_num-1)+$o+($i*4);
937 -// if( !isset($datas[$key]) ){
938 -// break 2;
939 -// }
940 - if( $o === 1 && $datas[$key] == '' ){
941 - $opflg = false;
942 - break 1;
943 - }
944 - switch($o){
945 - case 1:
946 - $optvalue['name'] = trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS'));
947 - break;
948 - case 2:
949 - $optvalue['means'] = (int)$datas[$key];
950 - break;
951 - case 3:
952 - $optvalue['essential'] = (int)$datas[$key];
953 - break;
954 - case 4:
955 - if( !empty($datas[$key]) ) {
956 - $optvalue['value'] = str_replace(';', "\n", trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS')));
957 - }else{
958 - $optvalue['value'] = "";
959 - }
960 - break;
961 - }
962 - }
963 -
964 - if( $opflg && !empty($optvalue) ){
965 - $optvalue['sort'] = $i;
966 - $resopt = usces_add_opt($post_id, $optvalue, false);
967 - }
968 - //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($ometa_key)."', '" . mysql_real_escape_string(maybe_serialize($opt)) . "'),";
969 - }
970 -
971 - }else{
972 - $sku_index++;
973 - }
974 -
975 - /******************/
976 - $optionmeta = microtime(true);
977 - $AddOptiontime += $optionmeta-$add_postmeta;
978 - /*////////////////////////////////////////*/
979 - // addSku
980 - /*////////////////////////////////////////*/
981 - // Add Item SKU
982 - $sku_code = trim(mb_convert_encoding($datas[USCES_COL_SKU_CODE], 'UTF-8', 'SJIS'));
983 - $skuvalue['code'] = $sku_code;
984 - $skuvalue['name'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_NAME], 'UTF-8', 'SJIS'));
985 - $skuvalue['cprice'] = $datas[USCES_COL_SKU_CPRICE];
986 - $skuvalue['price'] = $datas[USCES_COL_SKU_PRICE];
987 - $skuvalue['unit'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_UNIT], 'UTF-8', 'SJIS'));
988 - $skuvalue['stocknum'] = $datas[USCES_COL_SKU_ZAIKONUM];
989 - $skuvalue['stock'] = $datas[USCES_COL_SKU_ZAIKO];
990 - $skuvalue['gp'] = $datas[USCES_COL_SKU_GPTEKIYO];
991 - $skuvalue['sort'] = $sku_index;
992 - usces_add_sku( $post_id, $skuvalue, false );
993 -
994 -// usces_log('skuvalue : '.$post_id.print_r($skuvalue,true), 'acting_transaction.log');
995 - $comp_num++;
996 - $pre_code = $datas[USCES_COL_ITEM_CODE];
997 - clean_post_cache($post_id);
998 - wp_cache_delete($post_id, 'posts');
999 - wp_cache_delete($post_id, 'post_meta');
1000 - clean_object_term_cache($post_id, 'post');
1001 - /******************/
1002 - $addsku = microtime(true);
1003 - $AddSKUtime += $addsku-$optionmeta;
1004 - $onelinetime += $addsku-$linestart;
1005 -
1006 - if( 0 === (($rows_num+1) % 10) ){
1007 - $av = $onelinetime/$rows_num;
1008 - $nt = round($av*($total_num-$rows_num));
1009 - if( 60 < $nt ){
1010 - $mtime = ceil($nt/60) . __('min', 'usces') . ($nt%60) . __('sec', 'usces') . ' ';
1011 - }else{
1012 - $mtime = $nt . __('sec', 'usces') . ' ';
1013 - }
1014 - echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . __('Remaining time:', 'usces').$mtime.'");</script>'.$yn;
1015 - echo '<script type="text/javascript">setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn;
1016 - ob_flush();
1017 - flush();
1018 - }
1019 - }
1020 -
1021 - flock($fpi, LOCK_EX);
1022 - fputs($fpi, mb_convert_encoding($log, 'SJIS', 'UTF-8'));
1023 - flock($fpi, LOCK_UN);
1024 - fclose($fpi);
1025 -
1026 - $res['status'] = 'success';
1027 - $res['message'] = __(sprintf('%2$s of %1$s lines registration completion, error on %3$s lines.',$total_num,$comp_num,$err_num), 'usces');
1028 - /******************/
1029 - $finish = microtime(true);
1030 - usces_log('ready : '.round($readytime-$start, 4), 'acting_transaction.log');
1031 - usces_log('lineReady : '.round($linereadytime/$total_num, 4), 'acting_transaction.log');
1032 - usces_log('dataCheck : '.round($checktime/$total_num, 4), 'acting_transaction.log');
1033 - usces_log('insPost : '.round($insertposttime/$total_num, 4), 'acting_transaction.log');
1034 - usces_log('delMeta : '.round($metadeletetime/$total_num, 4), 'acting_transaction.log');
1035 - usces_log('addMeta : '.round($addmetatime/$total_num, 4), 'acting_transaction.log');
1036 - usces_log('addOption : '.round($AddOptiontime/$total_num, 4), 'acting_transaction.log');
1037 - usces_log('addSku : '.round($AddSKUtime/$total_num, 4), 'acting_transaction.log');
1038 - usces_log('oneline : '.round($onelinetime/$total_num, 4), 'acting_transaction.log');
1039 - usces_log('finish : '.round($finish-$start, 4), 'acting_transaction.log');
1040 - usces_log('totalLines: '.$total_num, 'acting_transaction.log');
1041 - usces_log('--------------------------------------------------------------', 'acting_transaction.log');
1042 -
1043 - echo '<script type="text/javascript">changeMsg("', __(sprintf('Successful %1$s lines, Failed %2$s lines.',$comp_num,$err_num), 'usces'), ' ");setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn;
1044 - if( $log ){
1045 - //echo '<div class="error_log">' . $log . '</div>'.$yn;
1046 - }else{
1047 - echo __('Finished', 'usces').$yn;
1048 - }
1049 - echo '</div>'.$yn;
1050 - unlink($path.$file_name);
1051 - return $res;
1052 -}
1053 -endif;
1054 -
1055 -// item list download
1056 -if( !function_exists('usces_download_item_list') ):
1057 -function usces_download_item_list() {
1058 - require_once( USCES_PLUGIN_DIR . "/classes/itemList.class.php" );
1059 - global $wpdb, $usces;
1060 -
1061 - $ext = 'csv';
1062 - if($ext == 'csv') {//CSV
1063 - $table_h = "";
1064 - $table_f = "";
1065 - $tr_h = "";
1066 - $tr_f = "";
1067 - $th_h1 = '"';
1068 - $th_h = ',"';
1069 - $th_f = '"';
1070 - $td_h1 = '"';
1071 - $td_h = ',"';
1072 - $td_f = '"';
1073 - $sp = ";";
1074 - $eq = "=";
1075 - $lf = "\n";
1076 - } else {
1077 - exit();
1078 - }
1079 -
1080 - //==========================================================================
1081 - $usces_opt_item = get_option('usces_opt_item');
1082 - if(!is_array($usces_opt_item)){
1083 - $usces_opt_item = array();
1084 - }
1085 - $usces_opt_item['chk_header'] = (isset($_REQUEST['chk_header'])) ? 1 : 0;
1086 - $usces_opt_item['ftype_item'] = $ext;
1087 - update_option('usces_opt_item', $usces_opt_item);
1088 - //==========================================================================
1089 -
1090 - $tableName = $wpdb->posts;
1091 - if( USCES_MYSQL_VERSION >= 5 ) {
1092 - $arr_column = array(
1093 - __('item code', 'usces') => 'item_code',
1094 - __('item name', 'usces') => 'item_name',
1095 - __('SKU code', 'usces') => 'sku_key',
1096 - __('selling price', 'usces') => 'price',
1097 - __('stock', 'usces') => 'zaiko_num',
1098 - __('stock status', 'usces') => 'zaiko',
1099 - __('Categories', 'usces') => 'category',
1100 - __('display status', 'usces') => 'display_status');
1101 - } else {
1102 - $arr_column = array(
1103 - __('item code', 'usces') => 'item_code',
1104 - __('page title', 'usces') => 'post_title',
1105 - __('SKU code', 'usces') => 'sku_key',
1106 - __('selling price', 'usces') => 'price',
1107 - __('stock', 'usces') => 'zaiko_num',
1108 - __('stock status', 'usces') => 'zaiko',
1109 - __('Categories', 'usces') => 'category',
1110 - __('display status', 'usces') => 'display_status');
1111 - }
1112 -
1113 - $_REQUEST['searchIn'] = "searchIn";
1114 - $DT = new dataList($tableName, $arr_column);
1115 - $DT->pageLimit = 'off';
1116 - $DT->exportMode = true;
1117 - $res = $DT->MakeTable();
1118 - $rows = $DT->rows;
1119 - //==========================================================================
1120 - $line = $table_h;
1121 - if($usces_opt_item['chk_header'] == 1) {
1122 - $line .= $tr_h;
1123 - $line .= $th_h1.'Post ID'.$th_f;
1124 - $line .= $th_h.__('Post Author', 'usces').$th_f;
1125 - $line .= $th_h.__('explanation', 'usces').$th_f;
1126 - $line .= $th_h.__('Title', 'usces').$th_f;
1127 - $line .= $th_h.__('excerpt', 'usces').$th_f;
1128 - $line .= $th_h.__('display status', 'usces').$th_f;
1129 - $line .= $th_h.__('Comment Status', 'usces').$th_f;
1130 - $line .= $th_h.__('Post PAssword', 'usces').$th_f;
1131 - $line .= $th_h.__('Post Name', 'usces').$th_f;
1132 - $line .= $th_h.__('Publish on:').$th_f;
1133 -
1134 - $line .= $th_h.__('item code', 'usces').$th_f;
1135 - $line .= $th_h.__('item name', 'usces').$th_f;
1136 - $line .= $th_h.__('Limited amount for purchase', 'usces').$th_f;
1137 - $line .= $th_h.__('Percentage of points', 'usces').$th_f;
1138 - $line .= $th_h.__('Business package discount', 'usces').'1-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'1-'.__('rate', 'usces').$th_f;
1139 - $line .= $th_h.__('Business package discount', 'usces').'2-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'2-'.__('rate', 'usces').$th_f;
1140 - $line .= $th_h.__('Business package discount', 'usces').'3-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'3-'.__('rate', 'usces').$th_f;
1141 - $line .= $th_h.__('estimated shipping date', 'usces').$th_f;
1142 - $line .= $th_h.__('shipping option', 'usces').$th_f;
1143 - $line .= $th_h.__('Shipping', 'usces').$th_f;
1144 - $line .= $th_h.__('Postage individual charging', 'usces').$th_f;
1145 -
1146 - $line .= $th_h.__('Categories', 'usces').$th_f;
1147 - $line .= $th_h.__('tag', 'usces').$th_f;
1148 - $line .= $th_h.__('Custom Field', 'usces').$th_f;
1149 -
1150 - $line .= $th_h.__('SKU code', 'usces').$th_f;
1151 - $line .= $th_h.__('SKU display name ', 'usces').$th_f;
1152 - $line .= $th_h.__('normal price', 'usces').$th_f;
1153 - $line .= $th_h.__('Sale price', 'usces').$th_f;
1154 - $line .= $th_h.__('stock', 'usces').$th_f;
1155 - $line .= $th_h.__('stock status', 'usces').$th_f;
1156 - $line .= $th_h.__('unit', 'usces').$th_f;
1157 - $line .= $th_h.__('Apply business package', 'usces').$th_f;
1158 - $line .= $th_h.__('option name', 'usces').$th_f.$th_h.__('Field type', 'usces').$th_f.$th_h.__('Required', 'usces').$th_f.$th_h.__('selected amount', 'usces').$th_f;
1159 - $line .= $tr_f.$lf;
1160 - }
1161 - //==========================================================================
1162 - mb_http_output('pass');
1163 - set_time_limit(3600);
1164 - header("Content-Type: application/octet-stream");
1165 - header("Content-Disposition: attachment; filename=usces_item_list.".$ext);
1166 - ob_end_flush();
1167 - flush();
1168 -
1169 - foreach((array)$rows as $array) {
1170 - $post_id = $array['ID'];
1171 - $post = get_post($post_id);
1172 -
1173 - //Post Data
1174 - $line_item = $td_h1.$post->ID.$td_f;
1175 - $line_item .= $td_h.$post->post_author.$td_f;
1176 - $line_item .= $td_h.usces_entity_decode($post->post_content, $ext).$td_f;
1177 - $line_item .= $td_h.usces_entity_decode($post->post_title, $ext).$td_f;
1178 - $line_item .= $td_h.usces_entity_decode($post->post_excerpt, $ext).$td_f;
1179 - $line_item .= $td_h.$post->post_status.$td_f;
1180 - $line_item .= $td_h.$post->comment_status.$td_f;
1181 - $line_item .= $td_h.$post->post_password.$td_f;
1182 - $line_item .= $td_h.urldecode($post->post_name).$td_f;
1183 - $line_item .= $td_h.$post->post_date.$td_f;
1184 -
1185 - //Item Meta
1186 - $line_item .= $td_h.$usces->getItemCode($post_id).$td_f;
1187 - $line_item .= $td_h.usces_entity_decode($usces->getItemName($post_id), $ext).$td_f;
1188 - $line_item .= $td_h.$usces->getItemRestriction($post_id).$td_f;
1189 - $line_item .= $td_h.$usces->getItemPointrate($post_id).$td_f;
1190 - $line_item .= $td_h.$usces->getItemGpNum1($post_id).$td_f.$td_h.$usces->getItemGpDis1($post_id).$td_f;
1191 - $line_item .= $td_h.$usces->getItemGpNum2($post_id).$td_f.$td_h.$usces->getItemGpDis2($post_id).$td_f;
1192 - $line_item .= $td_h.$usces->getItemGpNum3($post_id).$td_f.$td_h.$usces->getItemGpDis3($post_id).$td_f;
1193 - $line_item .= $td_h.$usces->getItemShipping($post_id).$td_f;
1194 - $delivery_method = '';
1195 - $itemDeliveryMethod = $usces->getItemDeliveryMethod($post_id);
1196 - foreach((array)$itemDeliveryMethod as $k => $v) {
1197 - $delivery_method .= $v.$sp;
1198 - }
1199 - $delivery_method = rtrim($delivery_method, $sp);
1200 - $line_item .= $td_h.$delivery_method.$td_f;
1201 - $line_item .= $td_h.$usces->getItemShippingCharge($post_id).$td_f;
1202 - $line_item .= $td_h.$usces->getItemIndividualSCharge($post_id).$td_f;
1203 -
1204 - //Categories
1205 - $category = '';
1206 - $cat_ids = wp_get_post_categories($post_id);
1207 - if(!empty($cat_ids)) {
1208 - foreach($cat_ids as $id) {
1209 - $category .= $id.$sp;
1210 - }
1211 - $category = rtrim($category, $sp);
1212 - }
1213 - $line_item .= $td_h.$category.$td_f;
1214 -
1215 - //Tags
1216 - $tag = '';
1217 - $tags_ob = wp_get_object_terms($post_id, 'post_tag');
1218 - foreach($tags_ob as $ob) {
1219 - $tag .= $ob->name.$sp;
1220 - }
1221 - $tag = rtrim($tag, $sp);
1222 - $line_item .= $td_h.$tag.$td_f;
1223 -
1224 - //Custom Fields
1225 - $cfield = '';
1226 - $custom_fields = $usces->get_post_user_custom($post_id);
1227 - if( is_array($custom_fields) && 0 < count($custom_fields) ){
1228 - foreach($custom_fields as $cfkey => $cfvalues ) {
1229 - if( is_array($cfvalues) )
1230 - $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues[0], $ext) . $sp;
1231 - else
1232 - $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues, $ext) . $sp;
1233 - }
1234 - $cfield = rtrim($cfield, $sp);
1235 - }
1236 - $line_item .= $td_h.$cfield.$td_f;
1237 -
1238 - //Item Options
1239 - $line_options = '';
1240 - $option_meta = usces_get_opts( $post_id, 'sort' );
1241 - foreach($option_meta as $option_value) {
1242 - $value = '';
1243 - if(is_array($option_value['value'])) {
1244 - foreach($option_value['value'] as $k => $v) {
1245 - $values = explode("\n", $v);
1246 - foreach($values as $val) {
1247 - $value .= $val.$sp;
1248 - }
1249 - }
1250 - $value = rtrim($value, $sp);
1251 - } else {
1252 - $value = trim($option_value['value']);
1253 - $value = str_replace("\n", ';', $value);
1254 - }
1255 -
1256 - $line_options .= $td_h.usces_entity_decode($option_value['name'], $ext).$td_f;
1257 - $line_options .= $td_h.$option_value['means'].$td_f;
1258 - $line_options .= $td_h.$option_value['essential'].$td_f;
1259 - $line_options .= $td_h.usces_entity_decode($value, $ext).$td_f;
1260 -
1261 - }
1262 -
1263 - //SKUs
1264 - $sku_meta = $usces->get_skus( $post_id, 'sort' );
1265 - foreach($sku_meta as $sku_value) {
1266 -
1267 - $line_sku = $td_h.$sku_value['code'].$td_f;
1268 - $line_sku .= $td_h.usces_entity_decode($sku_value['name'], $ext).$td_f;
1269 - $line_sku .= $td_h.usces_crform($sku_value['cprice'], false, false, 'return', false).$td_f;
1270 - $line_sku .= $td_h.usces_crform($sku_value['price'], false, false, 'return', false).$td_f;
1271 - $line_sku .= $td_h.$sku_value['stocknum'].$td_f;
1272 - $line_sku .= $td_h.$sku_value['stock'].$td_f;
1273 - $line_sku .= $td_h.usces_entity_decode($sku_value['unit'], $ext).$td_f;
1274 - $line_sku .= $td_h.$sku_value['gp'].$td_f;
1275 -
1276 - $line .= $tr_h.$line_item.$line_sku.$line_options.$tr_f.$lf;
1277 - }
1278 - print(mb_convert_encoding($line, "SJIS-win", "UTF-8"));
1279 - if(ob_get_contents ()){
1280 - ob_flush();
1281 - flush();
1282 - }
1283 - $line = '';
1284 - wp_cache_flush();
1285 - }
1286 - $end = $table_f.$lf;
1287 - //==========================================================================
1288 - print(mb_convert_encoding($end, "SJIS-win", "UTF-8"));
1289 - unset($rows, $DT, $line, $line_item, $line_options, $line_sku);
1290 - exit();
1291 -}
1292 -endif;
1293 -
1294 -}
1295 -?>
1 +<?php
2 +/**
3 + * Welcart Product CSV bulk processing.
4 + *
5 + * @package Welcart
6 + */
7 +
8 +defined( 'ABSPATH' ) || exit;
9 +// phpcs:disable WordPress.Security.NonceVerification -- Already verified inside 'wel_item_upload_ajax' function
10 +// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment
11 +// phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL
12 +// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
13 +
14 +/**
15 + * CSV generation process and bulk update by CSV.
16 + *
17 + * @since 2.2.2
18 + */
19 +function usces_define_functions() {
20 +
21 + if ( ! function_exists( 'usces_item_uploadcsv' ) ) :
22 +
23 + /**
24 + * All columns Bulk registration & update by CSV.
25 + *
26 + * @since 2.2.2
27 + */
28 + function usces_item_uploadcsv() {
29 + global $wpdb, $usces, $user_ID;
30 + $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
31 + $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
32 + if ( $check_mode ) {
33 + define( 'USCES_ITEM_UP_INTERBAL', 200 );
34 + } else {
35 + define( 'USCES_ITEM_UP_INTERBAL', 100 );
36 + }
37 +
38 + // Custom capability 'wel_others_products' is registered on the plugins_loaded hook.
39 + // phpcs:ignore WordPress.WP.Capabilities.Unknown
40 + if ( ! current_user_can( 'wel_others_products' ) ) {
41 + $progress = array(
42 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
43 + 'progress' => __( 'The process was not completed', 'usces' ),
44 + 'log' => 'Error : ' . __( 'You do not have permission to do that.', 'usces' ),
45 + 'flag' => 'complete',
46 + );
47 + record_item_up_progress( $progress );
48 + return;
49 + }
50 +
51 + $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
52 + $datas = array();
53 +
54 + // Upload.
55 + if ( isset( $_REQUEST['action'] ) && 'itemcsv' === $_REQUEST['action'] ) {
56 +
57 + $progress = array( 'log' => 'clear' );
58 + record_item_up_progress( $progress );
59 +
60 + $upload_mode = isset( $_REQUEST['upload_mode'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['upload_mode'] ) ) : '';
61 + $mode_name = usces_get_upmode_name( $upload_mode );
62 + $org_filename = isset( $_FILES['usces_upcsv']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['usces_upcsv']['name'] ) ) : '';
63 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
64 + $filechecked = wp_check_filetype_and_ext( $_FILES['usces_upcsv']['tmp_name'], $org_filename, array( 'csv' => 'text/csv' ) );
65 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
66 + $tmp_filename = $filechecked ? $_FILES['usces_upcsv']['tmp_name'] : '';
67 +
68 + list( $fname, $fext ) = explode( '.', $org_filename, 2 );
69 + $new_filename = base64_encode( $fname . '_' . time() . '.' . $fext );
70 +
71 + $file_info = array(
72 + 'filename' => $org_filename,
73 + 'mode' => $mode_name,
74 + 'rowcount' => '',
75 + 'header' => '',
76 + );
77 +
78 + $db_check = usces_item_code_duplication_check();
79 + if ( $db_check ) {
80 + $code = '';
81 + foreach ( $db_check as $d_item ) {
82 + $code .= ' , ' . $d_item['itemCode'];
83 + }
84 + $log = 'Error : ' . __( 'The same product cord is registered.', 'usces' ) . "\n";
85 + $log .= __( 'The following product code is duplicated. Please eliminate duplicates before uploading.', 'usces' ) . "\n";
86 + $log .= ltrim( $code, ' , ' );
87 +
88 + $progress = array(
89 + 'info' => $file_info,
90 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
91 + 'progress' => __( 'The process was not completed', 'usces' ),
92 + 'log' => $log,
93 + 'flag' => 'complete',
94 + );
95 + record_item_up_progress( $progress );
96 + return;
97 + }
98 +
99 + if ( ! is_uploaded_file( $tmp_filename ) ) {
100 + $progress = array(
101 + 'info' => $file_info,
102 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
103 + 'progress' => __( 'The process was not completed', 'usces' ),
104 + 'log' => 'Error : ' . __( 'The file was not uploaded.', 'usces' ),
105 + 'flag' => 'complete',
106 + );
107 + record_item_up_progress( $progress );
108 + return;
109 + }
110 +
111 + /* check ext */
112 + if ( 'csv' !== $fext ) {
113 + $progress = array(
114 + 'info' => $file_info,
115 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
116 + 'progress' => __( 'The process was not completed', 'usces' ),
117 + 'log' => 'Error : ' . __( 'The file is not supported.', 'usces' ) . ' ( ' . $org_filename . ' )',
118 + 'flag' => 'complete',
119 + );
120 + record_item_up_progress( $progress );
121 + $file_path = $upload_folder . $new_filename;
122 + $real_path = realpath( $file_path );
123 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
124 + unlink( $file_path );
125 + }
126 + return;
127 + }
128 +
129 + if ( ! move_uploaded_file( $tmp_filename, $upload_folder . $new_filename ) ) {
130 + $progress = array(
131 + 'info' => $file_info,
132 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
133 + 'progress' => __( 'The process was not completed', 'usces' ),
134 + 'log' => 'Error : ' . __( 'The file was not stored.', 'usces' ),
135 + 'flag' => 'complete',
136 + );
137 + record_item_up_progress( $progress );
138 + $file_path = $upload_folder . $new_filename;
139 + $real_path = realpath( $file_path );
140 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
141 + unlink( $file_path );
142 + }
143 + return;
144 + }
145 +
146 + $progress = array(
147 + 'info' => $file_info,
148 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
149 + 'progress' => __( 'File upload is complete', 'usces' ),
150 + );
151 + record_item_up_progress( $progress );
152 +
153 + return $new_filename;
154 + }
155 +
156 + // Registration.
157 + if ( isset( $_REQUEST['regfile'] ) && ! empty( $_REQUEST['regfile'] ) && isset( $_REQUEST['action'] ) && 'upload_register' === $_REQUEST['action'] ) {
158 +
159 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
160 +
161 + $upload_mode = isset( $_REQUEST['mode'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['mode'] ) ) : '';
162 + $mode_name = usces_get_upmode_name( $upload_mode );
163 + $file_name = sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) );
164 + $file_name = wel_esc_upload_file_name( $file_name );
165 + $decode_filename = base64_decode( $file_name );
166 + $decode_filename = wel_esc_upload_file_name( $decode_filename );
167 +
168 + list( $dfname, $dfext ) = explode( '.', $decode_filename, 2 );
169 +
170 + $lpos = strrpos( $dfname, '_' );
171 + if ( 0 < $lpos ) {
172 + $org_filename = substr( $dfname, 0, $lpos ) . '.' . $dfext;
173 + } else {
174 + $org_filename = $decode_filename;
175 + }
176 +
177 + $file_info = array(
178 + 'filename' => $org_filename,
179 + 'mode' => $mode_name,
180 + 'rowcount' => '',
181 + 'header' => '',
182 + );
183 + if ( 'csv' !== $dfext ) {
184 + $progress = array(
185 + 'info' => $file_info,
186 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
187 + 'progress' => __( 'The process was not completed', 'usces' ),
188 + 'flag' => 'complete',
189 + 'log' => 'Error : ' . __( 'The file is not supported.', 'usces' ) . ' ( ' . $file_name . ' )',
190 + );
191 + record_item_up_progress( $progress );
192 + $file_path = $upload_folder . $file_name;
193 + $real_path = realpath( $file_path );
194 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
195 + unlink( $file_path );
196 + }
197 + die( wp_json_encode( $progress ) );
198 + }
199 + $progress = array(
200 + 'info' => $file_info,
201 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
202 + 'progress' => '',
203 + );
204 + record_item_up_progress( $progress );
205 +
206 + if ( ! file_exists( $upload_folder . $file_name ) ) {
207 + $progress = array(
208 + 'info' => $file_info,
209 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
210 + 'progress' => __( 'The process was not completed', 'usces' ),
211 + 'log' => 'Error : ' . __( 'CSV file does not exist.', 'usces' ),
212 + 'flag' => 'complete',
213 + );
214 + record_item_up_progress( $progress );
215 + die( wp_json_encode( $progress ) );
216 + }
217 + } else {
218 +
219 + $progress = array(
220 + 'info' => $file_info,
221 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
222 + 'progress' => __( 'The process was not completed', 'usces' ),
223 + 'log' => 'Error : ' . __( 'Bad request.', 'usces' ),
224 + 'flag' => 'complete',
225 + );
226 + record_item_up_progress( $progress );
227 + $file_path = $upload_folder . $file_name;
228 + $real_path = realpath( $file_path );
229 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
230 + unlink( $file_path );
231 + }
232 + die( wp_json_encode( $progress ) );
233 + }
234 +
235 + /* read data */
236 + $file_path = $upload_folder . $file_name;
237 + $fpo = fopen( $file_path, 'r' );
238 + if ( false === $fpo ) {
239 + $progress = array(
240 + 'info' => $file_info,
241 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
242 + 'progress' => __( 'The process was not completed', 'usces' ),
243 + 'log' => 'Error : ' . __( 'A file does not open.', 'usces' ),
244 + 'flag' => 'complete',
245 + );
246 + record_item_up_progress( $progress );
247 + $real_path = realpath( $file_path );
248 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
249 + unlink( $file_path );
250 + }
251 + die( wp_json_encode( $progress ) );
252 + }
253 +
254 + // Correct line breaks in the middle of a line.
255 + $orglines = array();
256 + $buf = '';
257 + while ( ! feof( $fpo ) ) {
258 + $temp = fgets( $fpo, 65535 );
259 + if ( 0 === strlen( $temp ) ) {
260 + continue;
261 + }
262 +
263 + $num = substr_count( $temp, '"' );
264 + if ( 0 === $num % 2 && '' === $buf ) {
265 + $orglines[] = $temp;
266 + } elseif ( 1 === $num % 2 && '' === $buf ) {
267 + $buf .= $temp;
268 + } elseif ( 0 === $num % 2 && '' !== $buf ) {
269 + $buf .= $temp;
270 + } elseif ( 1 === $num % 2 && '' !== $buf ) {
271 + $buf .= $temp;
272 + $orglines[] = $buf;
273 + $buf = '';
274 + }
275 + }
276 + fclose( $fpo );
277 +
278 + // Data generation and checking.
279 + $total_num = 0;
280 + $lines = array();
281 + foreach ( $orglines as $index => $line ) {
282 + $line = trim( $line );
283 + if ( empty( $line ) ) {
284 + continue;
285 + }
286 + $lines[] = $line;
287 + }
288 + $total_num = count( $lines );
289 + if ( $csv_encode_type_sjis ) {
290 + $header = trim( mb_convert_encoding( $lines[0], 'UTF-8', 'SJIS' ) );
291 + } else {
292 + $header = trim( $lines[0] );
293 + }
294 +
295 + $file_info = array(
296 + 'filename' => $org_filename,
297 + 'mode' => $mode_name,
298 + 'rowcount' => __( 'Number of lines', 'usces' ) . ' ' . $total_num,
299 + 'header' => $header,
300 + );
301 +
302 + // Ready.
303 + $wpdb->query( 'SET SQL_BIG_SELECTS=1' );
304 + set_time_limit( 3600 );
305 + $category_format_slug = ( isset( $usces->options['system']['csv_category_format'] ) && 1 === (int) $usces->options['system']['csv_category_format'] ) ? true : false;
306 +
307 + // Processing branch for each mode.
308 + $results = apply_filters( 'usces_filter_item_uploadcsv_mode', array(), $lines, $file_info );
309 + if ( ! empty( $results ) ) {
310 +
311 + extract( $results );
312 + } elseif ( 'stock' === $upload_mode ) {
313 +
314 + $results = usces_item_stock_uploadcsv( $lines, $file_info );
315 + if ( ! empty( $results ) ) {
316 + extract( $results );
317 + }
318 + } elseif ( 'sku' === $upload_mode ) {
319 +
320 + $results = usces_item_sku_uploadcsv( $lines, $file_info );
321 + if ( ! empty( $results ) ) {
322 + extract( $results );
323 + }
324 + } elseif ( 'meta' === $upload_mode ) {
325 +
326 + $results = usces_item_meta_uploadcsv( $lines, $file_info );
327 + if ( ! empty( $results ) ) {
328 + extract( $results );
329 + }
330 + } else {
331 +
332 + // All columns.
333 +
334 + define( 'USCES_COL_POST_ID', 0 );
335 + define( 'USCES_COL_POST_AUTHOR', 1 );
336 + define( 'USCES_COL_POST_CONTENT', 2 );
337 + define( 'USCES_COL_POST_TITLE', 3 );
338 + define( 'USCES_COL_POST_EXCERPT', 4 );
339 + define( 'USCES_COL_POST_STATUS', 5 );
340 + define( 'USCES_COL_POST_COMMENT_STATUS', 6 );
341 + define( 'USCES_COL_POST_PASSWORD', 7 );
342 + define( 'USCES_COL_POST_NAME', 8 );
343 + define( 'USCES_COL_POST_MODIFIED', 9 );
344 +
345 + define( 'USCES_COL_ITEM_CODE', 10 );
346 + define( 'USCES_COL_ITEM_NAME', 11 );
347 + define( 'USCES_COL_ITEM_RESTRICTION', 12 );
348 + define( 'USCES_COL_ITEM_POINTRATE', 13 );
349 + define( 'USCES_COL_ITEM_GPNUM1', 14 );
350 + define( 'USCES_COL_ITEM_GPDIS1', 15 );
351 + define( 'USCES_COL_ITEM_GPNUM2', 16 );
352 + define( 'USCES_COL_ITEM_GPDIS2', 17 );
353 + define( 'USCES_COL_ITEM_GPNUM3', 18 );
354 + define( 'USCES_COL_ITEM_GPDIS3', 19 );
355 + define( 'USCES_COL_ITEM_ORDER_ACCEPTABLE', 20 );
356 + define( 'USCES_COL_ITEM_SHIPPING', 21 );
357 + define( 'USCES_COL_ITEM_DELIVERYMETHOD', 22 );
358 + define( 'USCES_COL_ITEM_SHIPPINGCHARGE', 23 );
359 + define( 'USCES_COL_ITEM_INDIVIDUALSCHARGE', 24 );
360 +
361 + define( 'USCES_COL_CATEGORY', 25 );
362 + define( 'USCES_COL_POST_TAG', 26 );
363 + define( 'USCES_COL_CUSTOM_FIELD', 27 );
364 +
365 + $add_field_num = apply_filters( 'usces_filter_uploadcsv_item_field_num', 0 );
366 + $add_field_num = apply_filters( 'usces_filter_uploadcsv_add_item_field_num', $add_field_num );
367 +
368 + define( 'USCES_COL_SKU_CODE', 28 + $add_field_num );
369 + define( 'USCES_COL_SKU_NAME', 29 + $add_field_num );
370 + define( 'USCES_COL_SKU_CPRICE', 30 + $add_field_num );
371 + define( 'USCES_COL_SKU_PRICE', 31 + $add_field_num );
372 + define( 'USCES_COL_SKU_ZAIKONUM', 32 + $add_field_num );
373 + define( 'USCES_COL_SKU_ZAIKO', 33 + $add_field_num );
374 + define( 'USCES_COL_SKU_UNIT', 34 + $add_field_num );
375 + define( 'USCES_COL_SKU_GPTEKIYO', 35 + $add_field_num );
376 + define( 'USCES_COL_SKU_APPLICABLE_TAXRATE', 36 + $add_field_num );
377 +
378 + $normal_field_num = 37;
379 +
380 + $column_num = 0;
381 + $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
382 + $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
383 + $line_num = 0;
384 + $min_field_num = apply_filters( 'usces_filter_uploadcsv_min_field_num', $normal_field_num + $add_field_num );
385 + $min_field_num = apply_filters( 'usces_filter_uploadcsv_add_min_field_num', $min_field_num );
386 + $error = false;
387 + $pre_code = '';
388 + $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
389 + $work_number = 0;
390 + $sku_index = 0;
391 + $date_pattern = '/(\d{4})-(\d{2}|\d)-(\d{2}|\d) (\d{2}):(\d{2}|\d):(\d{2}|\d)/';
392 + $item_table = usces_get_tablename( 'usces_item' );
393 +
394 + $yn = "\n";
395 + $cf_sp = ';;';// Custom field separator.
396 +
397 + // Registration loop.
398 + foreach ( $lines as $rows_num => $line ) {
399 +
400 + $logtemp = '';
401 + $line = trim( $line );
402 + if ( empty( $line ) ) {
403 + continue;
404 + }
405 +
406 + // Divide the line and store it in $datas.
407 + $datas = usces_make_line_data( $line );
408 +
409 + if ( $column_num < count( $datas ) ) {
410 + $column_num = count( $datas );
411 + }
412 + $file_info = array(
413 + 'filename' => $org_filename,
414 + 'mode' => $mode_name,
415 + 'rowcount' => __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num,
416 + 'header' => $header,
417 + );
418 + if ( $min_field_num > $column_num || ( 0 === $rows_num && 'Post ID' !== $datas[ USCES_COL_POST_ID ] ) ) {
419 + $progress = array(
420 + 'info' => $file_info,
421 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
422 + 'progress' => __( 'The process was not completed', 'usces' ),
423 + 'log' => 'Error : ' . __( 'This file may not be the item CSV for "All columns".', 'usces' ),
424 + 'flag' => 'complete',
425 + );
426 + record_item_up_progress( $progress );
427 + $file_path = $upload_folder . $file_name;
428 + $real_path = realpath( $file_path );
429 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
430 + unlink( $file_path );
431 + }
432 + die( wp_json_encode( $progress ) );
433 + }
434 +
435 + // Skip the first line.
436 + if ( 'Post ID' === $datas[ USCES_COL_POST_ID ] ) {
437 + continue;
438 + }
439 +
440 + $line_num = $rows_num + 1;
441 + $item_code = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_CODE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_CODE ] );
442 +
443 + // Split processing.
444 + if ( $start_number > $work_number ) {
445 + if ( $pre_code !== $item_code ) {
446 + ++$work_number;
447 + }
448 + $pre_code = $item_code;
449 +
450 + continue;
451 + }
452 + if ( $pre_code !== $item_code ) {
453 + if ( 0 === ( $work_number % USCES_ITEM_UP_INTERBAL ) && $start_number != $work_number ) {
454 + $progress = array(
455 + 'info' => $file_info,
456 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
457 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
458 + 'i' => $line_num,
459 + 'all' => $total_num,
460 + 'flag' => 'continue',
461 + 'work_number' => $work_number,
462 + 'comp_num' => $comp_num,
463 + 'err_num' => $err_num,
464 + );
465 + record_item_up_progress( $progress );
466 + die( wp_json_encode( $progress ) );
467 + }
468 +
469 + ++$work_number;
470 + }
471 +
472 + // Update mode determined.
473 + if ( $pre_code === $item_code && WCUtils::is_blank( $datas[ USCES_COL_POST_ID ] ) ) {
474 + $mode = 'add';
475 +
476 + } else {
477 + $post_id = ( ! WCUtils::is_blank( $datas[ USCES_COL_POST_ID ] ) ) ? (int) $datas[ USCES_COL_POST_ID ] : null;
478 + if ( $post_id ) {
479 + $db_res = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID = %d AND post_mime_type = %s", $post_id, 'item' ) );
480 + if ( ! $db_res ) {
481 + ++$err_num;
482 + $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Post-ID %s is not product data.', 'usces' ), $post_id );
483 + $progress = array(
484 + 'log' => $mes,
485 + );
486 + record_item_up_progress( $progress );
487 + $error = true;
488 + continue;
489 + }
490 + }
491 + if ( $post_id ) {
492 + $mode = 'upd';
493 + } else {
494 + $mode = 'add';
495 + }
496 + }
497 +
498 + // Column check loop.
499 + foreach ( $datas as $key => $data ) {
500 +
501 + $data = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $data, 'UTF-8', 'SJIS' ) ) : trim( $data );
502 +
503 + switch ( $key ) {
504 + case USCES_COL_ITEM_CODE:
505 + if ( 0 === strlen( $data ) ) {
506 + $mes = 'No.' . $line_num . "\t" . __( 'An item cord is non-input.', 'usces' );
507 + $logtemp .= $mes . $yn;
508 + } else {
509 + $db_res1 = $wpdb->get_results(
510 + $wpdb->prepare(
511 + "SELECT `itemCode`, `post_id` FROM {$item_table}
512 + LEFT JOIN {$wpdb->posts} ON `ID` = `post_id`
513 + WHERE `itemCode` = %s AND `post_type` = 'post'
514 + AND `post_status` IN ('pending', 'publish', 'draft', 'private', 'future')",
515 + $data
516 + ),
517 + ARRAY_A
518 + );
519 +
520 + if ( 'upd' === $mode ) {
521 +
522 + if ( $db_res1 && is_array( $db_res1 ) && 1 < count( $db_res1 ) ) {
523 + $mes = 'No.' . $line_num . "\t" . __( 'This Item-Code has been duplicated.', 'usces' );
524 + $logtemp .= $mes . $yn;
525 + $mes = '';
526 + foreach ( $db_res1 as $res_val ) {
527 + $mes .= 'itemCode=' . $res_val['itemCode'] . ', post_id=' . $res_val['post_id'];
528 + }
529 + $logtemp .= $mes . $yn;
530 + }
531 + $query = $wpdb->prepare(
532 + "SELECT `itemCode`, `post_id` FROM {$item_table}
533 + LEFT JOIN {$wpdb->posts} ON `ID` = `post_id`
534 + WHERE `post_id` <> %d AND `itemCode` = %s AND `post_type` = 'post'
535 + AND `post_status` IN ('pending', 'publish', 'draft', 'private', 'future')",
536 + $post_id,
537 + $data
538 + );
539 + $db_res2 = $wpdb->get_results( $query, ARRAY_A );
540 + if ( $db_res2 && is_array( $db_res2 ) && 0 < count( $db_res2 ) ) {
541 + $mes = 'No.' . $line_num . "\t" . __( 'This Item-Code has already been used.', 'usces' );
542 + $logtemp .= $mes . $yn;
543 + $mes = '';
544 + foreach ( $db_res2 as $res_val ) {
545 + $mes .= 'itemCode=' . $res_val['itemCode'] . ', post_id=' . $res_val['post_id'];
546 + }
547 + $logtemp .= $mes . $yn;
548 + }
549 + } elseif ( 'add' === $mode ) {
550 +
551 + if ( $data != $pre_code ) {
552 + if ( $db_res1 && is_array( $db_res1 ) && 0 < count( $db_res1 ) ) {
553 + $mes = 'No.' . $line_num . "\t" . __( 'This Item-Code has already been used.', 'usces' );
554 + $logtemp .= $mes . $yn;
555 + $mes = '';
556 + foreach ( $db_res1 as $res_val ) {
557 + $mes .= 'itemCode=' . $res_val['itemCode'] . ', post_id=' . $res_val['post_id'];
558 + }
559 + $logtemp .= $mes . $yn;
560 + }
561 + }
562 + }
563 + }
564 + break;
565 + case USCES_COL_ITEM_NAME:
566 + if ( 0 === strlen( $data ) ) {
567 + $mes = 'No.' . $line_num . "\t" . __( 'An item name is non-input.', 'usces' );
568 + $logtemp .= $mes . $yn;
569 + }
570 + break;
571 + case USCES_COL_ITEM_RESTRICTION:
572 + if ( ! preg_match( '/^[0-9;]+$/', $data ) && 0 !== strlen( $data ) ) {
573 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the purchase limit number is abnormal.', 'usces' );
574 + $logtemp .= $mes . $yn;
575 + }
576 + break;
577 + case USCES_COL_ITEM_POINTRATE:
578 + if ( ! preg_match( '/^[0-9;]+$/', $data ) ) {
579 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the point rate is abnormal.', 'usces' );
580 + $logtemp .= $mes . $yn;
581 + }
582 + break;
583 + case USCES_COL_ITEM_GPNUM1:
584 + if ( ! preg_match( '/^[0-9;]+$/', $data ) ) {
585 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '1-' . __( 'umerical value is abnormality.', 'usces' );
586 + $logtemp .= $mes . $yn;
587 + }
588 + break;
589 + case USCES_COL_ITEM_GPDIS1:
590 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || ( 0 < $datas[ USCES_COL_ITEM_GPNUM1 ] && 1 > $data ) ) {
591 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '1-' . __( 'rate is abnormal.', 'usces' );
592 + $logtemp .= $mes . $yn;
593 + }
594 + break;
595 + case USCES_COL_ITEM_GPNUM2:
596 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || ( $datas[ USCES_COL_ITEM_GPNUM1 ] >= $data && 0 != $data ) ) {
597 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '2-' . __( 'umerical value is abnormality.', 'usces' );
598 + $logtemp .= $mes . $yn;
599 + }
600 + break;
601 + case USCES_COL_ITEM_GPDIS2:
602 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || ( 0 < $datas[ USCES_COL_ITEM_GPNUM2 ] && 1 > $data ) ) {
603 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '2-' . __( 'rate is abnormal.', 'usces' );
604 + $logtemp .= $mes . $yn;
605 + }
606 + break;
607 + case USCES_COL_ITEM_GPNUM3:
608 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || ( $datas[ USCES_COL_ITEM_GPNUM2 ] >= $data && 0 != $data ) ) {
609 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '3-' . __( 'umerical value is abnormality.', 'usces' );
610 + $logtemp .= $mes . $yn;
611 + }
612 + break;
613 + case USCES_COL_ITEM_GPDIS3:
614 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || ( 0 < $datas[ USCES_COL_ITEM_GPNUM3 ] && 1 > $data ) ) {
615 + $mes = 'No.' . $line_num . "\t" . __( 'Business package discount', 'usces' ) . '3-' . __( 'rate is abnormal.', 'usces' );
616 + $logtemp .= $mes . $yn;
617 + }
618 + break;
619 + case USCES_COL_ITEM_ORDER_ACCEPTABLE:
620 + if ( ! preg_match( '/^[0-9]+$/', $data ) ) {
621 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Sold out limit is abnormal.', 'usces' );
622 + $logtemp .= $mes . $yn;
623 + }
624 + break;
625 + case USCES_COL_ITEM_SHIPPING:
626 + if ( ! preg_match( '/^[0-9]+$/', $data ) || 9 < $data ) {
627 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the shipment day is abnormal.', 'usces' );
628 + $logtemp .= $mes . $yn;
629 + }
630 + break;
631 +
632 + case USCES_COL_ITEM_DELIVERYMETHOD:
633 + if ( 0 === strlen( $data ) || ! preg_match( '/^[0-9;]+$/', $data ) ) {
634 + $mes = 'No.' . $line_num . "\t" . __( 'Invalid value of Delivery method.', 'usces' );
635 + $logtemp .= $mes . $yn;
636 + }
637 + break;
638 + case USCES_COL_ITEM_SHIPPINGCHARGE:
639 + if ( 0 === strlen( $data ) || ! preg_match( '/^[0-9;]+$/', $data ) ) {
640 + $mes = 'No.' . $line_num . "\t" . __( 'Invalid type of shipping charge.', 'usces' );
641 + $logtemp .= $mes . $yn;
642 + }
643 + break;
644 + case USCES_COL_ITEM_INDIVIDUALSCHARGE:
645 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
646 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the postage individual charging is abnormal.', 'usces' );
647 + $logtemp .= $mes . $yn;
648 + }
649 + break;
650 + case USCES_COL_POST_ID:
651 + if ( ! preg_match( '/^[0-9;]+$/', $data ) && 0 !== strlen( $data ) ) {
652 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Post-ID is abnormal.', 'usces' );
653 + $logtemp .= $mes . $yn;
654 + }
655 + break;
656 + case USCES_COL_POST_AUTHOR:
657 + case USCES_COL_POST_COMMENT_STATUS:
658 + case USCES_COL_POST_PASSWORD:
659 + case USCES_COL_POST_NAME:
660 + case USCES_COL_POST_TITLE:
661 + case USCES_COL_POST_CONTENT:
662 + case USCES_COL_POST_EXCERPT:
663 + break;
664 + case USCES_COL_POST_STATUS:
665 + $array17 = array( 'publish', 'future', 'draft', 'pending', 'private' );
666 + if ( ! in_array( $data, $array17, true ) || WCUtils::is_blank( $data ) ) {
667 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the display status is abnormal.', 'usces' );
668 + $logtemp .= $mes . $yn;
669 + }
670 + break;
671 + case USCES_COL_POST_MODIFIED:
672 + if ( 'future' === $datas[ USCES_COL_POST_STATUS ] && ( WCUtils::is_blank( $data ) || '0000-00-00 00:00:00' === $data ) ) {
673 + if ( preg_match( $date_pattern, $data, $match ) ) {
674 + if ( checkdate( $match[2], $match[3], $match[1] )
675 + && ( 0 < $match[4] && 24 > $match[4] )
676 + && ( 0 < $match[5] && 60 > $match[5] )
677 + && ( 0 < $match[6] && 60 > $match[6] )
678 + ) {
679 + $mes = '';
680 + } else {
681 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the schedule is abnormal.', 'usces' );
682 + $logtemp .= $mes . $yn;
683 + }
684 + } else {
685 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the schedule is abnormal.', 'usces' );
686 + $logtemp .= $mes . $yn;
687 + }
688 + } elseif ( ! WCUtils::is_blank( $data ) && '0000-00-00 00:00:00' !== $data ) {
689 + if ( preg_match( '/^[0-9;]+$/', substr( $data, 0, 4 ) ) ) {// First 4 digits are numbers only.
690 + if ( strtotime( $data ) === false ) {
691 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the schedule is abnormal.', 'usces' );
692 + $logtemp .= $mes . $yn;
693 + }
694 + } else {
695 + $datetime = explode( ' ', $data );
696 + $date_str = usces_dates_interconv( $datetime[0] ) . ' ' . $datetime[1];
697 + if ( strtotime( $date_str ) === false ) {
698 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the schedule is abnormal.', 'usces' );
699 + $logtemp .= $mes . $yn;
700 + }
701 + }
702 + }
703 + break;
704 + case USCES_COL_CATEGORY:
705 + if ( 0 === strlen( $data ) ) {
706 + $mes = 'No.' . $line_num . "\t" . __( 'A category is non-input.', 'usces' );
707 + $logtemp .= $mes . $yn;
708 + }
709 + break;
710 + case USCES_COL_POST_TAG:
711 + break;
712 + case USCES_COL_CUSTOM_FIELD:
713 + break;
714 + case USCES_COL_SKU_CODE:
715 + if ( 0 === strlen( $data ) ) {
716 + $mes = 'No.' . $line_num . "\t" . __( 'A SKU cord is non-input.', 'usces' );
717 + $logtemp .= $mes . $yn;
718 + }
719 + break;
720 + case USCES_COL_SKU_NAME:
721 + break;
722 + case USCES_COL_SKU_CPRICE:
723 + if ( 0 < strlen( $data ) && ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) ) {
724 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the normal price is abnormal.', 'usces' );
725 + $logtemp .= $mes . $yn;
726 + }
727 + break;
728 + case USCES_COL_SKU_PRICE:
729 + if ( ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) || 0 === strlen( $data ) ) {
730 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the sale price is abnormal.', 'usces' );
731 + $logtemp .= $mes . $yn;
732 + }
733 + break;
734 + case USCES_COL_SKU_ZAIKONUM:
735 + if ( 0 < strlen( $data ) ) {
736 + $item_oder_acceptable = (int) $datas[ USCES_COL_ITEM_ORDER_ACCEPTABLE ];
737 + if ( 1 !== $item_oder_acceptable ) {
738 + if ( ! preg_match( '/^[0-9;]+$/', $data ) ) {
739 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
740 + $logtemp .= $mes . $yn;
741 + }
742 + } else {
743 + if ( ! preg_match( '/^[-]?[0-9]+$/', $data ) ) {
744 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
745 + $logtemp .= $mes . $yn;
746 + }
747 + }
748 + }
749 + break;
750 + case USCES_COL_SKU_ZAIKO:
751 + $stock_status = apply_filters( 'usces_filter_csv_upload_check_stock_status', $data );
752 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || $stock_status < $data ) {
753 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock status is abnormal.', 'usces' );
754 + $logtemp .= $mes . $yn;
755 + }
756 + break;
757 + case USCES_COL_SKU_UNIT:
758 + break;
759 + case USCES_COL_SKU_GPTEKIYO:
760 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
761 + $mes = 'No.' . $line_num . "\t" . __( 'The value of the duties pack application is abnormal.', 'usces' );
762 + $logtemp .= $mes . $yn;
763 + }
764 + break;
765 + }
766 +
767 + $logtemp = apply_filters( 'usces_filter_item_uploadcsv_data_check', $logtemp, $line_num, $key, $data );
768 + }
769 +
770 + // Option column check loop.
771 + $opnum = ceil( ( count( $datas ) - $min_field_num ) / 4 );
772 + for ( $i = 0; $i < $opnum; $i++ ) {
773 +
774 + $val = array();
775 + $oplogtemp = '';
776 +
777 + for ( $o = 1; $o <= 4; $o++ ) {
778 +
779 + $key = ( $min_field_num - 1 ) + $o + ( $i * 4 );
780 + if ( isset( $datas[ $key ] ) ) {
781 + $value = trim( $datas[ $key ] );
782 + } else {
783 + $value = null;
784 + }
785 +
786 + switch ( $o ) {
787 + case 1:
788 + if ( empty( $value ) ) {
789 + $oplogtemp .= 'No.' . $line_num . "\t" . sprintf( __( 'Option name of No.%s option is non-input.', 'usces' ), ( $i + 1 ) ) . $yn;
790 + }
791 + $val['name'] = $value;
792 + break;
793 + case 2:
794 + if ( null !== $value && ( ( 0 > (int) $value ) || ( 5 < (int) $value ) ) ) {
795 + $oplogtemp .= 'No.' . $line_num . "\t" . sprintf( __( 'Option-entry-field of No.%s option is abnormal.', 'usces' ), ( $i + 1 ) ) . $yn;
796 + }
797 + $val['mean'] = $value;
798 + break;
799 + case 3:
800 + if ( null !== $value && ( ! preg_match( '/^[0-9;]+$/', $value ) || 1 < (int) $value ) ) {
801 + $oplogtemp .= 'No.' . $line_num . "\t" . sprintf( __( 'Option-required-item of No.%s option is abnormal.', 'usces' ), ( $i + 1 ) ) . $yn;
802 + }
803 + $val['essential'] = $value;
804 + break;
805 + case 4:
806 + if ( ( null !== $value && '' === $value ) && ( 2 > $datas[ ( $key - 2 ) ] && 0 < strlen( $datas[ ( $key - 2 ) ] ) ) ) {
807 + $oplogtemp .= 'No.' . $line_num . "\t" . sprintf( __( 'Option-select of No.%s option is non-input.', 'usces' ), ( $i + 1 ) ) . $yn;
808 + }
809 + $val['value'] = $value;
810 + break;
811 + }
812 + }
813 + if ( ! WCUtils::is_blank( $val['name'] ) || ! WCUtils::is_blank( $val['mean'] ) || ! WCUtils::is_blank( $val['essential'] ) || ! WCUtils::is_blank( $val['value'] ) ) {
814 + $logtemp .= $oplogtemp;
815 + }
816 + }
817 +
818 + // End of data check.
819 + if ( 0 < strlen( $logtemp ) ) {
820 + ++$err_num;
821 + $progress = array(
822 + 'log' => $logtemp,
823 + );
824 + record_item_up_progress( $progress );
825 + $error = true;
826 +
827 + continue;
828 + }
829 +
830 + if ( ! $check_mode ) {
831 +
832 + /**
833 + * Insert Post.
834 + */
835 +
836 + // wp_posts data reg.
837 + $cdatas = array();
838 + $post_fields = array();
839 + $sku = array();
840 + $opt = array();
841 + $valstr = '';
842 +
843 + if ( $pre_code !== $item_code ) {
844 + if ( isset( $pre_skus_count ) && $pre_skus_count > 0 && $pre_skus_count > $sku_index + 1 && ! empty( $pre_code ) ) {
845 + $pre_post_id = wel_get_id_by_item_code( $pre_code, false );
846 + wel_del_skus_by_postid_and_sort( $pre_post_id, $sku_index );
847 + }
848 + $sku_index = 0;
849 + $current_date = current_time( 'mysql' );
850 + $current_date_gmt = current_time( 'mysql', 1 );
851 + $cdatas['ID'] = $post_id;
852 +
853 + $post_modified = $datas[ USCES_COL_POST_MODIFIED ];
854 + if ( '' === $post_modified || '0000-00-00 00:00:00' === $post_modified ) {
855 + if ( 'add' === $mode ) {
856 + $cdatas['post_date'] = $current_date;
857 + $cdatas['post_date_gmt'] = $current_date_gmt;
858 + }
859 + $cdatas['post_modified'] = $current_date;
860 + $cdatas['post_modified_gmt'] = $current_date_gmt;
861 + } else {
862 + if ( preg_match( '/^[0-9;]+$/', substr( $post_modified, 0, 4 ) ) ) {// First 4 digits are numbers only.
863 + $time_data = strtotime( $post_modified );
864 + } else {
865 + $datetime = explode( ' ', $post_modified );
866 + $date_str = usces_dates_interconv( $datetime[0] ) . ' ' . $datetime[1];
867 + $time_data = strtotime( $date_str );
868 + }
869 + $difference = get_option( 'gmt_offset' ) * 60 * 60;
870 + $cdatas['post_date'] = date( 'Y-m-d H:i:s', $time_data );
871 + $cdatas['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', ( $time_data - $difference ) );
872 + $cdatas['post_modified'] = date( 'Y-m-d H:i:s', $time_data );
873 + $cdatas['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', ( $time_data - $difference ) );
874 + }
875 + if ( 'publish' === $datas[ USCES_COL_POST_STATUS ] ) {
876 + if ( mysql2date( 'U', $cdatas['post_modified'], false ) > mysql2date( 'U', $current_date, false ) ) {
877 + $datas[ USCES_COL_POST_STATUS ] = 'future';
878 + }
879 + } elseif ( 'future' === $datas[ USCES_COL_POST_STATUS ] ) {
880 + if ( mysql2date( 'U', $cdatas['post_modified'], false ) <= mysql2date( 'U', $current_date, false ) ) {
881 + $datas[ USCES_COL_POST_STATUS ] = 'publish';
882 + }
883 + }
884 + $cdatas['ID'] = $post_id;
885 + $cdatas['post_author'] = ( ! WCUtils::is_blank( $datas[ USCES_COL_POST_AUTHOR ] ) ) ? $datas[ USCES_COL_POST_AUTHOR ] : $user_ID;
886 + $cdatas['post_content'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_POST_CONTENT ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_POST_CONTENT ] );
887 + $cdatas['post_title'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_POST_TITLE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_POST_TITLE ] );
888 + $cdatas['post_excerpt'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_POST_EXCERPT ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_POST_EXCERPT ] );
889 + $cdatas['post_status'] = $datas[ USCES_COL_POST_STATUS ];
890 + $cdatas['comment_status'] = ( ! WCUtils::is_blank( $datas[ USCES_COL_POST_COMMENT_STATUS ] ) ) ? $datas[ USCES_COL_POST_COMMENT_STATUS ] : 'close';
891 + $cdatas['ping_status'] = 'close';
892 + $cdatas['post_password'] = ( 'private' === $cdatas['post_status'] ) ? '' : $datas[ USCES_COL_POST_PASSWORD ];
893 + $cdatas['post_type'] = 'post';
894 + $cdatas['post_parent'] = 0;
895 +
896 + $spname = ( $csv_encode_type_sjis ) ? sanitize_title( trim( mb_convert_encoding( $datas[ USCES_COL_POST_NAME ], 'UTF-8', 'SJIS' ) ) ) : sanitize_title( trim( $datas[ USCES_COL_POST_NAME ] ) );
897 +
898 + $cdatas['post_name'] = wp_unique_post_slug( $spname, $cdatas['ID'], $cdatas['post_status'], $cdatas['post_type'], $cdatas['post_parent'] );
899 + $cdatas['to_ping'] = '';
900 + $cdatas['pinged'] = '';
901 + $cdatas['menu_order'] = 0;
902 + $cdatas['post_mime_type'] = 'item';
903 + $cdatas['post_content_filtered'] = '';
904 +
905 + if ( empty( $cdatas['post_name'] ) && ! in_array( $cdatas['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) {
906 + $cdatas['post_name'] = sanitize_title( $cdatas['post_title'], $post_id );
907 + }
908 +
909 + $cfdata = array();
910 + $cfrows = ( $csv_encode_type_sjis ) ? explode( $cf_sp, trim( mb_convert_encoding( $datas[ USCES_COL_CUSTOM_FIELD ], 'UTF-8', 'SJIS' ) ) ) : explode( $cf_sp, trim( $datas[ USCES_COL_CUSTOM_FIELD ] ) );
911 + if ( is_array( $cfrows ) && 0 < count( $cfrows ) ) {
912 + reset( $cfrows );
913 +
914 + foreach ( $cfrows as $cfindex => $row ) {
915 + if ( false !== strpos( $row, '=' ) ) {
916 + $cfdata[] = $row;
917 + } else {
918 + $cfdend = count( $cfdata ) - 1;
919 + if ( $cfdend && 0 <= $cfdend ) {
920 + $cfdata[ $cfdend ] = $cfdata[ $cfdend ] . ';' . $row;
921 + }
922 + }
923 + }
924 + }
925 +
926 + $cdatas = apply_filters( 'usces_filter_pre_registered_data', $cdatas, $datas );
927 +
928 + if ( 'add' === $mode ) {
929 + /* Register */
930 +
931 + $cdatas['guid'] = '';
932 + if ( false === $wpdb->insert( $wpdb->posts, $cdatas ) ) {
933 + ++$err_num;
934 + $pre_code = $item_code;
935 +
936 + $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
937 + $progress = array(
938 + 'log' => $mes,
939 + );
940 + record_item_up_progress( $progress );
941 + $error = true;
942 + continue;
943 + }
944 + $post_id = $wpdb->insert_id;
945 + $where = array( 'ID' => $post_id );
946 + $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where );
947 +
948 + } elseif ( 'upd' === $mode ) {
949 +
950 + $where = array( 'ID' => $post_id );
951 + if ( false === $wpdb->update( $wpdb->posts, $cdatas, $where ) ) {
952 + ++$err_num;
953 + $pre_code = $item_code;
954 +
955 + $mes = 'No.' . $line_num . "\t" . __( 'The data were not registered with a database.', 'usces' );
956 + $progress = array(
957 + 'log' => $mes,
958 + );
959 + record_item_up_progress( $progress );
960 + $error = true;
961 + continue;
962 + }
963 + }
964 + // End of wp_insert_post.
965 +
966 + // Delete metas of Item only.
967 + $meta_key_table = array(
968 + '_itemCode',
969 + '_itemName',
970 + '_itemRestriction',
971 + '_itemPointrate',
972 + '_itemGpNum1',
973 + '_itemGpDis1',
974 + '_itemGpNum2',
975 + '_itemGpDis2',
976 + '_itemGpNum3',
977 + '_itemGpDis3',
978 + '_itemShipping',
979 + '_itemDeliveryMethod',
980 + '_itemShippingCharge',
981 + '_itemIndividualSCharge',
982 + '_iopt_',
983 + '_isku_',
984 + '_itemPicts',
985 + '_itemOrderAcceptable',
986 + );
987 +
988 + if ( is_array( $cfrows ) && 0 < count( $cfrows ) ) {
989 + reset( $cfrows );
990 +
991 + foreach ( $cfdata as $row ) {
992 + $cf = explode( '=', $row );
993 + if ( ! WCUtils::is_blank( $cf[0] ) ) {
994 + array_push( $meta_key_table, trim( $cf[0] ) );
995 + }
996 + }
997 + }
998 + $meta_key_table = apply_filters( 'usces_filter_uploadcsv_delete_postmeta', $meta_key_table );
999 + $meta_key_table = array_values( $meta_key_table );
1000 + $placeholders = implode( ', ', array_fill( 0, count( $meta_key_table ), '%s' ) );
1001 + $prepare_args = $meta_key_table;
1002 + $prepare_args[] = $post_id;
1003 + $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ( {$placeholders} ) AND post_id = %d", $prepare_args );
1004 + $db_res = $wpdb->query( $query );
1005 + if ( false === $db_res ) {
1006 + ++$err_num;
1007 + $pre_code = $item_code;
1008 +
1009 + $mes = 'No.' . $line_num . "\t" . __( 'Error : delete postmeta', 'usces' );
1010 + $progress = array(
1011 + 'log' => $mes,
1012 + );
1013 + record_item_up_progress( $progress );
1014 + $error = true;
1015 + continue;
1016 + }
1017 +
1018 + // Delete Item wcct.
1019 + $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND post_id = %d", 'wccs_%', $post_id );
1020 + $db_res = $wpdb->query( $query );
1021 + if ( false === $db_res ) {
1022 + ++$err_num;
1023 + $pre_code = $item_code;
1024 +
1025 + $mes = 'No.' . $line_num . "\t" . __( 'Error : delete wcct', 'usces' );
1026 + $progress = array(
1027 + 'log' => $mes,
1028 + );
1029 + record_item_up_progress( $progress );
1030 + $error = true;
1031 + continue;
1032 + }
1033 +
1034 + // Delete Item revisions.
1035 + $query = $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s", $post_id, 'revision' );
1036 + $db_res = $wpdb->query( $query );
1037 + if ( false === $db_res ) {
1038 + ++$err_num;
1039 + $pre_code = $item_code;
1040 +
1041 + $mes = 'No.' . $line_num . "\t" . __( 'Error : delete revisions', 'usces' );
1042 + $progress = array(
1043 + 'log' => $mes,
1044 + );
1045 + record_item_up_progress( $progress );
1046 + $error = true;
1047 + continue;
1048 + }
1049 +
1050 + // publish_future_post.
1051 + if ( 'future' === $datas[ USCES_COL_POST_STATUS ] && $cdatas['post_date'] > current_time( 'Y-m-d H:i:s' ) ) {
1052 + wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) );
1053 + wp_schedule_single_event( strtotime( get_gmt_from_date( $cdatas['post_date'] ) . ' GMT' ), 'publish_future_post', array( $post_id ) );
1054 + }
1055 +
1056 + // addMeta.
1057 + // Add postmeta.
1058 + if ( 'add' === $mode ) {
1059 + $wel_item = new Welcart\ItemData( $post_id, false );
1060 + $item = $wel_item->get_item_format();
1061 + $item['post_id'] = $post_id;
1062 + } elseif ( 'upd' === $mode ) {
1063 + $item = Wel_get_item( $post_id, false );
1064 + }
1065 +
1066 + $item_delivery_method = explode( ';', $datas[ USCES_COL_ITEM_DELIVERYMETHOD ] );
1067 +
1068 + $item['itemCode'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_CODE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_CODE ] );
1069 + $item['itemName'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_NAME ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_NAME ] );
1070 + $item['itemRestriction'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_RESTRICTION ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_RESTRICTION ] );
1071 + $item['itemPointrate'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_POINTRATE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_POINTRATE ] );
1072 + $item['itemGpNum1'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM1 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM1 ] );
1073 + $item['itemGpDis1'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS1 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS1 ] );
1074 + $item['itemGpNum2'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM2 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM2 ] );
1075 + $item['itemGpDis2'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS2 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS2 ] );
1076 + $item['itemGpNum3'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM3 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM3 ] );
1077 + $item['itemGpDis3'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS3 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS3 ] );
1078 + $item['itemShipping'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_SHIPPING ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_SHIPPING ] );
1079 + $item['itemDeliveryMethod'] = $item_delivery_method;
1080 + $item['itemShippingCharge'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_SHIPPINGCHARGE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_SHIPPINGCHARGE ] );
1081 + $item['itemIndividualSCharge'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_INDIVIDUALSCHARGE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_INDIVIDUALSCHARGE ] );
1082 + $item['itemOrderAcceptable'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_ORDER_ACCEPTABLE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_ORDER_ACCEPTABLE ] );
1083 +
1084 + wel_update_item_data( $item, $post_id );
1085 +
1086 + // add term_relationships, edit term_taxonomy.
1087 + // Category.
1088 + if ( $category_format_slug ) {
1089 + $categories = array();
1090 + $category_slug = explode( ';', $datas[ USCES_COL_CATEGORY ] );
1091 + foreach ( (array) $category_slug as $slug ) {
1092 + $categories[] = usces_get_cat_id( $slug );
1093 + }
1094 + } else {
1095 + $categories = explode( ';', $datas[ USCES_COL_CATEGORY ] );
1096 + }
1097 + wp_set_post_categories( $post_id, $categories );
1098 + wp_update_term_count( $categories, 'category' );
1099 +
1100 + // Tag.
1101 + $tags = $csv_encode_type_sjis ? explode( ';', trim( mb_convert_encoding( $datas[ USCES_COL_POST_TAG ], 'UTF-8', 'SJIS' ) ) ) : explode( ';', trim( $datas[ USCES_COL_POST_TAG ] ) );
1102 + wp_set_post_tags( $post_id, $tags );
1103 +
1104 + // Add Custom Field.
1105 + if ( is_array( $cfdata ) && 0 <= count( $cfdata ) ) {
1106 + reset( $cfdata );
1107 + $cfstr = '';
1108 +
1109 + foreach ( $cfdata as $row ) {
1110 + preg_match( '/^([^=]+)=([\s\S]*)$/m', $row, $cf );
1111 + if ( isset( $cf[1] ) && ! WCUtils::is_blank( $cf[1] ) ) {
1112 + $cfstr .= '(' . $post_id . ", '" . esc_sql( $cf[1] ) . "','" . esc_sql( $cf[2] ) . "'),";
1113 + }
1114 + }
1115 +
1116 + if ( ! WCUtils::is_blank( $cfstr ) ) {
1117 + $cfstr = rtrim( $cfstr, ',' );
1118 + $db_res = $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES {$cfstr}" );
1119 + }
1120 + }
1121 + do_action( 'usces_action_uploadcsv_itemvalue', $post_id, $datas, $add_field_num );
1122 +
1123 + // addOption.
1124 + // Add Item Option.
1125 + wel_delete_all_opt_data( $post_id );
1126 +
1127 + for ( $i = 0; $i < $opnum; $i++ ) {
1128 + $opflg = true;
1129 + $optvalue = array();
1130 + for ( $o = 1; $o <= 4; $o++ ) {
1131 + $key = ( $min_field_num - 1 ) + $o + ( $i * 4 );
1132 + if ( 1 === $o && '' === $datas[ $key ] ) {
1133 + $opflg = false;
1134 + break 1;
1135 + }
1136 + switch ( $o ) {
1137 + case 1:
1138 + $optvalue['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ $key ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ $key ] );
1139 + break;
1140 + case 2:
1141 + $optvalue['means'] = (int) $datas[ $key ];
1142 + break;
1143 + case 3:
1144 + $optvalue['essential'] = (int) $datas[ $key ];
1145 + break;
1146 + case 4:
1147 + if ( ! empty( $datas[ $key ] ) ) {
1148 + $cr = array( "\r\n", "\r" );
1149 + $datavalue = trim( $datas[ $key ] );
1150 + $datavalue = str_replace( $cr, '', $datavalue );
1151 + $optvalue['value'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? str_replace( ';', "\n", mb_convert_encoding( $datavalue, 'UTF-8', 'SJIS' ) ) : str_replace( ';', "\n", $datavalue );
1152 + } else {
1153 + $optvalue['value'] = '';
1154 + }
1155 + break;
1156 + }
1157 + }
1158 +
1159 + if ( $opflg && ! empty( $optvalue ) ) {
1160 +
1161 + $optvalue['sort'] = $i;
1162 +
1163 + $resopt = wel_add_opt_data( $post_id, $optvalue );
1164 + }
1165 + }
1166 + } else {
1167 + ++$sku_index;
1168 + }
1169 +
1170 + // addSku.
1171 + // Add Item SKU.
1172 +
1173 + $skus = wel_get_skus( $post_id, 'sort', false );
1174 + $sku = isset( $skus[ $sku_index ] ) ? $skus[ $sku_index ] : false;
1175 +
1176 + if ( false === $sku ) {
1177 +
1178 + $sku = array();
1179 + $sku['code'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_CODE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_CODE ] );
1180 + $sku['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_NAME ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_NAME ] );
1181 + $sku['cprice'] = $datas[ USCES_COL_SKU_CPRICE ];
1182 + $sku['price'] = $datas[ USCES_COL_SKU_PRICE ];
1183 + $sku['stocknum'] = $datas[ USCES_COL_SKU_ZAIKONUM ];
1184 + $sku['stock'] = $datas[ USCES_COL_SKU_ZAIKO ];
1185 + $sku['unit'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_UNIT ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_UNIT ] );
1186 + $sku['gp'] = $datas[ USCES_COL_SKU_GPTEKIYO ];
1187 + $sku['sort'] = $sku_index;
1188 + $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[ USCES_COL_SKU_APPLICABLE_TAXRATE ] );
1189 +
1190 + $sku = apply_filters( 'usces_filter_uploadcsv_skuvalue', $sku, $datas );
1191 +
1192 + wel_add_sku_data( $post_id, $sku );
1193 +
1194 + } else {
1195 +
1196 + $sku['code'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_CODE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_CODE ] );
1197 + $sku['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_NAME ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_NAME ] );
1198 + $sku['cprice'] = $datas[ USCES_COL_SKU_CPRICE ];
1199 + $sku['price'] = $datas[ USCES_COL_SKU_PRICE ];
1200 + $sku['stocknum'] = $datas[ USCES_COL_SKU_ZAIKONUM ];
1201 + $sku['stock'] = $datas[ USCES_COL_SKU_ZAIKO ];
1202 + $sku['unit'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ USCES_COL_SKU_UNIT ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_SKU_UNIT ] );
1203 + $sku['gp'] = $datas[ USCES_COL_SKU_GPTEKIYO ];
1204 + $sku['sort'] = $sku_index;
1205 + $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[ USCES_COL_SKU_APPLICABLE_TAXRATE ] );
1206 +
1207 + $sku = apply_filters( 'usces_filter_uploadcsv_skuvalue', $sku, $datas );
1208 +
1209 + $meta_id = $sku['meta_id'];
1210 + wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1211 +
1212 + }
1213 + } else {
1214 + if ( $pre_code !== $item_code ) {
1215 + $pre_post_id = ! empty( wel_get_id_by_item_code( $pre_code, false ) ) ? wel_get_id_by_item_code( $pre_code, false ) : $post_id;
1216 + $skus = wel_get_skus( $pre_post_id, 'sort', false );
1217 + } else {
1218 + $skus = array();
1219 + }
1220 + }
1221 + $pre_code = $item_code;
1222 + $pre_skus_count = count( (array) $skus );
1223 + ++$comp_num;
1224 +
1225 + do_action(
1226 + 'usces_after_uploadcsv_line_processed',
1227 + array(
1228 + 'line' => $line,
1229 + 'datas' => $datas,
1230 + 'check_mode' => $check_mode,
1231 + 'post_id' => $post_id,
1232 + 'sku' => ! empty( $sku ) ? $sku : null,
1233 + )
1234 + );
1235 +
1236 + // Status update.
1237 + if ( 0 === ( $line_num % 10 ) ) {
1238 + $progress = array(
1239 + 'info' => $file_info,
1240 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
1241 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1242 + 'i' => $line_num,
1243 + 'all' => $total_num,
1244 + );
1245 + record_item_up_progress( $progress );
1246 + }
1247 + }
1248 +
1249 + if ( ! $check_mode ) {
1250 + if ( isset( $pre_skus_count ) && $pre_skus_count > 0 && $pre_skus_count > $sku_index + 1 && ! empty( $post_id ) ) {
1251 + wel_del_skus_by_postid_and_sort( $post_id, $sku_index );
1252 + }
1253 + }
1254 + }
1255 +
1256 + do_action(
1257 + 'usces_after_uploadcsv_lines_processed',
1258 + array(
1259 + 'lines' => $lines,
1260 + 'datas' => $datas,
1261 + 'check_mode' => $check_mode,
1262 + 'error' => $error,
1263 + )
1264 + );
1265 +
1266 + // Final status.
1267 + if ( $error ) {
1268 + $progress = array(
1269 + 'info' => $file_info,
1270 + 'status' => __( 'End (with error)', 'usces' ) . $check_label,
1271 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1272 + 'i' => $line_num,
1273 + 'all' => $total_num,
1274 + 'flag' => 'complete',
1275 + );
1276 + record_item_up_progress( $progress );
1277 + } else {
1278 + $progress = array(
1279 + 'info' => $file_info,
1280 + 'status' => __( 'End', 'usces' ) . $check_label,
1281 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1282 + 'log' => __( 'No abnormality', 'usces' ),
1283 + 'i' => $line_num,
1284 + 'all' => $total_num,
1285 + 'flag' => 'complete',
1286 + );
1287 + record_item_up_progress( $progress );
1288 + }
1289 + $file_path = $upload_folder . $file_name;
1290 + $real_path = realpath( $file_path );
1291 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
1292 + unlink( $file_path );
1293 + }
1294 + die( wp_json_encode( $progress ) );
1295 + }
1296 + endif;// End of All columns Product CSV upload.
1297 +
1298 + if ( ! function_exists( 'usces_download_item_list' ) ) :
1299 +
1300 + /**
1301 + * All columns Product CSV download.
1302 + *
1303 + * @since 2.2.2
1304 + */
1305 + function usces_download_item_list() {
1306 + global $wpdb, $usces;
1307 +
1308 + require_once USCES_PLUGIN_DIR . '/classes/itemList.class.php';
1309 +
1310 + $ext = 'csv';
1311 + $th_h1 = '"';
1312 + $th_h = ',"';
1313 + $th_f = '"';
1314 + $td_h1 = '"';
1315 + $td_h = ',"';
1316 + $td_f = '"';
1317 + $sp = ';';
1318 + $cf_sp = ';;'; // custom field separator.
1319 + $eq = '=';
1320 + $lf = "\n";
1321 +
1322 + // Save the selection status of download columns.
1323 + $usces_opt_item = get_option( 'usces_opt_item' );
1324 + if ( ! is_array( $usces_opt_item ) ) {
1325 + $usces_opt_item = array();
1326 + }
1327 + $usces_opt_item['chk_header'] = ( isset( $_REQUEST['chk_header'] ) ) ? 1 : 0;
1328 + $usces_opt_item['ftype_item'] = $ext;
1329 + update_option( 'usces_opt_item', $usces_opt_item );
1330 +
1331 + // Get data.
1332 + $table_name = $wpdb->posts;
1333 + $arr_column = array(
1334 + __( 'Post ID', 'usces' ) => 'post_id',
1335 + __( 'item code', 'usces' ) => 'item_code',
1336 + __( 'item name', 'usces' ) => 'item_name',
1337 + __( 'SKU code', 'usces' ) => 'sku_key',
1338 + __( 'selling price', 'usces' ) => 'price',
1339 + __( 'stock', 'usces' ) => 'zaiko_num',
1340 + __( 'stock status', 'usces' ) => 'zaiko',
1341 + __( 'Categories', 'usces' ) => 'category',
1342 + __( 'display status', 'usces' ) => 'display_status',
1343 + );
1344 +
1345 + $_REQUEST['searchIn'] = 'searchIn';
1346 + $dt = new dataList( $table_name, $arr_column );
1347 + $dt->pageLimit = 'off';
1348 + $dt->exportMode = true;
1349 + $res = $dt->MakeTable();
1350 + $rows = $dt->rows;
1351 +
1352 + // Processing branch for each mode.
1353 + $results = apply_filters( 'usces_filter_item_downloadcsv_mode', array(), $rows, $usces_opt_item );
1354 + if ( ! empty( $results ) ) {
1355 +
1356 + extract( $results );
1357 + } elseif ( isset( $_REQUEST['mode'] ) && 'stock' === $_REQUEST['mode'] ) {
1358 +
1359 + $results = usces_download_item_stock_list( $rows, $usces_opt_item );
1360 + if ( ! empty( $results ) ) {
1361 + extract( $results );
1362 + }
1363 + } elseif ( isset( $_REQUEST['mode'] ) && 'sku' === $_REQUEST['mode'] ) {
1364 +
1365 + $results = usces_download_item_sku_list( $rows, $usces_opt_item );
1366 + if ( ! empty( $results ) ) {
1367 + extract( $results );
1368 + }
1369 + } elseif ( isset( $_REQUEST['mode'] ) && 'meta' === $_REQUEST['mode'] ) {
1370 +
1371 + $results = usces_download_item_meta_list( $rows, $usces_opt_item );
1372 + if ( ! empty( $results ) ) {
1373 + extract( $results );
1374 + }
1375 + } else {
1376 +
1377 + $line = '';
1378 +
1379 + // Heading.
1380 + if ( $usces_opt_item['chk_header'] == 1 ) {
1381 + $line .= $th_h1 . 'Post ID' . $th_f;
1382 + $line .= $th_h . __( 'Post Author', 'usces' ) . $th_f;
1383 + $line .= $th_h . __( 'explanation', 'usces' ) . $th_f;
1384 + $line .= $th_h . __( 'Title', 'usces' ) . $th_f;
1385 + $line .= $th_h . __( 'excerpt', 'usces' ) . $th_f;
1386 + $line .= $th_h . __( 'display status', 'usces' ) . $th_f;
1387 + $line .= $th_h . __( 'Comment Status', 'usces' ) . $th_f;
1388 + $line .= $th_h . __( 'Post Password', 'usces' ) . $th_f;
1389 + $line .= $th_h . __( 'Post Name', 'usces' ) . $th_f;
1390 + $line .= $th_h . __( 'Publish date', 'usces' ) . $th_f;
1391 +
1392 + $line .= $th_h . __( 'item code', 'usces' ) . $th_f;
1393 + $line .= $th_h . __( 'item name', 'usces' ) . $th_f;
1394 + $line .= $th_h . __( 'Limited amount for purchase', 'usces' ) . $th_f;
1395 + $line .= $th_h . __( 'Percentage of points', 'usces' ) . $th_f;
1396 + $line .= $th_h . __( 'Business package discount', 'usces' ) . '1-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '1-' . __( 'rate', 'usces' ) . $th_f;
1397 + $line .= $th_h . __( 'Business package discount', 'usces' ) . '2-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '2-' . __( 'rate', 'usces' ) . $th_f;
1398 + $line .= $th_h . __( 'Business package discount', 'usces' ) . '3-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '3-' . __( 'rate', 'usces' ) . $th_f;
1399 + $line .= $th_h . __( 'Sold out limit', 'usces' ) . $th_f;
1400 +
1401 + $line .= $th_h . __( 'estimated shipping date', 'usces' ) . $th_f;
1402 + $line .= $th_h . __( 'shipping option', 'usces' ) . $th_f;
1403 + $line .= $th_h . __( 'Shipping', 'usces' ) . $th_f;
1404 + $line .= $th_h . __( 'Postage individual charging', 'usces' ) . $th_f;
1405 +
1406 + $line .= $th_h . __( 'Categories', 'usces' ) . $th_f;
1407 + $line .= $th_h . __( 'tag', 'usces' ) . $th_f;
1408 + $line .= $th_h . __( 'Custom Field', 'usces' ) . $th_f;
1409 +
1410 + $line .= apply_filters( 'usces_filter_downloadcsv_itemheader', '' );
1411 + $line = apply_filters( 'usces_filter_downloadcsv_add_itemheader', $line );
1412 +
1413 + $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;
1414 + $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;
1415 + $line .= $th_h . __( 'normal price', 'usces' ) . $th_f;
1416 + $line .= $th_h . __( 'Sale price', 'usces' ) . $th_f;
1417 + $line .= $th_h . __( 'stock', 'usces' ) . $th_f;
1418 + $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;
1419 + $line .= $th_h . __( 'unit', 'usces' ) . $th_f;
1420 + $line .= $th_h . __( 'Apply business package', 'usces' ) . $th_f;
1421 + $line .= $th_h . __( 'Applicable tax rate', 'usces' ) . $th_f;
1422 +
1423 + $line .= apply_filters( 'usces_filter_downloadcsv_header', '' );
1424 + $line = apply_filters( 'usces_filter_downloadcsv_add_header', $line );
1425 + $line .= $th_h . __( 'option name', 'usces' ) . $th_f . $th_h . __( 'Field type', 'usces' ) . $th_f . $th_h . __( 'Required', 'usces' ) . $th_f . $th_h . __( 'selected amount', 'usces' ) . $th_f;
1426 + $line .= $lf;
1427 + }
1428 +
1429 + $category_format_slug = ( isset( $usces->options['system']['csv_category_format'] ) && 1 === (int) $usces->options['system']['csv_category_format'] ) ? true : false;
1430 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1431 +
1432 + while ( @ob_get_level() > 0 ) {
1433 + ob_end_clean();
1434 + }
1435 +
1436 + if ( $csv_encode_type_sjis ) {
1437 + $charset = 'Shift_JIS';
1438 + } else {
1439 + $charset = 'UTF-8';
1440 + }
1441 +
1442 + mb_http_output( 'pass' );
1443 + set_time_limit( 3600 );
1444 + header( 'Content-Type: application/octet-stream; charset=' . $charset );
1445 + header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
1446 +
1447 + if ( ! $csv_encode_type_sjis ) {
1448 + echo "\xEF\xBB\xBF";
1449 + }
1450 +
1451 + // Outupt data.
1452 + foreach ( (array) $rows as $row ) {
1453 +
1454 + $post_id = $row['ID'];
1455 + $product = wel_get_product( $post_id );
1456 + $post = $product['_pst'];
1457 +
1458 + // Post Data.
1459 + $line_item = $td_h1 . $product['ID'] . $td_f;
1460 + $line_item .= $td_h . $post->post_author . $td_f;
1461 + $line_item .= $td_h . usces_entity_decode( $post->post_content, $ext ) . $td_f;
1462 + $line_item .= $td_h . usces_entity_decode( $post->post_title, $ext ) . $td_f;
1463 + $line_item .= $td_h . usces_entity_decode( $post->post_excerpt, $ext ) . $td_f;
1464 + $line_item .= $td_h . $post->post_status . $td_f;
1465 + $line_item .= $td_h . $post->comment_status . $td_f;
1466 + $line_item .= $td_h . $post->post_password . $td_f;
1467 + $line_item .= $td_h . urldecode( $post->post_name ) . $td_f;
1468 + $line_item .= $td_h . $post->post_date . $td_f;
1469 +
1470 + // Item Meta.
1471 + $line_item .= $td_h . $product['itemCode'] . $td_f;
1472 + $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
1473 + $line_item .= $td_h . $product['itemRestriction'] . $td_f;
1474 + $line_item .= $td_h . $product['itemPointrate'] . $td_f;
1475 + $line_item .= $td_h . $product['itemGpNum1'] . $td_f . $td_h . $product['itemGpDis1'] . $td_f;
1476 + $line_item .= $td_h . $product['itemGpNum2'] . $td_f . $td_h . $product['itemGpDis2'] . $td_f;
1477 + $line_item .= $td_h . $product['itemGpNum3'] . $td_f . $td_h . $product['itemGpDis3'] . $td_f;
1478 + $line_item .= $td_h . $product['itemOrderAcceptable'] . $td_f;
1479 + $line_item .= $td_h . $product['itemShipping'] . $td_f;
1480 +
1481 + $delivery_method = '';
1482 + $item_delivery_method = $product['itemDeliveryMethod'];
1483 + foreach ( (array) $item_delivery_method as $k => $v ) {
1484 + $delivery_method .= $v . $sp;
1485 + }
1486 + $delivery_method = rtrim( $delivery_method, $sp );
1487 +
1488 + $line_item .= $td_h . $delivery_method . $td_f;
1489 + $line_item .= $td_h . $product['itemShippingCharge'] . $td_f;
1490 + $line_item .= $td_h . $product['itemIndividualSCharge'] . $td_f;
1491 +
1492 + // Categories.
1493 + $category = '';
1494 + $cat_ids = wp_get_post_categories( $post_id );
1495 + if ( ! empty( $cat_ids ) ) {
1496 + if ( $category_format_slug ) {
1497 + foreach ( $cat_ids as $id ) {
1498 + $cat = get_category( $id );
1499 + $category .= $cat->slug . $sp;
1500 + }
1501 + } else {
1502 + foreach ( $cat_ids as $id ) {
1503 + $category .= $id . $sp;
1504 + }
1505 + }
1506 + $category = rtrim( $category, $sp );
1507 + }
1508 + $line_item .= $td_h . $category . $td_f;
1509 +
1510 + // Tags.
1511 + $tag = '';
1512 + $tags_ob = wp_get_object_terms( $post_id, 'post_tag' );
1513 + foreach ( $tags_ob as $ob ) {
1514 + $tag .= $ob->name . $sp;
1515 + }
1516 + $tag = rtrim( $tag, $sp );
1517 +
1518 + $line_item .= $td_h . $tag . $td_f;
1519 +
1520 + // Custom Fields.
1521 + $cfield = '';
1522 + $custom_fields = wel_get_extra_data( $post_id );
1523 +
1524 + if ( $custom_fields && is_array( $custom_fields ) && 0 < count( $custom_fields ) ) {
1525 + foreach ( $custom_fields as $cfkey => $cfvalues ) {
1526 + if ( '_itemOrderAcceptable' === $cfkey ) {
1527 + continue;
1528 + }
1529 + if ( is_array( $cfvalues ) ) {
1530 + foreach ( $cfvalues as $value ) {
1531 + $cfield .= usces_entity_decode( $cfkey, $ext ) . $eq . usces_entity_decode( $value, $ext ) . $cf_sp;
1532 + }
1533 + } else {
1534 + $cfield .= usces_entity_decode( $cfkey, $ext ) . $eq . usces_entity_decode( $cfvalues, $ext ) . $cf_sp;
1535 + }
1536 + }
1537 + $cfield = rtrim( $cfield, $sp );
1538 + }
1539 + $line_item .= $td_h . $cfield . $td_f;
1540 +
1541 + $line_item .= apply_filters( 'usces_filter_downloadcsv_itemvalue', '', $post_id );
1542 + $line_item = apply_filters( 'usces_filter_downloadcsv_add_itemvalue', $line_item, $post_id, $post );
1543 +
1544 + // Item Options.
1545 + $line_options = '';
1546 + $opts = $product['_opt'];
1547 +
1548 + foreach ( $opts as $opt ) {
1549 + $value = '';
1550 +
1551 + if ( is_array( $opt['value'] ) ) {
1552 + foreach ( $opt['value'] as $v ) {
1553 + $v = usces_change_line_break( $v );
1554 + $values = explode( "\n", $v );
1555 + foreach ( $values as $val ) {
1556 + $value .= $val . $sp;
1557 + }
1558 + }
1559 + $value = rtrim( $value, $sp );
1560 +
1561 + } else {
1562 + $value = usces_change_line_break( $opt['value'] );
1563 + $value = str_replace( "\n", ';', $value );
1564 + }
1565 + $line_options .= $td_h . usces_entity_decode( $opt['name'], $ext ) . $td_f;
1566 + $line_options .= $td_h . $opt['means'] . $td_f;
1567 + $line_options .= $td_h . $opt['essential'] . $td_f;
1568 + $line_options .= $td_h . usces_entity_decode( $value, $ext ) . $td_f;
1569 + }
1570 +
1571 + // SKU.
1572 + $skus = $product['_sku'];
1573 + foreach ( $skus as $sku ) {
1574 + $line_sku = $td_h . $sku['code'] . $td_f;
1575 + $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
1576 + $line_sku .= $td_h . usces_crform( $sku['cprice'], false, false, 'return', false ) . $td_f;
1577 + $line_sku .= $td_h . usces_crform( $sku['price'], false, false, 'return', false ) . $td_f;
1578 + $line_sku .= $td_h . $sku['stocknum'] . $td_f;
1579 + $line_sku .= $td_h . $sku['stock'] . $td_f;
1580 + $line_sku .= $td_h . usces_entity_decode( $sku['unit'], $ext ) . $td_f;
1581 + $line_sku .= $td_h . $sku['gp'] . $td_f;
1582 + $line_sku .= $td_h . usces_csv_get_sku_applicable_taxrate( $sku ) . $td_f;
1583 +
1584 + $line_sku .= apply_filters( 'usces_filter_downloadcsv_skuvalue', '', $sku );
1585 + $line_sku = apply_filters( 'usces_filter_downloadcsv_add_skuvalue', $line_sku, $sku );
1586 +
1587 + $line .= $line_item . $line_sku . $line_options . $lf;
1588 + }
1589 + if ( $csv_encode_type_sjis ) {
1590 + $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
1591 + }
1592 +
1593 + // This is a direct binary/CSV download response, so HTML escaping is not applicable.
1594 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1595 + print( $line );
1596 + flush();
1597 +
1598 + $line = '';
1599 + wp_cache_flush();
1600 + }
1601 + }
1602 +
1603 + unset( $rows, $dt, $line, $line_item, $line_options, $line_sku );
1604 + exit();
1605 + }
1606 +
1607 + endif;// End of All columns Product CSV download.
1608 +
1609 + /**
1610 + * Stock columns Product CSV upload.
1611 + *
1612 + * @since 2.2.2
1613 + * @param string $lines 1 line of text.
1614 + * @param array $file_info Information.
1615 + */
1616 + function usces_item_stock_uploadcsv( $lines, $file_info ) {
1617 + global $wpdb, $usces;
1618 +
1619 + $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
1620 + $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
1621 + $file_name = wel_esc_upload_file_name( $file_name );
1622 +
1623 + $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
1624 + $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
1625 + $line_num = 0;
1626 + $column_num = 0;
1627 + $total_num = count( $lines );
1628 + $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
1629 + $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
1630 + $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
1631 + $work_number = 0;
1632 + $error = false;
1633 +
1634 + $yn = "\n";
1635 +
1636 + // Stock columns.
1637 +
1638 + // Registration loop.
1639 + foreach ( $lines as $rows_num => $line ) {
1640 +
1641 + $logtemp = '';
1642 + $line = trim( $line );
1643 + if ( empty( $line ) ) {
1644 + continue;
1645 + }
1646 +
1647 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1648 +
1649 + // Divide the line and store it in $datas.
1650 + $datas = usces_make_line_data( $line );
1651 +
1652 + if ( $column_num < count( $datas ) ) {
1653 + $column_num = count( $datas );
1654 + }
1655 + $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
1656 +
1657 + if ( 8 !== count( $datas ) || ( 0 === $rows_num && 'Post ID' !== $datas[0] ) ) {
1658 + $progress = array(
1659 + 'info' => $file_info,
1660 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
1661 + 'progress' => __( 'The process was not completed', 'usces' ),
1662 + 'log' => 'Error : ' . __( 'This file may not be the item CSV for "Stock columns".', 'usces' ),
1663 + 'flag' => 'complete',
1664 + );
1665 + record_item_up_progress( $progress );
1666 + $error = true;
1667 +
1668 + $file_path = $upload_folder . $file_name;
1669 + $real_path = realpath( $file_path );
1670 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
1671 + unlink( $file_path );
1672 + }
1673 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
1674 + return $results;
1675 + }
1676 +
1677 + // Skip the first line.
1678 + if ( 'Post ID' === $datas[0] ) {
1679 + continue;
1680 + }
1681 +
1682 + $line_num = $rows_num + 1;
1683 +
1684 + // Split processing.
1685 + if ( $start_number > $work_number ) {
1686 + ++$work_number;
1687 + continue;
1688 + }
1689 + if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 10 ) ) && $start_number != $work_number ) {
1690 + $progress = array(
1691 + 'info' => $file_info,
1692 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
1693 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1694 + 'i' => $line_num,
1695 + 'all' => $total_num,
1696 + 'flag' => 'continue',
1697 + 'work_number' => $work_number,
1698 + 'comp_num' => $comp_num,
1699 + 'err_num' => $err_num,
1700 + );
1701 + record_item_up_progress( $progress );
1702 + die( wp_json_encode( $progress ) );
1703 + }
1704 + ++$work_number;
1705 +
1706 + // Column check loop.
1707 + foreach ( $datas as $key => $data ) {
1708 +
1709 + $post_id = ( ! WCUtils::is_blank( $datas[0] ) ) ? (int) $datas[0] : null;
1710 + $meta_id = ( ! WCUtils::is_blank( $datas[3] ) ) ? (int) $datas[3] : null;
1711 + $product = wel_get_product( $post_id );
1712 +
1713 + switch ( $key ) {
1714 + case 0:// Post ID.
1715 + if ( $post_id ) {
1716 + if ( false === $product ) {
1717 + ++$err_num;
1718 + $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Post-ID %s does not exist in the database.', 'usces' ), $post_id );
1719 + $logtemp .= $mes . $yn;
1720 + }
1721 + } else {
1722 + ++$err_num;
1723 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Post-ID is abnormal.', 'usces' );
1724 + $logtemp .= $mes . $yn;
1725 + }
1726 + break;
1727 +
1728 + case 3:// Meta ID.
1729 + if ( $post_id && $meta_id ) {
1730 + $skus = $product['_sku'];
1731 + $meta_flag = false;
1732 + foreach ( $skus as $sku ) {
1733 + if ( $meta_id === (int) $sku['meta_id'] ) {
1734 + $meta_flag = true;
1735 + break;
1736 + }
1737 + }
1738 + if ( false === $meta_flag ) {
1739 + ++$err_num;
1740 + $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Meta ID %s does not exist in the database.', 'usces' ), $meta_id );
1741 + $logtemp .= $mes . $yn;
1742 + }
1743 + } else {
1744 + ++$err_num;
1745 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Meta ID is abnormal.', 'usces' );
1746 + $logtemp .= $mes . $yn;
1747 + }
1748 + break;
1749 +
1750 + case 4:// SKU Code.
1751 + if ( 0 === strlen( $data ) ) {
1752 + $mes = 'No.' . $line_num . "\t" . __( 'A SKU cord is non-input.', 'usces' );
1753 + $logtemp .= $mes . $yn;
1754 + }
1755 + break;
1756 +
1757 + case 5:// SKU Name.
1758 + break;
1759 +
1760 + case 6:// Stock.
1761 + if ( 0 < strlen( $data ) && ! preg_match( '/^[0-9]+$/', $data ) ) {
1762 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
1763 + $logtemp .= $mes . $yn;
1764 + }
1765 + break;
1766 +
1767 + case 7:// Stock Status.
1768 + $stock_status = apply_filters( 'usces_filter_csv_upload_check_stock_status', $data );
1769 + if ( ! preg_match( '/^[0-9]+$/', $data ) || $stock_status < $data ) {
1770 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock status is abnormal.', 'usces' );
1771 + $logtemp .= $mes . $yn;
1772 + }
1773 + break;
1774 + }
1775 + }
1776 +
1777 + // End of data check.
1778 + if ( 0 < strlen( $logtemp ) ) {
1779 + ++$err_num;
1780 + $progress = array(
1781 + 'log' => $logtemp,
1782 + );
1783 + record_item_up_progress( $progress );
1784 + $error = true;
1785 + continue;
1786 + }
1787 +
1788 + if ( ! $check_mode ) {
1789 +
1790 + $post_id = (int) $datas[0];
1791 + $meta_id = (int) $datas[3];
1792 + $skus = wel_get_skus( $post_id, 'meta_id', false );
1793 + $sku = $skus[ $meta_id ];
1794 + if ( empty( $skus ) || ! isset( $skus[ $meta_id ] ) ) {
1795 + ++$err_num;
1796 + $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
1797 + $progress = array(
1798 + 'log' => $mes,
1799 + );
1800 + record_item_up_progress( $progress );
1801 + $error = true;
1802 + continue;
1803 + }
1804 +
1805 + $sku['code'] = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $datas[4], 'UTF-8', 'SJIS' ) ) : trim( $datas[4] );
1806 + $sku['name'] = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $datas[5], 'UTF-8', 'SJIS' ) ) : trim( $datas[5] );
1807 + $sku['stocknum'] = $datas[6];
1808 + $sku['stock'] = $datas[7];
1809 +
1810 + $db_res = wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1811 +
1812 + if ( 0 > $db_res ) {
1813 + ++$err_num;
1814 + $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
1815 + $progress = array(
1816 + 'log' => $mes,
1817 + );
1818 + record_item_up_progress( $progress );
1819 + $error = true;
1820 + continue;
1821 + }
1822 + wp_cache_delete( $post_id, 'post_meta' );
1823 + }
1824 + ++$comp_num;
1825 +
1826 + if ( 0 === ( $line_num % 10 ) ) {
1827 + $progress = array(
1828 + 'info' => $file_info,
1829 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
1830 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1831 + 'i' => $line_num,
1832 + 'all' => $total_num,
1833 + );
1834 + record_item_up_progress( $progress );
1835 + }
1836 + }
1837 +
1838 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num' );
1839 +
1840 + return $results;
1841 + }
1842 +
1843 + /**
1844 + * Stock columns Product CSV download.
1845 + *
1846 + * @since 2.2.2
1847 + * @param string $rows Data.
1848 + * @param array $usces_opt_item Item option.
1849 + */
1850 + function usces_download_item_stock_list( $rows, $usces_opt_item ) {
1851 + global $usces;
1852 +
1853 + $ext = 'csv';
1854 + $th_h1 = '"';
1855 + $th_h = ',"';
1856 + $th_f = '"';
1857 + $td_h1 = '"';
1858 + $td_h = ',"';
1859 + $td_f = '"';
1860 + $sp = ';';
1861 + $eq = '=';
1862 + $lf = "\n";
1863 +
1864 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1865 +
1866 + $line = '';
1867 +
1868 + if ( 1 === (int) $usces_opt_item['chk_header'] ) {
1869 + $line .= $th_h1 . 'Post ID' . $th_f;
1870 + $line .= $th_h . __( 'item code', 'usces' ) . $th_f;
1871 + $line .= $th_h . __( 'item name', 'usces' ) . $th_f;
1872 + $line .= $th_h . 'Meta ID' . $th_f;
1873 + $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;
1874 + $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;
1875 + $line .= $th_h . __( 'stock', 'usces' ) . $th_f;
1876 + $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;
1877 + $line .= $lf;
1878 + }
1879 +
1880 + while ( @ob_get_level() > 0 ) {
1881 + ob_end_clean();
1882 + }
1883 +
1884 + if ( $csv_encode_type_sjis ) {
1885 + $charset = 'Shift_JIS';
1886 + } else {
1887 + $charset = 'UTF-8';
1888 + }
1889 +
1890 + mb_http_output( 'pass' );
1891 + set_time_limit( 3600 );
1892 + header( 'Content-Type: application/octet-stream; charset=' . $charset );
1893 + header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
1894 +
1895 + if ( ! $csv_encode_type_sjis ) {
1896 + echo "\xEF\xBB\xBF";
1897 + }
1898 +
1899 + foreach ( (array) $rows as $row ) {
1900 +
1901 + $post_id = $row['ID'];
1902 + $product = wel_get_product( $post_id );
1903 +
1904 + $line_item = $td_h1 . $post_id . $td_f;
1905 + $line_item .= $td_h . $product['itemCode'] . $td_f;
1906 + $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
1907 +
1908 + $skus = $product['_sku'];
1909 + foreach ( $skus as $sku ) {
1910 + $line_sku = $td_h . $sku['meta_id'] . $td_f;
1911 + $line_sku .= $td_h . $sku['code'] . $td_f;
1912 + $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
1913 + $line_sku .= $td_h . $sku['stocknum'] . $td_f;
1914 + $line_sku .= $td_h . $sku['stock'] . $td_f;
1915 +
1916 + $line .= $line_item . $line_sku . $lf;
1917 + }
1918 + if ( $csv_encode_type_sjis ) {
1919 + $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
1920 + }
1921 +
1922 + // This is a direct binary/CSV download response, so HTML escaping is not applicable.
1923 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1924 + print( $line );
1925 + flush();
1926 +
1927 + $line = '';
1928 + wp_cache_flush();
1929 + }
1930 +
1931 + $results = array();
1932 +
1933 + return $results;
1934 + }
1935 +
1936 + /**
1937 + * Item SKU Upload.
1938 + */
1939 + if ( ! function_exists( 'usces_item_sku_uploadcsv' ) ) :
1940 +
1941 + /**
1942 + * SKU columns Product CSV upload.
1943 + *
1944 + * @since 2.2.2
1945 + * @param string $lines 1 line of text.
1946 + * @param array $file_info Information.
1947 + */
1948 + function usces_item_sku_uploadcsv( $lines, $file_info ) {
1949 + global $wpdb, $usces;
1950 +
1951 + $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
1952 + $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
1953 + $file_name = wel_esc_upload_file_name( $file_name );
1954 +
1955 + $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
1956 + $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
1957 + $line_num = 0;
1958 + $column_num = 0;
1959 + $total_num = count( $lines );
1960 + $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
1961 + $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
1962 + $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
1963 + $work_number = 0;
1964 + $error = false;
1965 +
1966 + $yn = "\n";
1967 +
1968 + $min_field_num = 13;
1969 + $min_field_num = apply_filters( 'usces_filter_sku_uploadcsv_min_field_num', $min_field_num );
1970 +
1971 + // SKU columns.
1972 +
1973 + // Registration loop.
1974 + foreach ( $lines as $rows_num => $line ) {
1975 +
1976 + $logtemp = '';
1977 + $line = trim( $line );
1978 + if ( empty( $line ) ) {
1979 + continue;
1980 + }
1981 +
1982 + // Divide the line and store it in $datas.
1983 + $datas = usces_make_line_data( $line );
1984 +
1985 + if ( count( $datas ) !== $column_num ) {
1986 + $column_num = count( $datas );
1987 + }
1988 + $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
1989 +
1990 + if ( $min_field_num !== $column_num || ( 0 === $rows_num && 'Post ID' !== $datas[0] ) ) {
1991 + $progress = array(
1992 + 'info' => $file_info,
1993 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
1994 + 'progress' => __( 'The process was not completed', 'usces' ),
1995 + 'log' => 'Error : ' . __( 'This file may not be the item CSV for "SKU columns".', 'usces' ),
1996 + 'flag' => 'complete',
1997 + );
1998 + record_item_up_progress( $progress );
1999 + $error = true;
2000 +
2001 + $file_path = $upload_folder . $file_name;
2002 + $real_path = realpath( $file_path );
2003 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2004 + unlink( $file_path );
2005 + }
2006 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2007 + return $results;
2008 + }
2009 +
2010 + // Skip the first line.
2011 + if ( 'Post ID' === $datas[0] ) {
2012 + continue;
2013 + }
2014 +
2015 + $line_num = $rows_num + 1;
2016 +
2017 + // Split processing.
2018 + if ( $start_number > $work_number ) {
2019 + ++$work_number;
2020 + continue;
2021 + }
2022 + if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 10 ) ) && $start_number !== $work_number ) {
2023 + $progress = array(
2024 + 'info' => $file_info,
2025 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
2026 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2027 + 'i' => $line_num,
2028 + 'all' => $total_num,
2029 + 'flag' => 'continue',
2030 + 'work_number' => $work_number,
2031 + 'comp_num' => $comp_num,
2032 + 'err_num' => $err_num,
2033 + );
2034 + record_item_up_progress( $progress );
2035 + die( wp_json_encode( $progress ) );
2036 + }
2037 + ++$work_number;
2038 +
2039 + // Column check loop.
2040 + foreach ( $datas as $key => $data ) {
2041 +
2042 + $post_id = ( ! WCUtils::is_blank( $datas[0] ) ) ? (int) $datas[0] : null;
2043 + $meta_id = ( ! WCUtils::is_blank( $datas[3] ) ) ? (int) $datas[3] : null;
2044 + $product = wel_get_product( $post_id );
2045 +
2046 + switch ( $key ) {
2047 + case 0:// Post ID.
2048 + if ( $post_id ) {
2049 + if ( false === $product ) {
2050 + ++$err_num;
2051 + $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Post-ID %s does not exist in the database.', 'usces' ), $post_id );
2052 + $logtemp .= $mes . $yn;
2053 + }
2054 + } else {
2055 + ++$err_num;
2056 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Post-ID is abnormal.', 'usces' );
2057 + $logtemp .= $mes . $yn;
2058 + }
2059 + break;
2060 +
2061 + case 3:// Meta ID.
2062 + if ( $post_id && $meta_id ) {
2063 + $skus = $product['_sku'];
2064 + $meta_flag = false;
2065 + foreach ( $skus as $sku ) {
2066 + if ( $meta_id === (int) $sku['meta_id'] ) {
2067 + $meta_flag = true;
2068 + break;
2069 + }
2070 + }
2071 + if ( false === $meta_flag ) {
2072 + ++$err_num;
2073 + $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Meta ID %s does not exist in the database.', 'usces' ), $meta_id );
2074 + $logtemp .= $mes . $yn;
2075 + }
2076 + } else {
2077 + ++$err_num;
2078 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the Meta ID is abnormal.', 'usces' );
2079 + $logtemp .= $mes . $yn;
2080 + }
2081 + break;
2082 +
2083 + case 4:// SKU Code.
2084 + if ( 0 === strlen( $data ) ) {
2085 + $mes = 'No.' . $line_num . "\t" . __( 'A SKU cord is non-input.', 'usces' );
2086 + $logtemp .= $mes . $yn;
2087 + }
2088 + break;
2089 +
2090 + case 5:// SKU Name.
2091 + break;
2092 +
2093 + case 6:// Normal Price.
2094 + if ( 0 < strlen( $data ) && ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) ) {
2095 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the normal price is abnormal.', 'usces' );
2096 + $logtemp .= $mes . $yn;
2097 + }
2098 + break;
2099 +
2100 + case 7:// Sale Price.
2101 + if ( ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) || 0 === strlen( $data ) ) {
2102 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the sale price is abnormal.', 'usces' );
2103 + $logtemp .= $mes . $yn;
2104 + }
2105 + break;
2106 +
2107 + case 8:// Stock.
2108 + if ( 0 < strlen( $data ) && ! preg_match( '/^[0-9;]+$/', $data ) ) {
2109 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
2110 + $logtemp .= $mes . $yn;
2111 + }
2112 + break;
2113 +
2114 + case 9:// Stock Status.
2115 + $stock_status = apply_filters( 'usces_filter_csv_upload_check_stock_status', $data );
2116 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || $stock_status < $data ) {
2117 + $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock status is abnormal.', 'usces' );
2118 + $logtemp .= $mes . $yn;
2119 + }
2120 + break;
2121 +
2122 + case 10:// Unit.
2123 + break;
2124 +
2125 + case 11:// Apply business package.
2126 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
2127 + $mes = 'No.' . $line_num . "\t" . __( 'The value of the duties pack application is abnormal.', 'usces' );
2128 + $logtemp .= $mes . $yn;
2129 + }
2130 + break;
2131 +
2132 + case 12:// Applicable tax rate.
2133 + if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
2134 + $mes = 'No.' . $line_num . "\t" . __( 'Invalid value of Applicable tax rate.', 'usces' );
2135 + $logtemp .= $mes . $yn;
2136 + }
2137 + break;
2138 + }
2139 + }
2140 +
2141 + // End of data check.
2142 + if ( 0 < strlen( $logtemp ) ) {
2143 + ++$err_num;
2144 + $progress = array(
2145 + 'log' => $logtemp,
2146 + );
2147 + record_item_up_progress( $progress );
2148 + $error = true;
2149 + continue;
2150 + }
2151 +
2152 + if ( ! $check_mode ) {
2153 +
2154 + $post_id = (int) $datas[0];
2155 + $meta_id = (int) $datas[3];
2156 + $skus = wel_get_skus( $post_id, 'meta_id', false );
2157 + $sku = $skus[ $meta_id ];
2158 + if ( empty( $skus ) || ! isset( $skus[ $meta_id ] ) ) {
2159 + ++$err_num;
2160 + $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
2161 + $progress = array(
2162 + 'log' => $mes,
2163 + );
2164 + record_item_up_progress( $progress );
2165 + $error = true;
2166 + continue;
2167 + }
2168 +
2169 + $sku['code'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[4], 'UTF-8', 'SJIS' ) ) : trim( $datas[4] );
2170 + $sku['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[5], 'UTF-8', 'SJIS' ) ) : trim( $datas[5] );
2171 + $sku['cprice'] = $datas[6];
2172 + $sku['price'] = $datas[7];
2173 + $sku['stocknum'] = $datas[8];
2174 + $sku['stock'] = $datas[9];
2175 + $sku['unit'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[10], 'UTF-8', 'SJIS' ) ) : trim( $datas[10] );
2176 + $sku['gp'] = $datas[11];
2177 + $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[12] );
2178 +
2179 + $sku = apply_filters( 'usces_filter_sku_uploadcsv_skuvalue', $sku, $datas );
2180 +
2181 + $db_res = wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
2182 +
2183 + if ( 0 > $db_res ) {
2184 + ++$err_num;
2185 + $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
2186 + $progress = array(
2187 + 'log' => $mes,
2188 + );
2189 + record_item_up_progress( $progress );
2190 + $error = true;
2191 + continue;
2192 + }
2193 + wp_cache_delete( $post_id, 'post_meta' );
2194 + }
2195 + ++$comp_num;
2196 +
2197 + if ( 0 === ( $line_num % 10 ) ) {
2198 + $progress = array(
2199 + 'info' => $file_info,
2200 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
2201 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2202 + 'i' => $line_num,
2203 + 'all' => $total_num,
2204 + );
2205 + record_item_up_progress( $progress );
2206 + }
2207 + }
2208 +
2209 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info', 'min_field_num' );
2210 +
2211 + return $results;
2212 + }
2213 + endif;// End of SKU columns Product CSV Upload.
2214 +
2215 + /**
2216 + * Item SKU Download.
2217 + */
2218 + if ( ! function_exists( 'usces_download_item_sku_list' ) ) :
2219 +
2220 + /**
2221 + * SKU columns CSV download.
2222 + *
2223 + * @since 2.2.2
2224 + * @param string $rows Data.
2225 + * @param array $usces_opt_item Item option.
2226 + */
2227 + function usces_download_item_sku_list( $rows, $usces_opt_item ) {
2228 + global $usces;
2229 +
2230 + $ext = 'csv';
2231 + $tr_h = '';
2232 + $tr_f = '';
2233 + $th_h1 = '"';
2234 + $th_h = ',"';
2235 + $th_f = '"';
2236 + $td_h1 = '"';
2237 + $td_h = ',"';
2238 + $td_f = '"';
2239 + $sp = ';';
2240 + $eq = '=';
2241 + $lf = "\n";
2242 +
2243 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2244 +
2245 + $line = '';
2246 +
2247 + if ( 1 === (int) $usces_opt_item['chk_header'] ) {
2248 + $line .= $tr_h;
2249 + $line .= $th_h1 . 'Post ID' . $th_f;// 0.
2250 + $line .= $th_h . __( 'item code', 'usces' ) . $th_f;// 1.
2251 + $line .= $th_h . __( 'item name', 'usces' ) . $th_f;// 2.
2252 + $line .= $th_h . 'Meta ID' . $th_f;// 3.
2253 + $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;// 4.
2254 + $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;// 5.
2255 + $line .= $th_h . __( 'normal price', 'usces' ) . $th_f;// 6.
2256 + $line .= $th_h . __( 'Sale price', 'usces' ) . $th_f;// 7.
2257 + $line .= $th_h . __( 'stock', 'usces' ) . $th_f;// 8.
2258 + $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;// 9.
2259 + $line .= $th_h . __( 'unit', 'usces' ) . $th_f;// 10.
2260 + $line .= $th_h . __( 'Apply business package', 'usces' ) . $th_f;// 11.
2261 + $line .= $th_h . __( 'Applicable tax rate', 'usces' ) . $th_f;// 12.
2262 +
2263 + $line = apply_filters( 'usces_filter_downloadcsv_header_skulist', $line );
2264 +
2265 + $line .= $tr_f . $lf;
2266 + }
2267 +
2268 + while ( @ob_get_level() > 0 ) {
2269 + ob_end_clean();
2270 + }
2271 +
2272 + if ( $csv_encode_type_sjis ) {
2273 + $charset = 'Shift_JIS';
2274 + } else {
2275 + $charset = 'UTF-8';
2276 + }
2277 +
2278 + mb_http_output( 'pass' );
2279 + set_time_limit( 3600 );
2280 + header( 'Content-Type: application/octet-stream; charset=' . $charset );
2281 + header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
2282 +
2283 + if ( ! $csv_encode_type_sjis ) {
2284 + echo "\xEF\xBB\xBF";
2285 + }
2286 +
2287 + foreach ( (array) $rows as $row ) {
2288 +
2289 + $post_id = $row['ID'];
2290 + $product = wel_get_product( $post_id );
2291 +
2292 + $line_item = $td_h1 . $post_id . $td_f;
2293 + $line_item .= $td_h . $product['itemCode'] . $td_f;
2294 + $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
2295 +
2296 + $skus = $product['_sku'];
2297 + foreach ( $skus as $sku ) {
2298 + $line_sku = $td_h . $sku['meta_id'] . $td_f;
2299 + $line_sku .= $td_h . $sku['code'] . $td_f;
2300 + $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
2301 + $line_sku .= $td_h . usces_crform( $sku['cprice'], false, false, 'return', false ) . $td_f;
2302 + $line_sku .= $td_h . usces_crform( $sku['price'], false, false, 'return', false ) . $td_f;
2303 + $line_sku .= $td_h . $sku['stocknum'] . $td_f;
2304 + $line_sku .= $td_h . $sku['stock'] . $td_f;
2305 + $line_sku .= $td_h . usces_entity_decode( $sku['unit'], $ext ) . $td_f;
2306 + $line_sku .= $td_h . $sku['gp'] . $td_f;
2307 + $line_sku .= $td_h . usces_csv_get_sku_applicable_taxrate( $sku ) . $td_f;
2308 +
2309 + $line_sku = apply_filters( 'usces_filter_downloadcsv_skuvalue_skulist', $line_sku, $sku );
2310 +
2311 + $line .= $tr_h . $line_item . $line_sku . $tr_f . $lf;
2312 + }
2313 + if ( $csv_encode_type_sjis ) {
2314 + $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2315 + }
2316 +
2317 + // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2318 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2319 + print( $line );
2320 + flush();
2321 +
2322 + $line = '';
2323 + wp_cache_flush();
2324 + }
2325 +
2326 + $results = '';
2327 +
2328 + return $results;
2329 + }
2330 +
2331 + endif;// End of SKU columns Product CSV download.
2332 +
2333 + /**
2334 + * Custom field columns CSV upload.
2335 + *
2336 + * @since 2.2.2
2337 + * @param string $lines 1 line of text.
2338 + * @param array $file_info Information.
2339 + */
2340 + function usces_item_meta_uploadcsv( $lines, $file_info ) {
2341 + global $wpdb, $usces;
2342 +
2343 + $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
2344 + $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
2345 + $file_name = wel_esc_upload_file_name( $file_name );
2346 +
2347 + $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
2348 + $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
2349 + $line_num = 0;
2350 + $column_num = 0;
2351 + $total_num = count( $lines );
2352 + $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
2353 + $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
2354 + $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
2355 + $work_number = 0;
2356 + $error = false;
2357 +
2358 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2359 +
2360 + $yn = "\n";
2361 +
2362 + $labels = array();
2363 +
2364 + // Custom field columns.
2365 +
2366 + // Registration loop.
2367 + foreach ( $lines as $rows_num => $line ) {
2368 +
2369 + $logtemp = '';
2370 + $line = trim( $line );
2371 + if ( empty( $line ) ) {
2372 + continue;
2373 + }
2374 +
2375 + // Divide the line and store it in $datas.
2376 + $datas = usces_make_line_data( $line );
2377 +
2378 + if ( count( $datas ) !== $column_num ) {
2379 + $column_num = count( $datas );
2380 + }
2381 + $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
2382 + if ( 0 === $rows_num ) {
2383 + $labels = $datas;
2384 + }
2385 +
2386 + if ( 0 === (int) $rows_num ) {
2387 +
2388 + if ( 'post_id_meta' !== $datas[0] ) {
2389 + $progress = array(
2390 + 'info' => $file_info,
2391 + 'status' => __( 'forced termination', 'usces' ) . $check_label,
2392 + 'progress' => __( 'The process was not completed', 'usces' ),
2393 + 'log' => 'Error : ' . __( 'This CSV file is not for custom fields', 'usces' ),
2394 + 'flag' => 'complete',
2395 + );
2396 + record_item_up_progress( $progress );
2397 + $error = true;
2398 +
2399 + $file_path = $upload_folder . $file_name;
2400 + $real_path = realpath( $file_path );
2401 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2402 + unlink( $file_path );
2403 + }
2404 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2405 + return $results;
2406 + }
2407 +
2408 + continue;
2409 + }
2410 +
2411 + $line_num = $rows_num + 1;
2412 + $post_id = $datas[0];
2413 +
2414 + // Split processing.
2415 + if ( $start_number > $work_number ) {
2416 + ++$work_number;
2417 + ++$comp_num;
2418 + continue;
2419 + }
2420 + if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 2 ) ) && $start_number !== $work_number ) {
2421 + $progress = array(
2422 + 'info' => $file_info,
2423 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
2424 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2425 + 'i' => $line_num,
2426 + 'all' => $total_num,
2427 + 'flag' => 'continue',
2428 + 'work_number' => $work_number,
2429 + 'comp_num' => $comp_num,
2430 + 'err_num' => $err_num,
2431 + );
2432 + record_item_up_progress( $progress );
2433 + die( wp_json_encode( $progress ) );
2434 + }
2435 + ++$work_number;
2436 +
2437 + // Column check loop.
2438 + foreach ( $datas as $key => $data ) {
2439 +
2440 + // Skip reference columns.
2441 + if ( 3 > $key ) {
2442 + continue;
2443 + }
2444 +
2445 + if ( ! $check_mode ) {
2446 + $meta_key = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $labels[ $key ], 'UTF-8', 'SJIS' ) ) : trim( $labels[ $key ] );
2447 +
2448 + // delete.
2449 + $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` = %s", $post_id, $meta_key );
2450 + $db_res = $wpdb->query( $query );
2451 +
2452 + // add.
2453 + if ( 'null' !== $data ) {
2454 +
2455 + if ( false !== strpos( $data, ']][[' ) ) {
2456 +
2457 + $m = explode( ']][[', $data );
2458 + foreach ( $m as $tempval ) {
2459 + $tempval = rtrim( $tempval, ']]' );
2460 + $tempval = ltrim( $tempval, '[[' );
2461 + $meta_value = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $tempval, 'UTF-8', 'SJIS' ) ) : trim( $tempval );
2462 +
2463 + $query = $wpdb->prepare( "INSERT INTO {$wpdb->postmeta} ( `post_id`, `meta_key`, `meta_value` ) VALUES ( %d, %s, %s ) ", $post_id, $meta_key, $meta_value );
2464 + $db_res = $wpdb->query( $query, ARRAY_A );
2465 + }
2466 + } else {
2467 +
2468 + $meta_value = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $data, 'UTF-8', 'SJIS' ) ) : trim( $data );
2469 +
2470 + $query = $wpdb->prepare( "INSERT INTO {$wpdb->postmeta} ( `post_id`, `meta_key`, `meta_value` ) VALUES ( %d, %s, %s ) ", $post_id, $meta_key, $meta_value );
2471 + $db_res = $wpdb->query( $query, ARRAY_A );
2472 +
2473 + }
2474 + }
2475 + }
2476 + }
2477 +
2478 + ++$comp_num;
2479 +
2480 + if ( 0 === ( $line_num % 10 ) ) {
2481 + $progress = array(
2482 + 'info' => $file_info,
2483 + 'status' => __( 'Processing...', 'usces' ) . $check_label,
2484 + 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2485 + 'i' => $line_num,
2486 + 'all' => $total_num,
2487 + );
2488 + record_item_up_progress( $progress );
2489 + }
2490 + }
2491 +
2492 + $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2493 +
2494 + return $results;
2495 + }
2496 +
2497 + /**
2498 + * Custom field columns CSV download.
2499 + *
2500 + * @since 2.2.2
2501 + * @param string $rows Data.
2502 + * @param array $usces_opt_item Item option.
2503 + */
2504 + function usces_download_item_meta_list( $rows, $usces_opt_item ) {
2505 + global $usces;
2506 +
2507 + $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2508 +
2509 + $meta_keys = array(
2510 + 'post_id_meta' => 'null',
2511 + 'item_code' => 'null',
2512 + 'item_name' => 'null',
2513 + );
2514 +
2515 + foreach ( $rows as $row ) {
2516 +
2517 + $product = wel_get_product( $row['ID'] );
2518 + $metas = $product['_ext'];
2519 + foreach ( $metas as $key => $value ) {
2520 + if ( '' === $key || '_' === substr( $key, 0, 1 ) || is_array( wel_safe_maybe_unserialize( $key ) ) ) {
2521 + continue;
2522 + }
2523 + $meta_keys[ $key ] = 'null';
2524 + }
2525 + }
2526 +
2527 + $data = array();
2528 + foreach ( $rows as $r => $row ) {
2529 +
2530 + $product = wel_get_product( $row['ID'] );
2531 + $metas = $product['_ext'];
2532 + $new = $meta_keys;
2533 +
2534 + $new['post_id_meta'] = $product['ID'];
2535 + $new['item_code'] = $product['itemCode'];
2536 + $new['item_name'] = $product['itemName'];
2537 +
2538 + foreach ( $metas as $key => $values ) {
2539 + if ( '' === $key || '_' === substr( $key, 0, 1 ) || is_array( wel_safe_maybe_unserialize( $key ) ) ) {
2540 + continue;
2541 + }
2542 + $vc = is_array( $values ) ? count( $values ) : 0;
2543 + if ( is_array( $values ) && 1 < $vc ) {
2544 + $val = '';
2545 + foreach ( $values as $v ) {
2546 + $val .= '[[' . $v . ']]';
2547 + }
2548 + } elseif ( is_array( $values ) && 1 === $vc ) {
2549 + $val = $values[0];
2550 + } else {
2551 + $val = $values;
2552 + }
2553 + $new[ $key ] = $val;
2554 + }
2555 + $data[ $r ] = $new;
2556 + }
2557 +
2558 + $ext = 'csv';
2559 + $h = '"';
2560 + $f = '",';
2561 + $lf = "\n";
2562 +
2563 + $line = '';
2564 + foreach ( $meta_keys as $label => $lv ) {
2565 + $line .= $h . $label . $f;
2566 + }
2567 + $line = trim( $line, ',' );
2568 + $line .= $lf;
2569 +
2570 + while ( @ob_get_level() > 0 ) {
2571 + ob_end_clean();
2572 + }
2573 +
2574 + if ( $csv_encode_type_sjis ) {
2575 + $charset = 'Shift_JIS';
2576 + } else {
2577 + $charset = 'UTF-8';
2578 + }
2579 +
2580 + mb_http_output( 'pass' );
2581 + set_time_limit( 3600 );
2582 + header( 'Content-Type: application/octet-stream; charset=' . $charset );
2583 + header( 'Content-Disposition: attachment; filename=usces_item_meta.' . $ext );
2584 +
2585 + if ( ! $csv_encode_type_sjis ) {
2586 + echo "\xEF\xBB\xBF";
2587 + }
2588 +
2589 + if ( $csv_encode_type_sjis ) {
2590 + $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2591 + }
2592 +
2593 + // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2594 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2595 + print( $line );
2596 +
2597 + foreach ( $data as $d ) {
2598 + $line = '';
2599 +
2600 + foreach ( $d as $dv ) {
2601 + $line .= $h . str_replace( '"', '""', $dv ) . $f;
2602 + }
2603 + $line = trim( $line, ',' );
2604 + $line .= $lf;
2605 +
2606 + if ( $csv_encode_type_sjis ) {
2607 + $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2608 + }
2609 +
2610 + // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2611 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2612 + print( $line );
2613 + flush();
2614 +
2615 + }
2616 + exit();
2617 + }
2618 +}
2619 +
2620 +/**
2621 + * Explode one line of CSV to generate data.
2622 + *
2623 + * @since 2.2.2
2624 + * @param string $line A line of CSV.
2625 + * @return array
2626 + */
2627 +function usces_make_line_data( $line ) {
2628 +
2629 + if ( 0 === strpos( $line, "\xEF\xBB\xBF" ) ) {
2630 + $line = substr( $line, 3 );
2631 + }
2632 +
2633 + $datas = array();
2634 + $array = explode( ',', $line );
2635 + $buffer = '';
2636 + $sp = ',';
2637 +
2638 + foreach ( $array as $data ) {
2639 +
2640 + $num = substr_count( $data, '"' );
2641 + if ( 0 === ( $num % 2 ) && '' === $buffer ) {
2642 +
2643 + if ( '"' === substr( $data, 0, 1 ) ) {
2644 + $data = substr( $data, 1 );
2645 + }
2646 + if ( '"' === substr( $data, -1 ) ) {
2647 + $data = substr( $data, 0, -1 );
2648 + }
2649 + $data = str_replace( array( '""' ), '"', $data );
2650 + $datas[] = ( false !== $data ) ? $data : '';
2651 +
2652 + } elseif ( 1 === ( $num % 2 ) && '' === $buffer ) {
2653 +
2654 + $buffer .= $data;
2655 +
2656 + } elseif ( 0 === ( $num % 2 ) && '' !== $buffer ) {
2657 +
2658 + $buffer .= $sp . $data;
2659 +
2660 + } elseif ( 1 === ( $num % 2 ) && '' !== $buffer ) {
2661 +
2662 + $buffer .= $sp . $data;
2663 +
2664 + if ( '"' === substr( $buffer, 0, 1 ) ) {
2665 + $buffer = substr( $buffer, 1 );
2666 + }
2667 + if ( '"' === substr( $buffer, -1 ) ) {
2668 + $buffer = substr( $buffer, 0, -1 );
2669 + }
2670 + $buffer = str_replace( array( '""' ), '"', $buffer );
2671 + $datas[] = ( false !== $buffer ) ? $buffer : '';
2672 + $buffer = '';
2673 + }
2674 + }
2675 + return $datas;
2676 +}
2677 +
2678 +/**
2679 + * Product custom field acquisition function.
2680 + *
2681 + * @since 2.2.2
2682 + * @return array
2683 + */
2684 +function usces_get_item_custom_fields() {
2685 + $item_custom_fields = apply_filters( 'usces_filter_item_custom_fields', array( '_itemOrderAcceptable' ) );
2686 + return $item_custom_fields;
2687 +}
2688 +
2689 +/**
2690 + * Product custom field value acquisition function.
2691 + *
2692 + * @since 2.2.2
2693 + * @param string $key Custom field key.
2694 + * @param array $custom_field Custom field.
2695 + * @return string
2696 + */
2697 +function usces_get_item_custom_field_value( $key, $custom_field ) {
2698 + global $usces;
2699 +
2700 + $cf_sp = ';;'; // custom field separator.
2701 +
2702 + $value = '';
2703 + $cfdata = array();
2704 + $cfrows = explode( $cf_sp, $custom_field );
2705 +
2706 + foreach ( $cfrows as $cfindex => $row ) {
2707 + if ( false !== strpos( $row, '=' ) ) {
2708 + $cfdata[] = $row;
2709 + } else {
2710 + $cfdend = count( $cfdata ) - 1;
2711 + if ( $cfdend && 0 <= $cfdend ) {
2712 + $cfdata[ $cfdend ] = $cfdata[ $cfdend ] . ';' . $row;
2713 + }
2714 + }
2715 + }
2716 +
2717 + foreach ( $cfdata as $row ) {
2718 + $cf = explode( '=', $row );
2719 + if ( ! WCUtils::is_blank( $cf[0] ) ) {
2720 + if ( $key == $cf[0] ) {
2721 + $value = $cf[1];
2722 + break;
2723 + }
2724 + }
2725 + }
2726 + return trim( $value );
2727 +}
2728 +
2729 +/**
2730 + * Applicable taxrate flag.
2731 + *
2732 + * @since 2.2.2
2733 + * @param array $sku SKU.
2734 + * @return int
2735 + */
2736 +function usces_csv_get_sku_applicable_taxrate( $sku = array() ) {
2737 + $taxrate = '0';
2738 + if ( isset( $sku['taxrate'] ) && ! empty( $sku['taxrate'] ) ) {
2739 + $taxrate = ( 'reduced' === $sku['taxrate'] ) ? '1' : '0';
2740 + }
2741 + return $taxrate;
2742 +}
2743 +
2744 +/**
2745 + * Applicable taxrate string.
2746 + *
2747 + * @since 2.2.2
2748 + * @param int $taxrate Taxrate.
2749 + * @return string
2750 + */
2751 +function usces_csv_set_sku_applicable_taxrate( $taxrate = 0 ) {
2752 + $value = ( 1 === (int) $taxrate ) ? 'reduced' : 'standard';
2753 + return $value;
2754 +}
2755 +
2756 +/**
2757 + * Item code duplication check.
2758 + *
2759 + * @since 2.2.2
2760 + * @return boolean
2761 + */
2762 +function usces_item_code_duplication_check() {
2763 + global $wpdb;
2764 +
2765 + $item_table = usces_get_tablename( 'usces_item' );
2766 +
2767 + $query = $wpdb->prepare(
2768 + "SELECT `post_id`, `itemCode` FROM {$item_table}
2769 + LEFT JOIN {$wpdb->posts} ON `ID` = `post_id`
2770 + WHERE `post_type` = %s AND `post_status` IN ('pending', 'publish', 'draft', 'private', 'future')
2771 + GROUP BY `itemCode` HAVING COUNT(*) > 1",
2772 + 'post'
2773 + );
2774 + $res = $wpdb->get_results( $query, ARRAY_A );
2775 + return $res;
2776 +}
2777 +
2778 +/**
2779 + * Record progress.
2780 + *
2781 + * @since 2.2.2
2782 + * @param array $arr_content Content.
2783 + */
2784 +function record_item_up_progress( $arr_content ) {
2785 +
2786 + $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
2787 + $mkdir = wp_mkdir_p( $upload_folder );
2788 +
2789 + if ( $mkdir ) {
2790 + if ( isset( $arr_content['status'] ) || isset( $arr_content['progress'] ) ) {
2791 + $progress_file = $upload_folder . 'progress.txt';
2792 + file_put_contents( $progress_file, wp_json_encode( $arr_content ) );
2793 + }
2794 + if ( isset( $arr_content['log'] ) ) {
2795 + $file_path = $upload_folder . 'log.txt';
2796 + $real_path = realpath( $file_path );
2797 + if ( 'clear' === $arr_content['log'] ) {
2798 + if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2799 + unlink( $file_path );
2800 + }
2801 + } else {
2802 + $add_text = $arr_content['log'] . "\n";
2803 + file_put_contents( $file_path, $add_text, FILE_APPEND | LOCK_EX );
2804 + }
2805 + }
2806 + }
2807 +}
2808 +
2809 +/**
2810 + * Upload mode name.
2811 + *
2812 + * @since 2.2.2
2813 + * @param string $upload_mode Upload mode.
2814 + * @return string $mode_name Mode name.
2815 + */
2816 +function usces_get_upmode_name( $upload_mode ) {
2817 + switch ( $upload_mode ) {
2818 + case 'all':
2819 + $mode_name = __( 'All columns', 'usces' );
2820 + break;
2821 + case 'stock':
2822 + $mode_name = __( 'Stock columns', 'usces' );
2823 + break;
2824 + case 'sku':
2825 + $mode_name = __( 'SKU columns', 'usces' );
2826 + break;
2827 + case 'meta':
2828 + $mode_name = __( 'Custom Field columns', 'usces' );
2829 + break;
2830 + default:
2831 + $mode_name = '';
2832 + }
2833 + return $mode_name;
2834 +}
2835 +
2836 +/**
2837 + * Delete skus by Post ID and Sort number.
2838 + *
2839 + * @since 2.8.11
2840 + * @param string $post_id Post ID.
2841 + * @param int $sort Sort Number.
2842 + * @return mixed $res success:delete row number/error:false.
2843 + */
2844 +function wel_del_skus_by_postid_and_sort( $post_id, $sort = 0 ) {
2845 + global $wpdb;
2846 +
2847 + $table_name = usces_get_tablename( 'usces_skus' );
2848 + $query = $wpdb->prepare( "DELETE FROM {$table_name} WHERE `post_id` = %d AND `sort` > %d", $post_id, $sort );
2849 + $res = $wpdb->query( $query );
2850 +
2851 + return $res;
2852 +}