PluginProbe
Welcart e-Commerce / 2.12.4
Welcart e-Commerce v2.12.4
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
usc-e-shop / functions / utility.php

utility.php in Welcart e-Commerce 2.12.4, at functions/utility.php

1,838 lines 53.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utility
4 *
5 * @package Welcart
6 */
7
8 // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL
9 // phpcs:disable WordPress.PHP.DevelopmentFunctions, WordPress.PHP.NoSilencedErrors
10
11 /**
12 * Upgrade 143
13 *
14 * @return bool
15 */
16 function usces_upgrade_143() {
17
18 $upgrade = (int) get_option( 'usces_upgrade3' );
19 if ( 0 !== $upgrade ) {
20 return true;
21 }
22
23 global $wpdb;
24 $table = $wpdb->prefix . 'usces_ordercart';
25 $wpdb->query( "ALTER TABLE $table CHANGE `cprice` `cprice` DECIMAL( 15, 2 ) NULL DEFAULT NULL" );
26 $wpdb->query( "ALTER TABLE $table CHANGE `price` `price` DECIMAL( 15, 2 ) NULL DEFAULT NULL" );
27 $wpdb->query( "ALTER TABLE $table CHANGE `tax` `tax` DECIMAL( 13, 2 ) NULL DEFAULT NULL" );
28
29 $upgrade += USCES_UP143;
30 update_option( 'usces_upgrade3', $upgrade );
31 usces_log( 'USCES_UP143 : Completed', 'db' );
32
33 return true;
34 }
35
36 /**
37 * Upgrade 141
38 *
39 * @return bool
40 */
41 function usces_upgrade_141() {
42
43 $upgrade = (int) get_option( 'usces_upgrade2' );
44 if ( 3 !== $upgrade ) {
45 return true;
46 }
47
48 global $wpdb, $usces;
49 $rets = array();
50
51 $options = get_option( 'usces', array() );
52 if ( empty( $options['tax_rate'] ) ) {
53 $options['tax_mode'] = 'include';
54 } else {
55 $options['tax_mode'] = 'exclude';
56 }
57 $options['tax_target'] = 'all';
58 update_option( 'usces', $options );
59
60 $order_table = $wpdb->prefix . 'usces_order';
61 $cart_table = $wpdb->prefix . 'usces_ordercart';
62 $cart_meta_table = $wpdb->prefix . 'usces_ordercart_meta';
63
64 $query = "SELECT ID, order_cart, order_condition FROM $order_table";
65 $results = $wpdb->get_results( $query );
66 if ( $results ) {
67 foreach ( $results as $order ) {
68
69 $query = $wpdb->prepare( "SELECT order_id FROM $cart_table WHERE order_id = %d", $order->ID );
70 $order_id = $wpdb->get_var( $query );
71 if ( $order_id == $order->ID ) {
72 continue;
73 }
74 $condition = wel_safe_maybe_unserialize( $order->order_condition );
75 if ( ! isset( $condition['tax_mode'] ) ) {
76 $condition['tax_mode'] = $options['tax_mode'];
77 }
78 if ( ! isset( $condition['tax_rate'] ) ) {
79 $condition['tax_rate'] = (int) $options['tax_rate'];
80 }
81 if ( ! isset( $condition['tax_target'] ) ) {
82 $condition['tax_target'] = $options['tax_target'];
83 }
84
85 $cart = wel_safe_unserialize( $order->order_cart );
86 foreach ( (array) $cart as $row_index => $value ) {
87 $product = wel_get_product( $value['post_id'] );
88 $item_code = $product['itemCode'];
89 $item_name = $product['itemName'];
90 $skus = $usces->get_skus( $value['post_id'], 'code' );
91 $sku_code = urldecode( $value['sku'] );
92 $sku_encoded = $value['sku'];
93 $sku = isset( $skus[ $sku_code ] ) ? $skus[ $sku_code ] : array( 'name' => 'notfound', 'cprice' => 0, 'unit' => '' );
94 $tax = 0;
95 $query = $wpdb->prepare(
96 "INSERT INTO $cart_table
97 (
98 order_id, group_id, row_index,
99 post_id, item_code, item_name,
100 sku_code, sku_name, cprice, price, quantity, unit,
101 tax, destination_id, cart_serial
102 ) VALUES (
103 %d, %d, %d,
104 %d, %s, %s,
105 %s, %s, %f, %f, %d, %s,
106 %f, %d, %s
107 )",
108 $order->ID, 0, $row_index,
109 $value['post_id'], $item_code, $item_name,
110 $sku_code, $sku['name'], $sku['cprice'], $value['price'], $value['quantity'], $sku['unit'],
111 $tax, NULL, $value['serial']
112 );
113 $wpdb->query( $query );
114
115 $cart_id = $wpdb->insert_id;
116 if ( $cart_id ) {
117 if ( $value['options'] ) {
118 foreach ( (array) $value['options'] as $okey => $ovalue ) {
119 $okey = urldecode( $okey );
120 if ( is_array( $ovalue ) ) {
121 $ovalue = serialize( $ovalue );
122 } else {
123 $ovalue = urldecode( $ovalue );
124 }
125 $oquery = $wpdb->prepare(
126 "INSERT INTO $cart_meta_table
127 (
128 cart_id, meta_type, meta_key, meta_value
129 ) VALUES (
130 %d, 'option', %s, %s
131 )",
132 $cart_id, $okey, $ovalue
133 );
134 $wpdb->query( $oquery );
135 }
136 }
137 if ( $value['advance'] ) {
138 foreach ( (array) $value['advance'] as $akey => $avalue ) {
139 $advance = wel_safe_maybe_unserialize( $avalue );
140 if ( is_array( $advance ) ) {
141 $post_id = $value['post_id'];
142 if ( is_array( $advance[ $post_id ][ $sku_encoded ] ) ) {
143 $akeys = array_keys( $advance[ $post_id ][ $sku_encoded ] );
144 foreach ( (array) $akeys as $akey ) {
145 $avalue = serialize( $advance[ $post_id ][ $sku_encoded ][ $akey ] );
146 $aquery = $wpdb->prepare(
147 "INSERT INTO $cart_meta_table
148 (
149 cart_id, meta_type, meta_key, meta_value
150 ) VALUES (
151 %d, 'advance', %s, %s
152 )",
153 $cart_id, $akey, $avalue
154 );
155 $wpdb->query( $aquery );
156 }
157 } else {
158 $akeys = array_keys( $advance );
159 $akey = ( empty( $akeys[0] ) ) ? 'advance' : $akeys[0];
160 $avalue = serialize( $advance );
161 $aquery = $wpdb->prepare(
162 "INSERT INTO $cart_meta_table
163 (
164 cart_id, meta_type, meta_key, meta_value
165 ) VALUES (
166 %d, 'advance', %s, %s
167 )",
168 $cart_id, $akey, $avalue
169 );
170 $wpdb->query( $aquery );
171 }
172 } else {
173 $avalue = urldecode( $avalue );
174 $aquery = $wpdb->prepare(
175 "INSERT INTO $cart_meta_table
176 (
177 cart_id, meta_type, meta_key, meta_value
178 ) VALUES (
179 %d, 'advance', 'advance', %s
180 )",
181 $cart_id, $avalue
182 );
183 $wpdb->query( $aquery );
184 }
185 }
186 }
187 }
188 }
189
190 $upquery = $wpdb->prepare(
191 "UPDATE $order_table SET order_condition = %s WHERE ID = %d",
192 serialize( $condition ), $order->ID
193 );
194 $wpdb->query( $upquery );
195 }
196 }
197 usces_log( 'USCES_UP141 : Completed : from ' . $upgrade, 'db' );
198 $upgrade += USCES_UP141;
199 update_option( 'usces_upgrade2', $upgrade );
200
201 return true;
202 }
203
204 /**
205 * Upgrade 14
206 *
207 * @return bool
208 */
209 function usces_upgrade_14() {
210
211 $upgrade = (int) get_option( 'usces_upgrade2' );
212 if ( 0 < $upgrade ) {
213 return true;
214 }
215
216 global $wpdb, $usces;
217 $rets = array();
218
219 $options = get_option( 'usces', array() );
220 if ( empty( $options['tax_rate'] ) ) {
221 $options['tax_mode'] = 'include';
222 } else {
223 $options['tax_mode'] = 'exclude';
224 }
225 $options['tax_target'] = 'all';
226 update_option( 'usces', $options );
227
228 $order_table = $wpdb->prefix . 'usces_order';
229 $cart_table = $wpdb->prefix . 'usces_ordercart';
230 $cart_meta_table = $wpdb->prefix . 'usces_ordercart_meta';
231
232 $query = "SELECT ID, order_cart, order_condition FROM $order_table";
233 $results = $wpdb->get_results( $query );
234 if ( $results ) {
235 foreach ( $results as $order ) {
236 $condition = wel_safe_maybe_unserialize( $order->order_condition );
237 if ( ! isset( $condition['tax_mode'] ) ) {
238 $condition['tax_mode'] = $options['tax_mode'];
239 }
240 if ( ! isset( $condition['tax_rate'] ) ) {
241 $condition['tax_rate'] = (int) $options['tax_rate'];
242 }
243 if ( ! isset( $condition['tax_target'] ) ) {
244 $condition['tax_target'] = $options['tax_target'];
245 }
246
247 $cart = wel_safe_unserialize( $order->order_cart );
248 foreach ( (array) $cart as $row_index => $value ) {
249 $product = wel_get_product( $value['post_id'] );
250 $item_code = $product['itemCode'];
251 $item_name = $product['itemName'];
252 $skus = $usces->get_skus( $value['post_id'], 'code' );
253 $sku_code = urldecode( $value['sku'] );
254 $sku_encoded = $value['sku'];
255 $sku = isset( $skus[ $sku_code ] ) ? $skus[ $sku_code ] : array( 'name' => 'notfound', 'cprice' => 0, 'unit' => '' );
256 $tax = 0;
257 $query = $wpdb->prepare(
258 "INSERT INTO $cart_table
259 (
260 order_id, group_id, row_index,
261 post_id, item_code, item_name,
262 sku_code, sku_name, cprice, price, quantity, unit,
263 tax, destination_id, cart_serial
264 ) VALUES (
265 %d, %d, %d,
266 %d, %s, %s,
267 %s, %s, %f, %f, %d, %s,
268 %f, %d, %s
269 )",
270 $order->ID, 0, $row_index,
271 $value['post_id'], $item_code, $item_name,
272 $sku_code, $sku['name'], $sku['cprice'], $value['price'], $value['quantity'], $sku['unit'],
273 $tax, NULL, $value['serial']
274 );
275 $wpdb->query( $query );
276
277 $cart_id = $wpdb->insert_id;
278 if ( $cart_id ) {
279 if ( $value['options'] ) {
280 foreach ( (array) $value['options'] as $okey => $ovalue ) {
281 $okey = urldecode( $okey );
282 if ( is_array( $ovalue ) ) {
283 $ovalue = serialize( $ovalue );
284 } else {
285 $ovalue = urldecode( $ovalue );
286 }
287 $oquery = $wpdb->prepare(
288 "INSERT INTO $cart_meta_table
289 (
290 cart_id, meta_type, meta_key, meta_value
291 ) VALUES (
292 %d, 'option', %s, %s
293 )",
294 $cart_id, $okey, $ovalue
295 );
296 $wpdb->query( $oquery );
297 }
298 }
299 if ( $value['advance'] ) {
300 foreach ( (array) $value['advance'] as $akey => $avalue ) {
301 $advance = wel_safe_maybe_unserialize( $avalue );
302 if ( is_array( $advance ) ) {
303 $post_id = $value['post_id'];
304 if ( is_array( $advance[ $post_id ][ $sku_encoded ] ) ) {
305 $akeys = array_keys( $advance[ $post_id ][ $sku_encoded ] );
306 foreach ( (array) $akeys as $akey ) {
307 $avalue = serialize( $advance[ $post_id ][ $sku_encoded ][ $akey ] );
308 $aquery = $wpdb->prepare(
309 "INSERT INTO $cart_meta_table
310 (
311 cart_id, meta_type, meta_key, meta_value
312 ) VALUES (
313 %d, 'advance', %s, %s
314 )",
315 $cart_id, $akey, $avalue
316 );
317 $wpdb->query( $aquery );
318 }
319 } else {
320 $akeys = array_keys( $advance );
321 $akey = ( empty( $akeys[0] ) ) ? 'advance' : $akeys[0];
322 $avalue = serialize( $advance );
323 $aquery = $wpdb->prepare(
324 "INSERT INTO $cart_meta_table
325 (
326 cart_id, meta_type, meta_key, meta_value
327 ) VALUES (
328 %d, 'advance', %s, %s
329 )",
330 $cart_id, $akey, $avalue
331 );
332 $wpdb->query( $aquery );
333 }
334 } else {
335 $avalue = urldecode( $avalue );
336 $aquery = $wpdb->prepare(
337 "INSERT INTO $cart_meta_table
338 (
339 cart_id, meta_type, meta_key, meta_value
340 ) VALUES (
341 %d, 'advance', 'advance', %s
342 )",
343 $cart_id, $avalue
344 );
345 $wpdb->query( $aquery );
346 }
347 }
348 }
349 }
350 }
351
352 $upquery = $wpdb->prepare(
353 "UPDATE $order_table SET order_condition = %s WHERE ID = %d",
354 serialize( $condition ), $order->ID
355 );
356 $wpdb->query( $upquery );
357 }
358 }
359 usces_log( 'USCES_UP14 : Completed : from ' . $upgrade, 'db' );
360 $upgrade += USCES_UP14;
361 update_option( 'usces_upgrade2', $upgrade );
362
363 return true;
364 }
365
366 /**
367 * Upgrade 07
368 *
369 * @return bool
370 */
371 function usces_upgrade_07() {
372
373 $upgrade = (int) get_option( 'usces_upgrade' );
374 if ( 0 !== $upgrade ) {
375 return true;
376 }
377
378 global $wpdb;
379 $rets = array();
380
381 $tablename = $wpdb->prefix . 'postmeta';
382 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'iopt_', '_iopt_') WHERE meta_key LIKE 'iopt_%'";
383 if ( $wpdb->query( $mquery ) ) {
384 $rets[] = 1;
385 }
386 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'isku_', '_isku_') WHERE meta_key LIKE 'isku_%'";
387 if ( $wpdb->query( $mquery ) ) {
388 $rets[] = 1;
389 }
390 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemCode', '_itemCode') WHERE meta_key LIKE 'itemCode'";
391 if( $wpdb->query( $mquery ) )
392 $rets[] = 1;
393
394 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemName', '_itemName') WHERE meta_key LIKE 'itemName'";
395 if ( $wpdb->query( $mquery ) ) {
396 $rets[] = 1;
397 }
398 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemRestriction', '_itemRestriction') WHERE meta_key LIKE 'itemRestriction'";
399 if ( $wpdb->query( $mquery ) ) {
400 $rets[] = 1;
401 }
402 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemPointrate', '_itemPointrate') WHERE meta_key LIKE 'itemPointrate'";
403 if ( $wpdb->query( $mquery ) ) {
404 $rets[] = 1;
405 }
406 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpNum1', '_itemGpNum1') WHERE meta_key LIKE 'itemGpNum1'";
407 if ( $wpdb->query( $mquery ) ) {
408 $rets[] = 1;
409 }
410 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpDis1', '_itemGpDis1') WHERE meta_key LIKE 'itemGpDis1'";
411 if ( $wpdb->query( $mquery ) ) {
412 $rets[] = 1;
413 }
414 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpNum2', '_itemGpNum2') WHERE meta_key LIKE 'itemGpNum2'";
415 if ( $wpdb->query( $mquery ) ) {
416 $rets[] = 1;
417 }
418 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpDis2', '_itemGpDis2') WHERE meta_key LIKE 'itemGpDis2'";
419 if ( $wpdb->query( $mquery ) ) {
420 $rets[] = 1;
421 }
422 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpDis3', '_itemGpDis3') WHERE meta_key LIKE 'itemGpDis3'";
423 if ( $wpdb->query( $mquery ) ) {
424 $rets[] = 1;
425 }
426 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemGpNum3', '_itemGpNum3') WHERE meta_key LIKE 'itemGpNum3'";
427 if ( $wpdb->query( $mquery ) ) {
428 $rets[] = 1;
429 }
430 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemShipping', '_itemShipping') WHERE meta_key LIKE 'itemShipping'";
431 if ( $wpdb->query( $mquery ) ) {
432 $rets[] = 1;
433 }
434 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemDeliveryMethod', '_itemDeliveryMethod') WHERE meta_key LIKE 'itemDeliveryMethod'";
435 if ( $wpdb->query( $mquery ) ) {
436 $rets[] = 1;
437 }
438 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemShippingCharge', '_itemShippingCharge') WHERE meta_key LIKE 'itemShippingCharge'";
439 if ( $wpdb->query( $mquery ) ) {
440 $rets[] = 1;
441 }
442 $mquery = "UPDATE $tablename SET meta_key = REPLACE(meta_key, 'itemIndividualSCharge', '_itemIndividualSCharge') WHERE meta_key LIKE 'itemIndividualSCharge'";
443 if ( $wpdb->query( $mquery ) ) {
444 $rets[] = 1;
445 }
446 usces_log( 'USCES_UP07 : ' . print_r( $rets, true ), 'db' );
447 $upgrade += USCES_UP07;
448 update_option( 'usces_upgrade', $upgrade );
449 return true;
450 }
451
452 /**
453 * Upgrade 11
454 *
455 * @return bool
456 */
457 function usces_upgrade_11() {
458
459 $options = get_option( 'usces', array() );
460 $upgrade = (int) get_option( 'usces_upgrade' );
461 if ( 1 !== $upgrade ) {
462 return true;
463 }
464
465 global $wpdb;
466 $rets = array();
467
468 /* ITEM SKU DATA */
469 $sort = (int) $options['system']['orderby_itemsku'];
470 if ( $sort ) {
471 $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key <> '_isku_' AND meta_key LIKE '_isku_%' ORDER BY post_id, meta_key";
472 } else {
473 $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key <> '_isku_' AND meta_key LIKE '_isku_%' ORDER BY post_id, meta_id";
474 }
475 $res = $wpdb->get_results( $query, ARRAY_A );
476
477 $conclusion = true;
478 $pre_post_id = 0;
479 $sort_id = 0;
480 $check_code = array();
481 $dep_num = array();
482 foreach ( (array) $res as $metarow ) {
483 $meta_value = wel_safe_unserialize( $metarow['meta_value'] );
484 $newvalue = array();
485 $newvalue['code'] = substr( $metarow['meta_key'], 6 );
486 if ( $pre_post_id == $metarow['post_id'] ) {
487 if ( in_array( $newvalue['code'], $check_code ) ) {
488 $newvalue['code'] .= 'dupricate_' . (int) $dep_num[ $newvalue['code'] ];
489 $dep_num[ $newvalue['code'] ]++;
490 }
491 } else {
492 $check_code = array();
493 $dep_num = array();
494 $sort_id = 0;
495 }
496 foreach ( (array) $meta_value as $k => $v ) {
497 switch ( $k ) {
498 case 'disp':
499 $newvalue['name'] = $v;
500 break;
501 case 'cprice':
502 $newvalue['cprice'] = $v;
503 break;
504 case 'price':
505 $newvalue['price'] = $v;
506 break;
507 case 'unit':
508 $newvalue['unit'] = $v;
509 break;
510 case 'zaikonum':
511 $newvalue['stocknum'] = $v;
512 break;
513 case 'zaiko':
514 $newvalue['stock'] = $v;
515 break;
516 case 'gptekiyo':
517 $newvalue['gp'] = $v;
518 break;
519 case 'charging_type':
520 break;
521 default:
522 $newvalue[ $k ] = $v;
523 }
524 }
525 $newvalue['sort'] = $sort_id;
526
527 $id = usces_add_sku( $metarow['post_id'], $newvalue, false );
528 $pre_post_id = $metarow['post_id'];
529 $check_code[] = $newvalue['code'];
530 $sort_id++;
531
532 $res_key = $metarow['post_id'] . '_' . $newvalue['code'];
533 if ( $id ) {
534 delete_post_meta( $metarow['post_id'], $metarow['meta_key'] );
535 $rets['sku'][ $res_key ] = 1;
536 } else {
537 $rets['sku'][ $res_key ] = 0;
538 usces_log( 'meta_id ' . $metarow['meta_id'] . ' : ' . __( 'This SKU-data has not been rebuilt.', 'usces' ), 'database_error.log' );
539 }
540 }
541
542 /* ITEM OPTION DATA */
543 $sort = (int) $options['system']['orderby_itemopt'];
544 if ( $sort ) {
545 $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key <> '_iopt_' AND meta_key LIKE '_iopt_%' ORDER BY post_id, meta_key";
546 } else {
547 $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key <> '_iopt_' AND meta_key LIKE '_iopt_%' ORDER BY post_id, meta_id";
548 }
549 $res = $wpdb->get_results( $query, ARRAY_A );
550
551 $conclusion = true;
552 $pre_post_id = 0;
553 $sort_id = 0;
554 $check_code = array();
555 $dep_num = array();
556 foreach ( (array) $res as $metarow ) {
557 $meta_value = wel_safe_unserialize( $metarow['meta_value'] );
558 $newvalue = array();
559 $newvalue['name'] = substr( $metarow['meta_key'], 6 );
560 if ( $pre_post_id == $metarow['post_id'] ) {
561 if ( in_array( $newvalue['name'], $check_code ) ) {
562 $newvalue['name'] .= 'dupricate_' . (int) $dep_num[ $newvalue['name'] ];
563 $dep_num[ $newvalue['name'] ]++;
564 }
565 } else {
566 $check_code = array();
567 $dep_num = array();
568 $sort_id = 0;
569 }
570 foreach ( (array) $meta_value as $k => $v ) {
571 switch ( $k ) {
572 case 'means':
573 $newvalue['means'] = $v;
574 break;
575 case 'essential':
576 $newvalue['essential'] = $v;
577 break;
578 case 'value':
579 if ( is_array( $v ) ) {
580 $nov = '';
581 foreach ( (array) $v as $vs ) {
582 if ( ! WCUtils::is_blank( $vs ) ) {
583 $nov .= $vs . "\n";
584 }
585 }
586 $newvalue['value'] = trim( $nov );
587 } else {
588 $newvalue['value'] = trim( $v );
589 }
590 break;
591 default:
592 $newvalue[ $k ] = trim( $v );
593 }
594 }
595 $newvalue['sort'] = $sort_id;
596
597 $id = usces_add_opt( $metarow['post_id'], $newvalue, false );
598 $pre_post_id = $metarow['post_id'];
599 $check_code[] = $newvalue['name'];
600 $sort_id++;
601
602 $res_key = $metarow['post_id'] . '_' . $newvalue['name'];
603 if ( $id ) {
604 delete_post_meta( $metarow['post_id'], $metarow['meta_key'] );
605 $rets['opt'][ $res_key ] = 1;
606 } else {
607 $rets['opt'][ $res_key ] = 0;
608 usces_log( 'meta_id ' . $metarow['meta_id'] . ' : ' . __( 'This Item-Option-data has not been rebuilt.', 'usces' ), 'database_error.log' );
609 }
610 }
611
612 /* PAYMENT METHOD DATA */
613 $payment = get_option( 'usces_payment_method' );
614 if ( empty( $payment ) ) {
615
616 $options = get_option( 'usces', array() );
617 $old_payment = isset( $options['payment_method'] ) ? $options['payment_method'] : '';
618 usces_log( 'old_payment : ' . print_r( $old_payment, true ), 'database_error.log' );
619 if ( ! empty( $old_payment ) && is_array( $old_payment ) ) {
620 foreach ( $old_payment as $key => $value ) {
621 $res_key = $key . '_' . $value['name'];
622 $id = usces_add_system_option( 'usces_payment_method', $value );
623 if ( $id ) {
624 $rets['payment'][ $res_key ] = 1;
625 } else {
626 $rets['payment'][ $res_key ] = 0;
627 usces_log( 'payment_method ' . $value['name'] . ' : ' . __( 'This Payment-Method-data has not been rebuilt.', 'usces' ), 'database_error.log' );
628 }
629 }
630 }
631 }
632
633 usces_log( 'USCES_UP11 : ' . print_r( $rets, true ), 'db' );
634
635 $upgrade += USCES_UP11;
636 update_option( 'usces_upgrade', $upgrade );
637
638 return true;
639 }
640
641 /**
642 * Output log data.
643 *
644 * @param mixed $log Log.
645 * @param string $file Output.
646 * @param string $type Type.
647 * @param string $key Key.
648 */
649 function usces_log( $log, $file, $type = '', $key = '' ) {
650 global $wpdb;
651
652 if ( 'db' == $file ) {
653
654 $table_name = $wpdb->prefix . 'usces_log';
655 $query = $wpdb->prepare( "INSERT INTO $table_name ( datetime, log, log_type, log_key ) VALUES( %s, %s, %s, %s )", current_time( 'mysql' ), $log, $type, $key );
656 $wpdb->query( $query );
657
658 } else {
659
660 $logdir = USCES_PLUGIN_DIR . '/logs';
661 $file_path = $logdir . '/' . $file;
662 if ( is_dir( $file_path ) ) {
663 return;
664 }
665
666 $htaccess_path = $logdir . '/.htaccess';
667 if ( ! file_exists( $htaccess_path ) ) {
668 if ( $ht = @fopen( $htaccess_path, 'w' ) ) {
669 fwrite( $ht, 'Order deny,allow' . "\n" );
670 fwrite( $ht, 'Deny from all' . "\n" );
671 fclose( $ht );
672 }
673 }
674
675 if ( is_writable( $file_path ) || ( 'db' != $file && 'test' == $type ) ) {
676 $log = date( '[Y-m-d H:i:s]', current_time( 'timestamp' ) ) . "\t" . $log . "\n";
677 if ( $fp = @fopen( $file_path, 'a' ) ) {
678 fwrite( $fp, $log );
679 fclose( $fp );
680 }
681 } else {
682
683 $type = 'unwritable';
684 $key = $file;
685 $table_name = $wpdb->prefix . 'usces_log';
686 $query = $wpdb->prepare( "INSERT INTO $table_name ( datetime, log, log_type, log_key ) VALUES( %s, %s, %s, %s )", current_time( 'mysql' ), $log, $type, $key );
687 $wpdb->query( $query );
688
689 }
690 }
691 }
692
693 /**
694 * Convenience store name
695 *
696 * @param string $code Convenience store code.
697 * @return string
698 */
699 function usces_get_conv_name( $code ) {
700 switch ( $code ) {
701 case 'D001':
702 $name = 'セブン-イレブン';
703 break;
704 case '010':
705 $name = 'セブンイレブン';
706 break;
707 case 'D002':
708 case '020':
709 $name = 'ローソン';
710 break;
711 case 'D015':
712 case '760':
713 $name = 'セイコーマート';
714 break;
715 case 'D405':
716 $name = 'ペイジー';
717 break;
718 case 'D003':
719 $name = 'サンクス';
720 break;
721 case 'D004':
722 $name = 'サークルK';
723 break;
724 case 'D040':
725 $name = 'サークルKサンクス';
726 break;
727 case 'D005':
728 case '080':
729 case 'D050':
730 $name = 'ミニストップ';
731 break;
732 case 'D010':
733 case 'D060':
734 $name = 'デイリーヤマザキ';
735 break;
736 case 'D011':
737 $name = 'ヤマザキデイリーストア';
738 break;
739 case 'D030':
740 case '030':
741 $name = 'ファミリーマート';
742 break;
743 case 'D401':
744 $name = '楽天Edy';
745 break;
746 case 'D404':
747 $name = '楽天銀行';
748 break;
749 case 'D406':
750 $name = 'ジャパネット銀行';
751 break;
752 case 'D407':
753 $name = 'Suicaインターネットサービス';
754 break;
755 case 'D451':
756 $name = 'ウェブマネー';
757 break;
758 case 'D452':
759 $name = 'ビットキャッシュ';
760 break;
761 case 'D453':
762 $name = 'JCBプレモカード';
763 break;
764 case 'P901':
765 $name = 'コンビニ払込票';
766 break;
767 case 'P902':
768 $name = 'コンビニ払込票(郵便振替対応)';
769 break;
770 case '050':
771 $name = 'デイリーヤマザキ・ヤマザキデイリーストア・タイムリー';
772 break;
773 case '060':
774 $name = 'サークルK・サンクス';
775 break;
776 case '110':
777 $name = 'am/pm';
778 break;
779 default:
780 $name = '';
781 }
782 return $name;
783 }
784
785 /**
786 * Payment Method Details
787 *
788 * @param array $usces_entries Entry data.
789 * @return string
790 */
791 function usces_payment_detail( $usces_entries ) {
792 global $usces;
793
794 $payments = usces_get_payments_by_name( $usces_entries['order']['payment_name'] );
795 $acting_flag = ( 'acting' == $payments['settlement'] ) ? $payments['module'] : $payments['settlement'];
796 $str = '';
797 switch ( $acting_flag ) {
798 case 'paypal.php':
799 break;
800
801 case 'epsilon.php':
802 break;
803
804 case 'acting_remise_card':
805 if ( isset( $usces_entries['order']['div'] ) ) {
806 switch ( $usces_entries['order']['div'] ) {
807 case '0':
808 $str = ' 一括払い';
809 break;
810 case '1':
811 $str = ' 分割(2回)';
812 break;
813 case '2':
814 $str = ' 分割(リボ払い)';
815 break;
816 }
817 }
818 break;
819
820 case 'acting_remise_conv':
821 break;
822
823 case 'transferAdvance':
824 if ( ! empty( $usces->options['tatransfer_limit'] ) ) {
825 $payment_detail = __( '(', 'usces' ) . sprintf( __( 'Payment is valid for %s days from the date of order.', 'usces' ), $usces->options['tatransfer_limit'] ) . __( ')', 'usces' );
826 $str = apply_filters( 'usces_filter_transferAdvance_payment_limit', $payment_detail, $usces->options['tatransfer_limit'] );
827 }
828 break;
829
830 case 'transferDeferred':
831 if ( ! empty( $usces->options['tatransfer_limit'] ) ) {
832 $payment_detail = __( '(', 'usces' ) . sprintf( __( 'Payment is valid for %s days from after the item arrives.', 'usces' ), $usces->options['tatransfer_limit'] ) . __( ')', 'usces' );
833 $str = apply_filters( 'usces_filter_transferDeferred_payment_limit', $payment_detail, $usces->options['tatransfer_limit'] );
834 }
835 break;
836 }
837
838 $str = apply_filters( 'usces_filter_payment_detail', $str, $usces_entries );
839 return $str;
840 }
841
842 /**
843 * Payment Method Details
844 *
845 * @param array $order_data Order data.
846 * @return string
847 */
848 function usces_payment_detail_confirm( $order_data ) {
849 global $usces;
850
851 $payments = usces_get_payments_by_name( $order_data['order_payment_name'] );
852 $acting_flag = ( 'acting' == $payments['settlement'] ) ? $payments['module'] : $payments['settlement'];
853 $str = '';
854 switch ( $acting_flag ) {
855 case 'paypal.php':
856 break;
857 case 'epsilon.php':
858 break;
859 case 'acting_remise_card':
860 break;
861 case 'acting_remise_conv':
862 break;
863
864 case 'transferAdvance':
865 if ( ! empty( $usces->options['tatransfer_limit'] ) && $usces->is_status( 'noreceipt', $order_data['order_status'] ) ) {
866 $payment_detail = "\n" . sprintf( __( 'Payment is valid for %s days from the date of order.', 'usces' ), $usces->options['tatransfer_limit'] );
867 $str = apply_filters( 'usces_filter_transferAdvance_payment_limit_confirm', $payment_detail, $usces->options['tatransfer_limit'] );
868 }
869 break;
870
871 case 'transferDeferred':
872 if ( ! empty( $usces->options['tatransfer_limit'] ) && $usces->is_status( 'noreceipt', $order_data['order_status'] ) ) {
873 $payment_detail = "\n" . sprintf( __( 'Payment is valid for %s days from after the item arrives.', 'usces' ), $usces->options['tatransfer_limit'] );
874 $str = apply_filters( 'usces_filter_transferDeferred_payment_limit_confirm', $payment_detail, $usces->options['tatransfer_limit'] );
875 }
876 break;
877 }
878
879 $str = apply_filters( 'usces_filter_payment_detail_confirm', $str, $order_data );
880 return $str;
881 }
882
883 /**
884 * Check custom order
885 *
886 * @param string $mes Message.
887 * @return string
888 */
889 function usces_filter_delivery_check_custom_order( $mes ) {
890
891 $meta = usces_has_custom_field_meta( 'order' );
892 if ( is_array( $meta ) ) {
893 unset( $_SESSION['usces_entry']['custom_order'] );
894 if ( isset( $_POST['custom_order'] ) ) {
895 foreach ( $_POST['custom_order'] as $key => $value ) {
896 if ( ! isset( $meta[ $key ] ) ) {
897 continue;
898 }
899 if ( is_array( $value ) ) {
900 foreach ( $value as $k => $v ) {
901 $_SESSION['usces_entry']['custom_order'][ $key ][ trim( $v ) ] = trim( $v );
902 }
903 } else {
904 $_SESSION['usces_entry']['custom_order'][ $key ] = trim( $value );
905 }
906 }
907 }
908 }
909
910 foreach ( $meta as $key => $entry ) {
911 $essential = $entry['essential'];
912 if ( 1 === (int) $essential ) {
913 $name = $entry['name'];
914 $means = $entry['means'];
915 if ( 2 === (int) $means ) { /* Text */
916 if ( isset( $_POST['custom_order'][ $key ] ) && WCUtils::is_blank( $_POST['custom_order'][ $key ] ) ) {
917 $mes .= sprintf( __( 'Input the %s', 'usces' ), $name ) . '<br />';
918 }
919 } else {
920 if ( ! isset( $_POST['custom_order'][ $key ] ) || '#NONE#' == $_POST['custom_order'][ $key ] ) {
921 $mes .= sprintf( __( 'Chose the %s', 'usces' ), $name ) . '<br />';
922 }
923 }
924 }
925 }
926
927 return $mes;
928 }
929
930 /**
931 * Check custom customer
932 *
933 * @param string $mes Message.
934 * @return string
935 */
936 function usces_filter_customer_check_custom_customer( $mes ) {
937
938 $meta = usces_has_custom_field_meta( 'customer' );
939 foreach ( $meta as $key => $entry ) {
940 $essential = $entry['essential'];
941 if ( 1 === (int) $essential ) {
942 $name = $entry['name'];
943 $means = $entry['means'];
944 if ( 2 === (int) $means ) { /* Text */
945 if ( WCUtils::is_blank( $_POST['custom_customer'][ $key ] ) ) {
946 $mes .= sprintf( __( 'Input the %s', 'usces' ), $name ) . '<br />';
947 }
948 } else {
949 if ( ! isset( $_POST['custom_customer'][ $key ] ) || '#NONE#' == $_POST['custom_customer'][ $key ] ) {
950 $mes .= sprintf( __( 'Chose the %s', 'usces' ), $name ) . '<br />';
951 }
952 }
953 }
954 }
955
956 return $mes;
957 }
958
959 /**
960 * Check custom delivery
961 *
962 * @param string $mes Message.
963 * @return string
964 */
965 function usces_filter_delivery_check_custom_delivery( $mes ) {
966
967 if ( isset( $_POST['delivery']['delivery_flag'] ) && 1 === (int) $_POST['delivery']['delivery_flag'] ) {
968 $meta = usces_has_custom_field_meta( 'delivery' );
969 foreach ( $meta as $key => $entry ) {
970 $essential = $entry['essential'];
971 if ( 1 === (int) $essential ) {
972 $name = $entry['name'];
973 $means = $entry['means'];
974 if ( 2 === (int) $means ) { /* Text */
975 if ( WCUtils::is_blank( $_POST['custom_delivery'][ $key ] ) ) {
976 $mes .= sprintf( __( 'Input the %s', 'usces' ), $name ) . '<br />';
977 }
978 } else {
979 if ( ! isset( $_POST['custom_delivery'][ $key ] ) || '#NONE#' == $_POST['custom_delivery'][ $key ] ) {
980 $mes .= sprintf( __( 'Chose the %s', 'usces' ), $name ) . '<br />';
981 }
982 }
983 }
984 }
985 }
986
987 return $mes;
988 }
989
990 /**
991 * Check custom member
992 *
993 * @param string $mes Message.
994 * @return string
995 */
996 function usces_filter_member_check_custom_member( $mes ) {
997
998 unset( $_SESSION['usces_member']['custom_member'] );
999 if ( isset( $_POST['custom_member'] ) ) {
1000 foreach ( $_POST['custom_member'] as $key => $value ) {
1001 if ( is_array( $value ) ) {
1002 foreach ( $value as $k => $v ) {
1003 $_SESSION['usces_member']['custom_member'][ $key ][ trim( $v ) ] = trim( $v );
1004 }
1005 } else {
1006 $_SESSION['usces_member']['custom_member'][ $key ] = trim( $value );
1007 }
1008 }
1009 }
1010
1011 $meta = usces_has_custom_field_meta( 'member' );
1012 foreach ( $meta as $key => $entry ) {
1013 $essential = $entry['essential'];
1014 if ( 1 === (int) $essential ) {
1015 $name = $entry['name'];
1016 $means = $entry['means'];
1017 if ( 2 === (int) $means ) { /* Text */
1018 if ( WCUtils::is_blank( $_POST['custom_member'][ $key ] ) ) {
1019 $mes .= sprintf( __( 'Input the %s', 'usces' ), $name ) . '<br />';
1020 }
1021 } else {
1022 if ( ! isset( $_POST['custom_member'][ $key ] ) || '#NONE#' == $_POST['custom_member'][ $key ] ) {
1023 $mes .= sprintf( __( 'Chose the %s', 'usces' ), $name ) . '<br />';
1024 }
1025 }
1026 }
1027 }
1028
1029 return $mes;
1030 }
1031
1032 /**
1033 * Dashboard widget
1034 */
1035 function usces_dashboard_setup() {
1036 wp_add_dashboard_widget( 'usces_db_widget', 'Welcart Information', 'usces_db_widget' );
1037 }
1038
1039 /**
1040 * Admin login
1041 *
1042 * @return void
1043 */
1044 function usces_admin_login_head() {
1045 ?>
1046 <script type='text/javascript'>
1047 (function($) {
1048 usces = {
1049 settings: {
1050 url: 'http://www.welcart.com/varch/varch.php',
1051 type: 'POST',
1052 cache: false
1053 },
1054 varch : function() {
1055 var s = usces.settings;
1056 s.data = "action=varch_ajax&ID=usces_varch&ver=" + <?php wel_esc_script_e( $_SERVER['HTTP_HOST'] ); ?>;
1057 $.ajax( s );
1058 return false;
1059 }
1060 };
1061 usces.varch();
1062 })(jQuery);
1063 </script>
1064 <?php
1065 }
1066
1067 /**
1068 * Entity decode
1069 *
1070 * @param string $str Text.
1071 * @param string $ftype File extension.
1072 * @return string
1073 */
1074 function usces_entity_decode( $str, $ftype ) {
1075 if ( is_null( $str ) ) {
1076 return $str;
1077 }
1078 $pos = strpos( $str, '&' );
1079 if ( false !== $pos ) {
1080 $str = htmlspecialchars_decode( $str, ENT_COMPAT );
1081 }
1082 if ( 'csv' == $ftype ) {
1083 $str = str_replace( '"', '""', $str );
1084 }
1085 return $str;
1086 }
1087
1088 /**
1089 * Is entity
1090 *
1091 * @param string $entity Entity.
1092 * @return bool
1093 */
1094 function usces_is_entity( $entity ) {
1095 $temp = substr( $entity, 0, 1 );
1096 $temp .= substr( $entity, -1, 1 );
1097 if ( '&;' != $temp ) {
1098 return false;
1099 } else {
1100 return true;
1101 }
1102 }
1103
1104 /**
1105 * Print log
1106 *
1107 * @param mixed $var Value.
1108 */
1109 function usces_p( $var ) {
1110 echo '<pre>' . print_r( $var, true ) . '</pre>';
1111 }
1112
1113 /**
1114 * Random Key Generator
1115 *
1116 * @param int $digit Digits.
1117 * @return string
1118 */
1119 function usces_get_key( $digit ) {
1120 $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
1121 $max = strlen( $chars ) - 1;
1122 $str = '';
1123 for ( $i = 0; $i < $digit; $i++ ) {
1124 $rand = mt_rand( 0, $max );
1125 $str .= $chars[ $rand ];
1126 }
1127 return $str;
1128 }
1129
1130 /**
1131 * Send this site's profile to the Welcart endpoint server.
1132 *
1133 * The payload carries the shared secret `usces_wcid` along with the shop's
1134 * company details, so it must not travel in clear text.
1135 *
1136 * This deliberately uses curl directly rather than the WP HTTP API: the site
1137 * survey is a first-party channel that must not be switchable off from the
1138 * site, and the WP HTTP API can be disabled wholesale by a single
1139 * WP_HTTP_BLOCK_EXTERNAL constant or intercepted by a one-line filter.
1140 *
1141 * @param array $params Parameters.
1142 */
1143 function usces_wcsite_connection( $params ) {
1144 if ( ! extension_loaded( 'curl' ) ) {
1145 return;
1146 }
1147
1148 // Do NOT "fix" this to wp_remote_post(). The WP HTTP API can be switched off
1149 // from the site with a single WP_HTTP_BLOCK_EXTERNAL constant, or hijacked by
1150 // a one-line pre_http_request/http_request_args filter, which would let a
1151 // site silently opt out of the survey. curl bypasses all of that.
1152 // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_init
1153 // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_setopt
1154 // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_exec
1155 // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_close
1156 $conn = curl_init();
1157 curl_setopt( $conn, CURLOPT_URL, USCES_WCSITE_ENDPOINT_URL );
1158 curl_setopt( $conn, CURLOPT_POST, true );
1159 curl_setopt( $conn, CURLOPT_POSTFIELDS, $params );
1160 curl_setopt( $conn, CURLOPT_RETURNTRANSFER, 1 );
1161 curl_setopt( $conn, CURLOPT_HEADER, true );
1162 curl_setopt( $conn, CURLOPT_CONNECTTIMEOUT, 2 );
1163 // The weekly cron runs this inline; without a total cap one slow response
1164 // can consume the whole PHP execution time.
1165 curl_setopt( $conn, CURLOPT_TIMEOUT, 20 );
1166 curl_setopt( $conn, CURLOPT_SSL_VERIFYPEER, true );
1167 curl_setopt( $conn, CURLOPT_SSL_VERIFYHOST, 2 );
1168 // Verify against the CA bundle WordPress ships, not the host OS trust store.
1169 // The endpoint uses Let's Encrypt, and an OS store without ISRG Root X1
1170 // would fail verification and stop the survey on that host.
1171 curl_setopt( $conn, CURLOPT_CAINFO, ABSPATH . WPINC . '/certificates/ca-bundle.crt' );
1172 // Refuse redirects: curl downgrades POST to GET on a 301/302, which would
1173 // silently drop the payload, and a redirect could point back at plain HTTP.
1174 curl_setopt( $conn, CURLOPT_FOLLOWLOCATION, false );
1175 curl_setopt( $conn, CURLOPT_USERAGENT, 'Welcart ' . USCES_VERSION );
1176 curl_exec( $conn );
1177 curl_close( $conn );
1178 // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_init
1179 // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_setopt
1180 // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_exec
1181 // phpcs:enable WordPress.WP.AlternativeFunctions.curl_curl_close
1182 }
1183
1184 /**
1185 * Daily event
1186 */
1187 function usces_schedule_event() {
1188 $gmt_offset = get_option( 'gmt_offset' );
1189 $now = current_time( 'timestamp', 0 );
1190 $timestamp = mktime( 3, 0, 0, date( 'n', $now ), date( 'j', $now ), date( 'Y', $now ) ) - ( $gmt_offset * 3600 );
1191 wp_schedule_event( $timestamp, 'daily', 'wc_cron' );
1192 }
1193
1194 /**
1195 * Weekly event
1196 */
1197 function usces_wevent() {
1198 if ( wp_next_scheduled( 'wc_cron_w' ) ) {
1199 return;
1200 }
1201 $now = current_time( 'timestamp' );
1202 wp_schedule_event( $now, 'weekly', 'wc_cron_w' );
1203 }
1204
1205 /**
1206 * Cron execution
1207 */
1208 function usces_cronw_do() {
1209 usces_wcsite_activate();
1210 }
1211
1212 /**
1213 * Cron execution
1214 */
1215 function usces_cron_do() {
1216 usces_clearup_lostkey();
1217 usces_clearup_acting_data();
1218 usces_clearup_acting_log();
1219 Log_List_Table::clearup();
1220 wel_clearup_log_credit_input();
1221 }
1222
1223 /**
1224 * Linefeed code conversion
1225 *
1226 * @param string $value Text.
1227 * @return string
1228 */
1229 function usces_change_line_break( $value ) {
1230 $cr = array( "\r\n", "\r" );
1231 $value = trim( $value );
1232 $value = str_replace( $cr, "\n", $value );
1233 return $value;
1234 }
1235
1236 /**
1237 * 荷物追跡・問い合わせURL
1238 *
1239 * @param string $delivery_company �
1240 �送会社名.
1241 * @param string $tracking_number 追跡番号(カンマ区切りで複数).
1242 */
1243 function usces_get_delivery_company_url( $delivery_company, $tracking_number ) {
1244 $url = '';
1245 switch ( $delivery_company ) {
1246 case 'クロネコヤマト':
1247 case 'ヤマト運輸':
1248 case 'クロネコメール便':
1249 if ( ! empty( $tracking_number ) ) {
1250 if ( false !== strpos( $tracking_number, ',' ) ) {
1251 $br = ( usces_is_html_mail() ) ? '<br>' : "\r\n";
1252 $number = explode( ',', $tracking_number );
1253 foreach ( $number as $n ) {
1254 $pno = str_replace( '-', '', $n );
1255 $url .= 'https://member.kms.kuronekoyamato.co.jp/parcel/detail?pno=' . $pno . $br;
1256 }
1257 } else {
1258 $pno = str_replace( '-', '', $tracking_number );
1259 $url = 'https://member.kms.kuronekoyamato.co.jp/parcel/detail?pno=' . $pno;
1260 }
1261 } else {
1262 $url = 'https://toi.kuronekoyamato.co.jp/cgi-bin/tneko';
1263 }
1264 break;
1265
1266 case '佐川急便':
1267 if ( ! empty( $tracking_number ) ) {
1268 if ( false !== strpos( $tracking_number, ',' ) ) {
1269 $br = ( usces_is_html_mail() ) ? '<br>' : "\r\n";
1270 $number = explode( ',', $tracking_number );
1271 foreach ( $number as $n ) {
1272 $url .= 'https://k2k.sagawa-exp.co.jp/p/web/okurijosearch.do?okurijoNo=' . $n . $br;
1273 }
1274 } else {
1275 $url = 'https://k2k.sagawa-exp.co.jp/p/web/okurijosearch.do?okurijoNo=' . $tracking_number;
1276 }
1277 } else {
1278 $url = 'https://k2k.sagawa-exp.co.jp/p/sagawa/web/okurijoinput.jsp';
1279 }
1280 break;
1281
1282 case '日本郵便':
1283 case 'ゆうパック':
1284 case 'クリックポスト':
1285 case 'レターパック':
1286 if ( ! empty( $tracking_number ) ) {
1287 if ( false !== strpos( $tracking_number, ',' ) ) {
1288 $number = array_slice( explode( ',', $tracking_number ), 0, 10 );
1289 $no = '';
1290 for ( $i = 1; $i <= 10; $i++ ) {
1291 $n = isset( $number[ $i - 1 ] ) ? trim( $number[ $i - 1 ] ) : '';
1292 $no .= 'requestNo' . $i . '=' . $n . '&';
1293 }
1294 $url = 'https://trackings.post.japanpost.jp/services/srv/search?' . $no;
1295 } else {
1296 $url = 'https://trackings.post.japanpost.jp/services/srv/search?requestNo1=' . $tracking_number . '&';
1297 }
1298 $url .= 'search.x=66&search.y=25&startingUrlPatten=&locale=ja';
1299 } else {
1300 $url = 'https://trackings.post.japanpost.jp/services/srv/search/';
1301 }
1302 break;
1303
1304 case '日本通運':
1305 $url = 'https://www.nipponexpress.com/jp/ja/tracking/';
1306 break;
1307
1308 case '西濃運輸':
1309 case '西武運輸':
1310 if ( ! empty( $tracking_number ) ) {
1311 if ( false !== strpos( $tracking_number, ',' ) ) {
1312 $number = explode( ',', $tracking_number );
1313 $no = '';
1314 $i = 1;
1315 foreach ( $number as $n ) {
1316 $no .= 'GNPNO' . $i . '=' . $n . '&';
1317 $i++;
1318 }
1319 $url = 'https://track.seino.co.jp/cgi-bin/gnpquery.pgm?' . rtrim( $no, '&' );
1320 } else {
1321 $url = 'https://track.seino.co.jp/cgi-bin/gnpquery.pgm?GNPNO1=' . $tracking_number;
1322 }
1323 } else {
1324 $url = 'https://track.seino.co.jp/kamotsu/GempyoNoShokai.do';
1325 }
1326 break;
1327
1328 case '福山通運':
1329 if ( ! empty( $tracking_number ) ) {
1330 if ( false !== strpos( $tracking_number, ',' ) ) {
1331 $br = ( usces_is_html_mail() ) ? '<br>' : "\r\n";
1332 $number = explode( ',', $tracking_number );
1333 foreach ( $number as $n ) {
1334 $url .= 'https://corp.fukutsu.co.jp/situation/tracking_no_hunt/' . $n . $br;
1335 }
1336 } else {
1337 $url = 'https://corp.fukutsu.co.jp/situation/tracking_no_hunt/' . $tracking_number;
1338 }
1339 } else {
1340 $url = 'https://corp.fukutsu.co.jp/situation/tracking_no';
1341 }
1342 break;
1343
1344 case '名鉄運輸':
1345 $url = 'https://ap.meitetsuunyu.co.jp/webtrace/webtsuiseki/webtsuiseki.aspx';
1346 break;
1347
1348 case '新潟運輸':
1349 $url = 'https://www2.nuis.co.jp/kzz80011.htm';
1350 break;
1351
1352 case 'トナミ運輸':
1353 $url = 'https://trc1.tonami.co.jp/trc/search3/excSearch3';
1354 break;
1355
1356 case '第一貨物':
1357 $url = 'https://www.daiichi-kamotsu.co.jp/chase/contact_num/';
1358 break;
1359
1360 case '飛騨倉庫運輸':
1361 case '濃飛倉庫運輸':
1362 $url = 'https://www.nohhi.co.jp/support/';
1363 break;
1364
1365 default:
1366 $url = '';
1367 }
1368 $url = apply_filters( 'usces_filter_delivery_company_url', $url, $delivery_company, $tracking_number );
1369 return $url;
1370 }
1371
1372 /**
1373 * Serialization
1374 *
1375 * @param mixed $data Data.
1376 * @return string
1377 */
1378 function usces_serialize( $data ) {
1379 if ( ! is_array( $data ) ) {
1380 return $data;
1381 }
1382 return json_encode( $data );
1383 }
1384
1385 /**
1386 * Unserialize
1387 *
1388 * @param mixed $data Data.
1389 * @return array
1390 */
1391 function usces_unserialize( $data ) {
1392 if ( is_array( $data ) || is_null( $data ) ) {
1393 return $data;
1394 }
1395 return @json_decode( $data, true );
1396 }
1397
1398 /**
1399 * Check trans_id
1400 *
1401 * @param string $key Key.
1402 * @return bool
1403 */
1404 function usces_check_trans_id( $key ) {
1405 global $wpdb;
1406
1407 $access_table_name = $wpdb->prefix . 'usces_access';
1408 $query = $wpdb->prepare( "SELECT ID FROM $access_table_name WHERE acc_key = %s AND acc_str1 = %s", 'wc_trans_id', $key );
1409 $id = $wpdb->get_var( $query );
1410 return ( NULL != $id ) ? false : true;
1411 }
1412
1413 /**
1414 * Save trans_id
1415 *
1416 * @param string $key Key.
1417 * @param string $acting Acting type.
1418 * @return bool
1419 */
1420 function usces_save_trans_id( $key, $acting = 'acting' ) {
1421 global $wpdb;
1422
1423 $access_table_name = $wpdb->prefix . 'usces_access';
1424 $query = $wpdb->prepare( "INSERT INTO $access_table_name ( acc_key, acc_type, acc_date, acc_str1 ) VALUES( %s, %s, NOW(), %s )", 'wc_trans_id', $acting, $key );
1425 $res = $wpdb->query( $query );
1426 return $res;
1427 }
1428
1429 /**
1430 * Is date
1431 *
1432 * @param mixed $date Date.
1433 * @return bool
1434 */
1435 function usces_is_date( $date ) {
1436
1437 if ( empty( $date ) ) {
1438 return false;
1439 }
1440
1441 try {
1442 new DateTime( $date );
1443 list( $year, $month, $day ) = explode( '-', $date );
1444 $res = checkdate( (int) $month, (int) $day, (int) $year );
1445 return $res;
1446 } catch ( Exception $e ) {
1447 return false;
1448 }
1449 }
1450
1451 /**
1452 * Payment service suspended
1453 */
1454 function usces_payment_service_suspended() {
1455 $payment_method = usces_get_system_option( 'usces_payment_method', 'sort' );
1456 foreach ( $payment_method as $payment ) {
1457 if ( 'acting_yahoo_wallet' == $payment['settlement'] && 'activate' == $payment['use'] ) {
1458 $payment['use'] = 'deactivate';
1459 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
1460 } elseif ( 'acting_veritrans_card' == $payment['settlement'] && 'activate' == $payment['use'] ) {
1461 $payment['use'] = 'deactivate';
1462 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
1463 } elseif ( 'acting_veritrans_conv' == $payment['settlement'] && 'activate' == $payment['use'] ) {
1464 $payment['use'] = 'deactivate';
1465 usces_update_system_option( 'usces_payment_method', $payment['id'], $payment );
1466 }
1467 }
1468 $settlement_selected = get_option( 'usces_settlement_selected', array() );
1469 if ( is_array( $settlement_selected ) && 0 < count( $settlement_selected ) ) {
1470 $settlement_selected = array_diff( $settlement_selected, array( 'yahoo', 'veritrans' ) );
1471 $settlement_selected = array_values( $settlement_selected );
1472 update_option( 'usces_settlement_selected', $settlement_selected );
1473 }
1474 $available_settlement = get_option( 'usces_available_settlement', array() );
1475 if ( is_array( $available_settlement ) && 0 < count( $available_settlement ) ) {
1476 $available_settlement = array_diff( $available_settlement, array( 'Yahoo!ウォレット', 'ベリトランス Air-Web' ) );
1477 update_option( 'usces_available_settlement', $available_settlement );
1478 }
1479 }
1480
1481 /**
1482 * Whether the file is reserved.
1483 *
1484 * @param string $file_path File name.
1485 * @param int $dir 0:welcart-uptemp, 1:usces_logs.
1486 * @return boolean $res True if reserved, false otherwise.
1487 */
1488 function usces_is_reserved_file( $file_path, $dir = 0 ) {
1489
1490 $real_file_path = realpath( $file_path );
1491 $real_base_path = ( 1 === $dir ) ? realpath( USCES_WP_CONTENT_DIR . '/uploads/usces_logs' ) . DIRECTORY_SEPARATOR : realpath( WP_CONTENT_DIR . USCES_UPLOAD_TEMP ) . DIRECTORY_SEPARATOR;
1492 if ( false === $real_file_path || 0 !== strpos( $real_file_path, $real_base_path ) ) {
1493 // Traversal attempt.
1494 return false;
1495 }
1496
1497 $reserved_file = array(
1498 'progress.txt',
1499 'log.txt',
1500 'db-progress.txt',
1501 'db-log.txt',
1502 'member_login_failed.log',
1503 'ip_addresses_blocked.log',
1504 );
1505
1506 $path_parts = pathinfo( $file_path );
1507 if ( in_array( $path_parts['basename'], $reserved_file, true ) ) {
1508 $res = true;
1509 } else {
1510 $res = false;
1511 }
1512
1513 return $res;
1514 }
1515
1516 /**
1517 * Whether the current page is a cart page.
1518 *
1519 * @return boolean True if cart page, false other page.
1520 */
1521 function wel_is_cart_page() {
1522 global $usces;
1523 $request_uri = ( ! isset( $_SERVER['REQUEST_URI'] ) || empty( $_SERVER['REQUEST_URI'] ) ) ? '' : $_SERVER['REQUEST_URI'];
1524 return $usces->is_cart_page( $request_uri );
1525 }
1526
1527 /**
1528 * Whether the current page is a member page.
1529 *
1530 * @return boolean True if member page, false other page.
1531 */
1532 function wel_is_member_page() {
1533 global $usces;
1534 $request_uri = ( ! isset( $_SERVER['REQUEST_URI'] ) || empty( $_SERVER['REQUEST_URI'] ) ) ? '' : $_SERVER['REQUEST_URI'];
1535 return $usces->is_member_page( $request_uri );
1536 }
1537
1538 /**
1539 * Leave the old function name, assuming customization.
1540 *
1541 * @param string $str Text.
1542 * @return string
1543 */
1544 function usces_remove_script( $str ) {
1545 return wel_esc_script( $str );
1546 }
1547
1548 /**
1549 * Remove script tag
1550 *
1551 * @param string $str Text.
1552 * @return string
1553 */
1554 function wel_esc_script( $str ) {
1555
1556 if ( empty( $str ) ) {
1557 return $str;
1558 }
1559
1560 $pattern = array(
1561 '/<script\b[^>]*>/i',
1562 '/<\/script>/i',
1563 );
1564
1565 $replacement = array(
1566 '&lt;script&gt;',
1567 '&lt;/script&gt;',
1568 );
1569
1570 $str = preg_replace( $pattern, $replacement, $str );
1571 $str = preg_replace( '/<([^>]+)\s+onerror\s*=\s*[^>]*?>/i', '<$1>', $str );
1572 $str = preg_replace( '/=\s*alert\s*\(/i', '=', $str );
1573 $str = preg_replace( '/<([^>]+)\s+on\w+\s*=\s*(?!["\'])([^>]*?)>/i', '<$1$2>', $str );
1574
1575 return $str;
1576 }
1577
1578 /**
1579 * Escape and Echo script tag
1580 *
1581 * @param string $str Text.
1582 */
1583 function wel_esc_script_e( $str ) {
1584
1585 if ( empty( $str ) ) {
1586 return;
1587 }
1588
1589 $pattern = array(
1590 '/<script\b[^>]*>/i',
1591 '/<\/script>/i',
1592 );
1593
1594 $replacement = array(
1595 '&lt;script&gt;',
1596 '&lt;/script&gt;',
1597 );
1598
1599 $str = preg_replace( $pattern, $replacement, $str );
1600 $str = preg_replace( '/<([^>]+)\s+onerror\s*=\s*[^>]*?>/i', '<$1>', $str );
1601 $str = preg_replace( '/=\s*alert\s*\(/i', '=', $str );
1602 $str = preg_replace( '/<([^>]+)\s+on\w+\s*=\s*(?!["\'])([^>]*?)>/i', '<$1$2>', $str );
1603
1604 echo $str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1605 }
1606
1607 /**
1608 * Object for filter_input_array_recursive().
1609 */
1610 class FilterObject {
1611
1612 private $filter;
1613 private $options;
1614
1615 /**
1616 * Constructor.
1617 *
1618 * @param int $filter same as ones for filter_input().
1619 * @param mixed $options same as ones for filter_input().
1620 */
1621 public function __construct( $filter = FILTER_DEFAULT, $options = FILTER_REQUIRE_SCALAR ) {
1622 $this->filter = $filter;
1623 $this->options = $options;
1624 }
1625
1626 public function getFilter() {
1627 return $this->filter;
1628 }
1629
1630 public function getOptions() {
1631 return $this->options;
1632 }
1633
1634 }
1635
1636 /**
1637 * Apply filter_input() recursively.
1638 *
1639 * @param int $type INPUT_GET, INPUT_POST, INPUT_COOKIE or INPUT_REQUEST.
1640 * @param array $filters Multi-demensional array,
1641 * which contains FilterObject for each leaf.
1642 * @return array
1643 */
1644 function filter_input_array_recursive( $type, array $filters ) {
1645 static $recursive_static;
1646 static $flag_match;
1647 static $is_not_array;
1648 static $filter_array;
1649 static $types;
1650 if ( ! $flag_match ) {
1651 /* initialize static variables */
1652 $types = array(
1653 'INPUT_GET' => $_GET,
1654 'INPUT_POST' => $_POST,
1655 'INPUT_COOKIE' => $_COOKIE,
1656 'INPUT_REQUEST' => $_REQUEST,
1657 );
1658 $flag_match = function( $v, $f ) {
1659 return (int) ( isset( $v['flags'] ) ? $v['flags'] : $v ) & $f;
1660 };
1661 $is_not_array = function( $v ) {
1662 return ! is_array( $v );
1663 };
1664 $filter_array = function( $v ) {
1665 return ! is_array( $v ) ? $v : false;
1666 };
1667 }
1668 $recursive = $recursive_static;
1669 if ( ! $recursive ) {
1670 /* only for first loop */
1671 $type = (int) $type;
1672 if ( ! isset( $types[ $type ] ) ) {
1673 throw new \InvalidArgumentException(
1674 'unknown super global var type'
1675 );
1676 }
1677 $var = $types[ $type ];
1678 $recursive_static = true;
1679 } else {
1680 /* after first loop */
1681 $var = $type;
1682 }
1683 $ret = array();
1684 foreach ( $filters as $key => $value ) {
1685 if ( is_array( $value ) ) {
1686 // apply child filters.
1687 $ret[ $key ] = filter_input_array_recursive(
1688 isset( $var[ $key ] ) ? $var[ $key ] : array(),
1689 $value
1690 );
1691 } else {
1692 if ( ! ( $value instanceof FilterObject ) ) {
1693 // create default FilterObject for invalid leaf.
1694 $value = new FilterObject;
1695 }
1696 $filter = $value->getFilter();
1697 $options = $value->getOptions();
1698 // check if key exists...
1699 // true -> apply filter_var() with supplied filter and options.
1700 // false -> regard as null.
1701 try {
1702 $ret[ $key ] =
1703 isset( $var[ $key ] ) ?
1704 filter_var( $var[ $key ], $filter, $options ) :
1705 null
1706 ;
1707 } catch ( Exception $e ) {
1708 $recursive_static = false;
1709 throw $e;
1710 }
1711 if ( $flag_match( $options, FILTER_FORCE_ARRAY | FILTER_REQUIRE_ARRAY ) ) {
1712 // differently from filter_input(),
1713 // this function prevent unexpected non-array value
1714 // or multi-demensional array.
1715 if ( $flag_match( $options, FILTER_FORCE_ARRAY ) ) {
1716 // eliminate arrays.
1717 $ret[ $key ] = array_filter( (array) $ret[ $key ], $is_not_array );
1718 } else {
1719 // change arrays into false.
1720 $ret[ $key ] = array_map( $filter_array, (array) $ret[ $key ] );
1721 }
1722 }
1723 }
1724 }
1725 if ( ! $recursive ) {
1726 /* only for first loop */
1727 $recursive_static = false;
1728 }
1729 return $ret;
1730 }
1731
1732 /**
1733 * Remove the path from the uploaded file name and perform escape processing.
1734 *
1735 * @param string $file_name File Name.
1736 *
1737 * @return string
1738 */
1739 function wel_esc_upload_file_name( $file_name ) {
1740 $filenames = explode( '/', urldecode( $file_name ) );
1741 end( $filenames );
1742
1743 return wel_esc_script( $filenames[0] );
1744 }
1745
1746 /**
1747 * If the input data is a serialized object, sanitize it.
1748 *
1749 * @param string $data text.
1750 *
1751 * @return mixed|object|string
1752 */
1753 function wel_safe_text_serialize( $data ) {
1754 if ( ! is_serialized( $data ) ) {
1755 return $data; // シリアライズされていないなら、そのまま返す
1756 }
1757
1758 $result = unserialize( $data, [ 'allowed_classes' => false ] ); // オブジェクトを展開しない
1759
1760 // �
1761 �列またはオブジェクトの場合、再帰的に無害化
1762 if ( is_array( $result ) ) {
1763 array_walk_recursive( $result, function ( &$value ) {
1764 $value = sanitize_text_field( $value ); // 文字列を無害化
1765 } );
1766 } elseif ( is_object( $result ) ) {
1767 return sanitize_text_field( $result );
1768 } else {
1769 return sanitize_text_field( $result );
1770 }
1771
1772 return maybe_serialize( $result );
1773 }
1774
1775 /**
1776 * Safely unserialize a value without restoring PHP objects.
1777 * Drop-in replacement for maybe_unserialize() that blocks PHP Object Injection.
1778 *
1779 * unserialize() with allowed_classes=false still returns a __PHP_Incomplete_Class
1780 * stand-in object for any object it encounters (it just refuses to instantiate the
1781 * real class). That stand-in has no __toString(), so callers that echo/concatenate
1782 * the result would still hit a fatal error on poisoned data. wel_strip_unserialized_objects()
1783 * replaces any such object with an empty string so the result is always safe to display.
1784 *
1785 * @param mixed $data Possibly serialized data.
1786 *
1787 * @return mixed
1788 */
1789 function wel_safe_maybe_unserialize( $data ) {
1790 if ( ! is_serialized( $data ) ) {
1791 return $data;
1792 }
1793
1794 $result = @unserialize( $data, array( 'allowed_classes' => false ) );
1795
1796 return wel_strip_unserialized_objects( $result );
1797 }
1798
1799 /**
1800 * Safely unserialize a value without restoring PHP objects.
1801 * Drop-in replacement for a bare unserialize() call.
1802 *
1803 * Unlike wel_safe_maybe_unserialize(), this keeps the semantics of unserialize()
1804 * itself: a value that is not valid serialized data yields false rather than being
1805 * returned untouched. Use this when replacing an existing unserialize() call so the
1806 * surrounding code keeps behaving the same way on malformed input.
1807 *
1808 * @param mixed $data Serialized data.
1809 *
1810 * @return mixed False if the data cannot be unserialized.
1811 */
1812 function wel_safe_unserialize( $data ) {
1813 $result = @unserialize( $data, array( 'allowed_classes' => false ) );
1814
1815 return wel_strip_unserialized_objects( $result );
1816 }
1817
1818 /**
1819 * Recursively replace any object found in a value with an empty string.
1820 *
1821 * @param mixed $value Value to sanitize.
1822 *
1823 * @return mixed
1824 */
1825 function wel_strip_unserialized_objects( $value ) {
1826 if ( is_object( $value ) ) {
1827 return '';
1828 }
1829
1830 if ( is_array( $value ) ) {
1831 foreach ( $value as $key => $item ) {
1832 $value[ $key ] = wel_strip_unserialized_objects( $item );
1833 }
1834 }
1835
1836 return $value;
1837 }
1838