PluginProbe
Welcart e-Commerce / 2.12.3
Welcart e-Commerce v2.12.3
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 1.4.13 All 291 releases
usc-e-shop / functions / define_function.php

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

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