| @@ -1,1315 +1,2852 @@ | ||
| 1 | -<?php | |
| 2 | -function usces_define_functions(){ | |
| 3 | - | |
| 4 | -//20101111ysk start | |
| 5 | -if( !function_exists('usces_item_uploadcsv') ): | |
| 6 | -function usces_item_uploadcsv(){ | |
| 7 | - global $wpdb, $usces, $user_ID; | |
| 8 | - | |
| 9 | - if( !current_user_can( 'import' ) ){ | |
| 10 | - $res['status'] = 'error'; | |
| 11 | - $res['message'] = __('You do not have permission to do that.'); | |
| 12 | - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']); | |
| 13 | - wp_redirect($url); | |
| 14 | - exit; | |
| 15 | - } | |
| 16 | - | |
| 17 | - //check dataSELECT id,title FROM table GROUP BY id HAVING COUNT(id) > 1; | |
| 18 | - $query = $wpdb->prepare("SELECT post_id, meta_value FROM {$wpdb->postmeta} | |
| 19 | - LEFT JOIN {$wpdb->posts} ON ID = post_id | |
| 20 | - WHERE meta_key = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future') | |
| 21 | - GROUP BY meta_value HAVING COUNT(meta_value) > 1", | |
| 22 | - '_itemCode'); | |
| 23 | - $db_check = $wpdb->get_results( $query, ARRAY_A ); | |
| 24 | - if( $db_check ) { | |
| 25 | - $res['status'] = 'error'; | |
| 26 | - $res['message'] .= __('The same product cord is registered.', 'usces'); | |
| 27 | - foreach($db_check as $d_item){ | |
| 28 | - $res['message'] .= ' , ' . $d_item['meta_value']; | |
| 29 | - } | |
| 30 | - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']); | |
| 31 | - wp_redirect($url); | |
| 32 | - exit; | |
| 33 | - } | |
| 34 | - | |
| 35 | - | |
| 36 | - $path = WP_CONTENT_DIR . '/uploads/'; | |
| 37 | -/*********************************************************************/ | |
| 38 | -// Upload | |
| 39 | -/**********************************************************************/ | |
| 40 | - if( isset($_REQUEST['action']) && 'itemcsv' == $_REQUEST['action'] ){ | |
| 41 | - $workfile = $_FILES["usces_upcsv"]["tmp_name"]; | |
| 42 | - | |
| 43 | - if ( !is_uploaded_file($workfile) ) { | |
| 44 | - $res['status'] = 'error'; | |
| 45 | - $res['message'] = __('The file was not uploaded.', 'usces'); | |
| 46 | - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']); | |
| 47 | - wp_redirect($url); | |
| 48 | - exit; | |
| 49 | - } | |
| 50 | - | |
| 51 | - //check ext | |
| 52 | - list($fname, $fext) = explode('.', $_FILES["usces_upcsv"]["name"], 2); | |
| 53 | - if( $fext != 'csv' ) { | |
| 54 | - $res['status'] = 'error'; | |
| 55 | - $res['message'] = __('The file is not supported.', 'usces').$fname.'.'.$fext; | |
| 56 | - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']); | |
| 57 | - wp_redirect($url); | |
| 58 | - exit; | |
| 59 | - } | |
| 60 | - | |
| 61 | - $new_filename = base64_encode($fname . '_' . time() . '.' .$fext); | |
| 62 | - if ( ! move_uploaded_file($_FILES['usces_upcsv']['tmp_name'], $path.$new_filename) ) { | |
| 63 | - $res['status'] = 'error'; | |
| 64 | - $res['message'] = __('The file was not stored.', 'usces').$fname.'.'.$fext; | |
| 65 | - $url = USCES_ADMIN_URL . '?page=usces_itemedit&usces_status=' . $res['status'] . '&usces_message=' . urlencode($res['message']); | |
| 66 | - wp_redirect($url); | |
| 67 | - exit; | |
| 68 | - } | |
| 69 | - return $new_filename; | |
| 70 | - } | |
| 71 | - | |
| 72 | - | |
| 73 | - $yn = "\r\n"; | |
| 74 | - $br = "<br />"; | |
| 75 | -/*********************************************************************/ | |
| 76 | -// Register | |
| 77 | -/**********************************************************************/ | |
| 78 | - if( isset($_REQUEST['regfile']) && !WCUtils::is_blank($_REQUEST['regfile']) && isset($_REQUEST['action']) && 'upload_register' == $_REQUEST['action'] ){ | |
| 79 | - $file_name = $_REQUEST['regfile']; | |
| 80 | - $decode_filename = base64_decode($file_name); | |
| 81 | - if( ! file_exists($path.$file_name) ){ | |
| 82 | - $res['status'] = 'error'; | |
| 83 | - $res['message'] = __('CSV file does not exist.', 'usces').esc_html($decode_filename); | |
| 84 | - //echo $res['status'] . ' : ' . $res['message']; | |
| 85 | - //return; | |
| 86 | - return( $res ); | |
| 87 | - } | |
| 88 | - } | |
| 89 | - /*////////////////////////////////////////*/ | |
| 90 | - // ready | |
| 91 | - /*////////////////////////////////////////*/ | |
| 92 | - $start = microtime(true); | |
| 93 | - | |
| 94 | - //$wpdb->show_errors(); | |
| 95 | - $wpdb->query( 'SET SQL_BIG_SELECTS=1' ); | |
| 96 | - set_time_limit(3600); | |
| 97 | - | |
| 98 | - define('USCES_COL_POST_ID', 0); //new | |
| 99 | - define('USCES_COL_POST_AUTHOR', 1); //new | |
| 100 | - define('USCES_COL_POST_CONTENT', 2); //15 | |
| 101 | - define('USCES_COL_POST_TITLE', 3); //14 | |
| 102 | - define('USCES_COL_POST_EXCERPT', 4); //16 | |
| 103 | - define('USCES_COL_POST_STATUS', 5); //17 | |
| 104 | - define('USCES_COL_POST_COMMENT_STATUS', 6); //new | |
| 105 | - define('USCES_COL_POST_PASSWORD', 7); //new | |
| 106 | - define('USCES_COL_POST_NAME', 8); //new | |
| 107 | - define('USCES_COL_POST_MODIFIED', 9); //18 | |
| 108 | - | |
| 109 | - define('USCES_COL_ITEM_CODE', 10); //0 | |
| 110 | - define('USCES_COL_ITEM_NAME', 11); //1 | |
| 111 | - define('USCES_COL_ITEM_RESTRICTION', 12); //2 | |
| 112 | - define('USCES_COL_ITEM_POINTRATE', 13); //3 | |
| 113 | - define('USCES_COL_ITEM_GPNUM1', 14); //4 | |
| 114 | - define('USCES_COL_ITEM_GPDIS1', 15); //5 | |
| 115 | - define('USCES_COL_ITEM_GPNUM2', 16); //6 | |
| 116 | - define('USCES_COL_ITEM_GPDIS2', 17); //7 | |
| 117 | - define('USCES_COL_ITEM_GPNUM3', 18); //8 | |
| 118 | - define('USCES_COL_ITEM_GPDIS3', 19); //9 | |
| 119 | - define('USCES_COL_ITEM_SHIPPING', 20); //10 | |
| 120 | - define('USCES_COL_ITEM_DELIVERYMETHOD', 21); //11 | |
| 121 | - define('USCES_COL_ITEM_SHIPPINGCHARGE', 22); //12 | |
| 122 | - define('USCES_COL_ITEM_INDIVIDUALSCHARGE', 23); //13 | |
| 123 | - | |
| 124 | - define('USCES_COL_CATEGORY', 24); //19 | |
| 125 | - define('USCES_COL_POST_TAG', 25); //20 | |
| 126 | - define('USCES_COL_CUSTOM_FIELD', 26); //new | |
| 127 | - | |
| 128 | - define('USCES_COL_SKU_CODE', 27); //21 | |
| 129 | - define('USCES_COL_SKU_NAME', 28); //22 | |
| 130 | - define('USCES_COL_SKU_CPRICE', 29); //23 | |
| 131 | - define('USCES_COL_SKU_PRICE', 30); //24 | |
| 132 | - define('USCES_COL_SKU_ZAIKONUM', 31); //25 | |
| 133 | - define('USCES_COL_SKU_ZAIKO', 32); //26 | |
| 134 | - define('USCES_COL_SKU_UNIT', 33); //27 | |
| 135 | - define('USCES_COL_SKU_GPTEKIYO', 34); //28 | |
| 136 | -// define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1)); | |
| 137 | - | |
| 138 | - | |
| 139 | - $lines = array(); | |
| 140 | - $total_num = 0; | |
| 141 | - $comp_num = 0; | |
| 142 | - $err_num = 0; | |
| 143 | - $min_field_num = 35;//29 | |
| 144 | - $log = ''; | |
| 145 | - $pre_code = ''; | |
| 146 | - $sku_index = 0; | |
| 147 | - $res = array(); | |
| 148 | - $date_pattern = "/(\d{4})-(\d{2}|\d)-(\d{2}|\d) (\d{2}):(\d{2}|\d):(\d{2}|\d)/"; | |
| 149 | - $linereadytime = 0; | |
| 150 | - $checktime = 0; | |
| 151 | - $insertposttime = 0; | |
| 152 | - $metadeletetime = 0; | |
| 153 | - $addmetatime = 0; | |
| 154 | - $AddOptiontime = 0; | |
| 155 | - $AddSKUtime = 0; | |
| 156 | - $onelinetime = 0; | |
| 157 | - | |
| 158 | - //log | |
| 159 | - if ( ! ($fpi = fopen (USCES_PLUGIN_DIR.'/logs/itemcsv_log.txt', "w"))) { | |
| 160 | - $res['status'] = 'error'; | |
| 161 | - $res['message'] = __('The log file was not prepared for.', 'usces').esc_html($decode_filename); | |
| 162 | - echo $res['status'] . ' : ' . $res['message']; | |
| 163 | - return; | |
| 164 | - } | |
| 165 | - //read data | |
| 166 | - if ( ! ($fpo = fopen ($path.$file_name, "r"))) { | |
| 167 | - $res['status'] = 'error'; | |
| 168 | - $res['message'] = __('A file does not open.', 'usces').esc_html($decode_filename); | |
| 169 | - echo $res['status'] . ' : ' . $res['message']; | |
| 170 | - return; | |
| 171 | - } | |
| 172 | - | |
| 173 | - | |
| 174 | - $orglines = array(); | |
| 175 | - $sp = ","; | |
| 176 | - $fname_parts = explode('.', $decode_filename); | |
| 177 | - if('csv' !== end($fname_parts)) { | |
| 178 | - $res['status'] = 'error'; | |
| 179 | - $res['message'] = __('This file is not in the CSV file.', 'usces').esc_html($decode_filename); | |
| 180 | - echo $res['status'] . ' : ' . $res['message']; | |
| 181 | - return; | |
| 182 | - | |
| 183 | - } else { | |
| 184 | - $buf = ''; | |
| 185 | - while (! feof ($fpo)) { | |
| 186 | - $temp = fgets ($fpo, 10240); | |
| 187 | - if( 0 == strlen($temp) ) continue; | |
| 188 | - | |
| 189 | - $num = substr_count($temp, '"'); | |
| 190 | - if( 0 == $num % 2 && '' == $buf ){ | |
| 191 | - $orglines[] = $temp; | |
| 192 | - }elseif( 1 == $num % 2 && '' == $buf ){ | |
| 193 | - $buf .= $temp; | |
| 194 | - }elseif( 0 == $num % 2 && '' != $buf ){ | |
| 195 | - $buf .= $temp; | |
| 196 | - }elseif( 1 == $num % 2 && '' != $buf ){ | |
| 197 | - $buf .= $temp; | |
| 198 | - $orglines[] = $buf; | |
| 199 | - $buf = ''; | |
| 200 | - } | |
| 201 | - } | |
| 202 | - } | |
| 203 | - fclose($fpo); | |
| 204 | - | |
| 205 | - echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . '");</script>'.$yn; | |
| 206 | - echo '<div class="error_log">'.$yn; | |
| 207 | - ob_flush(); | |
| 208 | - flush(); | |
| 209 | - | |
| 210 | - foreach($orglines as $line){ | |
| 211 | - $line = trim($line); | |
| 212 | - if( 0 !== strpos( $line, 'Post ID' ) && !empty($line) ) | |
| 213 | - $lines[] = $line; | |
| 214 | - } | |
| 215 | - $total_num = count($lines); | |
| 216 | - | |
| 217 | - $readytime = microtime(true); | |
| 218 | - //reg loop | |
| 219 | - foreach($lines as $rows_num => $line){ | |
| 220 | - | |
| 221 | - | |
| 222 | - /*////////////////////////////////////////*/ | |
| 223 | - // lineReady | |
| 224 | - /*////////////////////////////////////////*/ | |
| 225 | - $linestart = microtime(true); | |
| 226 | - $datas = array(); | |
| 227 | - | |
| 228 | - $logtemp = ''; | |
| 229 | - $mestemp = ''; | |
| 230 | - $line = trim($line); | |
| 231 | - if( empty($line) ) continue; | |
| 232 | - | |
| 233 | - $d = explode($sp, $line); | |
| 234 | - $buf = ''; | |
| 235 | - foreach($d as $data) { | |
| 236 | - $num = substr_count($data, '"'); | |
| 237 | - if( 0 == $num % 2 && '' == $buf ){ | |
| 238 | - if( '"' == substr($data, 0, 1) ) | |
| 239 | - $data = substr($data, 1); | |
| 240 | - if( '"' == substr($data, -1) ) | |
| 241 | - $data = substr($data, 0, -1); | |
| 242 | - $data = str_replace(array('""'), '"', $data); | |
| 243 | - $datas[] = ( false !== $data ) ? $data : ''; | |
| 244 | - }elseif( 1 == $num % 2 && '' == $buf ){ | |
| 245 | - $buf .= $data; | |
| 246 | - }elseif( 0 == $num % 2 && '' != $buf ){ | |
| 247 | - $buf .= $sp.$data; | |
| 248 | - }elseif( 1 == $num % 2 && '' != $buf ){ | |
| 249 | - $buf .= $sp.$data; | |
| 250 | - if( '"' == substr($buf, 0, 1) ) | |
| 251 | - $buf = substr($buf, 1); | |
| 252 | - if( '"' == substr($buf, -1) ) | |
| 253 | - $buf = substr($buf, 0, -1); | |
| 254 | - $buf = str_replace(array('""'), '"', $buf); | |
| 255 | - $datas[] = ( false !== $buf ) ? $buf : ''; | |
| 256 | - $buf = ''; | |
| 257 | - } | |
| 258 | - } | |
| 259 | - | |
| 260 | - if( 'Post ID' == $datas[USCES_COL_POST_ID] ) | |
| 261 | - continue; | |
| 262 | - | |
| 263 | - | |
| 264 | - if( $min_field_num > count($datas) ){ | |
| 265 | - $err_num++; | |
| 266 | - $mes = "No." . ($rows_num+1)." ".count($datas) . "\t".__('The number of the columns is abnormal.', 'usces'); | |
| 267 | - $logtemp .= $mes.$yn; | |
| 268 | - $log .= $logtemp; | |
| 269 | - echo $mes.$br.$yn; | |
| 270 | - continue; | |
| 271 | - } | |
| 272 | - | |
| 273 | - if( $pre_code == $datas[USCES_COL_ITEM_CODE] && WCUtils::is_blank($datas[USCES_COL_POST_ID]) ){ | |
| 274 | - $mode = 'add'; | |
| 275 | - | |
| 276 | - }else{ | |
| 277 | - $post_id = ( !WCUtils::is_blank($datas[USCES_COL_POST_ID]) ) ? (int)$datas[USCES_COL_POST_ID] : NULL; | |
| 278 | - if( $post_id ){ | |
| 279 | - $db_res = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $post_id) ); | |
| 280 | - if( !$db_res ){ | |
| 281 | - $err_num++; | |
| 282 | - $mes = "No." . ($rows_num+1) . "\t".__("Post-ID {$post_id} does not exist in the database.", 'usces'); | |
| 283 | - $logtemp .= $mes.$yn; | |
| 284 | - $log .= $logtemp; | |
| 285 | - echo $mes.$br.$yn; | |
| 286 | - continue; | |
| 287 | - } | |
| 288 | - } | |
| 289 | - if( $post_id ){ | |
| 290 | - $mode = 'upd'; | |
| 291 | - }else{ | |
| 292 | - $mode = 'add'; | |
| 293 | - } | |
| 294 | - } | |
| 295 | - | |
| 296 | - $lineready = microtime(true); | |
| 297 | - $linereadytime += $lineready-$linestart; | |
| 298 | - /*////////////////////////////////////////*/ | |
| 299 | - // dataCheck | |
| 300 | - /*////////////////////////////////////////*/ | |
| 301 | - //data check loop | |
| 302 | - foreach($datas as $key => $data){ | |
| 303 | - $data = trim(mb_convert_encoding($data, 'UTF-8', 'SJIS')); | |
| 304 | - switch($key){ | |
| 305 | - case USCES_COL_ITEM_CODE: | |
| 306 | - if( 0 == strlen($data) ){ | |
| 307 | - $mes = "No." . ($rows_num+1) . "\t".__('An item cord is non-input.', 'usces'); | |
| 308 | - $logtemp .= $mes.$yn; | |
| 309 | - $mestemp .= $mes.$br.$yn; | |
| 310 | - }else{ | |
| 311 | - $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta} | |
| 312 | - LEFT JOIN {$wpdb->posts} ON ID = post_id | |
| 313 | - WHERE meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')", | |
| 314 | - '_itemCode', $data); | |
| 315 | - $db_res1 = $wpdb->get_results( $query, ARRAY_A ); | |
| 316 | - if( 'upd' == $mode ){ | |
| 317 | - if( 1 < count($db_res1) ){ | |
| 318 | - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has been duplicated.', 'usces'); | |
| 319 | - $logtemp .= $mes.$yn; | |
| 320 | - $mestemp .= $mes.$br.$yn; | |
| 321 | - $mes = ''; | |
| 322 | - foreach($db_res1 as $res_val){ | |
| 323 | - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />"; | |
| 324 | - } | |
| 325 | - $logtemp .= $mes.$yn; | |
| 326 | - $mestemp .= $mes.$br.$yn; | |
| 327 | - } | |
| 328 | - $query = $wpdb->prepare("SELECT meta_id, post_id FROM {$wpdb->postmeta} | |
| 329 | - LEFT JOIN {$wpdb->posts} ON ID = post_id | |
| 330 | - WHERE post_id <> %d AND meta_key = %s AND meta_value = %s AND (post_status = 'pending' OR post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR post_status = 'future')", | |
| 331 | - $post_id, '_itemCode', $data); | |
| 332 | - $db_res2 = $wpdb->get_results( $query, ARRAY_A ); | |
| 333 | - if( 0 < count($db_res2) ){ | |
| 334 | - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces'); | |
| 335 | - $logtemp .= $mes.$yn; | |
| 336 | - $mestemp .= $mes.$br.$yn; | |
| 337 | - $mes = ''; | |
| 338 | - foreach($db_res2 as $res_val){ | |
| 339 | - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />"; | |
| 340 | - } | |
| 341 | - $logtemp .= $mes.$yn; | |
| 342 | - $mestemp .= $mes.$br.$yn; | |
| 343 | - } | |
| 344 | - }else if( 'add' == $mode ){ | |
| 345 | - if( $data != $pre_code ) { | |
| 346 | - if( 0 < count($db_res1) ){ | |
| 347 | - $mes = "No." . ($rows_num+1) . "\t".__('This Item-Code has already been used.', 'usces'); | |
| 348 | - $logtemp .= $mes.$yn; | |
| 349 | - $mestemp .= $mes.$br.$yn; | |
| 350 | - $mes = ''; | |
| 351 | - foreach($db_res1 as $res_val){ | |
| 352 | - $mes .= "meta_id=" . $res_val['meta_id'] . ", post_id=" . $res_val['post_id'] . "<br />"; | |
| 353 | - } | |
| 354 | - $logtemp .= $mes.$yn; | |
| 355 | - $mestemp .= $mes.$br.$yn; | |
| 356 | - } | |
| 357 | - } | |
| 358 | - } | |
| 359 | - } | |
| 360 | - break; | |
| 361 | - case USCES_COL_ITEM_NAME: | |
| 362 | - if( 0 == strlen($data) ){ | |
| 363 | - $mes = "No." . ($rows_num+1) . "\t".__('An item name is non-input.', 'usces'); | |
| 364 | - $logtemp .= $mes.$yn; | |
| 365 | - $mestemp .= $mes.$br.$yn; | |
| 366 | - } | |
| 367 | - break; | |
| 368 | - case USCES_COL_ITEM_RESTRICTION: | |
| 369 | - if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){ | |
| 370 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the purchase limit number is abnormal.', 'usces'); | |
| 371 | - $logtemp .= $mes.$yn; | |
| 372 | - $mestemp .= $mes.$br.$yn; | |
| 373 | - } | |
| 374 | - break; | |
| 375 | - case USCES_COL_ITEM_POINTRATE: | |
| 376 | - if( !preg_match("/^[0-9]+$/", $data) ){ | |
| 377 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the point rate is abnormal.', 'usces'); | |
| 378 | - $logtemp .= $mes.$yn; | |
| 379 | - $mestemp .= $mes.$br.$yn; | |
| 380 | - } | |
| 381 | - break; | |
| 382 | - case USCES_COL_ITEM_GPNUM1: | |
| 383 | - if( !preg_match("/^[0-9]+$/", $data) ){ | |
| 384 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('umerical value is abnormality.', 'usces'); | |
| 385 | - $logtemp .= $mes.$yn; | |
| 386 | - $mestemp .= $mes.$br.$yn; | |
| 387 | - } | |
| 388 | - break; | |
| 389 | - case USCES_COL_ITEM_GPDIS1: | |
| 390 | - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM1] && 1 > $data ) ){ | |
| 391 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."1-".__('rate is abnormal.', 'usces'); | |
| 392 | - $logtemp .= $mes.$yn; | |
| 393 | - $mestemp .= $mes.$br.$yn; | |
| 394 | - } | |
| 395 | - break; | |
| 396 | - case USCES_COL_ITEM_GPNUM2: | |
| 397 | - if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM1] >= $data && 0 != $data) ){ | |
| 398 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('umerical value is abnormality.', 'usces'); | |
| 399 | - $logtemp .= $mes.$yn; | |
| 400 | - $mestemp .= $mes.$br.$yn; | |
| 401 | - } | |
| 402 | - break; | |
| 403 | - case USCES_COL_ITEM_GPDIS2: | |
| 404 | - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM2] && 1 > $data ) ){ | |
| 405 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."2-".__('rate is abnormal.', 'usces'); | |
| 406 | - $logtemp .= $mes.$yn; | |
| 407 | - $mestemp .= $mes.$br.$yn; | |
| 408 | - } | |
| 409 | - break; | |
| 410 | - case USCES_COL_ITEM_GPNUM3: | |
| 411 | - if( !preg_match("/^[0-9]+$/", $data) || ($datas[USCES_COL_ITEM_GPNUM2] >= $data && 0 != $data) ){ | |
| 412 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('umerical value is abnormality.', 'usces'); | |
| 413 | - $logtemp .= $mes.$yn; | |
| 414 | - $mestemp .= $mes.$br.$yn; | |
| 415 | - } | |
| 416 | - break; | |
| 417 | - case USCES_COL_ITEM_GPDIS3: | |
| 418 | - if( !preg_match("/^[0-9]+$/", $data) || ( 0 < $datas[USCES_COL_ITEM_GPNUM3] && 1 > $data ) ){ | |
| 419 | - $mes = "No." . ($rows_num+1) . "\t".__('Business package discount', 'usces')."3-".__('rate is abnormal.', 'usces'); | |
| 420 | - $logtemp .= $mes.$yn; | |
| 421 | - $mestemp .= $mes.$br.$yn; | |
| 422 | - } | |
| 423 | - break; | |
| 424 | - case USCES_COL_ITEM_SHIPPING: | |
| 425 | - if( !preg_match("/^[0-9]+$/", $data) || 9 < $data ){ | |
| 426 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the shipment day is abnormal.', 'usces'); | |
| 427 | - $logtemp .= $mes.$yn; | |
| 428 | - $mestemp .= $mes.$br.$yn; | |
| 429 | - } | |
| 430 | - break; | |
| 431 | - case USCES_COL_ITEM_DELIVERYMETHOD: | |
| 432 | - if( 0 === strlen($data) || !preg_match("/^[0-9;]+$/", $data) ){ | |
| 433 | - $mes = "No." . ($rows_num+1) . "\t".__('Invalid value of Delivery method.', 'usces'); | |
| 434 | - $logtemp .= $mes.$yn; | |
| 435 | - $mestemp .= $mes.$br.$yn; | |
| 436 | - } | |
| 437 | - break; | |
| 438 | - case USCES_COL_ITEM_SHIPPINGCHARGE: | |
| 439 | - if( 0 === strlen($data) || !preg_match("/^[0-9]+$/", $data) ){ | |
| 440 | - $mes = "No." . ($rows_num+1) . "\t".__('Invalid type of shipping charge.', 'usces'); | |
| 441 | - $logtemp .= $mes.$yn; | |
| 442 | - $mestemp .= $mes.$br.$yn; | |
| 443 | - } | |
| 444 | - break; | |
| 445 | - case USCES_COL_ITEM_INDIVIDUALSCHARGE: | |
| 446 | - if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){ | |
| 447 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the postage individual charging is abnormal.', 'usces'); | |
| 448 | - $logtemp .= $mes.$yn; | |
| 449 | - $mestemp .= $mes.$br.$yn; | |
| 450 | - } | |
| 451 | - break; | |
| 452 | - case USCES_COL_POST_ID: | |
| 453 | - if( !preg_match("/^[0-9]+$/", $data) && 0 != strlen($data) ){ | |
| 454 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the Post-ID is abnormal.', 'usces'); | |
| 455 | - $logtemp .= $mes.$yn; | |
| 456 | - $mestemp .= $mes.$br.$yn; | |
| 457 | - } | |
| 458 | - break; | |
| 459 | - case USCES_COL_POST_AUTHOR: | |
| 460 | - case USCES_COL_POST_COMMENT_STATUS: | |
| 461 | - case USCES_COL_POST_PASSWORD: | |
| 462 | - case USCES_COL_POST_NAME: | |
| 463 | - case USCES_COL_POST_TITLE: | |
| 464 | - case USCES_COL_POST_CONTENT: | |
| 465 | - case USCES_COL_POST_EXCERPT: | |
| 466 | - break; | |
| 467 | - case USCES_COL_POST_STATUS: | |
| 468 | - $array17 = array('publish', 'future', 'draft', 'pending', 'private'); | |
| 469 | - if( !in_array($data, $array17) || WCUtils::is_blank($data) ){ | |
| 470 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the display status is abnormal.', 'usces'); | |
| 471 | - $logtemp .= $mes.$yn; | |
| 472 | - $mestemp .= $mes.$br.$yn; | |
| 473 | - } | |
| 474 | - break; | |
| 475 | - case USCES_COL_POST_MODIFIED: | |
| 476 | - if( 'future' == $datas[USCES_COL_POST_STATUS] && (WCUtils::is_blank($data) || '0000-00-00 00:00:00' === $data) ){ | |
| 477 | - if( preg_match($date_pattern, $data, $match) ){ | |
| 478 | - if( checkdate($match[2], $match[3], $match[1]) && | |
| 479 | - (0 < $match[4] && 24 > $match[4]) && | |
| 480 | - (0 < $match[5] && 60 > $match[5]) && | |
| 481 | - (0 < $match[6] && 60 > $match[6]) ){ | |
| 482 | - $mes = ""; | |
| 483 | - }else{ | |
| 484 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces'); | |
| 485 | - $logtemp .= $mes.$yn; | |
| 486 | - $mestemp .= $mes.$br.$yn; | |
| 487 | - } | |
| 488 | - | |
| 489 | - }else{ | |
| 490 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces'); | |
| 491 | - $logtemp .= $mes.$yn; | |
| 492 | - $mestemp .= $mes.$br.$yn; | |
| 493 | - } | |
| 494 | - }else if( !WCUtils::is_blank($data) && '0000-00-00 00:00:00' !== $data ){ | |
| 495 | - if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//先頭4桁が数値のみ | |
| 496 | - if(strtotime($data) === false){ | |
| 497 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces'); | |
| 498 | - $logtemp .= $mes.$yn; | |
| 499 | - $mestemp .= $mes.$br.$yn; | |
| 500 | - } | |
| 501 | - } else { | |
| 502 | - $datetime = explode(' ', $data); | |
| 503 | - $date_str = usces_dates_interconv($datetime[0]).' '.$datetime[1]; | |
| 504 | - if(strtotime($date_str) === false){ | |
| 505 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the schedule is abnormal.', 'usces'); | |
| 506 | - $logtemp .= $mes.$yn; | |
| 507 | - $mestemp .= $mes.$br.$yn; | |
| 508 | - } | |
| 509 | - } | |
| 510 | - } | |
| 511 | - break; | |
| 512 | - case USCES_COL_CATEGORY: | |
| 513 | - if( 0 == strlen($data) ){ | |
| 514 | - $mes = "No." . ($rows_num+1) . "\t".__('A category is non-input.', 'usces'); | |
| 515 | - $logtemp .= $mes.$yn; | |
| 516 | - $mestemp .= $mes.$br.$yn; | |
| 517 | - } | |
| 518 | - break; | |
| 519 | - case USCES_COL_POST_TAG: | |
| 520 | - break; | |
| 521 | - case USCES_COL_CUSTOM_FIELD: | |
| 522 | - break; | |
| 523 | - case USCES_COL_SKU_CODE: | |
| 524 | - if( 0 == strlen($data) ){ | |
| 525 | - $mes = "No." . ($rows_num+1) . "\t".__('A SKU cord is non-input.', 'usces'); | |
| 526 | - $logtemp .= $mes.$yn; | |
| 527 | - $mestemp .= $mes.$br.$yn; | |
| 528 | - } | |
| 529 | - break; | |
| 530 | - case USCES_COL_SKU_NAME: | |
| 531 | - break; | |
| 532 | - case USCES_COL_SKU_CPRICE: | |
| 533 | - if( 0 < strlen($data) and !preg_match("/^\d$|^\d+\.?\d+$/", $data) ){ | |
| 534 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the price is abnormal.', 'usces'); | |
| 535 | - $logtemp .= $mes.$yn; | |
| 536 | - $mestemp .= $mes.$br.$yn; | |
| 537 | - } | |
| 538 | - break; | |
| 539 | - case USCES_COL_SKU_PRICE: | |
| 540 | - if( !preg_match("/^\d$|^\d+\.?\d+$/", $data) || 0 == strlen($data) ){ | |
| 541 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the normal price is abnormal.', 'usces'); | |
| 542 | - $logtemp .= $mes.$yn; | |
| 543 | - $mestemp .= $mes.$br.$yn; | |
| 544 | - } | |
| 545 | - break; | |
| 546 | - case USCES_COL_SKU_ZAIKONUM: | |
| 547 | - if( 0 < strlen($data) and !preg_match("/^[0-9]+$/", $data) ){ | |
| 548 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock amount is abnormal.', 'usces'); | |
| 549 | - $logtemp .= $mes.$yn; | |
| 550 | - $mestemp .= $mes.$br.$yn; | |
| 551 | - } | |
| 552 | - break; | |
| 553 | - case USCES_COL_SKU_ZAIKO: | |
| 554 | - if( !preg_match("/^[0-9]+$/", $data) || 4 < $data ){ | |
| 555 | - $mes = "No." . ($rows_num+1) . "\t".__('A value of the stock status is abnormal.', 'usces'); | |
| 556 | - $logtemp .= $mes.$yn; | |
| 557 | - $mestemp .= $mes.$br.$yn; | |
| 558 | - } | |
| 559 | - break; | |
| 560 | - case USCES_COL_SKU_UNIT: | |
| 561 | - break; | |
| 562 | - case USCES_COL_SKU_GPTEKIYO: | |
| 563 | - if( !preg_match("/^[0-9]+$/", $data) || 1 < $data ){ | |
| 564 | - $mes = "No." . ($rows_num+1) . "\t".__('The value of the duties pack application is abnormal.', 'usces'); | |
| 565 | - $logtemp .= $mes.$yn; | |
| 566 | - $mestemp .= $mes.$br.$yn; | |
| 567 | - } | |
| 568 | - break; | |
| 569 | - } | |
| 570 | - } | |
| 571 | - $opnum = ceil((count($datas) - $min_field_num) / 4); | |
| 572 | - for($i=0; $i<$opnum; $i++){ | |
| 573 | - $val = array(); | |
| 574 | - $oplogtemp = ''; | |
| 575 | - $opmestemp = ''; | |
| 576 | - for($o=1; $o<=4; $o++){ | |
| 577 | - $key = ($min_field_num-1)+$o+($i*4); | |
| 578 | - if( isset($datas[$key]) ){ | |
| 579 | - $value = trim($datas[$key]); | |
| 580 | - }else{ | |
| 581 | - $value = NULL; | |
| 582 | - } | |
| 583 | - switch($o){ | |
| 584 | - case 1: | |
| 585 | - if( empty($value) ){ | |
| 586 | - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$yn; | |
| 587 | - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option name of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn; | |
| 588 | - } | |
| 589 | - $val['name'] = $value; | |
| 590 | - break; | |
| 591 | - case 2: | |
| 592 | - if( $value != NULL && ((0 != (int)$value) and (1 != (int)$value) and (2 != (int)$value) and (5 != (int)$value)) ){ | |
| 593 | - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$yn; | |
| 594 | - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-entry-field of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn; | |
| 595 | - } | |
| 596 | - $val['mean'] = $value; | |
| 597 | - break; | |
| 598 | - case 3: | |
| 599 | - if( $value != NULL && (!preg_match("/^[0-9]+$/", $value) || 1 < (int)$value) ){ | |
| 600 | - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$yn; | |
| 601 | - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-required-item of No.%s option is abnormal.', ($i+1)), 'usces').$br.$yn; | |
| 602 | - } | |
| 603 | - $val['essential'] = $value; | |
| 604 | - break; | |
| 605 | - case 4: | |
| 606 | - if( ($value != NULL && $value == '') && (2 > $datas[($key-2)] && 0 < strlen($datas[($key-2)])) ){ | |
| 607 | - $oplogtemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$yn; | |
| 608 | - $opmestemp .= "No." . ($rows_num+1) . "\t" . __(sprintf('Option-select of No.%s option is non-input.', ($i+1)), 'usces').$br.$yn; | |
| 609 | - } | |
| 610 | - $val['value'] = $value; | |
| 611 | - break; | |
| 612 | - } | |
| 613 | - } | |
| 614 | - if( !WCUtils::is_blank($val['name']) || !WCUtils::is_blank($val['mean']) || !WCUtils::is_blank($val['essential']) || !WCUtils::is_blank($val['value']) ){ | |
| 615 | - $logtemp .= $oplogtemp; | |
| 616 | - $mestemp .= $opmestemp; | |
| 617 | - } | |
| 618 | - } | |
| 619 | - if( 0 < strlen($logtemp) ){ | |
| 620 | - $err_num++; | |
| 621 | - $log .= $logtemp; | |
| 622 | - echo $mestemp; | |
| 623 | - continue; | |
| 624 | - } | |
| 625 | - | |
| 626 | - | |
| 627 | - | |
| 628 | - /******************/ | |
| 629 | - $checkend = microtime(true); | |
| 630 | - $checktime += $checkend-$lineready; | |
| 631 | - /*////////////////////////////////////////*/ | |
| 632 | - // insPost | |
| 633 | - /*////////////////////////////////////////*/ | |
| 634 | - //wp_posts data reg; | |
| 635 | - $cdatas = array(); | |
| 636 | - $post_fields = array(); | |
| 637 | - $sku = array(); | |
| 638 | - $opt = array(); | |
| 639 | - $valstr = ''; | |
| 640 | - | |
| 641 | - | |
| 642 | - if( $pre_code != $datas[USCES_COL_ITEM_CODE] ){ | |
| 643 | - $sku_index = 0; | |
| 644 | - | |
| 645 | - $cdatas['ID'] = $post_id; | |
| 646 | - | |
| 647 | - $data = $datas[USCES_COL_POST_MODIFIED]; | |
| 648 | - if( $data == '' || $data == '0000-00-00 00:00:00' ){ | |
| 649 | - $cdatas['post_date'] = current_time( 'mysql' ); | |
| 650 | - $cdatas['post_date_gmt'] = current_time( 'mysql', 1 ); | |
| 651 | - $cdatas['post_modified'] = current_time( 'mysql' ); | |
| 652 | - $cdatas['post_modified_gmt'] = current_time( 'mysql', 1 ); | |
| 653 | - }else{ | |
| 654 | - if(preg_match("/^[0-9]+$/", substr($data,0,4))) {//先頭4桁が数値のみ | |
| 655 | - $time_data =strtotime($data); | |
| 656 | - } else { | |
| 657 | - $datetime = explode(' ', $data); | |
| 658 | - $date_str = usces_dates_interconv( $datetime[0] ).' '.$datetime[1]; | |
| 659 | - $time_data =strtotime($date_str); | |
| 660 | - } | |
| 661 | - $cdatas['post_date'] = date('Y-m-d H:i:s', $time_data); | |
| 662 | - $cdatas['post_date_gmt'] = gmdate('Y-m-d H:i:s', $time_data); | |
| 663 | - $cdatas['post_modified'] = date('Y-m-d H:i:s', $time_data); | |
| 664 | - $cdatas['post_modified_gmt'] = gmdate('Y-m-d H:i:s', $time_data); | |
| 665 | - } | |
| 666 | -//usces_log('mysql2date : '.mysql2date('U', $cdatas['post_modified'], false) . '/' . mysql2date('U', current_time( 'mysql' ), false), 'acting_transaction.log'); | |
| 667 | - if ( 'publish' == $datas[USCES_COL_POST_STATUS] ) { | |
| 668 | - $now = current_time( 'mysql' ); | |
| 669 | - if ( mysql2date('U', $cdatas['post_modified'], false) > mysql2date('U', $now, false) ) | |
| 670 | - $datas[USCES_COL_POST_STATUS] = 'future'; | |
| 671 | - } elseif( 'future' == $datas[USCES_COL_POST_STATUS] ) { | |
| 672 | - $now = current_time( 'mysql' ); | |
| 673 | - if ( mysql2date('U', $cdatas['post_modified'], false) <= mysql2date('U', $now, false) ) | |
| 674 | - $datas[USCES_COL_POST_STATUS] = 'publish'; | |
| 675 | - } | |
| 676 | - | |
| 677 | - $cdatas['ID'] = $post_id; | |
| 678 | - $cdatas['post_author'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_AUTHOR]) ) ? $datas[USCES_COL_POST_AUTHOR] : 1; | |
| 679 | - $cdatas['post_content'] = trim(mb_convert_encoding($datas[USCES_COL_POST_CONTENT], 'UTF-8', 'SJIS')); | |
| 680 | - $cdatas['post_title'] = trim(mb_convert_encoding($datas[USCES_COL_POST_TITLE], 'UTF-8', 'SJIS')); | |
| 681 | - $cdatas['post_excerpt'] = trim(mb_convert_encoding($datas[USCES_COL_POST_EXCERPT], 'UTF-8', 'SJIS')); | |
| 682 | - $cdatas['post_status'] = $datas[USCES_COL_POST_STATUS]; | |
| 683 | - $cdatas['comment_status'] = ( !WCUtils::is_blank($datas[USCES_COL_POST_COMMENT_STATUS]) ) ? $datas[USCES_COL_POST_COMMENT_STATUS] : 'close'; | |
| 684 | - $cdatas['ping_status'] = 'close'; | |
| 685 | - $cdatas['post_password'] = ( 'private' == $cdatas['post_status'] ) ? '' : $datas[USCES_COL_POST_PASSWORD]; | |
| 686 | - $cdatas['post_type'] = 'post'; | |
| 687 | - $cdatas['post_parent'] = 0; | |
| 688 | - $cdatas['post_name'] = urlencode(wp_unique_post_slug(trim(mb_convert_encoding($datas[USCES_COL_POST_NAME], 'UTF-8', 'SJIS')), $cdatas['ID'], $cdatas['post_status'], $cdatas['post_type'], $cdatas['post_parent'])); | |
| 689 | - $cdatas['to_ping'] = ''; | |
| 690 | - $cdatas['pinged'] = ''; | |
| 691 | - $cdatas['menu_order'] = 0; | |
| 692 | - $cdatas['post_mime_type'] = 'item'; | |
| 693 | - $cdatas['post_content_filtered'] = ''; | |
| 694 | - | |
| 695 | - | |
| 696 | - if ( empty($cdatas['post_name']) && !in_array( $cdatas['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { | |
| 697 | - $cdatas['post_name'] = sanitize_title($cdatas['post_title'], $post_id); | |
| 698 | - } | |
| 699 | - | |
| 700 | - if($mode == 'add') { | |
| 701 | - | |
| 702 | - $cdatas['guid'] = ''; | |
| 703 | - if ( false === $wpdb->insert( $wpdb->posts, $cdatas ) ) { | |
| 704 | - $err_num++; | |
| 705 | - $mes = "No." . ($rows_num+1) . "\t".__('This data was not registered in the database.', 'usces'); | |
| 706 | - $log .= $mes.$yn; | |
| 707 | - echo $mes.$br.$yn; | |
| 708 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 709 | - continue; | |
| 710 | - } | |
| 711 | - $post_id = $wpdb->insert_id; | |
| 712 | - $where = array( 'ID' => $post_id ); | |
| 713 | - $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_id ) ), $where ); | |
| 714 | - | |
| 715 | - /******************/ | |
| 716 | - $insert_post = microtime(true); | |
| 717 | - $insertposttime += $insert_post-$checkend; | |
| 718 | - | |
| 719 | - }elseif($mode == 'upd') { | |
| 720 | - | |
| 721 | - $where = array( 'ID' => $post_id ); | |
| 722 | - if ( false === $wpdb->update( $wpdb->posts, $cdatas, $where ) ) { | |
| 723 | - $err_num++; | |
| 724 | - $mes = "No." . ($rows_num+1) . "\t".__('The data were not registered with a database.', 'usces'); | |
| 725 | - $log .= $mes.$yn; | |
| 726 | - echo $mes.$br.$yn; | |
| 727 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 728 | - continue; | |
| 729 | - } | |
| 730 | - | |
| 731 | - /******************/ | |
| 732 | - $insert_post = microtime(true); | |
| 733 | - $insertposttime += $insert_post-$checkend; | |
| 734 | - //end of wp_insert_post | |
| 735 | - /************************************************************************************************/ | |
| 736 | - /*////////////////////////////////////////*/ | |
| 737 | - // delMeta | |
| 738 | - /*////////////////////////////////////////*/ | |
| 739 | - | |
| 740 | - //delete all metas of Item | |
| 741 | - //$query = $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE ( meta_key = '_iopt_' OR meta_key = '_isku_' OR ( SUBSTRING(meta_key,1,5) = '_item' AND SUBSTRING(meta_key,1,6) <> '_item_') ) AND post_id = %d", $post_id); | |
| 742 | - $query = $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_key <> '_thumbnail_id' AND post_id = %d", $post_id); | |
| 743 | - $dbres = $wpdb->query( $query ); | |
| 744 | - if( $dbres === false ) { | |
| 745 | - $err_num++; | |
| 746 | - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete postmeta', 'usces'); | |
| 747 | - $log .= $mes.$yn; | |
| 748 | - echo $mes.$br.$yn; | |
| 749 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 750 | - continue; | |
| 751 | - } | |
| 752 | - // delete Item revisions | |
| 753 | - $query = $wpdb->prepare("DELETE FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $post_id, 'revision'); | |
| 754 | - $dbres = $wpdb->query( $query ); | |
| 755 | - if( $dbres === false ) { | |
| 756 | - $err_num++; | |
| 757 | - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete revisions', 'usces'); | |
| 758 | - $log .= $mes.$yn; | |
| 759 | - echo $mes.$br.$yn; | |
| 760 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 761 | - continue; | |
| 762 | - } | |
| 763 | - // delete relationships of category | |
| 764 | - $query = $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d", $post_id); | |
| 765 | - $dbres = $wpdb->query( $query ); | |
| 766 | - if( $dbres === false ) { | |
| 767 | - $err_num++; | |
| 768 | - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(category)', 'usces'); | |
| 769 | - $log .= $mes.$yn; | |
| 770 | - echo $mes.$br.$yn; | |
| 771 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 772 | - continue; | |
| 773 | - } | |
| 774 | - // delete relationships of tag | |
| 775 | - $query = "SELECT term_taxonomy_id, COUNT(*) AS ct FROM $wpdb->term_relationships GROUP BY term_taxonomy_id"; | |
| 776 | - $relation_data = $wpdb->get_results( $query, ARRAY_A ); | |
| 777 | - foreach((array)$relation_data as $relation_rows) { | |
| 778 | - $term_taxonomy_ids['term_taxonomy_id'] = $relation_rows['term_taxonomy_id']; | |
| 779 | - $term_taxonomy_updatas['count'] = $relation_rows['ct']; | |
| 780 | - $dbres = $wpdb->update( $wpdb->term_taxonomy, $term_taxonomy_updatas, $term_taxonomy_ids ); | |
| 781 | - if( $dbres === false ) { | |
| 782 | - $err_num++; | |
| 783 | - $mes = "No." . ($rows_num+1) . "\t".__('Error : delete term_relationships(tag)', 'usces'); | |
| 784 | - $log .= $mes.$yn; | |
| 785 | - echo $mes.$br.$yn; | |
| 786 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 787 | - continue; | |
| 788 | - } | |
| 789 | - } | |
| 790 | - } | |
| 791 | - | |
| 792 | - /******************/ | |
| 793 | - $metadelete = microtime(true); | |
| 794 | - $metadeletetime += $metadelete-$insert_post; | |
| 795 | - /*////////////////////////////////////////*/ | |
| 796 | - // addMeta | |
| 797 | - /*////////////////////////////////////////*/ | |
| 798 | - //add postmeta | |
| 799 | - $itemDeliveryMethod = explode(';', $datas[USCES_COL_ITEM_DELIVERYMETHOD]); | |
| 800 | - $valstr .= '(' . $post_id . ", '_itemCode','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_CODE], 'UTF-8', 'SJIS'))) . "'),"; | |
| 801 | - $valstr .= '(' . $post_id . ", '_itemName','" . mysql_real_escape_string(trim(mb_convert_encoding($datas[USCES_COL_ITEM_NAME], 'UTF-8', 'SJIS'))) . "'),"; | |
| 802 | - $valstr .= '(' . $post_id . ", '_itemRestriction','" . $datas[USCES_COL_ITEM_RESTRICTION] . "'),"; | |
| 803 | - $valstr .= '(' . $post_id . ", '_itemPointrate','" . $datas[USCES_COL_ITEM_POINTRATE] . "'),"; | |
| 804 | - $valstr .= '(' . $post_id . ", '_itemGpNum1','" . $datas[USCES_COL_ITEM_GPNUM1] . "'),"; | |
| 805 | - $valstr .= '(' . $post_id . ", '_itemGpDis1','" . $datas[USCES_COL_ITEM_GPDIS1] . "'),"; | |
| 806 | - $valstr .= '(' . $post_id . ", '_itemGpNum2','" . $datas[USCES_COL_ITEM_GPNUM2] . "'),"; | |
| 807 | - $valstr .= '(' . $post_id . ", '_itemGpDis2','" . $datas[USCES_COL_ITEM_GPDIS2] . "'),"; | |
| 808 | - $valstr .= '(' . $post_id . ", '_itemGpNum3','" . $datas[USCES_COL_ITEM_GPNUM3] . "'),"; | |
| 809 | - $valstr .= '(' . $post_id . ", '_itemGpDis3','" . $datas[USCES_COL_ITEM_GPDIS3] . "'),"; | |
| 810 | - $valstr .= '(' . $post_id . ", '_itemShipping','" . $datas[USCES_COL_ITEM_SHIPPING] . "'),"; | |
| 811 | - $valstr .= '(' . $post_id . ", '_itemDeliveryMethod','" . mysql_real_escape_string(serialize($itemDeliveryMethod)) . "'),"; | |
| 812 | - $valstr .= '(' . $post_id . ", '_itemShippingCharge','" . $datas[USCES_COL_ITEM_SHIPPINGCHARGE] . "'),"; | |
| 813 | - $valstr .= '(' . $post_id . ", '_itemIndividualSCharge','" . $datas[USCES_COL_ITEM_INDIVIDUALSCHARGE] . "'),"; | |
| 814 | - | |
| 815 | - //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($meta_key)."', '" . mysql_real_escape_string(serialize($sku)) . "'),"; | |
| 816 | - | |
| 817 | -// print_r($valstr); | |
| 818 | - $valstr = rtrim($valstr, ','); | |
| 819 | - $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr"); | |
| 820 | -// $dbres = mysql_query($query) or die(mysql_error()); | |
| 821 | - | |
| 822 | - /*////////////////////////////////////////*/ | |
| 823 | - // Set Taxonomy | |
| 824 | - /*////////////////////////////////////////*/ | |
| 825 | -// $post_categories = explode(';', $datas[USCES_COL_CATEGORY]); | |
| 826 | -// if ( is_object_in_taxonomy($cdatas['post_type'], 'category') ) | |
| 827 | -// wp_set_post_categories( $post_id, $post_category ); | |
| 828 | -// | |
| 829 | -// $tags_input = explode(';', trim(mb_convert_encoding($datas[USCES_COL_POST_TAG], 'UTF-8', 'SJIS'))); | |
| 830 | -// if ( isset( $tags_input ) && is_object_in_taxonomy($cdatas['post_type'], 'post_tag') ) | |
| 831 | -// wp_set_post_tags( $post_id, $tags_input ); | |
| 832 | -// | |
| 833 | -// // new-style support for all custom taxonomies | |
| 834 | -// if ( !empty($datas[USCES_COL_POST_TAX]) ) { | |
| 835 | -// foreach ( $datas[USCES_COL_POST_TAX] as $taxonomy => $tags ) { | |
| 836 | -// $taxonomy_obj = get_taxonomy($taxonomy); | |
| 837 | -// if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical. | |
| 838 | -// $tags = array_filter($tags); | |
| 839 | -// if ( current_user_can($taxonomy_obj->cap->assign_terms) ) | |
| 840 | -// wp_set_post_terms( $post_id, $tags, $taxonomy ); | |
| 841 | -// } | |
| 842 | -// } | |
| 843 | - | |
| 844 | - | |
| 845 | - | |
| 846 | - | |
| 847 | - | |
| 848 | - //add term_relationships, edit term_taxonomy | |
| 849 | - //category | |
| 850 | - $categories = explode(';', $datas[USCES_COL_CATEGORY]); | |
| 851 | - foreach((array)$categories as $category){ | |
| 852 | - $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy | |
| 853 | - WHERE term_id = %d", $category); | |
| 854 | - $term_taxonomy_id = $wpdb->get_var( $query ); | |
| 855 | - if($term_taxonomy_id == NULL) continue; | |
| 856 | - | |
| 857 | - $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships | |
| 858 | - (object_id, term_taxonomy_id, term_order) VALUES | |
| 859 | - (%d, %d, 0)", | |
| 860 | - $post_id, $term_taxonomy_id | |
| 861 | - ); | |
| 862 | - $dbres = $wpdb->query($query); | |
| 863 | - if( !$dbres ) continue; | |
| 864 | - | |
| 865 | - $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships | |
| 866 | - WHERE term_taxonomy_id = %d", $term_taxonomy_id); | |
| 867 | - $tct = $wpdb->get_var( $query ); | |
| 868 | - | |
| 869 | - $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d | |
| 870 | - WHERE term_taxonomy_id = %d", | |
| 871 | - $tct, $term_taxonomy_id | |
| 872 | - ); | |
| 873 | - $dbres = $wpdb->query($query); | |
| 874 | - } | |
| 875 | - //tag | |
| 876 | - $tags = explode(';', trim(mb_convert_encoding($datas[USCES_COL_POST_TAG], 'UTF-8', 'SJIS'))); | |
| 877 | -// wp_set_object_terms($post_id, (array)$tags, 'post_tag'); | |
| 878 | - foreach((array)$tags as $tag){ | |
| 879 | - $tag = trim($tag, " \n\t\r\0\x0B,"); | |
| 880 | - if( empty($tag) ) | |
| 881 | - continue; | |
| 882 | - $query = $wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $tag); | |
| 883 | - $term_id = $wpdb->get_var( $query ); | |
| 884 | - //new tag | |
| 885 | - if( empty($term_id) ){ | |
| 886 | - $query = $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES | |
| 887 | - (%s, %s, 0)", $tag, urlencode($tag)); | |
| 888 | - $dbres = $wpdb->query( $query ); | |
| 889 | - if( !$dbres ) | |
| 890 | - continue; | |
| 891 | - $term_id = $wpdb->insert_id; | |
| 892 | - } | |
| 893 | - $query = $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy | |
| 894 | - WHERE term_id = %d", $term_id); | |
| 895 | - $term_taxonomy_id = $wpdb->get_var( $query ); | |
| 896 | - if( !$term_taxonomy_id ){ | |
| 897 | - $query = $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy | |
| 898 | - (term_id, taxonomy, description, parent, count) VALUES | |
| 899 | - (%d, %s, %s, 0, 0)", $term_id, 'post_tag', ''); | |
| 900 | - $dbres = $wpdb->query( $query ); | |
| 901 | - if( !$dbres ) | |
| 902 | - continue; | |
| 903 | - $term_taxonomy_id = $wpdb->insert_id; | |
| 904 | - } | |
| 905 | - $query = $wpdb->prepare("INSERT INTO $wpdb->term_relationships | |
| 906 | - (object_id, term_taxonomy_id, term_order) VALUES | |
| 907 | - (%d, %d, 0)", | |
| 908 | - $post_id, $term_taxonomy_id | |
| 909 | - ); | |
| 910 | - $dbres = $wpdb->query($query); | |
| 911 | - if( !$dbres ) continue; | |
| 912 | - | |
| 913 | - $query = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships | |
| 914 | - WHERE term_taxonomy_id = %d", $term_taxonomy_id); | |
| 915 | - $tct = $wpdb->get_var( $query ); | |
| 916 | - | |
| 917 | - $query = $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d | |
| 918 | - WHERE term_taxonomy_id = %d", | |
| 919 | - $tct, $term_taxonomy_id | |
| 920 | - ); | |
| 921 | - $dbres = $wpdb->query($query); | |
| 922 | - } | |
| 923 | - | |
| 924 | - | |
| 925 | - | |
| 926 | - | |
| 927 | - | |
| 928 | - /*////////////////////////////////////////*/ | |
| 929 | - // Add Custom Field | |
| 930 | - /*////////////////////////////////////////*/ | |
| 931 | - $cfrows = explode(';', trim(mb_convert_encoding($datas[USCES_COL_CUSTOM_FIELD], 'UTF-8', 'SJIS'))); | |
| 932 | - if( !(1 === count($cfrows) && '' == reset($cfrows)) ){ | |
| 933 | - $valstr = ''; | |
| 934 | - foreach( $cfrows as $row ){ | |
| 935 | - list($meta_key, $meta_value) = explode( '=', $row, 2 ); | |
| 936 | - if( !WCUtils::is_blank($meta_key) ) | |
| 937 | - $valstr .= $wpdb->prepare("( %d, %s, %s),", $post_id, trim($meta_key), trim($meta_value)); | |
| 938 | - } | |
| 939 | - if( !WCUtils::is_blank($valstr) ){ | |
| 940 | - $valstr = rtrim($valstr, ','); | |
| 941 | - $dbres = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES $valstr"); | |
| 942 | - } | |
| 943 | - } | |
| 944 | - | |
| 945 | - /******************/ | |
| 946 | - $add_postmeta = microtime(true); | |
| 947 | - $addmetatime += $add_postmeta-$metadelete; | |
| 948 | - /*////////////////////////////////////////*/ | |
| 949 | - // addOption | |
| 950 | - /*////////////////////////////////////////*/ | |
| 951 | - // Add Item Option | |
| 952 | - for($i=0; $i<$opnum; $i++){ | |
| 953 | - $opflg = true; | |
| 954 | - $optvalue = array(); | |
| 955 | - for($o=1; $o<=4; $o++){ | |
| 956 | - $key = ($min_field_num-1)+$o+($i*4); | |
| 957 | -// if( !isset($datas[$key]) ){ | |
| 958 | -// break 2; | |
| 959 | -// } | |
| 960 | - if( $o === 1 && $datas[$key] == '' ){ | |
| 961 | - $opflg = false; | |
| 962 | - break 1; | |
| 963 | - } | |
| 964 | - switch($o){ | |
| 965 | - case 1: | |
| 966 | - $optvalue['name'] = trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS')); | |
| 967 | - break; | |
| 968 | - case 2: | |
| 969 | - $optvalue['means'] = (int)$datas[$key]; | |
| 970 | - break; | |
| 971 | - case 3: | |
| 972 | - $optvalue['essential'] = (int)$datas[$key]; | |
| 973 | - break; | |
| 974 | - case 4: | |
| 975 | - if( !empty($datas[$key]) ) { | |
| 976 | - $optvalue['value'] = str_replace(';', "\n", trim(mb_convert_encoding($datas[$key], 'UTF-8', 'SJIS'))); | |
| 977 | - }else{ | |
| 978 | - $optvalue['value'] = ""; | |
| 979 | - } | |
| 980 | - break; | |
| 981 | - } | |
| 982 | - } | |
| 983 | - | |
| 984 | - if( $opflg && !empty($optvalue) ){ | |
| 985 | - $optvalue['sort'] = $i; | |
| 986 | - $resopt = usces_add_opt($post_id, $optvalue, false); | |
| 987 | - } | |
| 988 | - //$valstr .= '(' . $post_id . ", '".mysql_real_escape_string($ometa_key)."', '" . mysql_real_escape_string(maybe_serialize($opt)) . "'),"; | |
| 989 | - } | |
| 990 | - | |
| 991 | - }else{ | |
| 992 | - $sku_index++; | |
| 993 | - } | |
| 994 | - | |
| 995 | - /******************/ | |
| 996 | - $optionmeta = microtime(true); | |
| 997 | - $AddOptiontime += $optionmeta-$add_postmeta; | |
| 998 | - /*////////////////////////////////////////*/ | |
| 999 | - // addSku | |
| 1000 | - /*////////////////////////////////////////*/ | |
| 1001 | - // Add Item SKU | |
| 1002 | - $sku_code = trim(mb_convert_encoding($datas[USCES_COL_SKU_CODE], 'UTF-8', 'SJIS')); | |
| 1003 | - $skuvalue['code'] = $sku_code; | |
| 1004 | - $skuvalue['name'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_NAME], 'UTF-8', 'SJIS')); | |
| 1005 | - $skuvalue['cprice'] = $datas[USCES_COL_SKU_CPRICE]; | |
| 1006 | - $skuvalue['price'] = $datas[USCES_COL_SKU_PRICE]; | |
| 1007 | - $skuvalue['unit'] = trim(mb_convert_encoding($datas[USCES_COL_SKU_UNIT], 'UTF-8', 'SJIS')); | |
| 1008 | - $skuvalue['stocknum'] = $datas[USCES_COL_SKU_ZAIKONUM]; | |
| 1009 | - $skuvalue['stock'] = $datas[USCES_COL_SKU_ZAIKO]; | |
| 1010 | - $skuvalue['gp'] = $datas[USCES_COL_SKU_GPTEKIYO]; | |
| 1011 | - $skuvalue['sort'] = $sku_index; | |
| 1012 | - usces_add_sku( $post_id, $skuvalue, false ); | |
| 1013 | - | |
| 1014 | -// usces_log('skuvalue : '.$post_id.print_r($skuvalue,true), 'acting_transaction.log'); | |
| 1015 | - $comp_num++; | |
| 1016 | - $pre_code = $datas[USCES_COL_ITEM_CODE]; | |
| 1017 | - clean_post_cache($post_id); | |
| 1018 | - wp_cache_delete($post_id, 'posts'); | |
| 1019 | - wp_cache_delete($post_id, 'post_meta'); | |
| 1020 | - clean_object_term_cache($post_id, 'post'); | |
| 1021 | - /******************/ | |
| 1022 | - $addsku = microtime(true); | |
| 1023 | - $AddSKUtime += $addsku-$optionmeta; | |
| 1024 | - $onelinetime += $addsku-$linestart; | |
| 1025 | - | |
| 1026 | - if( 0 === (($rows_num+1) % 10) ){ | |
| 1027 | - $av = $onelinetime/$rows_num; | |
| 1028 | - $nt = round($av*($total_num-$rows_num)); | |
| 1029 | - if( 60 < $nt ){ | |
| 1030 | - $mtime = ceil($nt/60) . __('min', 'usces') . ($nt%60) . __('sec', 'usces') . ' '; | |
| 1031 | - }else{ | |
| 1032 | - $mtime = $nt . __('sec', 'usces') . ' '; | |
| 1033 | - } | |
| 1034 | - echo '<script type="text/javascript">changeMsg("' . __('Processing...', 'usces') . __('Remaining time:', 'usces').$mtime.'");</script>'.$yn; | |
| 1035 | - echo '<script type="text/javascript">setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn; | |
| 1036 | - ob_flush(); | |
| 1037 | - flush(); | |
| 1038 | - } | |
| 1039 | - } | |
| 1040 | - | |
| 1041 | - flock($fpi, LOCK_EX); | |
| 1042 | - fputs($fpi, mb_convert_encoding($log, 'SJIS', 'UTF-8')); | |
| 1043 | - flock($fpi, LOCK_UN); | |
| 1044 | - fclose($fpi); | |
| 1045 | - | |
| 1046 | - $res['status'] = 'success'; | |
| 1047 | - $res['message'] = __(sprintf('%2$s of %1$s lines registration completion, error on %3$s lines.',$total_num,$comp_num,$err_num), 'usces'); | |
| 1048 | - /******************/ | |
| 1049 | - $finish = microtime(true); | |
| 1050 | - usces_log('ready : '.round($readytime-$start, 4), 'acting_transaction.log'); | |
| 1051 | - usces_log('lineReady : '.round($linereadytime/$total_num, 4), 'acting_transaction.log'); | |
| 1052 | - usces_log('dataCheck : '.round($checktime/$total_num, 4), 'acting_transaction.log'); | |
| 1053 | - usces_log('insPost : '.round($insertposttime/$total_num, 4), 'acting_transaction.log'); | |
| 1054 | - usces_log('delMeta : '.round($metadeletetime/$total_num, 4), 'acting_transaction.log'); | |
| 1055 | - usces_log('addMeta : '.round($addmetatime/$total_num, 4), 'acting_transaction.log'); | |
| 1056 | - usces_log('addOption : '.round($AddOptiontime/$total_num, 4), 'acting_transaction.log'); | |
| 1057 | - usces_log('addSku : '.round($AddSKUtime/$total_num, 4), 'acting_transaction.log'); | |
| 1058 | - usces_log('oneline : '.round($onelinetime/$total_num, 4), 'acting_transaction.log'); | |
| 1059 | - usces_log('finish : '.round($finish-$start, 4), 'acting_transaction.log'); | |
| 1060 | - usces_log('totalLines: '.$total_num, 'acting_transaction.log'); | |
| 1061 | - usces_log('--------------------------------------------------------------', 'acting_transaction.log'); | |
| 1062 | - | |
| 1063 | - echo '<script type="text/javascript">changeMsg("', __(sprintf('Successful %1$s lines, Failed %2$s lines.',$comp_num,$err_num), 'usces'), ' ");setProgress(', ($rows_num+1) . ',' . $total_num, ');</script>'.$yn; | |
| 1064 | - if( $log ){ | |
| 1065 | - //echo '<div class="error_log">' . $log . '</div>'.$yn; | |
| 1066 | - }else{ | |
| 1067 | - echo __('Finished', 'usces').$yn; | |
| 1068 | - } | |
| 1069 | - echo '</div>'.$yn; | |
| 1070 | - unlink($path.$file_name); | |
| 1071 | - return $res; | |
| 1072 | -} | |
| 1073 | -endif; | |
| 1074 | - | |
| 1075 | -// item list download | |
| 1076 | -if( !function_exists('usces_download_item_list') ): | |
| 1077 | -function usces_download_item_list() { | |
| 1078 | - require_once( USCES_PLUGIN_DIR . "/classes/itemList.class.php" ); | |
| 1079 | - global $wpdb, $usces; | |
| 1080 | - | |
| 1081 | - $ext = 'csv'; | |
| 1082 | - if($ext == 'csv') {//CSV | |
| 1083 | - $table_h = ""; | |
| 1084 | - $table_f = ""; | |
| 1085 | - $tr_h = ""; | |
| 1086 | - $tr_f = ""; | |
| 1087 | - $th_h1 = '"'; | |
| 1088 | - $th_h = ',"'; | |
| 1089 | - $th_f = '"'; | |
| 1090 | - $td_h1 = '"'; | |
| 1091 | - $td_h = ',"'; | |
| 1092 | - $td_f = '"'; | |
| 1093 | - $sp = ";"; | |
| 1094 | - $eq = "="; | |
| 1095 | - $lf = "\n"; | |
| 1096 | - } else { | |
| 1097 | - exit(); | |
| 1098 | - } | |
| 1099 | - | |
| 1100 | - //========================================================================== | |
| 1101 | - $usces_opt_item = get_option('usces_opt_item'); | |
| 1102 | - if(!is_array($usces_opt_item)){ | |
| 1103 | - $usces_opt_item = array(); | |
| 1104 | - } | |
| 1105 | - $usces_opt_item['chk_header'] = (isset($_REQUEST['chk_header'])) ? 1 : 0; | |
| 1106 | - $usces_opt_item['ftype_item'] = $ext; | |
| 1107 | - update_option('usces_opt_item', $usces_opt_item); | |
| 1108 | - //========================================================================== | |
| 1109 | - | |
| 1110 | - $tableName = $wpdb->posts; | |
| 1111 | - if( USCES_MYSQL_VERSION >= 5 ) { | |
| 1112 | - $arr_column = array( | |
| 1113 | - __('item code', 'usces') => 'item_code', | |
| 1114 | - __('item name', 'usces') => 'item_name', | |
| 1115 | - __('SKU code', 'usces') => 'sku_key', | |
| 1116 | - __('selling price', 'usces') => 'price', | |
| 1117 | - __('stock', 'usces') => 'zaiko_num', | |
| 1118 | - __('stock status', 'usces') => 'zaiko', | |
| 1119 | - __('Categories', 'usces') => 'category', | |
| 1120 | - __('display status', 'usces') => 'display_status'); | |
| 1121 | - } else { | |
| 1122 | - $arr_column = array( | |
| 1123 | - __('item code', 'usces') => 'item_code', | |
| 1124 | - __('page title', 'usces') => 'post_title', | |
| 1125 | - __('SKU code', 'usces') => 'sku_key', | |
| 1126 | - __('selling price', 'usces') => 'price', | |
| 1127 | - __('stock', 'usces') => 'zaiko_num', | |
| 1128 | - __('stock status', 'usces') => 'zaiko', | |
| 1129 | - __('Categories', 'usces') => 'category', | |
| 1130 | - __('display status', 'usces') => 'display_status'); | |
| 1131 | - } | |
| 1132 | - | |
| 1133 | - $_REQUEST['searchIn'] = "searchIn"; | |
| 1134 | - $DT = new dataList($tableName, $arr_column); | |
| 1135 | - $DT->pageLimit = 'off'; | |
| 1136 | - $DT->exportMode = true; | |
| 1137 | - $res = $DT->MakeTable(); | |
| 1138 | - $rows = $DT->rows; | |
| 1139 | - //========================================================================== | |
| 1140 | - $line = $table_h; | |
| 1141 | - if($usces_opt_item['chk_header'] == 1) { | |
| 1142 | - $line .= $tr_h; | |
| 1143 | - $line .= $th_h1.'Post ID'.$th_f; | |
| 1144 | - $line .= $th_h.__('Post Author', 'usces').$th_f; | |
| 1145 | - $line .= $th_h.__('explanation', 'usces').$th_f; | |
| 1146 | - $line .= $th_h.__('Title', 'usces').$th_f; | |
| 1147 | - $line .= $th_h.__('excerpt', 'usces').$th_f; | |
| 1148 | - $line .= $th_h.__('display status', 'usces').$th_f; | |
| 1149 | - $line .= $th_h.__('Comment Status', 'usces').$th_f; | |
| 1150 | - $line .= $th_h.__('Post PAssword', 'usces').$th_f; | |
| 1151 | - $line .= $th_h.__('Post Name', 'usces').$th_f; | |
| 1152 | - $line .= $th_h.__('Publish on:').$th_f; | |
| 1153 | - | |
| 1154 | - $line .= $th_h.__('item code', 'usces').$th_f; | |
| 1155 | - $line .= $th_h.__('item name', 'usces').$th_f; | |
| 1156 | - $line .= $th_h.__('Limited amount for purchase', 'usces').$th_f; | |
| 1157 | - $line .= $th_h.__('Percentage of points', 'usces').$th_f; | |
| 1158 | - $line .= $th_h.__('Business package discount', 'usces').'1-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'1-'.__('rate', 'usces').$th_f; | |
| 1159 | - $line .= $th_h.__('Business package discount', 'usces').'2-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'2-'.__('rate', 'usces').$th_f; | |
| 1160 | - $line .= $th_h.__('Business package discount', 'usces').'3-'.__('num', 'usces').$th_f.$th_h.__('Business package discount', 'usces').'3-'.__('rate', 'usces').$th_f; | |
| 1161 | - $line .= $th_h.__('estimated shipping date', 'usces').$th_f; | |
| 1162 | - $line .= $th_h.__('shipping option', 'usces').$th_f; | |
| 1163 | - $line .= $th_h.__('Shipping', 'usces').$th_f; | |
| 1164 | - $line .= $th_h.__('Postage individual charging', 'usces').$th_f; | |
| 1165 | - | |
| 1166 | - $line .= $th_h.__('Categories', 'usces').$th_f; | |
| 1167 | - $line .= $th_h.__('tag', 'usces').$th_f; | |
| 1168 | - $line .= $th_h.__('Custom Field', 'usces').$th_f; | |
| 1169 | - | |
| 1170 | - $line .= $th_h.__('SKU code', 'usces').$th_f; | |
| 1171 | - $line .= $th_h.__('SKU display name ', 'usces').$th_f; | |
| 1172 | - $line .= $th_h.__('normal price', 'usces').$th_f; | |
| 1173 | - $line .= $th_h.__('Sale price', 'usces').$th_f; | |
| 1174 | - $line .= $th_h.__('stock', 'usces').$th_f; | |
| 1175 | - $line .= $th_h.__('stock status', 'usces').$th_f; | |
| 1176 | - $line .= $th_h.__('unit', 'usces').$th_f; | |
| 1177 | - $line .= $th_h.__('Apply business package', 'usces').$th_f; | |
| 1178 | - $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; | |
| 1179 | - $line .= $tr_f.$lf; | |
| 1180 | - } | |
| 1181 | - //========================================================================== | |
| 1182 | - mb_http_output('pass'); | |
| 1183 | - set_time_limit(3600); | |
| 1184 | - header("Content-Type: application/octet-stream"); | |
| 1185 | - header("Content-Disposition: attachment; filename=usces_item_list.".$ext); | |
| 1186 | - ob_end_flush(); | |
| 1187 | - flush(); | |
| 1188 | - | |
| 1189 | - foreach((array)$rows as $array) { | |
| 1190 | - $post_id = $array['ID']; | |
| 1191 | - $post = get_post($post_id); | |
| 1192 | - | |
| 1193 | - //Post Data | |
| 1194 | - $line_item = $td_h1.$post->ID.$td_f; | |
| 1195 | - $line_item .= $td_h.$post->post_author.$td_f; | |
| 1196 | - $line_item .= $td_h.usces_entity_decode($post->post_content, $ext).$td_f; | |
| 1197 | - $line_item .= $td_h.usces_entity_decode($post->post_title, $ext).$td_f; | |
| 1198 | - $line_item .= $td_h.usces_entity_decode($post->post_excerpt, $ext).$td_f; | |
| 1199 | - $line_item .= $td_h.$post->post_status.$td_f; | |
| 1200 | - $line_item .= $td_h.$post->comment_status.$td_f; | |
| 1201 | - $line_item .= $td_h.$post->post_password.$td_f; | |
| 1202 | - $line_item .= $td_h.urldecode($post->post_name).$td_f; | |
| 1203 | - $line_item .= $td_h.$post->post_date.$td_f; | |
| 1204 | - | |
| 1205 | - //Item Meta | |
| 1206 | - $line_item .= $td_h.$usces->getItemCode($post_id).$td_f; | |
| 1207 | - $line_item .= $td_h.usces_entity_decode($usces->getItemName($post_id), $ext).$td_f; | |
| 1208 | - $line_item .= $td_h.$usces->getItemRestriction($post_id).$td_f; | |
| 1209 | - $line_item .= $td_h.$usces->getItemPointrate($post_id).$td_f; | |
| 1210 | - $line_item .= $td_h.$usces->getItemGpNum1($post_id).$td_f.$td_h.$usces->getItemGpDis1($post_id).$td_f; | |
| 1211 | - $line_item .= $td_h.$usces->getItemGpNum2($post_id).$td_f.$td_h.$usces->getItemGpDis2($post_id).$td_f; | |
| 1212 | - $line_item .= $td_h.$usces->getItemGpNum3($post_id).$td_f.$td_h.$usces->getItemGpDis3($post_id).$td_f; | |
| 1213 | - $line_item .= $td_h.$usces->getItemShipping($post_id).$td_f; | |
| 1214 | - $delivery_method = ''; | |
| 1215 | - $itemDeliveryMethod = $usces->getItemDeliveryMethod($post_id); | |
| 1216 | - foreach((array)$itemDeliveryMethod as $k => $v) { | |
| 1217 | - $delivery_method .= $v.$sp; | |
| 1218 | - } | |
| 1219 | - $delivery_method = rtrim($delivery_method, $sp); | |
| 1220 | - $line_item .= $td_h.$delivery_method.$td_f; | |
| 1221 | - $line_item .= $td_h.$usces->getItemShippingCharge($post_id).$td_f; | |
| 1222 | - $line_item .= $td_h.$usces->getItemIndividualSCharge($post_id).$td_f; | |
| 1223 | - | |
| 1224 | - //Categories | |
| 1225 | - $category = ''; | |
| 1226 | - $cat_ids = wp_get_post_categories($post_id); | |
| 1227 | - if(!empty($cat_ids)) { | |
| 1228 | - foreach($cat_ids as $id) { | |
| 1229 | - $category .= $id.$sp; | |
| 1230 | - } | |
| 1231 | - $category = rtrim($category, $sp); | |
| 1232 | - } | |
| 1233 | - $line_item .= $td_h.$category.$td_f; | |
| 1234 | - | |
| 1235 | - //Tags | |
| 1236 | - $tag = ''; | |
| 1237 | - $tags_ob = wp_get_object_terms($post_id, 'post_tag'); | |
| 1238 | - foreach($tags_ob as $ob) { | |
| 1239 | - $tag .= $ob->name.$sp; | |
| 1240 | - } | |
| 1241 | - $tag = rtrim($tag, $sp); | |
| 1242 | - $line_item .= $td_h.$tag.$td_f; | |
| 1243 | - | |
| 1244 | - //Custom Fields | |
| 1245 | - $cfield = ''; | |
| 1246 | - $custom_fields = $usces->get_post_user_custom($post_id); | |
| 1247 | - if( is_array($custom_fields) && 0 < count($custom_fields) ){ | |
| 1248 | - foreach($custom_fields as $cfkey => $cfvalues ) { | |
| 1249 | - if( is_array($cfvalues) ) | |
| 1250 | - $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues[0], $ext) . $sp; | |
| 1251 | - else | |
| 1252 | - $cfield .= usces_entity_decode($cfkey, $ext) . $eq . usces_entity_decode($cfvalues, $ext) . $sp; | |
| 1253 | - } | |
| 1254 | - $cfield = rtrim($cfield, $sp); | |
| 1255 | - } | |
| 1256 | - $line_item .= $td_h.$cfield.$td_f; | |
| 1257 | - | |
| 1258 | - //Item Options | |
| 1259 | - $line_options = ''; | |
| 1260 | - $option_meta = usces_get_opts( $post_id, 'sort' ); | |
| 1261 | - foreach($option_meta as $option_value) { | |
| 1262 | - $value = ''; | |
| 1263 | - if(is_array($option_value['value'])) { | |
| 1264 | - foreach($option_value['value'] as $k => $v) { | |
| 1265 | - $values = explode("\n", $v); | |
| 1266 | - foreach($values as $val) { | |
| 1267 | - $value .= $val.$sp; | |
| 1268 | - } | |
| 1269 | - } | |
| 1270 | - $value = rtrim($value, $sp); | |
| 1271 | - } else { | |
| 1272 | - $value = trim($option_value['value']); | |
| 1273 | - $value = str_replace("\n", ';', $value); | |
| 1274 | - } | |
| 1275 | - | |
| 1276 | - $line_options .= $td_h.usces_entity_decode($option_value['name'], $ext).$td_f; | |
| 1277 | - $line_options .= $td_h.$option_value['means'].$td_f; | |
| 1278 | - $line_options .= $td_h.$option_value['essential'].$td_f; | |
| 1279 | - $line_options .= $td_h.usces_entity_decode($value, $ext).$td_f; | |
| 1280 | - | |
| 1281 | - } | |
| 1282 | - | |
| 1283 | - //SKUs | |
| 1284 | - $sku_meta = $usces->get_skus( $post_id, 'sort' ); | |
| 1285 | - foreach($sku_meta as $sku_value) { | |
| 1286 | - | |
| 1287 | - $line_sku = $td_h.$sku_value['code'].$td_f; | |
| 1288 | - $line_sku .= $td_h.usces_entity_decode($sku_value['name'], $ext).$td_f; | |
| 1289 | - $line_sku .= $td_h.usces_crform($sku_value['cprice'], false, false, 'return', false).$td_f; | |
| 1290 | - $line_sku .= $td_h.usces_crform($sku_value['price'], false, false, 'return', false).$td_f; | |
| 1291 | - $line_sku .= $td_h.$sku_value['stocknum'].$td_f; | |
| 1292 | - $line_sku .= $td_h.$sku_value['stock'].$td_f; | |
| 1293 | - $line_sku .= $td_h.usces_entity_decode($sku_value['unit'], $ext).$td_f; | |
| 1294 | - $line_sku .= $td_h.$sku_value['gp'].$td_f; | |
| 1295 | - | |
| 1296 | - $line .= $tr_h.$line_item.$line_sku.$line_options.$tr_f.$lf; | |
| 1297 | - } | |
| 1298 | - print(mb_convert_encoding($line, "SJIS-win", "UTF-8")); | |
| 1299 | - if(ob_get_contents ()){ | |
| 1300 | - ob_flush(); | |
| 1301 | - flush(); | |
| 1302 | - } | |
| 1303 | - $line = ''; | |
| 1304 | - wp_cache_flush(); | |
| 1305 | - } | |
| 1306 | - $end = $table_f.$lf; | |
| 1307 | - //========================================================================== | |
| 1308 | - print(mb_convert_encoding($end, "SJIS-win", "UTF-8")); | |
| 1309 | - unset($rows, $DT, $line, $line_item, $line_options, $line_sku); | |
| 1310 | - exit(); | |
| 1311 | -} | |
| 1312 | -endif; | |
| 1313 | - | |
| 1314 | -} | |
| 1315 | -?> | |
| 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 | +} | |