PluginProbe
Welcart e-Commerce / 2.11.31
Welcart e-Commerce v2.11.31
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
usc-e-shop / functions / define_function.php

define_function.php in Welcart e-Commerce 2.11.31, at functions/define_function.php

2,850 lines 104.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ( %s ) AND post_id = %d", implode( "','", $meta_key_table ), $post_id );
1000 $query = stripslashes( $query );
1001 $db_res = $wpdb->query( $query );
1002 if ( false === $db_res ) {
1003 ++$err_num;
1004 $pre_code = $item_code;
1005
1006 $mes = 'No.' . $line_num . "\t" . __( 'Error : delete postmeta', 'usces' );
1007 $progress = array(
1008 'log' => $mes,
1009 );
1010 record_item_up_progress( $progress );
1011 $error = true;
1012 continue;
1013 }
1014
1015 // Delete Item wcct.
1016 $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s AND post_id = %d", 'wccs_%', $post_id );
1017 $db_res = $wpdb->query( $query );
1018 if ( false === $db_res ) {
1019 ++$err_num;
1020 $pre_code = $item_code;
1021
1022 $mes = 'No.' . $line_num . "\t" . __( 'Error : delete wcct', 'usces' );
1023 $progress = array(
1024 'log' => $mes,
1025 );
1026 record_item_up_progress( $progress );
1027 $error = true;
1028 continue;
1029 }
1030
1031 // Delete Item revisions.
1032 $query = $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s", $post_id, 'revision' );
1033 $db_res = $wpdb->query( $query );
1034 if ( false === $db_res ) {
1035 ++$err_num;
1036 $pre_code = $item_code;
1037
1038 $mes = 'No.' . $line_num . "\t" . __( 'Error : delete revisions', 'usces' );
1039 $progress = array(
1040 'log' => $mes,
1041 );
1042 record_item_up_progress( $progress );
1043 $error = true;
1044 continue;
1045 }
1046
1047 // publish_future_post.
1048 if ( 'future' === $datas[ USCES_COL_POST_STATUS ] && $cdatas['post_date'] > current_time( 'Y-m-d H:i:s' ) ) {
1049 wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) );
1050 wp_schedule_single_event( strtotime( get_gmt_from_date( $cdatas['post_date'] ) . ' GMT' ), 'publish_future_post', array( $post_id ) );
1051 }
1052
1053 // addMeta.
1054 // Add postmeta.
1055 if ( 'add' === $mode ) {
1056 $wel_item = new Welcart\ItemData( $post_id, false );
1057 $item = $wel_item->get_item_format();
1058 $item['post_id'] = $post_id;
1059 } elseif ( 'upd' === $mode ) {
1060 $item = Wel_get_item( $post_id, false );
1061 }
1062
1063 $item_delivery_method = explode( ';', $datas[ USCES_COL_ITEM_DELIVERYMETHOD ] );
1064
1065 $item['itemCode'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_CODE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_CODE ] );
1066 $item['itemName'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_NAME ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_NAME ] );
1067 $item['itemRestriction'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_RESTRICTION ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_RESTRICTION ] );
1068 $item['itemPointrate'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_POINTRATE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_POINTRATE ] );
1069 $item['itemGpNum1'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM1 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM1 ] );
1070 $item['itemGpDis1'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS1 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS1 ] );
1071 $item['itemGpNum2'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM2 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM2 ] );
1072 $item['itemGpDis2'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS2 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS2 ] );
1073 $item['itemGpNum3'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPNUM3 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPNUM3 ] );
1074 $item['itemGpDis3'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_GPDIS3 ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_GPDIS3 ] );
1075 $item['itemShipping'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_SHIPPING ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_SHIPPING ] );
1076 $item['itemDeliveryMethod'] = $item_delivery_method;
1077 $item['itemShippingCharge'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_SHIPPINGCHARGE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_SHIPPINGCHARGE ] );
1078 $item['itemIndividualSCharge'] = $csv_encode_type_sjis ? trim( mb_convert_encoding( $datas[ USCES_COL_ITEM_INDIVIDUALSCHARGE ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ USCES_COL_ITEM_INDIVIDUALSCHARGE ] );
1079 $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 ] );
1080
1081 wel_update_item_data( $item, $post_id );
1082
1083 // add term_relationships, edit term_taxonomy.
1084 // Category.
1085 if ( $category_format_slug ) {
1086 $categories = array();
1087 $category_slug = explode( ';', $datas[ USCES_COL_CATEGORY ] );
1088 foreach ( (array) $category_slug as $slug ) {
1089 $categories[] = usces_get_cat_id( $slug );
1090 }
1091 } else {
1092 $categories = explode( ';', $datas[ USCES_COL_CATEGORY ] );
1093 }
1094 wp_set_post_categories( $post_id, $categories );
1095 wp_update_term_count( $categories, 'category' );
1096
1097 // Tag.
1098 $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 ] ) );
1099 wp_set_post_tags( $post_id, $tags );
1100
1101 // Add Custom Field.
1102 if ( is_array( $cfdata ) && 0 <= count( $cfdata ) ) {
1103 reset( $cfdata );
1104 $cfstr = '';
1105
1106 foreach ( $cfdata as $row ) {
1107 preg_match( '/^([^=]+)=([\s\S]*)$/m', $row, $cf );
1108 if ( isset( $cf[1] ) && ! WCUtils::is_blank( $cf[1] ) ) {
1109 $cfstr .= '(' . $post_id . ", '" . esc_sql( $cf[1] ) . "','" . esc_sql( $cf[2] ) . "'),";
1110 }
1111 }
1112
1113 if ( ! WCUtils::is_blank( $cfstr ) ) {
1114 $cfstr = rtrim( $cfstr, ',' );
1115 $db_res = $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES {$cfstr}" );
1116 }
1117 }
1118 do_action( 'usces_action_uploadcsv_itemvalue', $post_id, $datas, $add_field_num );
1119
1120 // addOption.
1121 // Add Item Option.
1122 wel_delete_all_opt_data( $post_id );
1123
1124 for ( $i = 0; $i < $opnum; $i++ ) {
1125 $opflg = true;
1126 $optvalue = array();
1127 for ( $o = 1; $o <= 4; $o++ ) {
1128 $key = ( $min_field_num - 1 ) + $o + ( $i * 4 );
1129 if ( 1 === $o && '' === $datas[ $key ] ) {
1130 $opflg = false;
1131 break 1;
1132 }
1133 switch ( $o ) {
1134 case 1:
1135 $optvalue['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[ $key ], 'UTF-8', 'SJIS' ) ) : trim( $datas[ $key ] );
1136 break;
1137 case 2:
1138 $optvalue['means'] = (int) $datas[ $key ];
1139 break;
1140 case 3:
1141 $optvalue['essential'] = (int) $datas[ $key ];
1142 break;
1143 case 4:
1144 if ( ! empty( $datas[ $key ] ) ) {
1145 $cr = array( "\r\n", "\r" );
1146 $datavalue = trim( $datas[ $key ] );
1147 $datavalue = str_replace( $cr, '', $datavalue );
1148 $optvalue['value'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? str_replace( ';', "\n", mb_convert_encoding( $datavalue, 'UTF-8', 'SJIS' ) ) : str_replace( ';', "\n", $datavalue );
1149 } else {
1150 $optvalue['value'] = '';
1151 }
1152 break;
1153 }
1154 }
1155
1156 if ( $opflg && ! empty( $optvalue ) ) {
1157
1158 $optvalue['sort'] = $i;
1159
1160 $resopt = wel_add_opt_data( $post_id, $optvalue );
1161 }
1162 }
1163 } else {
1164 ++$sku_index;
1165 }
1166
1167 // addSku.
1168 // Add Item SKU.
1169
1170 $skus = wel_get_skus( $post_id, 'sort', false );
1171 $sku = isset( $skus[ $sku_index ] ) ? $skus[ $sku_index ] : false;
1172
1173 if ( false === $sku ) {
1174
1175 $sku = array();
1176 $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 ] );
1177 $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 ] );
1178 $sku['cprice'] = $datas[ USCES_COL_SKU_CPRICE ];
1179 $sku['price'] = $datas[ USCES_COL_SKU_PRICE ];
1180 $sku['stocknum'] = $datas[ USCES_COL_SKU_ZAIKONUM ];
1181 $sku['stock'] = $datas[ USCES_COL_SKU_ZAIKO ];
1182 $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 ] );
1183 $sku['gp'] = $datas[ USCES_COL_SKU_GPTEKIYO ];
1184 $sku['sort'] = $sku_index;
1185 $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[ USCES_COL_SKU_APPLICABLE_TAXRATE ] );
1186
1187 $sku = apply_filters( 'usces_filter_uploadcsv_skuvalue', $sku, $datas );
1188
1189 wel_add_sku_data( $post_id, $sku );
1190
1191 } else {
1192
1193 $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 ] );
1194 $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 ] );
1195 $sku['cprice'] = $datas[ USCES_COL_SKU_CPRICE ];
1196 $sku['price'] = $datas[ USCES_COL_SKU_PRICE ];
1197 $sku['stocknum'] = $datas[ USCES_COL_SKU_ZAIKONUM ];
1198 $sku['stock'] = $datas[ USCES_COL_SKU_ZAIKO ];
1199 $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 ] );
1200 $sku['gp'] = $datas[ USCES_COL_SKU_GPTEKIYO ];
1201 $sku['sort'] = $sku_index;
1202 $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[ USCES_COL_SKU_APPLICABLE_TAXRATE ] );
1203
1204 $sku = apply_filters( 'usces_filter_uploadcsv_skuvalue', $sku, $datas );
1205
1206 $meta_id = $sku['meta_id'];
1207 wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1208
1209 }
1210 } else {
1211 if ( $pre_code !== $item_code ) {
1212 $pre_post_id = ! empty( wel_get_id_by_item_code( $pre_code, false ) ) ? wel_get_id_by_item_code( $pre_code, false ) : $post_id;
1213 $skus = wel_get_skus( $pre_post_id, 'sort', false );
1214 } else {
1215 $skus = array();
1216 }
1217 }
1218 $pre_code = $item_code;
1219 $pre_skus_count = count( (array) $skus );
1220 ++$comp_num;
1221
1222 do_action(
1223 'usces_after_uploadcsv_line_processed',
1224 array(
1225 'line' => $line,
1226 'datas' => $datas,
1227 'check_mode' => $check_mode,
1228 'post_id' => $post_id,
1229 'sku' => ! empty( $sku ) ? $sku : null,
1230 )
1231 );
1232
1233 // Status update.
1234 if ( 0 === ( $line_num % 10 ) ) {
1235 $progress = array(
1236 'info' => $file_info,
1237 'status' => __( 'Processing...', 'usces' ) . $check_label,
1238 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1239 'i' => $line_num,
1240 'all' => $total_num,
1241 );
1242 record_item_up_progress( $progress );
1243 }
1244 }
1245
1246 if ( ! $check_mode ) {
1247 if ( isset( $pre_skus_count ) && $pre_skus_count > 0 && $pre_skus_count > $sku_index + 1 && ! empty( $post_id ) ) {
1248 wel_del_skus_by_postid_and_sort( $post_id, $sku_index );
1249 }
1250 }
1251 }
1252
1253 do_action(
1254 'usces_after_uploadcsv_lines_processed',
1255 array(
1256 'lines' => $lines,
1257 'datas' => $datas,
1258 'check_mode' => $check_mode,
1259 'error' => $error,
1260 )
1261 );
1262
1263 // Final status.
1264 if ( $error ) {
1265 $progress = array(
1266 'info' => $file_info,
1267 'status' => __( 'End (with error)', 'usces' ) . $check_label,
1268 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1269 'i' => $line_num,
1270 'all' => $total_num,
1271 'flag' => 'complete',
1272 );
1273 record_item_up_progress( $progress );
1274 } else {
1275 $progress = array(
1276 'info' => $file_info,
1277 'status' => __( 'End', 'usces' ) . $check_label,
1278 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1279 'log' => __( 'No abnormality', 'usces' ),
1280 'i' => $line_num,
1281 'all' => $total_num,
1282 'flag' => 'complete',
1283 );
1284 record_item_up_progress( $progress );
1285 }
1286 $file_path = $upload_folder . $file_name;
1287 $real_path = realpath( $file_path );
1288 if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
1289 unlink( $file_path );
1290 }
1291 die( wp_json_encode( $progress ) );
1292 }
1293 endif;// End of All columns Product CSV upload.
1294
1295 if ( ! function_exists( 'usces_download_item_list' ) ) :
1296
1297 /**
1298 * All columns Product CSV download.
1299 *
1300 * @since 2.2.2
1301 */
1302 function usces_download_item_list() {
1303 global $wpdb, $usces;
1304
1305 require_once USCES_PLUGIN_DIR . '/classes/itemList.class.php';
1306
1307 $ext = 'csv';
1308 $th_h1 = '"';
1309 $th_h = ',"';
1310 $th_f = '"';
1311 $td_h1 = '"';
1312 $td_h = ',"';
1313 $td_f = '"';
1314 $sp = ';';
1315 $cf_sp = ';;'; // custom field separator.
1316 $eq = '=';
1317 $lf = "\n";
1318
1319 // Save the selection status of download columns.
1320 $usces_opt_item = get_option( 'usces_opt_item' );
1321 if ( ! is_array( $usces_opt_item ) ) {
1322 $usces_opt_item = array();
1323 }
1324 $usces_opt_item['chk_header'] = ( isset( $_REQUEST['chk_header'] ) ) ? 1 : 0;
1325 $usces_opt_item['ftype_item'] = $ext;
1326 update_option( 'usces_opt_item', $usces_opt_item );
1327
1328 // Get data.
1329 $table_name = $wpdb->posts;
1330 $arr_column = array(
1331 __( 'Post ID', 'usces' ) => 'post_id',
1332 __( 'item code', 'usces' ) => 'item_code',
1333 __( 'item name', 'usces' ) => 'item_name',
1334 __( 'SKU code', 'usces' ) => 'sku_key',
1335 __( 'selling price', 'usces' ) => 'price',
1336 __( 'stock', 'usces' ) => 'zaiko_num',
1337 __( 'stock status', 'usces' ) => 'zaiko',
1338 __( 'Categories', 'usces' ) => 'category',
1339 __( 'display status', 'usces' ) => 'display_status',
1340 );
1341
1342 $_REQUEST['searchIn'] = 'searchIn';
1343 $dt = new dataList( $table_name, $arr_column );
1344 $dt->pageLimit = 'off';
1345 $dt->exportMode = true;
1346 $res = $dt->MakeTable();
1347 $rows = $dt->rows;
1348
1349 // Processing branch for each mode.
1350 $results = apply_filters( 'usces_filter_item_downloadcsv_mode', array(), $rows, $usces_opt_item );
1351 if ( ! empty( $results ) ) {
1352
1353 extract( $results );
1354 } elseif ( isset( $_REQUEST['mode'] ) && 'stock' === $_REQUEST['mode'] ) {
1355
1356 $results = usces_download_item_stock_list( $rows, $usces_opt_item );
1357 if ( ! empty( $results ) ) {
1358 extract( $results );
1359 }
1360 } elseif ( isset( $_REQUEST['mode'] ) && 'sku' === $_REQUEST['mode'] ) {
1361
1362 $results = usces_download_item_sku_list( $rows, $usces_opt_item );
1363 if ( ! empty( $results ) ) {
1364 extract( $results );
1365 }
1366 } elseif ( isset( $_REQUEST['mode'] ) && 'meta' === $_REQUEST['mode'] ) {
1367
1368 $results = usces_download_item_meta_list( $rows, $usces_opt_item );
1369 if ( ! empty( $results ) ) {
1370 extract( $results );
1371 }
1372 } else {
1373
1374 $line = '';
1375
1376 // Heading.
1377 if ( $usces_opt_item['chk_header'] == 1 ) {
1378 $line .= $th_h1 . 'Post ID' . $th_f;
1379 $line .= $th_h . __( 'Post Author', 'usces' ) . $th_f;
1380 $line .= $th_h . __( 'explanation', 'usces' ) . $th_f;
1381 $line .= $th_h . __( 'Title', 'usces' ) . $th_f;
1382 $line .= $th_h . __( 'excerpt', 'usces' ) . $th_f;
1383 $line .= $th_h . __( 'display status', 'usces' ) . $th_f;
1384 $line .= $th_h . __( 'Comment Status', 'usces' ) . $th_f;
1385 $line .= $th_h . __( 'Post Password', 'usces' ) . $th_f;
1386 $line .= $th_h . __( 'Post Name', 'usces' ) . $th_f;
1387 $line .= $th_h . __( 'Publish date', 'usces' ) . $th_f;
1388
1389 $line .= $th_h . __( 'item code', 'usces' ) . $th_f;
1390 $line .= $th_h . __( 'item name', 'usces' ) . $th_f;
1391 $line .= $th_h . __( 'Limited amount for purchase', 'usces' ) . $th_f;
1392 $line .= $th_h . __( 'Percentage of points', 'usces' ) . $th_f;
1393 $line .= $th_h . __( 'Business package discount', 'usces' ) . '1-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '1-' . __( 'rate', 'usces' ) . $th_f;
1394 $line .= $th_h . __( 'Business package discount', 'usces' ) . '2-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '2-' . __( 'rate', 'usces' ) . $th_f;
1395 $line .= $th_h . __( 'Business package discount', 'usces' ) . '3-' . __( 'num', 'usces' ) . $th_f . $th_h . __( 'Business package discount', 'usces' ) . '3-' . __( 'rate', 'usces' ) . $th_f;
1396 $line .= $th_h . __( 'Sold out limit', 'usces' ) . $th_f;
1397
1398 $line .= $th_h . __( 'estimated shipping date', 'usces' ) . $th_f;
1399 $line .= $th_h . __( 'shipping option', 'usces' ) . $th_f;
1400 $line .= $th_h . __( 'Shipping', 'usces' ) . $th_f;
1401 $line .= $th_h . __( 'Postage individual charging', 'usces' ) . $th_f;
1402
1403 $line .= $th_h . __( 'Categories', 'usces' ) . $th_f;
1404 $line .= $th_h . __( 'tag', 'usces' ) . $th_f;
1405 $line .= $th_h . __( 'Custom Field', 'usces' ) . $th_f;
1406
1407 $line .= apply_filters( 'usces_filter_downloadcsv_itemheader', '' );
1408 $line = apply_filters( 'usces_filter_downloadcsv_add_itemheader', $line );
1409
1410 $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;
1411 $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;
1412 $line .= $th_h . __( 'normal price', 'usces' ) . $th_f;
1413 $line .= $th_h . __( 'Sale price', 'usces' ) . $th_f;
1414 $line .= $th_h . __( 'stock', 'usces' ) . $th_f;
1415 $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;
1416 $line .= $th_h . __( 'unit', 'usces' ) . $th_f;
1417 $line .= $th_h . __( 'Apply business package', 'usces' ) . $th_f;
1418 $line .= $th_h . __( 'Applicable tax rate', 'usces' ) . $th_f;
1419
1420 $line .= apply_filters( 'usces_filter_downloadcsv_header', '' );
1421 $line = apply_filters( 'usces_filter_downloadcsv_add_header', $line );
1422 $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;
1423 $line .= $lf;
1424 }
1425
1426 $category_format_slug = ( isset( $usces->options['system']['csv_category_format'] ) && 1 === (int) $usces->options['system']['csv_category_format'] ) ? true : false;
1427 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1428
1429 while ( @ob_get_level() > 0 ) {
1430 ob_end_clean();
1431 }
1432
1433 if ( $csv_encode_type_sjis ) {
1434 $charset = 'Shift_JIS';
1435 } else {
1436 $charset = 'UTF-8';
1437 }
1438
1439 mb_http_output( 'pass' );
1440 set_time_limit( 3600 );
1441 header( 'Content-Type: application/octet-stream; charset=' . $charset );
1442 header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
1443
1444 if ( ! $csv_encode_type_sjis ) {
1445 echo "\xEF\xBB\xBF";
1446 }
1447
1448 // Outupt data.
1449 foreach ( (array) $rows as $row ) {
1450
1451 $post_id = $row['ID'];
1452 $product = wel_get_product( $post_id );
1453 $post = $product['_pst'];
1454
1455 // Post Data.
1456 $line_item = $td_h1 . $product['ID'] . $td_f;
1457 $line_item .= $td_h . $post->post_author . $td_f;
1458 $line_item .= $td_h . usces_entity_decode( $post->post_content, $ext ) . $td_f;
1459 $line_item .= $td_h . usces_entity_decode( $post->post_title, $ext ) . $td_f;
1460 $line_item .= $td_h . usces_entity_decode( $post->post_excerpt, $ext ) . $td_f;
1461 $line_item .= $td_h . $post->post_status . $td_f;
1462 $line_item .= $td_h . $post->comment_status . $td_f;
1463 $line_item .= $td_h . $post->post_password . $td_f;
1464 $line_item .= $td_h . urldecode( $post->post_name ) . $td_f;
1465 $line_item .= $td_h . $post->post_date . $td_f;
1466
1467 // Item Meta.
1468 $line_item .= $td_h . $product['itemCode'] . $td_f;
1469 $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
1470 $line_item .= $td_h . $product['itemRestriction'] . $td_f;
1471 $line_item .= $td_h . $product['itemPointrate'] . $td_f;
1472 $line_item .= $td_h . $product['itemGpNum1'] . $td_f . $td_h . $product['itemGpDis1'] . $td_f;
1473 $line_item .= $td_h . $product['itemGpNum2'] . $td_f . $td_h . $product['itemGpDis2'] . $td_f;
1474 $line_item .= $td_h . $product['itemGpNum3'] . $td_f . $td_h . $product['itemGpDis3'] . $td_f;
1475 $line_item .= $td_h . $product['itemOrderAcceptable'] . $td_f;
1476 $line_item .= $td_h . $product['itemShipping'] . $td_f;
1477
1478 $delivery_method = '';
1479 $item_delivery_method = $product['itemDeliveryMethod'];
1480 foreach ( (array) $item_delivery_method as $k => $v ) {
1481 $delivery_method .= $v . $sp;
1482 }
1483 $delivery_method = rtrim( $delivery_method, $sp );
1484
1485 $line_item .= $td_h . $delivery_method . $td_f;
1486 $line_item .= $td_h . $product['itemShippingCharge'] . $td_f;
1487 $line_item .= $td_h . $product['itemIndividualSCharge'] . $td_f;
1488
1489 // Categories.
1490 $category = '';
1491 $cat_ids = wp_get_post_categories( $post_id );
1492 if ( ! empty( $cat_ids ) ) {
1493 if ( $category_format_slug ) {
1494 foreach ( $cat_ids as $id ) {
1495 $cat = get_category( $id );
1496 $category .= $cat->slug . $sp;
1497 }
1498 } else {
1499 foreach ( $cat_ids as $id ) {
1500 $category .= $id . $sp;
1501 }
1502 }
1503 $category = rtrim( $category, $sp );
1504 }
1505 $line_item .= $td_h . $category . $td_f;
1506
1507 // Tags.
1508 $tag = '';
1509 $tags_ob = wp_get_object_terms( $post_id, 'post_tag' );
1510 foreach ( $tags_ob as $ob ) {
1511 $tag .= $ob->name . $sp;
1512 }
1513 $tag = rtrim( $tag, $sp );
1514
1515 $line_item .= $td_h . $tag . $td_f;
1516
1517 // Custom Fields.
1518 $cfield = '';
1519 $custom_fields = wel_get_extra_data( $post_id );
1520
1521 if ( $custom_fields && is_array( $custom_fields ) && 0 < count( $custom_fields ) ) {
1522 foreach ( $custom_fields as $cfkey => $cfvalues ) {
1523 if ( '_itemOrderAcceptable' === $cfkey ) {
1524 continue;
1525 }
1526 if ( is_array( $cfvalues ) ) {
1527 foreach ( $cfvalues as $value ) {
1528 $cfield .= usces_entity_decode( $cfkey, $ext ) . $eq . usces_entity_decode( $value, $ext ) . $cf_sp;
1529 }
1530 } else {
1531 $cfield .= usces_entity_decode( $cfkey, $ext ) . $eq . usces_entity_decode( $cfvalues, $ext ) . $cf_sp;
1532 }
1533 }
1534 $cfield = rtrim( $cfield, $sp );
1535 }
1536 $line_item .= $td_h . $cfield . $td_f;
1537
1538 $line_item .= apply_filters( 'usces_filter_downloadcsv_itemvalue', '', $post_id );
1539 $line_item = apply_filters( 'usces_filter_downloadcsv_add_itemvalue', $line_item, $post_id, $post );
1540
1541 // Item Options.
1542 $line_options = '';
1543 $opts = $product['_opt'];
1544
1545 foreach ( $opts as $opt ) {
1546 $value = '';
1547
1548 if ( is_array( $opt['value'] ) ) {
1549 foreach ( $opt['value'] as $v ) {
1550 $v = usces_change_line_break( $v );
1551 $values = explode( "\n", $v );
1552 foreach ( $values as $val ) {
1553 $value .= $val . $sp;
1554 }
1555 }
1556 $value = rtrim( $value, $sp );
1557
1558 } else {
1559 $value = usces_change_line_break( $opt['value'] );
1560 $value = str_replace( "\n", ';', $value );
1561 }
1562 $line_options .= $td_h . usces_entity_decode( $opt['name'], $ext ) . $td_f;
1563 $line_options .= $td_h . $opt['means'] . $td_f;
1564 $line_options .= $td_h . $opt['essential'] . $td_f;
1565 $line_options .= $td_h . usces_entity_decode( $value, $ext ) . $td_f;
1566 }
1567
1568 // SKU.
1569 $skus = $product['_sku'];
1570 foreach ( $skus as $sku ) {
1571 $line_sku = $td_h . $sku['code'] . $td_f;
1572 $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
1573 $line_sku .= $td_h . usces_crform( $sku['cprice'], false, false, 'return', false ) . $td_f;
1574 $line_sku .= $td_h . usces_crform( $sku['price'], false, false, 'return', false ) . $td_f;
1575 $line_sku .= $td_h . $sku['stocknum'] . $td_f;
1576 $line_sku .= $td_h . $sku['stock'] . $td_f;
1577 $line_sku .= $td_h . usces_entity_decode( $sku['unit'], $ext ) . $td_f;
1578 $line_sku .= $td_h . $sku['gp'] . $td_f;
1579 $line_sku .= $td_h . usces_csv_get_sku_applicable_taxrate( $sku ) . $td_f;
1580
1581 $line_sku .= apply_filters( 'usces_filter_downloadcsv_skuvalue', '', $sku );
1582 $line_sku = apply_filters( 'usces_filter_downloadcsv_add_skuvalue', $line_sku, $sku );
1583
1584 $line .= $line_item . $line_sku . $line_options . $lf;
1585 }
1586 if ( $csv_encode_type_sjis ) {
1587 $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
1588 }
1589
1590 // This is a direct binary/CSV download response, so HTML escaping is not applicable.
1591 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1592 print( $line );
1593 flush();
1594
1595 $line = '';
1596 wp_cache_flush();
1597 }
1598 }
1599
1600 unset( $rows, $dt, $line, $line_item, $line_options, $line_sku );
1601 exit();
1602 }
1603
1604 endif;// End of All columns Product CSV download.
1605
1606 /**
1607 * Stock columns Product CSV upload.
1608 *
1609 * @since 2.2.2
1610 * @param string $lines 1 line of text.
1611 * @param array $file_info Information.
1612 */
1613 function usces_item_stock_uploadcsv( $lines, $file_info ) {
1614 global $wpdb, $usces;
1615
1616 $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
1617 $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
1618 $file_name = wel_esc_upload_file_name( $file_name );
1619
1620 $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
1621 $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
1622 $line_num = 0;
1623 $column_num = 0;
1624 $total_num = count( $lines );
1625 $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
1626 $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
1627 $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
1628 $work_number = 0;
1629 $error = false;
1630
1631 $yn = "\n";
1632
1633 // Stock columns.
1634
1635 // Registration loop.
1636 foreach ( $lines as $rows_num => $line ) {
1637
1638 $logtemp = '';
1639 $line = trim( $line );
1640 if ( empty( $line ) ) {
1641 continue;
1642 }
1643
1644 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1645
1646 // Divide the line and store it in $datas.
1647 $datas = usces_make_line_data( $line );
1648
1649 if ( $column_num < count( $datas ) ) {
1650 $column_num = count( $datas );
1651 }
1652 $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
1653
1654 if ( 8 !== count( $datas ) || ( 0 === $rows_num && 'Post ID' !== $datas[0] ) ) {
1655 $progress = array(
1656 'info' => $file_info,
1657 'status' => __( 'forced termination', 'usces' ) . $check_label,
1658 'progress' => __( 'The process was not completed', 'usces' ),
1659 'log' => 'Error : ' . __( 'This file may not be the item CSV for "Stock columns".', 'usces' ),
1660 'flag' => 'complete',
1661 );
1662 record_item_up_progress( $progress );
1663 $error = true;
1664
1665 $file_path = $upload_folder . $file_name;
1666 $real_path = realpath( $file_path );
1667 if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
1668 unlink( $file_path );
1669 }
1670 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
1671 return $results;
1672 }
1673
1674 // Skip the first line.
1675 if ( 'Post ID' === $datas[0] ) {
1676 continue;
1677 }
1678
1679 $line_num = $rows_num + 1;
1680
1681 // Split processing.
1682 if ( $start_number > $work_number ) {
1683 ++$work_number;
1684 continue;
1685 }
1686 if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 10 ) ) && $start_number != $work_number ) {
1687 $progress = array(
1688 'info' => $file_info,
1689 'status' => __( 'Processing...', 'usces' ) . $check_label,
1690 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1691 'i' => $line_num,
1692 'all' => $total_num,
1693 'flag' => 'continue',
1694 'work_number' => $work_number,
1695 'comp_num' => $comp_num,
1696 'err_num' => $err_num,
1697 );
1698 record_item_up_progress( $progress );
1699 die( wp_json_encode( $progress ) );
1700 }
1701 ++$work_number;
1702
1703 // Column check loop.
1704 foreach ( $datas as $key => $data ) {
1705
1706 $post_id = ( ! WCUtils::is_blank( $datas[0] ) ) ? (int) $datas[0] : null;
1707 $meta_id = ( ! WCUtils::is_blank( $datas[3] ) ) ? (int) $datas[3] : null;
1708 $product = wel_get_product( $post_id );
1709
1710 switch ( $key ) {
1711 case 0:// Post ID.
1712 if ( $post_id ) {
1713 if ( false === $product ) {
1714 ++$err_num;
1715 $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Post-ID %s does not exist in the database.', 'usces' ), $post_id );
1716 $logtemp .= $mes . $yn;
1717 }
1718 } else {
1719 ++$err_num;
1720 $mes = 'No.' . $line_num . "\t" . __( 'A value of the Post-ID is abnormal.', 'usces' );
1721 $logtemp .= $mes . $yn;
1722 }
1723 break;
1724
1725 case 3:// Meta ID.
1726 if ( $post_id && $meta_id ) {
1727 $skus = $product['_sku'];
1728 $meta_flag = false;
1729 foreach ( $skus as $sku ) {
1730 if ( $meta_id === (int) $sku['meta_id'] ) {
1731 $meta_flag = true;
1732 break;
1733 }
1734 }
1735 if ( false === $meta_flag ) {
1736 ++$err_num;
1737 $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Meta ID %s does not exist in the database.', 'usces' ), $meta_id );
1738 $logtemp .= $mes . $yn;
1739 }
1740 } else {
1741 ++$err_num;
1742 $mes = 'No.' . $line_num . "\t" . __( 'A value of the Meta ID is abnormal.', 'usces' );
1743 $logtemp .= $mes . $yn;
1744 }
1745 break;
1746
1747 case 4:// SKU Code.
1748 if ( 0 === strlen( $data ) ) {
1749 $mes = 'No.' . $line_num . "\t" . __( 'A SKU cord is non-input.', 'usces' );
1750 $logtemp .= $mes . $yn;
1751 }
1752 break;
1753
1754 case 5:// SKU Name.
1755 break;
1756
1757 case 6:// Stock.
1758 if ( 0 < strlen( $data ) && ! preg_match( '/^[0-9]+$/', $data ) ) {
1759 $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
1760 $logtemp .= $mes . $yn;
1761 }
1762 break;
1763
1764 case 7:// Stock Status.
1765 $stock_status = apply_filters( 'usces_filter_csv_upload_check_stock_status', $data );
1766 if ( ! preg_match( '/^[0-9]+$/', $data ) || $stock_status < $data ) {
1767 $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock status is abnormal.', 'usces' );
1768 $logtemp .= $mes . $yn;
1769 }
1770 break;
1771 }
1772 }
1773
1774 // End of data check.
1775 if ( 0 < strlen( $logtemp ) ) {
1776 ++$err_num;
1777 $progress = array(
1778 'log' => $logtemp,
1779 );
1780 record_item_up_progress( $progress );
1781 $error = true;
1782 continue;
1783 }
1784
1785 if ( ! $check_mode ) {
1786
1787 $post_id = (int) $datas[0];
1788 $meta_id = (int) $datas[3];
1789 $skus = wel_get_skus( $post_id, 'meta_id', false );
1790 $sku = $skus[ $meta_id ];
1791 if ( empty( $skus ) || ! isset( $skus[ $meta_id ] ) ) {
1792 ++$err_num;
1793 $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
1794 $progress = array(
1795 'log' => $mes,
1796 );
1797 record_item_up_progress( $progress );
1798 $error = true;
1799 continue;
1800 }
1801
1802 $sku['code'] = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $datas[4], 'UTF-8', 'SJIS' ) ) : trim( $datas[4] );
1803 $sku['name'] = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $datas[5], 'UTF-8', 'SJIS' ) ) : trim( $datas[5] );
1804 $sku['stocknum'] = $datas[6];
1805 $sku['stock'] = $datas[7];
1806
1807 $db_res = wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1808
1809 if ( 0 > $db_res ) {
1810 ++$err_num;
1811 $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
1812 $progress = array(
1813 'log' => $mes,
1814 );
1815 record_item_up_progress( $progress );
1816 $error = true;
1817 continue;
1818 }
1819 wp_cache_delete( $post_id, 'post_meta' );
1820 }
1821 ++$comp_num;
1822
1823 if ( 0 === ( $line_num % 10 ) ) {
1824 $progress = array(
1825 'info' => $file_info,
1826 'status' => __( 'Processing...', 'usces' ) . $check_label,
1827 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
1828 'i' => $line_num,
1829 'all' => $total_num,
1830 );
1831 record_item_up_progress( $progress );
1832 }
1833 }
1834
1835 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num' );
1836
1837 return $results;
1838 }
1839
1840 /**
1841 * Stock columns Product CSV download.
1842 *
1843 * @since 2.2.2
1844 * @param string $rows Data.
1845 * @param array $usces_opt_item Item option.
1846 */
1847 function usces_download_item_stock_list( $rows, $usces_opt_item ) {
1848 global $usces;
1849
1850 $ext = 'csv';
1851 $th_h1 = '"';
1852 $th_h = ',"';
1853 $th_f = '"';
1854 $td_h1 = '"';
1855 $td_h = ',"';
1856 $td_f = '"';
1857 $sp = ';';
1858 $eq = '=';
1859 $lf = "\n";
1860
1861 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
1862
1863 $line = '';
1864
1865 if ( 1 === (int) $usces_opt_item['chk_header'] ) {
1866 $line .= $th_h1 . 'Post ID' . $th_f;
1867 $line .= $th_h . __( 'item code', 'usces' ) . $th_f;
1868 $line .= $th_h . __( 'item name', 'usces' ) . $th_f;
1869 $line .= $th_h . 'Meta ID' . $th_f;
1870 $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;
1871 $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;
1872 $line .= $th_h . __( 'stock', 'usces' ) . $th_f;
1873 $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;
1874 $line .= $lf;
1875 }
1876
1877 while ( @ob_get_level() > 0 ) {
1878 ob_end_clean();
1879 }
1880
1881 if ( $csv_encode_type_sjis ) {
1882 $charset = 'Shift_JIS';
1883 } else {
1884 $charset = 'UTF-8';
1885 }
1886
1887 mb_http_output( 'pass' );
1888 set_time_limit( 3600 );
1889 header( 'Content-Type: application/octet-stream; charset=' . $charset );
1890 header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
1891
1892 if ( ! $csv_encode_type_sjis ) {
1893 echo "\xEF\xBB\xBF";
1894 }
1895
1896 foreach ( (array) $rows as $row ) {
1897
1898 $post_id = $row['ID'];
1899 $product = wel_get_product( $post_id );
1900
1901 $line_item = $td_h1 . $post_id . $td_f;
1902 $line_item .= $td_h . $product['itemCode'] . $td_f;
1903 $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
1904
1905 $skus = $product['_sku'];
1906 foreach ( $skus as $sku ) {
1907 $line_sku = $td_h . $sku['meta_id'] . $td_f;
1908 $line_sku .= $td_h . $sku['code'] . $td_f;
1909 $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
1910 $line_sku .= $td_h . $sku['stocknum'] . $td_f;
1911 $line_sku .= $td_h . $sku['stock'] . $td_f;
1912
1913 $line .= $line_item . $line_sku . $lf;
1914 }
1915 if ( $csv_encode_type_sjis ) {
1916 $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
1917 }
1918
1919 // This is a direct binary/CSV download response, so HTML escaping is not applicable.
1920 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1921 print( $line );
1922 flush();
1923
1924 $line = '';
1925 wp_cache_flush();
1926 }
1927
1928 $results = array();
1929
1930 return $results;
1931 }
1932
1933 /**
1934 * Item SKU Upload.
1935 */
1936 if ( ! function_exists( 'usces_item_sku_uploadcsv' ) ) :
1937
1938 /**
1939 * SKU columns Product CSV upload.
1940 *
1941 * @since 2.2.2
1942 * @param string $lines 1 line of text.
1943 * @param array $file_info Information.
1944 */
1945 function usces_item_sku_uploadcsv( $lines, $file_info ) {
1946 global $wpdb, $usces;
1947
1948 $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
1949 $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
1950 $file_name = wel_esc_upload_file_name( $file_name );
1951
1952 $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
1953 $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
1954 $line_num = 0;
1955 $column_num = 0;
1956 $total_num = count( $lines );
1957 $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
1958 $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
1959 $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
1960 $work_number = 0;
1961 $error = false;
1962
1963 $yn = "\n";
1964
1965 $min_field_num = 13;
1966 $min_field_num = apply_filters( 'usces_filter_sku_uploadcsv_min_field_num', $min_field_num );
1967
1968 // SKU columns.
1969
1970 // Registration loop.
1971 foreach ( $lines as $rows_num => $line ) {
1972
1973 $logtemp = '';
1974 $line = trim( $line );
1975 if ( empty( $line ) ) {
1976 continue;
1977 }
1978
1979 // Divide the line and store it in $datas.
1980 $datas = usces_make_line_data( $line );
1981
1982 if ( count( $datas ) !== $column_num ) {
1983 $column_num = count( $datas );
1984 }
1985 $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
1986
1987 if ( $min_field_num !== $column_num || ( 0 === $rows_num && 'Post ID' !== $datas[0] ) ) {
1988 $progress = array(
1989 'info' => $file_info,
1990 'status' => __( 'forced termination', 'usces' ) . $check_label,
1991 'progress' => __( 'The process was not completed', 'usces' ),
1992 'log' => 'Error : ' . __( 'This file may not be the item CSV for "SKU columns".', 'usces' ),
1993 'flag' => 'complete',
1994 );
1995 record_item_up_progress( $progress );
1996 $error = true;
1997
1998 $file_path = $upload_folder . $file_name;
1999 $real_path = realpath( $file_path );
2000 if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2001 unlink( $file_path );
2002 }
2003 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2004 return $results;
2005 }
2006
2007 // Skip the first line.
2008 if ( 'Post ID' === $datas[0] ) {
2009 continue;
2010 }
2011
2012 $line_num = $rows_num + 1;
2013
2014 // Split processing.
2015 if ( $start_number > $work_number ) {
2016 ++$work_number;
2017 continue;
2018 }
2019 if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 10 ) ) && $start_number !== $work_number ) {
2020 $progress = array(
2021 'info' => $file_info,
2022 'status' => __( 'Processing...', 'usces' ) . $check_label,
2023 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2024 'i' => $line_num,
2025 'all' => $total_num,
2026 'flag' => 'continue',
2027 'work_number' => $work_number,
2028 'comp_num' => $comp_num,
2029 'err_num' => $err_num,
2030 );
2031 record_item_up_progress( $progress );
2032 die( wp_json_encode( $progress ) );
2033 }
2034 ++$work_number;
2035
2036 // Column check loop.
2037 foreach ( $datas as $key => $data ) {
2038
2039 $post_id = ( ! WCUtils::is_blank( $datas[0] ) ) ? (int) $datas[0] : null;
2040 $meta_id = ( ! WCUtils::is_blank( $datas[3] ) ) ? (int) $datas[3] : null;
2041 $product = wel_get_product( $post_id );
2042
2043 switch ( $key ) {
2044 case 0:// Post ID.
2045 if ( $post_id ) {
2046 if ( false === $product ) {
2047 ++$err_num;
2048 $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Post-ID %s does not exist in the database.', 'usces' ), $post_id );
2049 $logtemp .= $mes . $yn;
2050 }
2051 } else {
2052 ++$err_num;
2053 $mes = 'No.' . $line_num . "\t" . __( 'A value of the Post-ID is abnormal.', 'usces' );
2054 $logtemp .= $mes . $yn;
2055 }
2056 break;
2057
2058 case 3:// Meta ID.
2059 if ( $post_id && $meta_id ) {
2060 $skus = $product['_sku'];
2061 $meta_flag = false;
2062 foreach ( $skus as $sku ) {
2063 if ( $meta_id === (int) $sku['meta_id'] ) {
2064 $meta_flag = true;
2065 break;
2066 }
2067 }
2068 if ( false === $meta_flag ) {
2069 ++$err_num;
2070 $mes = 'No.' . $line_num . "\t" . sprintf( __( 'Meta ID %s does not exist in the database.', 'usces' ), $meta_id );
2071 $logtemp .= $mes . $yn;
2072 }
2073 } else {
2074 ++$err_num;
2075 $mes = 'No.' . $line_num . "\t" . __( 'A value of the Meta ID is abnormal.', 'usces' );
2076 $logtemp .= $mes . $yn;
2077 }
2078 break;
2079
2080 case 4:// SKU Code.
2081 if ( 0 === strlen( $data ) ) {
2082 $mes = 'No.' . $line_num . "\t" . __( 'A SKU cord is non-input.', 'usces' );
2083 $logtemp .= $mes . $yn;
2084 }
2085 break;
2086
2087 case 5:// SKU Name.
2088 break;
2089
2090 case 6:// Normal Price.
2091 if ( 0 < strlen( $data ) && ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) ) {
2092 $mes = 'No.' . $line_num . "\t" . __( 'A value of the normal price is abnormal.', 'usces' );
2093 $logtemp .= $mes . $yn;
2094 }
2095 break;
2096
2097 case 7:// Sale Price.
2098 if ( ! preg_match( '/^\d$|^\d+\.?\d+$/', $data ) || 0 === strlen( $data ) ) {
2099 $mes = 'No.' . $line_num . "\t" . __( 'A value of the sale price is abnormal.', 'usces' );
2100 $logtemp .= $mes . $yn;
2101 }
2102 break;
2103
2104 case 8:// Stock.
2105 if ( 0 < strlen( $data ) && ! preg_match( '/^[0-9;]+$/', $data ) ) {
2106 $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock amount is abnormal.', 'usces' );
2107 $logtemp .= $mes . $yn;
2108 }
2109 break;
2110
2111 case 9:// Stock Status.
2112 $stock_status = apply_filters( 'usces_filter_csv_upload_check_stock_status', $data );
2113 if ( ! preg_match( '/^[0-9;]+$/', $data ) || $stock_status < $data ) {
2114 $mes = 'No.' . $line_num . "\t" . __( 'A value of the stock status is abnormal.', 'usces' );
2115 $logtemp .= $mes . $yn;
2116 }
2117 break;
2118
2119 case 10:// Unit.
2120 break;
2121
2122 case 11:// Apply business package.
2123 if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
2124 $mes = 'No.' . $line_num . "\t" . __( 'The value of the duties pack application is abnormal.', 'usces' );
2125 $logtemp .= $mes . $yn;
2126 }
2127 break;
2128
2129 case 12:// Applicable tax rate.
2130 if ( ! preg_match( '/^[0-9;]+$/', $data ) || 1 < $data ) {
2131 $mes = 'No.' . $line_num . "\t" . __( 'Invalid value of Applicable tax rate.', 'usces' );
2132 $logtemp .= $mes . $yn;
2133 }
2134 break;
2135 }
2136 }
2137
2138 // End of data check.
2139 if ( 0 < strlen( $logtemp ) ) {
2140 ++$err_num;
2141 $progress = array(
2142 'log' => $logtemp,
2143 );
2144 record_item_up_progress( $progress );
2145 $error = true;
2146 continue;
2147 }
2148
2149 if ( ! $check_mode ) {
2150
2151 $post_id = (int) $datas[0];
2152 $meta_id = (int) $datas[3];
2153 $skus = wel_get_skus( $post_id, 'meta_id', false );
2154 $sku = $skus[ $meta_id ];
2155 if ( empty( $skus ) || ! isset( $skus[ $meta_id ] ) ) {
2156 ++$err_num;
2157 $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
2158 $progress = array(
2159 'log' => $mes,
2160 );
2161 record_item_up_progress( $progress );
2162 $error = true;
2163 continue;
2164 }
2165
2166 $sku['code'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[4], 'UTF-8', 'SJIS' ) ) : trim( $datas[4] );
2167 $sku['name'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[5], 'UTF-8', 'SJIS' ) ) : trim( $datas[5] );
2168 $sku['cprice'] = $datas[6];
2169 $sku['price'] = $datas[7];
2170 $sku['stocknum'] = $datas[8];
2171 $sku['stock'] = $datas[9];
2172 $sku['unit'] = ( 0 === (int) $usces->options['system']['csv_encode_type'] ) ? trim( mb_convert_encoding( $datas[10], 'UTF-8', 'SJIS' ) ) : trim( $datas[10] );
2173 $sku['gp'] = $datas[11];
2174 $sku['taxrate'] = usces_csv_set_sku_applicable_taxrate( $datas[12] );
2175
2176 $sku = apply_filters( 'usces_filter_sku_uploadcsv_skuvalue', $sku, $datas );
2177
2178 $db_res = wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
2179
2180 if ( 0 > $db_res ) {
2181 ++$err_num;
2182 $mes = 'No.' . $line_num . "\t" . __( 'This data was not registered in the database.', 'usces' );
2183 $progress = array(
2184 'log' => $mes,
2185 );
2186 record_item_up_progress( $progress );
2187 $error = true;
2188 continue;
2189 }
2190 wp_cache_delete( $post_id, 'post_meta' );
2191 }
2192 ++$comp_num;
2193
2194 if ( 0 === ( $line_num % 10 ) ) {
2195 $progress = array(
2196 'info' => $file_info,
2197 'status' => __( 'Processing...', 'usces' ) . $check_label,
2198 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2199 'i' => $line_num,
2200 'all' => $total_num,
2201 );
2202 record_item_up_progress( $progress );
2203 }
2204 }
2205
2206 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info', 'min_field_num' );
2207
2208 return $results;
2209 }
2210 endif;// End of SKU columns Product CSV Upload.
2211
2212 /**
2213 * Item SKU Download.
2214 */
2215 if ( ! function_exists( 'usces_download_item_sku_list' ) ) :
2216
2217 /**
2218 * SKU columns CSV download.
2219 *
2220 * @since 2.2.2
2221 * @param string $rows Data.
2222 * @param array $usces_opt_item Item option.
2223 */
2224 function usces_download_item_sku_list( $rows, $usces_opt_item ) {
2225 global $usces;
2226
2227 $ext = 'csv';
2228 $tr_h = '';
2229 $tr_f = '';
2230 $th_h1 = '"';
2231 $th_h = ',"';
2232 $th_f = '"';
2233 $td_h1 = '"';
2234 $td_h = ',"';
2235 $td_f = '"';
2236 $sp = ';';
2237 $eq = '=';
2238 $lf = "\n";
2239
2240 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2241
2242 $line = '';
2243
2244 if ( 1 === (int) $usces_opt_item['chk_header'] ) {
2245 $line .= $tr_h;
2246 $line .= $th_h1 . 'Post ID' . $th_f;// 0.
2247 $line .= $th_h . __( 'item code', 'usces' ) . $th_f;// 1.
2248 $line .= $th_h . __( 'item name', 'usces' ) . $th_f;// 2.
2249 $line .= $th_h . 'Meta ID' . $th_f;// 3.
2250 $line .= $th_h . __( 'SKU code', 'usces' ) . $th_f;// 4.
2251 $line .= $th_h . __( 'SKU display name ', 'usces' ) . $th_f;// 5.
2252 $line .= $th_h . __( 'normal price', 'usces' ) . $th_f;// 6.
2253 $line .= $th_h . __( 'Sale price', 'usces' ) . $th_f;// 7.
2254 $line .= $th_h . __( 'stock', 'usces' ) . $th_f;// 8.
2255 $line .= $th_h . __( 'stock status', 'usces' ) . $th_f;// 9.
2256 $line .= $th_h . __( 'unit', 'usces' ) . $th_f;// 10.
2257 $line .= $th_h . __( 'Apply business package', 'usces' ) . $th_f;// 11.
2258 $line .= $th_h . __( 'Applicable tax rate', 'usces' ) . $th_f;// 12.
2259
2260 $line = apply_filters( 'usces_filter_downloadcsv_header_skulist', $line );
2261
2262 $line .= $tr_f . $lf;
2263 }
2264
2265 while ( @ob_get_level() > 0 ) {
2266 ob_end_clean();
2267 }
2268
2269 if ( $csv_encode_type_sjis ) {
2270 $charset = 'Shift_JIS';
2271 } else {
2272 $charset = 'UTF-8';
2273 }
2274
2275 mb_http_output( 'pass' );
2276 set_time_limit( 3600 );
2277 header( 'Content-Type: application/octet-stream; charset=' . $charset );
2278 header( 'Content-Disposition: attachment; filename=usces_item_list.' . $ext );
2279
2280 if ( ! $csv_encode_type_sjis ) {
2281 echo "\xEF\xBB\xBF";
2282 }
2283
2284 foreach ( (array) $rows as $row ) {
2285
2286 $post_id = $row['ID'];
2287 $product = wel_get_product( $post_id );
2288
2289 $line_item = $td_h1 . $post_id . $td_f;
2290 $line_item .= $td_h . $product['itemCode'] . $td_f;
2291 $line_item .= $td_h . usces_entity_decode( $product['itemName'], $ext ) . $td_f;
2292
2293 $skus = $product['_sku'];
2294 foreach ( $skus as $sku ) {
2295 $line_sku = $td_h . $sku['meta_id'] . $td_f;
2296 $line_sku .= $td_h . $sku['code'] . $td_f;
2297 $line_sku .= $td_h . usces_entity_decode( $sku['name'], $ext ) . $td_f;
2298 $line_sku .= $td_h . usces_crform( $sku['cprice'], false, false, 'return', false ) . $td_f;
2299 $line_sku .= $td_h . usces_crform( $sku['price'], false, false, 'return', false ) . $td_f;
2300 $line_sku .= $td_h . $sku['stocknum'] . $td_f;
2301 $line_sku .= $td_h . $sku['stock'] . $td_f;
2302 $line_sku .= $td_h . usces_entity_decode( $sku['unit'], $ext ) . $td_f;
2303 $line_sku .= $td_h . $sku['gp'] . $td_f;
2304 $line_sku .= $td_h . usces_csv_get_sku_applicable_taxrate( $sku ) . $td_f;
2305
2306 $line_sku = apply_filters( 'usces_filter_downloadcsv_skuvalue_skulist', $line_sku, $sku );
2307
2308 $line .= $tr_h . $line_item . $line_sku . $tr_f . $lf;
2309 }
2310 if ( $csv_encode_type_sjis ) {
2311 $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2312 }
2313
2314 // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2315 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2316 print( $line );
2317 flush();
2318
2319 $line = '';
2320 wp_cache_flush();
2321 }
2322
2323 $results = '';
2324
2325 return $results;
2326 }
2327
2328 endif;// End of SKU columns Product CSV download.
2329
2330 /**
2331 * Custom field columns CSV upload.
2332 *
2333 * @since 2.2.2
2334 * @param string $lines 1 line of text.
2335 * @param array $file_info Information.
2336 */
2337 function usces_item_meta_uploadcsv( $lines, $file_info ) {
2338 global $wpdb, $usces;
2339
2340 $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
2341 $file_name = isset( $_REQUEST['regfile'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['regfile'] ) ) : '';
2342 $file_name = wel_esc_upload_file_name( $file_name );
2343
2344 $comp_num = isset( $_REQUEST['comp_num'] ) ? (int) $_REQUEST['comp_num'] : 0;
2345 $err_num = isset( $_REQUEST['err_num'] ) ? (int) $_REQUEST['err_num'] : 0;
2346 $line_num = 0;
2347 $column_num = 0;
2348 $total_num = count( $lines );
2349 $start_number = isset( $_REQUEST['work_number'] ) ? (int) $_REQUEST['work_number'] : 0;
2350 $check_mode = isset( $_REQUEST['checkcsv'] ) ? true : false;
2351 $check_label = $check_mode ? __( '[Check mode]', 'usces' ) : '';
2352 $work_number = 0;
2353 $error = false;
2354
2355 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2356
2357 $yn = "\n";
2358
2359 $labels = array();
2360
2361 // Custom field columns.
2362
2363 // Registration loop.
2364 foreach ( $lines as $rows_num => $line ) {
2365
2366 $logtemp = '';
2367 $line = trim( $line );
2368 if ( empty( $line ) ) {
2369 continue;
2370 }
2371
2372 // Divide the line and store it in $datas.
2373 $datas = usces_make_line_data( $line );
2374
2375 if ( count( $datas ) !== $column_num ) {
2376 $column_num = count( $datas );
2377 }
2378 $file_info['rowcount'] = __( 'Number of lines', 'usces' ) . ' ' . $total_num . ' ' . __( 'Number of items', 'usces' ) . ' ' . $column_num;
2379 if ( 0 === $rows_num ) {
2380 $labels = $datas;
2381 }
2382
2383 if ( 0 === (int) $rows_num ) {
2384
2385 if ( 'post_id_meta' !== $datas[0] ) {
2386 $progress = array(
2387 'info' => $file_info,
2388 'status' => __( 'forced termination', 'usces' ) . $check_label,
2389 'progress' => __( 'The process was not completed', 'usces' ),
2390 'log' => 'Error : ' . __( 'This CSV file is not for custom fields', 'usces' ),
2391 'flag' => 'complete',
2392 );
2393 record_item_up_progress( $progress );
2394 $error = true;
2395
2396 $file_path = $upload_folder . $file_name;
2397 $real_path = realpath( $file_path );
2398 if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2399 unlink( $file_path );
2400 }
2401 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2402 return $results;
2403 }
2404
2405 continue;
2406 }
2407
2408 $line_num = $rows_num + 1;
2409 $post_id = $datas[0];
2410
2411 // Split processing.
2412 if ( $start_number > $work_number ) {
2413 ++$work_number;
2414 ++$comp_num;
2415 continue;
2416 }
2417 if ( 0 === ( $work_number % ( USCES_ITEM_UP_INTERBAL * 2 ) ) && $start_number !== $work_number ) {
2418 $progress = array(
2419 'info' => $file_info,
2420 'status' => __( 'Processing...', 'usces' ) . $check_label,
2421 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2422 'i' => $line_num,
2423 'all' => $total_num,
2424 'flag' => 'continue',
2425 'work_number' => $work_number,
2426 'comp_num' => $comp_num,
2427 'err_num' => $err_num,
2428 );
2429 record_item_up_progress( $progress );
2430 die( wp_json_encode( $progress ) );
2431 }
2432 ++$work_number;
2433
2434 // Column check loop.
2435 foreach ( $datas as $key => $data ) {
2436
2437 // Skip reference columns.
2438 if ( 3 > $key ) {
2439 continue;
2440 }
2441
2442 if ( ! $check_mode ) {
2443 $meta_key = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $labels[ $key ], 'UTF-8', 'SJIS' ) ) : trim( $labels[ $key ] );
2444
2445 // delete.
2446 $query = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` = %s", $post_id, $meta_key );
2447 $db_res = $wpdb->query( $query );
2448
2449 // add.
2450 if ( 'null' !== $data ) {
2451
2452 if ( false !== strpos( $data, ']][[' ) ) {
2453
2454 $m = explode( ']][[', $data );
2455 foreach ( $m as $tempval ) {
2456 $tempval = rtrim( $tempval, ']]' );
2457 $tempval = ltrim( $tempval, '[[' );
2458 $meta_value = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $tempval, 'UTF-8', 'SJIS' ) ) : trim( $tempval );
2459
2460 $query = $wpdb->prepare( "INSERT INTO {$wpdb->postmeta} ( `post_id`, `meta_key`, `meta_value` ) VALUES ( %d, %s, %s ) ", $post_id, $meta_key, $meta_value );
2461 $db_res = $wpdb->query( $query, ARRAY_A );
2462 }
2463 } else {
2464
2465 $meta_value = ( $csv_encode_type_sjis ) ? trim( mb_convert_encoding( $data, 'UTF-8', 'SJIS' ) ) : trim( $data );
2466
2467 $query = $wpdb->prepare( "INSERT INTO {$wpdb->postmeta} ( `post_id`, `meta_key`, `meta_value` ) VALUES ( %d, %s, %s ) ", $post_id, $meta_key, $meta_value );
2468 $db_res = $wpdb->query( $query, ARRAY_A );
2469
2470 }
2471 }
2472 }
2473 }
2474
2475 ++$comp_num;
2476
2477 if ( 0 === ( $line_num % 10 ) ) {
2478 $progress = array(
2479 'info' => $file_info,
2480 'status' => __( 'Processing...', 'usces' ) . $check_label,
2481 'progress' => sprintf( __( 'Successful %1$s lines, Failed %2$s lines.', 'usces' ), $comp_num, $err_num ),
2482 'i' => $line_num,
2483 'all' => $total_num,
2484 );
2485 record_item_up_progress( $progress );
2486 }
2487 }
2488
2489 $results = compact( 'error', 'total_num', 'comp_num', 'err_num', 'line_num', 'file_info' );
2490
2491 return $results;
2492 }
2493
2494 /**
2495 * Custom field columns CSV download.
2496 *
2497 * @since 2.2.2
2498 * @param string $rows Data.
2499 * @param array $usces_opt_item Item option.
2500 */
2501 function usces_download_item_meta_list( $rows, $usces_opt_item ) {
2502 global $usces;
2503
2504 $csv_encode_type_sjis = ( isset( $usces->options['system']['csv_encode_type'] ) && 1 === (int) $usces->options['system']['csv_encode_type'] ) ? false : true;
2505
2506 $meta_keys = array(
2507 'post_id_meta' => 'null',
2508 'item_code' => 'null',
2509 'item_name' => 'null',
2510 );
2511
2512 foreach ( $rows as $row ) {
2513
2514 $product = wel_get_product( $row['ID'] );
2515 $metas = $product['_ext'];
2516 foreach ( $metas as $key => $value ) {
2517 if ( '' === $key || '_' === substr( $key, 0, 1 ) || is_array( maybe_unserialize( $key ) ) ) {
2518 continue;
2519 }
2520 $meta_keys[ $key ] = 'null';
2521 }
2522 }
2523
2524 $data = array();
2525 foreach ( $rows as $r => $row ) {
2526
2527 $product = wel_get_product( $row['ID'] );
2528 $metas = $product['_ext'];
2529 $new = $meta_keys;
2530
2531 $new['post_id_meta'] = $product['ID'];
2532 $new['item_code'] = $product['itemCode'];
2533 $new['item_name'] = $product['itemName'];
2534
2535 foreach ( $metas as $key => $values ) {
2536 if ( '' === $key || '_' === substr( $key, 0, 1 ) || is_array( maybe_unserialize( $key ) ) ) {
2537 continue;
2538 }
2539 $vc = is_array( $values ) ? count( $values ) : 0;
2540 if ( is_array( $values ) && 1 < $vc ) {
2541 $val = '';
2542 foreach ( $values as $v ) {
2543 $val .= '[[' . $v . ']]';
2544 }
2545 } elseif ( is_array( $values ) && 1 === $vc ) {
2546 $val = $values[0];
2547 } else {
2548 $val = $values;
2549 }
2550 $new[ $key ] = $val;
2551 }
2552 $data[ $r ] = $new;
2553 }
2554
2555 $ext = 'csv';
2556 $h = '"';
2557 $f = '",';
2558 $lf = "\n";
2559
2560 $line = '';
2561 foreach ( $meta_keys as $label => $lv ) {
2562 $line .= $h . $label . $f;
2563 }
2564 $line = trim( $line, ',' );
2565 $line .= $lf;
2566
2567 while ( @ob_get_level() > 0 ) {
2568 ob_end_clean();
2569 }
2570
2571 if ( $csv_encode_type_sjis ) {
2572 $charset = 'Shift_JIS';
2573 } else {
2574 $charset = 'UTF-8';
2575 }
2576
2577 mb_http_output( 'pass' );
2578 set_time_limit( 3600 );
2579 header( 'Content-Type: application/octet-stream; charset=' . $charset );
2580 header( 'Content-Disposition: attachment; filename=usces_item_meta.' . $ext );
2581
2582 if ( ! $csv_encode_type_sjis ) {
2583 echo "\xEF\xBB\xBF";
2584 }
2585
2586 if ( $csv_encode_type_sjis ) {
2587 $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2588 }
2589
2590 // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2591 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2592 print( $line );
2593
2594 foreach ( $data as $d ) {
2595 $line = '';
2596
2597 foreach ( $d as $dv ) {
2598 $line .= $h . str_replace( '"', '""', $dv ) . $f;
2599 }
2600 $line = trim( $line, ',' );
2601 $line .= $lf;
2602
2603 if ( $csv_encode_type_sjis ) {
2604 $line = mb_convert_encoding( $line, apply_filters( 'usces_filter_output_csv_encode', 'SJIS-win' ), 'UTF-8' );
2605 }
2606
2607 // This is a direct binary/CSV download response, so HTML escaping is not applicable.
2608 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2609 print( $line );
2610 flush();
2611
2612 }
2613 exit();
2614 }
2615 }
2616
2617 /**
2618 * Explode one line of CSV to generate data.
2619 *
2620 * @since 2.2.2
2621 * @param string $line A line of CSV.
2622 * @return array
2623 */
2624 function usces_make_line_data( $line ) {
2625
2626 if ( 0 === strpos( $line, "\xEF\xBB\xBF" ) ) {
2627 $line = substr( $line, 3 );
2628 }
2629
2630 $datas = array();
2631 $array = explode( ',', $line );
2632 $buffer = '';
2633 $sp = ',';
2634
2635 foreach ( $array as $data ) {
2636
2637 $num = substr_count( $data, '"' );
2638 if ( 0 === ( $num % 2 ) && '' === $buffer ) {
2639
2640 if ( '"' === substr( $data, 0, 1 ) ) {
2641 $data = substr( $data, 1 );
2642 }
2643 if ( '"' === substr( $data, -1 ) ) {
2644 $data = substr( $data, 0, -1 );
2645 }
2646 $data = str_replace( array( '""' ), '"', $data );
2647 $datas[] = ( false !== $data ) ? $data : '';
2648
2649 } elseif ( 1 === ( $num % 2 ) && '' === $buffer ) {
2650
2651 $buffer .= $data;
2652
2653 } elseif ( 0 === ( $num % 2 ) && '' !== $buffer ) {
2654
2655 $buffer .= $sp . $data;
2656
2657 } elseif ( 1 === ( $num % 2 ) && '' !== $buffer ) {
2658
2659 $buffer .= $sp . $data;
2660
2661 if ( '"' === substr( $buffer, 0, 1 ) ) {
2662 $buffer = substr( $buffer, 1 );
2663 }
2664 if ( '"' === substr( $buffer, -1 ) ) {
2665 $buffer = substr( $buffer, 0, -1 );
2666 }
2667 $buffer = str_replace( array( '""' ), '"', $buffer );
2668 $datas[] = ( false !== $buffer ) ? $buffer : '';
2669 $buffer = '';
2670 }
2671 }
2672 return $datas;
2673 }
2674
2675 /**
2676 * Product custom field acquisition function.
2677 *
2678 * @since 2.2.2
2679 * @return array
2680 */
2681 function usces_get_item_custom_fields() {
2682 $item_custom_fields = apply_filters( 'usces_filter_item_custom_fields', array( '_itemOrderAcceptable' ) );
2683 return $item_custom_fields;
2684 }
2685
2686 /**
2687 * Product custom field value acquisition function.
2688 *
2689 * @since 2.2.2
2690 * @param string $key Custom field key.
2691 * @param array $custom_field Custom field.
2692 * @return string
2693 */
2694 function usces_get_item_custom_field_value( $key, $custom_field ) {
2695 global $usces;
2696
2697 $cf_sp = ';;'; // custom field separator.
2698
2699 $value = '';
2700 $cfdata = array();
2701 $cfrows = explode( $cf_sp, $custom_field );
2702
2703 foreach ( $cfrows as $cfindex => $row ) {
2704 if ( false !== strpos( $row, '=' ) ) {
2705 $cfdata[] = $row;
2706 } else {
2707 $cfdend = count( $cfdata ) - 1;
2708 if ( $cfdend && 0 <= $cfdend ) {
2709 $cfdata[ $cfdend ] = $cfdata[ $cfdend ] . ';' . $row;
2710 }
2711 }
2712 }
2713
2714 foreach ( $cfdata as $row ) {
2715 $cf = explode( '=', $row );
2716 if ( ! WCUtils::is_blank( $cf[0] ) ) {
2717 if ( $key == $cf[0] ) {
2718 $value = $cf[1];
2719 break;
2720 }
2721 }
2722 }
2723 return trim( $value );
2724 }
2725
2726 /**
2727 * Applicable taxrate flag.
2728 *
2729 * @since 2.2.2
2730 * @param array $sku SKU.
2731 * @return int
2732 */
2733 function usces_csv_get_sku_applicable_taxrate( $sku = array() ) {
2734 $taxrate = '0';
2735 if ( isset( $sku['taxrate'] ) && ! empty( $sku['taxrate'] ) ) {
2736 $taxrate = ( 'reduced' === $sku['taxrate'] ) ? '1' : '0';
2737 }
2738 return $taxrate;
2739 }
2740
2741 /**
2742 * Applicable taxrate string.
2743 *
2744 * @since 2.2.2
2745 * @param int $taxrate Taxrate.
2746 * @return string
2747 */
2748 function usces_csv_set_sku_applicable_taxrate( $taxrate = 0 ) {
2749 $value = ( 1 === (int) $taxrate ) ? 'reduced' : 'standard';
2750 return $value;
2751 }
2752
2753 /**
2754 * Item code duplication check.
2755 *
2756 * @since 2.2.2
2757 * @return boolean
2758 */
2759 function usces_item_code_duplication_check() {
2760 global $wpdb;
2761
2762 $item_table = usces_get_tablename( 'usces_item' );
2763
2764 $query = $wpdb->prepare(
2765 "SELECT `post_id`, `itemCode` FROM {$item_table}
2766 LEFT JOIN {$wpdb->posts} ON `ID` = `post_id`
2767 WHERE `post_type` = %s AND `post_status` IN ('pending', 'publish', 'draft', 'private', 'future')
2768 GROUP BY `itemCode` HAVING COUNT(*) > 1",
2769 'post'
2770 );
2771 $res = $wpdb->get_results( $query, ARRAY_A );
2772 return $res;
2773 }
2774
2775 /**
2776 * Record progress.
2777 *
2778 * @since 2.2.2
2779 * @param array $arr_content Content.
2780 */
2781 function record_item_up_progress( $arr_content ) {
2782
2783 $upload_folder = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . '/';
2784 $mkdir = wp_mkdir_p( $upload_folder );
2785
2786 if ( $mkdir ) {
2787 if ( isset( $arr_content['status'] ) || isset( $arr_content['progress'] ) ) {
2788 $progress_file = $upload_folder . 'progress.txt';
2789 file_put_contents( $progress_file, wp_json_encode( $arr_content ) );
2790 }
2791 if ( isset( $arr_content['log'] ) ) {
2792 $file_path = $upload_folder . 'log.txt';
2793 $real_path = realpath( $file_path );
2794 if ( 'clear' === $arr_content['log'] ) {
2795 if ( false !== $real_path && $real_path === $file_path && file_exists( $file_path ) ) {
2796 unlink( $file_path );
2797 }
2798 } else {
2799 $add_text = $arr_content['log'] . "\n";
2800 file_put_contents( $file_path, $add_text, FILE_APPEND | LOCK_EX );
2801 }
2802 }
2803 }
2804 }
2805
2806 /**
2807 * Upload mode name.
2808 *
2809 * @since 2.2.2
2810 * @param string $upload_mode Upload mode.
2811 * @return string $mode_name Mode name.
2812 */
2813 function usces_get_upmode_name( $upload_mode ) {
2814 switch ( $upload_mode ) {
2815 case 'all':
2816 $mode_name = __( 'All columns', 'usces' );
2817 break;
2818 case 'stock':
2819 $mode_name = __( 'Stock columns', 'usces' );
2820 break;
2821 case 'sku':
2822 $mode_name = __( 'SKU columns', 'usces' );
2823 break;
2824 case 'meta':
2825 $mode_name = __( 'Custom Field columns', 'usces' );
2826 break;
2827 default:
2828 $mode_name = '';
2829 }
2830 return $mode_name;
2831 }
2832
2833 /**
2834 * Delete skus by Post ID and Sort number.
2835 *
2836 * @since 2.8.11
2837 * @param string $post_id Post ID.
2838 * @param int $sort Sort Number.
2839 * @return mixed $res success:delete row number/error:false.
2840 */
2841 function wel_del_skus_by_postid_and_sort( $post_id, $sort = 0 ) {
2842 global $wpdb;
2843
2844 $table_name = usces_get_tablename( 'usces_skus' );
2845 $query = $wpdb->prepare( "DELETE FROM {$table_name} WHERE `post_id` = %d AND `sort` > %d", $post_id, $sort );
2846 $res = $wpdb->query( $query );
2847
2848 return $res;
2849 }
2850