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 / filters.php

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

2,979 lines 93.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart functions
4 *
5 * Hooking the main functions.
6 *
7 * @package Welcart
8 */
9
10 // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL
11 // phpcs:disable WordPress.PHP.DevelopmentFunctions, WordPress.PHP.NoSilencedErrors
12
13 /**
14 * Get postmeta.
15 *
16 * @param string $null Null value.
17 * @param int $object_id Post ID.
18 * @param string $meta_key Meta key.
19 * @param bool $single Text or Array.
20 * @return mixed
21 */
22 function usces_filter_get_post_metadata( $null, $object_id, $meta_key, $single ) {
23 global $wpdb;
24
25 $query = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", $object_id, $meta_key );
26 $metas = $wpdb->get_col( $query );
27 if ( ! empty( $metas ) ) {
28 return array_map( 'wel_safe_maybe_unserialize', $metas );
29 }
30
31 if ( $single ) {
32 return '';
33 } else {
34 return array();
35 }
36 }
37
38 /**
39 * Save decorated order id.
40 * usces_action_reg_orderdata
41 *
42 * @param array $args {
43 * The array of order-related data.
44 * @type array $cart Cart data.
45 * @type array $entry Entry data.
46 * @type int $order_id Order ID.
47 * @type int $member_id Member ID.
48 * @type array $payments Payment Method.
49 * @type array $charging_type Charge type.
50 * @type array $results Settlement result data.
51 * }
52 */
53 function usces_action_reg_orderdata( $args ) {
54 global $usces;
55 extract( $args );
56
57 $options = get_option( 'usces', array() );
58 $prefix = $options['system']['dec_orderID_prefix'] ?? '';
59 $prefix = apply_filters( 'usces_filter_dec_order_id_prefix', $prefix, $args );
60
61 $dec_order_id = usces_make_deco_order_id( $order_id );
62 $dec_order_id = apply_filters( 'usces_filter_dec_order_id', $dec_order_id, $args );
63 $dec_order_id = $prefix . $dec_order_id;
64
65 $usces->set_order_meta_value( 'dec_order_id', $dec_order_id, $order_id );
66 }
67
68 /**
69 * Inventory (stock) adjustment.
70 * usces_action_reg_orderdata
71 *
72 * @param array $args {
73 * The array of order-related data.
74 * @type array $cart Cart data.
75 * @type array $entry Entry data.
76 * @type int $order_id Order ID.
77 * @type int $member_id Member ID.
78 * @type array $payments Payment Method.
79 * @type array $charging_type Charge type.
80 * @type array $results Settlement result data.
81 * }
82 */
83 function usces_action_reg_orderdata_stocks( $args ) {
84 global $usces;
85 extract( $args );
86
87 foreach ( $cart as $cartrow ) {
88
89 $item_order_acceptable = (int) $usces->getItemOrderAcceptable( $cartrow['post_id'], false );
90 $sku = urldecode( $cartrow['sku'] );
91 $zaikonum = $usces->getItemZaikoNum( $cartrow['post_id'], $sku, false );
92 if ( WCUtils::is_blank( $zaikonum ) ) {
93 continue;
94 }
95 $zaikonum = (int) $zaikonum - (int) $cartrow['quantity'];
96 if ( 1 !== $item_order_acceptable ) {
97 if ( $zaikonum < 0 ) {
98 $zaikonum = 0;
99 }
100 }
101
102 $usces->updateItemZaikoNum( $cartrow['post_id'], $sku, $zaikonum );
103
104 if ( 1 !== $item_order_acceptable ) {
105 if ( $zaikonum <= 0 ) {
106 $default_empty_status = apply_filters( 'usces_filter_default_empty_status', 2 );
107 $usces->updateItemZaiko( $cartrow['post_id'], $sku, $default_empty_status );
108 do_action( 'usces_action_outofstock', $cartrow['post_id'], $sku, $cartrow, $args );
109 }
110 }
111 }
112 }
113
114 /**
115 * Add security headers (member, cart).
116 * send_headers
117 *
118 * @return void
119 */
120 function usces_add_security_headers() {
121 global $usces;
122 if ( is_customize_preview() ) {
123 return;
124 }
125 $request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) );
126 if ( $usces->is_member_page( $request_uri ) || $usces->is_cart_page( $request_uri ) ) {
127 header( 'X-Frame-Options: DENY' );
128 header( "Content-Security-Policy: frame-ancestors 'none'" );
129 }
130 }
131
132 /**
133 * OGP meta.
134 * wp_head
135 */
136 function usces_action_ogp_meta() {
137 global $usces, $post;
138
139 if ( empty( $post ) || ! $usces->is_item( $post ) || ! is_single() ) {
140 return;
141 }
142
143 $item = wel_get_product( $post->ID );
144 $pictid = $usces->get_mainpictid( $item['itemCode'] );
145 $image_info = wp_get_attachment_image_src( $pictid, 'thumbnail' );
146
147 $ogs['title'] = $item['itemName'];
148 $ogs['type'] = 'product';
149 $ogs['description'] = strip_tags( get_the_title( $post->ID ) );
150 $ogs['url'] = get_permalink( $post->ID );
151 $ogs['image'] = ( isset( $image_info[0] ) ) ? $image_info[0] : '';
152 $ogs['site_name'] = get_option( 'blogname' );
153
154 $ogs = apply_filters( 'usces_filter_ogp_meta', $ogs, $post->ID );
155
156 foreach ( $ogs as $key => $value ) {
157 echo "\n" . '<meta property="og:' . esc_attr( $key ) . '" content="' . ( ( 'url' === $key || 'image' === $key ) ? esc_url( $value ) : esc_attr( $value ) ) . '">';
158 }
159 }
160
161 /**
162 * 構造化データのJSON出力.
163 * wp_head
164 */
165 function usces_action_structured_data_json() {
166 global $post, $usces;
167
168 if ( 0 === USCES_STRUCTURED_DATA_PRODUCT::$opts['status'] || empty( $post ) ) {
169 return;
170 }
171
172 global $post, $usces;
173 if ( empty( $post ) || ! $usces->is_item( $post ) ) {
174 return;
175 }
176
177 $product = wel_get_product( $post->ID );
178 if ( is_admin() || ! is_single() || ! $product['itemCode'] ) {
179 return;
180 }
181
182 $itemname = $product['itemName'];
183 $skus = $product['_sku'];
184 $target_sku = null;
185 $structuredDataSkuCode = $product['structuredDataSku'];
186 $patch_skus = array();
187
188 foreach ( $skus as $sku ) {
189 // 廃盤は削除.
190 if ( 4 === (int) $usces->getItemZaikoStatusId( $post->ID, $sku['code'] ) ) {
191 continue;
192 }
193 // 特定のSKUがあればそれを対象とする.
194 if ( $structuredDataSkuCode === $sku['code'] ) {
195 $target_sku = $sku;
196 }
197 $patch_skus[] = $sku;
198 }
199 if ( null === $target_sku ) {
200 foreach ( $patch_skus as $sku ) {
201 // 1番目のSKU.
202 if ( 'first' === USCES_STRUCTURED_DATA_PRODUCT::$opts['default_price'] ) {
203 $target_sku = $sku;
204 break;
205 }
206 // 1番安価のSKU.
207 if ( 'minimum' === USCES_STRUCTURED_DATA_PRODUCT::$opts['default_price'] ) {
208 if ( $target_sku && (int) $target_sku['price'] <= (int) $sku['price'] ) {
209 continue;
210 }
211 $target_sku = $sku;
212 } else {
213 if ( $target_sku && (int) $target_sku['price'] >= (int) $sku['price'] ) { // 1番高価のSKU.
214 continue;
215 }
216 $target_sku = $sku;
217 }
218 }
219 }
220 if ( empty( $target_sku ) ) {
221 return;
222 }
223 $target_sku['availability'] = $usces->is_item_zaiko( $post->ID, $sku['code'] ) ? 'https://schema.org/InStock' : 'https://schema.org/SoldOut';
224
225 $url = get_the_permalink();
226 $image = usces_the_itemImageURL( 0, 'return' );
227 $description = function_exists( 'get_field' ) ? get_field( 'field_rich_result_product_description' ) : '';
228 $stock = usces_have_zaiko_anyone( $post->ID ) ? 'https://schema.org/InStock' : 'https://schema.org/SoldOut';
229 $cr = $usces->options['system']['currency'];
230 if ( isset( $usces_settings['currency'][ $cr ] ) ) {
231 list( $code, $decimal, $point, $seperator, $symbol ) = $usces_settings['currency'][ $cr ];
232 } else {
233 $code = 'JPY';
234 }
235 $data = array(
236 '@context' => 'https://schema.org/',
237 '@type' => 'Product',
238 'name' => $itemname,
239 'image' => $image,
240 'description' => $description,
241 'productID' => $product['itemCode'],
242 'offers' => [
243 '@type' => 'Offer',
244 'sku' => $product['itemCode'] . '-' . $target_sku['code'],
245 'priceCurrency' => $code,
246 'price' => $target_sku['price'],
247 'availability' => $target_sku['availability'],
248 ],
249 );
250 $script = json_encode( apply_filters( 'usces_filter_structured_data', $data, $post ) );
251
252 echo "\n" . '<script type="application/ld+json">' . $script . '</script>';
253 }
254
255 /**
256 * 特定のSKUデータを構造化データのに利用するためのフォーム.
257 * usces_item_master_first_section
258 *
259 * @param string $html First section area.
260 * @param int $post_ID Post Id.
261 */
262 function usces_item_master_structured_data( $html, $post_ID ) {
263 if ( 1 === USCES_STRUCTURED_DATA_PRODUCT::$opts['status'] ) {
264 $product = wel_get_product( $post_ID );
265 $structuredDataSku = ( isset( $product['structuredDataSku'] ) ) ? $product['structuredDataSku'] : '';
266 ?>
267 <tr>
268 <th><?php esc_html_e( 'SKUs Code used for structured data', 'usces' ); ?></th>
269 <td>
270 <input type="text" name="structuredDataSku" id="structuredDataSku" class="structuredDataSku" value="<?php echo esc_attr( $structuredDataSku ); ?>" />
271 <input type="hidden" name="structuredDataSku_nonce" id="structuredDataSku_nonce" value="<?php echo wp_create_nonce( 'structuredDataSku_nonce' ); ?>" />
272 </td>
273 </tr>
274 <?php
275 }
276 }
277
278 /**
279 * Make Directory.
280 */
281 function wc_mkdir() {
282 global $usces;
283 if ( is_admin() && ! WCUtils::is_blank( $usces->options['logs_path'] ) && false !== strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) ) {
284 $welcart_file_dir = $usces->options['logs_path'] . '/welcart';
285 $logs_dir = $welcart_file_dir . '/logs';
286 if ( ! file_exists( $welcart_file_dir ) ) {
287 $res = @mkdir( $welcart_file_dir, 0700 );
288 if ( ! $res ) {
289 $msg = '<div class="error"><p>下記のディレクトリーを、所有�
290 ' . get_current_user() . '、パーミッション:700 で作成してください。 <br />' . $welcart_file_dir . '</p></div>';
291 add_action(
292 'admin_notices',
293 function() {
294 echo addcslashes( $msg, '"' );
295 }
296 );
297 }
298 }
299 $stat = stat( $welcart_file_dir );
300 print_r( $stat );
301 }
302 }
303
304 /**
305 * Save the oreder cart data.
306 * usces_action_reg_orderdata
307 *
308 * @param array $args {
309 * The array of order-related data.
310 * @type array $cart Cart data.
311 * @type array $entry Entry data.
312 * @type int $order_id Order ID.
313 * @type int $member_id Member ID.
314 * @type array $payments Payment Method.
315 * @type array $charging_type Charge type.
316 * @type array $results Settlement result data.
317 * }
318 */
319 function usces_reg_ordercartdata( $args ) {
320 global $usces, $wpdb, $usces_settings;
321 extract( $args );
322
323 if ( ! $order_id ) {
324 return;
325 }
326 $cart_table = $wpdb->prefix . 'usces_ordercart';
327 $cart_meta_table = $wpdb->prefix . 'usces_ordercart_meta';
328
329 foreach ( $cart as $row_index => $value ) {
330 $post_id = (int) $value['post_id'];
331 $product = wel_get_product( $post_id, false );
332 $item_code = $product['itemCode'];
333 $item_name = $product['itemName'];
334 $skus = $usces->get_skus( $value['post_id'], 'code', false );
335 $sku_encoded = $value['sku'];
336 $skucode = urldecode( $value['sku'] );
337 $sku = $skus[ $skucode ];
338 $tax = 0;
339 $wpdb->query(
340 $wpdb->prepare(
341 "INSERT INTO {$cart_table}
342 (order_id, row_index, post_id, item_code, item_name, sku_code, sku_name, cprice, price, quantity,
343 unit, tax, destination_id, cart_serial )
344 VALUES (%d, %d, %d, %s, %s, %s, %s, %f, %f, %f, %s, %d, %d, %s)",
345 $order_id,
346 $row_index,
347 $post_id,
348 $item_code,
349 $item_name,
350 $skucode,
351 $sku['name'],
352 $sku['cprice'],
353 $value['price'],
354 $value['quantity'],
355 $sku['unit'],
356 $tax,
357 null,
358 $value['serial']
359 )
360 );
361
362 $cart_id = $wpdb->insert_id;
363 $opt_fields = wel_get_opts( $post_id, 'sort', false );
364
365 if ( $value['options'] ) {
366
367 foreach ( (array) $opt_fields as $okey => $val ) {
368
369 $enc_key = urlencode( $val['name'] );
370 $means = $opt_fields[ $okey ]['means'];
371
372 if ( 3 === (int) $means ) {
373
374 if ( '' == $value['options'][ $enc_key ] ) {
375 $ovalue = $value['options'][ $enc_key ];
376 } else {
377 $ovalue = urldecode( $value['options'][ $enc_key ] );
378 }
379 } elseif ( 4 === (int) $means ) {
380
381 if ( is_array( $value['options'][ $enc_key ] ) ) {
382
383 $temp = array();
384 foreach ( $value['options'][ $enc_key ] as $v ) {
385 $temp[] = urldecode( $v );
386 }
387 $ovalue = serialize( $temp );
388 } elseif ( '' == $value['options'][ $enc_key ] ) {
389
390 $ovalue = $value['options'][ $enc_key ];
391 } else {
392
393 $ovalue = urldecode( $value['options'][ $enc_key ] );
394 }
395 } else {
396
397 if ( is_array( $value['options'][ $enc_key ] ) ) {
398 $temp = array();
399 foreach ( $value['options'][ $enc_key ] as $k => $v ) {
400 $temp[ $k ] = urldecode( $v );
401 }
402 $ovalue = serialize( $temp );
403 } else {
404 $ovalue = urldecode( $value['options'][ $enc_key ] );
405 }
406 }
407 $ovalue = wel_safe_text_serialize( $ovalue );
408 $wpdb->query(
409 $wpdb->prepare(
410 "INSERT INTO {$cart_meta_table}
411 ( cart_id, meta_type, meta_key, meta_value ) VALUES (%d, %s, %s, %s)",
412 $cart_id,
413 'option',
414 $val['name'],
415 $ovalue
416 )
417 );
418 }
419 }
420
421 if ( $value['advance'] ) {
422
423 foreach ( (array) $value['advance'] as $akey => $avalue ) {
424 $advance = $usces->cart->wc_unserialize( $avalue );
425
426 if ( is_array( $advance ) ) {
427
428 if ( isset( $advance[ $post_id ][ $sku_encoded ] ) && is_array( $advance[ $post_id ][ $sku_encoded ] ) ) {
429 $akeys = array_keys( $advance[ $post_id ][ $sku_encoded ] );
430
431 foreach ( (array) $akeys as $akey ) {
432 if ( is_array( $advance[ $post_id ][ $sku_encoded ][ $akey ] ) ) {
433 $avalue = serialize( $advance[ $post_id ][ $sku_encoded ][ $akey ] );
434 } else {
435 $avalue = $advance[ $post_id ][ $sku_encoded ][ $akey ];
436 }
437 $wpdb->query(
438 $wpdb->prepare(
439 "INSERT INTO {$cart_meta_table}
440 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )",
441 $cart_id,
442 $akey,
443 $avalue
444 )
445 );
446 }
447 } else {
448 $akeys = array_keys( $advance );
449 $akey = empty( $akeys[0] ) ? 'advance' : $akeys[0];
450 $avalue = serialize( $advance );
451 $wpdb->query(
452 $wpdb->prepare(
453 "INSERT INTO {$cart_meta_table}
454 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )",
455 $cart_id,
456 $akey,
457 $avalue
458 )
459 );
460 }
461 } else {
462 $avalue = urldecode( $avalue );
463 $wpdb->query(
464 $wpdb->prepare(
465 "INSERT INTO {$cart_meta_table}
466 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'advance', %s, %s )",
467 $cart_id,
468 $akey,
469 $avalue
470 )
471 );
472 }
473 }
474 }
475
476 if ( $usces->is_reduced_taxrate() ) {
477 if ( isset( $sku['taxrate'] ) && 'reduced' == $sku['taxrate'] ) {
478 $tkey = 'reduced';
479 $tvalue = $usces->options['tax_rate_reduced'];
480 } else {
481 $tkey = 'standard';
482 $tvalue = $usces->options['tax_rate'];
483 }
484 $wpdb->query(
485 $wpdb->prepare(
486 "INSERT INTO {$cart_meta_table}
487 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'taxrate', %s, %s )",
488 $cart_id,
489 $tkey,
490 $tvalue
491 )
492 );
493 }
494
495 do_action( 'usces_action_reg_ordercart_row', $cart_id, $row_index, $value, $args );
496 }
497 }
498
499 /**
500 * Document title.
501 * pre_get_document_title
502 * wp_title
503 * alias fiter_mainTitle()
504 *
505 * @param string $title Document title.
506 * @param string $sep Separator.
507 * @return string
508 */
509 function filter_mainTitle( $title, $sep = '' ) {
510 return fiter_mainTitle( $title, $sep );
511 }
512
513 /**
514 * Document title.
515 *
516 * @param string $title Document title.
517 * @param string $sep Separator.
518 * @return string
519 */
520 function fiter_mainTitle( $title, $sep = '' ) {
521 global $usces;
522
523 if ( empty( $sep ) ) {
524 $sep = apply_filters( 'document_title_separator', '|' );
525 }
526
527 switch ( $usces->page ) {
528 case 'cart':
529 $newtitle = apply_filters( 'usces_filter_title_cart', __( 'In the cart', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
530 break;
531
532 case 'customer':
533 $newtitle = apply_filters( 'usces_filter_title_customer', __( 'Customer Information', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
534 break;
535
536 case 'delivery':
537 $newtitle = apply_filters( 'usces_filter_title_delivery', __( 'Shipping / Payment options', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
538 break;
539
540 case 'confirm':
541 $newtitle = apply_filters( 'usces_filter_title_confirm', __( 'Confirmation', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
542 break;
543
544 case 'ordercompletion':
545 $newtitle = apply_filters( 'usces_filter_title_ordercompletion', __( 'Order Complete', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
546 break;
547
548 case 'error':
549 $newtitle = apply_filters( 'usces_filter_title_error', __( 'Error', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' ); // new fitler name.
550 break;
551
552 case 'search_item':
553 $newtitle = apply_filters( 'usces_filter_title_search_item', __( "'AND' search by categories", 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
554 break;
555
556 case 'maintenance':
557 $newtitle = apply_filters( 'usces_filter_title_maintenance', __( 'Under Maintenance', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
558 break;
559
560 case 'login':
561 $newtitle = apply_filters( 'usces_filter_title_login', __( 'Log-in for members', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
562 break;
563
564 case 'member':
565 $newtitle = apply_filters( 'usces_filter_title_member', __( 'Membership information', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
566 break;
567
568 case 'newmemberform':
569 $newtitle = apply_filters( 'usces_filter_title_newmemberform', __( 'New enrollment form', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
570 break;
571
572 case 'newcompletion':
573 $newtitle = apply_filters( 'usces_filter_title_newcompletion', __( 'New enrollment complete', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' ); // new fitler name.
574 break;
575
576 case 'editmemberform':
577 $newtitle = apply_filters( 'usces_filter_title_editmemberform', __( 'Member information editing', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' ); // new fitler name.
578 break;
579
580 case 'editcompletion':
581 $newtitle = apply_filters( 'usces_filter_title_editcompletion', __( 'Membership information change is completed', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' ); // new fitler name.
582 break;
583
584 case 'lostmemberpassword':
585 $newtitle = apply_filters( 'usces_filter_title_lostmemberpassword', __( 'The new password acquisition', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
586 break;
587
588 case 'lostcompletion':
589 $newtitle = apply_filters( 'usces_filter_title_lostcompletion', __( 'New password procedures for obtaining complete', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' ); // new fitler name.
590 break;
591
592 case 'changepassword':
593 $newtitle = apply_filters( 'usces_filter_title_changepassword', __( 'Change password', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
594 break;
595
596 case 'changepasscompletion':
597 $newtitle = apply_filters( 'usces_filter_title_changepasscompletion', __( 'Password change is completed', 'usces' ) ) . ' ' . $sep . ' ' . get_bloginfo( 'name' );
598 break;
599
600 default:
601 $newtitle = $title;
602 }
603 return $newtitle;
604 }
605
606 /**
607 * Separator
608 * document_title_separator
609 *
610 * @param string $sep Separator.
611 * @return string
612 */
613 function usces_document_title_separator( $sep ) {
614 $sep = '|';
615 return $sep;
616 }
617
618 /**
619 * Univarsal Analytics( Dashboard )
620 *
621 * @return array
622 */
623 function usces_Universal_trackPageview() {
624 global $usces;
625
626 $push = array();
627
628 switch ( $usces->page ) {
629 case 'cart':
630 $push[] = "'page' : '/wc_cart'";
631 break;
632
633 case 'customer':
634 $push[] = "'page' : '/wc_customer'";
635 break;
636
637 case 'delivery':
638 $push[] = "'page' : '/wc_delivery'";
639 break;
640
641 case 'confirm':
642 $push[] = "'page' : '/wc_confirm'";
643 break;
644
645 case 'ordercompletion':
646 $push[] = "'page' : '/wc_ordercompletion'";
647 $sesdata = $usces->cart->get_entry();
648 if ( isset( $sesdata['order']['ID'] ) && ! empty( $sesdata['order']['ID'] ) ) {
649 $order_id = $sesdata['order']['ID'];
650 $data = $usces->get_order_data( $order_id, 'direct' );
651 $cart = wel_safe_unserialize( $data['order_cart'] );
652 $total_price = $usces->get_total_price( $cart ) + $data['order_discount'] - $data['order_usedpoint'];
653 if ( $total_price < 0 ) {
654 $total_price = 0;
655 }
656
657 $push[] = "'require', 'ecommerce', 'ecommerce.js'";
658 $push[] = "'ecommerce:addTransaction', {
659 id: '" . $order_id . "',
660 affiliation: '" . esc_js( get_option( 'blogname' ) ) . "',
661 revenue: '" . $total_price . "',
662 shipping: '" . $data['order_shipping_charge'] . "',
663 tax: '" . $data['order_tax'] . "' }";
664 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
665 for ( $i = 0; $i < $cart_count; $i++ ) {
666 $cart_row = $cart[ $i ];
667 $post_id = $cart_row['post_id'];
668 $sku = urldecode( $cart_row['sku'] );
669 $quantity = $cart_row['quantity'];
670 $itemname = $usces->getItemName( $post_id );
671 $skuprice = $cart_row['price'];
672 $cats = $usces->get_item_cat_genre_ids( $post_id );
673 if ( is_array( $cats ) ) {
674 sort( $cats );
675 }
676 $category = ( isset( $cats[0] ) ) ? get_cat_name( $cats[0] ) : '';
677
678 $push[] = "'ecommerce:addItem', {
679 id: '" . $order_id . "',
680 sku: '" . esc_js( $sku ) . "',
681 name: '" . esc_js( $itemname ) . "',
682 category: '" . esc_js( $category ) . "',
683 price: '" . $skuprice . "',
684 quantity: '" . $quantity . "' }";
685 }
686 $push[] = "'ecommerce:send'";
687 }
688 break;
689
690 case 'error':
691 $push[] = "'page' : '/wc_error'";
692 break;
693
694 case 'search_item':
695 $push[] = "'page' : '/wc_search_item'";
696 break;
697
698 case 'maintenance':
699 $push[] = "'page' : '/wc_maintenance'";
700 break;
701
702 case 'login':
703 $push[] = "'page' : '/wc_login'";
704 break;
705
706 case 'member':
707 $push[] = "'page' : '/wc_member'";
708 break;
709
710 case 'newmemberform':
711 $push[] = "'page' : '/wc_newmemberform'";
712 break;
713
714 case 'newcompletion':
715 $push[] = "'page' : '/wc_newcompletion'";
716 break;
717
718 case 'editmemberform':
719 $push[] = "'page' : '/wc_editmemberform'";
720 break;
721
722 case 'editcompletion':
723 $push[] = "'page' : '/wc_editcompletion'";
724 break;
725
726 case 'lostmemberpassword':
727 $push[] = "'page' : '/wc_lostmemberpassword'";
728 break;
729
730 case 'lostcompletion':
731 $push[] = "'page' : '/wc_lostcompletion'";
732 break;
733
734 case 'changepassword':
735 $push[] = "'page' : '/wc_changepassword'";
736 break;
737
738 case 'changepasscompletion':
739 $push[] = "'page' : '/wc_changepasscompletion'";
740 break;
741
742 default:
743 break;
744 }
745 return $push;
746 }
747
748 /**
749 * Classic Analytics ( Dashboard )
750 *
751 * @return array
752 */
753 function usces_Classic_trackPageview() {
754 global $usces;
755
756 $push = array();
757
758 switch ( $usces->page ) {
759 case 'cart':
760 $push = usces_trackPageview_cart( $push );
761 break;
762
763 case 'customer':
764 $push = usces_trackPageview_customer( $push );
765 break;
766
767 case 'delivery':
768 $push = usces_trackPageview_delivery( $push );
769 break;
770
771 case 'confirm':
772 $push = usces_trackPageview_confirm( $push );
773 break;
774
775 case 'ordercompletion':
776 $push = usces_trackPageview_ordercompletion( $push );
777 break;
778
779 case 'error':
780 $push = usces_trackPageview_error( $push );
781 break;
782
783 case 'login':
784 $push = usces_trackPageview_login( $push );
785 break;
786
787 case 'member':
788 $push = usces_trackPageview_member( $push );
789 break;
790
791 case 'newmemberform':
792 $push = usces_trackPageview_newmemberform( $push );
793 break;
794
795 case 'newcompletion':
796 $push = usces_trackPageview_newcompletion( $push );
797 break;
798
799 case 'editmemberform':
800 $push = usces_trackPageview_editmemberform( $push );
801 break;
802
803 case 'search_item':
804 $push = usces_trackPageview_search_item( $push );
805 break;
806
807 case 'maintenance':
808 case 'editcompletion':
809 case 'lostmemberpassword':
810 case 'lostcompletion':
811 case 'changepassword':
812 case 'changepasscompletion':
813 default:
814 break;
815 }
816 return $push;
817 }
818
819 /**
820 * Univarsal Analytics( Yoast )
821 * monsterinsights_frontend_tracking_options_analytics_end
822 * yoast-ga-push-array-universal
823 *
824 * @param array $push Push array.
825 * @return array
826 */
827 function usces_Universal_trackPageview_by_Yoast( $push ) {
828 global $usces;
829
830 foreach ( $push as $p_key => $p_val ) {
831 $pos1 = strpos( (string) $p_val, "'send'" );
832 $pos2 = strpos( (string) $p_val, "'pageview'" );
833 if ( false !== $pos1 && false !== $pos2 ) {
834 unset( $push[ $p_key ] );
835 }
836 }
837
838 switch ( $usces->page ) {
839 case 'cart':
840 $push[] = "'send', 'pageview', {'page' : '/wc_cart'}";
841 break;
842
843 case 'customer':
844 $push[] = "'send', 'pageview', {'page' : '/wc_customer'}";
845 break;
846
847 case 'delivery':
848 $push[] = "'send', 'pageview', {'page' : '/wc_delivery'}";
849 break;
850
851 case 'confirm':
852 $push[] = "'send', 'pageview', {'page' : '/wc_confirm'}";
853 break;
854
855 case 'ordercompletion':
856 $push[] = "'send', 'pageview', {'page' : '/wc_ordercompletion'}";
857 $sesdata = $usces->cart->get_entry();
858 if ( isset( $sesdata['order']['ID'] ) && ! empty( $sesdata['order']['ID'] ) ) {
859 $order_id = $sesdata['order']['ID'];
860 $data = $usces->get_order_data( $order_id, 'direct' );
861 $cart = wel_safe_unserialize( $data['order_cart'] );
862 $total_price = $usces->get_total_price( $cart ) + $data['order_discount'] - $data['order_usedpoint'];
863 if ( $total_price < 0 ) {
864 $total_price = 0;
865 }
866
867 $push[] = "'require', 'ecommerce', 'ecommerce.js'";
868 $push[] = "'ecommerce:addTransaction', {
869 id: '" . $order_id . "',
870 affiliation: '" . esc_js( get_option( 'blogname' ) ) . "',
871 revenue: '" . $total_price . "',
872 shipping: '" . $data['order_shipping_charge'] . "',
873 tax: '" . $data['order_tax'] . "'
874 }";
875 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
876 for ( $i = 0; $i < $cart_count; $i++ ) {
877 $cart_row = $cart[ $i ];
878 $post_id = $cart_row['post_id'];
879 $sku = urldecode( $cart_row['sku'] );
880 $quantity = $cart_row['quantity'];
881 $itemname = $usces->getItemName( $post_id );
882 $skuprice = $cart_row['price'];
883 $cats = $usces->get_item_cat_genre_ids( $post_id );
884 if ( is_array( $cats ) ) {
885 sort( $cats );
886 }
887 $category = ( isset( $cats[0] ) ) ? get_cat_name( $cats[0] ) : '';
888
889 $push[] = "'ecommerce:addItem', {
890 id: '" . $order_id . "',
891 sku: '" . esc_js( $sku ) . "',
892 name: '" . esc_js( $itemname ) . "',
893 category: '" . esc_js( $category ) . "',
894 price: '" . $skuprice . "',
895 quantity: '" . $quantity . "'
896 }";
897 }
898 $push[] = "'ecommerce:send'";
899 }
900 break;
901
902 case 'error':
903 $push[] = "'send', 'pageview', {'page' : '/wc_error'}";
904 break;
905
906 case 'search_item':
907 $push[] = "'send', 'pageview', {'page' : '/wc_search_item'}";
908 break;
909
910 case 'maintenance':
911 $push[] = "'send', 'pageview', {'page' : '/wc_maintenance'}";
912 break;
913
914 case 'login':
915 $push[] = "'send', 'pageview', {'page' : '/wc_login'}";
916 break;
917
918 case 'member':
919 $push[] = "'send', 'pageview', {'page' : '/wc_member'}";
920 break;
921
922 case 'newmemberform':
923 $push[] = "'send', 'pageview', {'page' : '/wc_newmemberform'}";
924 break;
925
926 case 'newcompletion':
927 $push[] = "'send', 'pageview', {'page' : '/wc_newcompletion'}";
928 break;
929
930 case 'editmemberform':
931 $push[] = "'send', 'pageview', {'page' : '/wc_editmemberform'}";
932 break;
933
934 case 'editcompletion':
935 $push[] = "'send', 'pageview', {'page' : '/wc_editcompletion'}";
936 break;
937
938 case 'lostmemberpassword':
939 $push[] = "'send', 'pageview', {'page' : '/wc_lostmemberpassword'}";
940 break;
941
942 case 'lostcompletion':
943 $push[] = "'send', 'pageview', {'page' : '/wc_lostcompletion'}";
944 break;
945
946 case 'changepassword':
947 $push[] = "'send', 'pageview', {'page' : '/wc_changepassword'}";
948 break;
949
950 case 'changepasscompletion':
951 $push[] = "'send', 'pageview', {'page' : '/wc_changepasscompletion'}";
952 break;
953
954 default:
955 $push[] = "'send', 'pageview'";
956 break;
957 }
958 return $push;
959 }
960
961 /**
962 * Order completion tracking.
963 * wp_enqueue_scripts
964 * monsterinsights_frontend_tracking_gtag_after_pageview
965 */
966 function usces_ecommerce_reporting() {
967 global $usces;
968
969 if ( 'ordercompletion' !== $usces->page ) {
970 return;
971 }
972
973 $sesdata = $usces->cart->get_entry();
974 if ( isset( $sesdata['order']['ID'] ) && ! empty( $sesdata['order']['ID'] ) ) {
975 $order_id = $sesdata['order']['ID'];
976 $data = $usces->get_order_data( $order_id, 'direct' );
977 $cart = wel_safe_unserialize( $data['order_cart'] );
978 $total_price = $usces->get_total_price( $cart ) + $data['order_discount'] - $data['order_usedpoint'];
979 if ( $total_price < 0 ) {
980 $total_price = 0;
981 }
982 $ecommerce_reporting = "__gtagTracker('event', 'purchase', {
983 transaction_id: '" . $order_id . "',
984 affiliation: '" . esc_js( get_option( 'blogname' ) ) . "',
985 value: '" . $total_price . "',
986 currency: '" . $usces->get_currency_code() . "',
987 tax: '" . $data['order_tax'] . "',
988 shipping: '" . $data['order_shipping_charge'] . "',
989 items: [";
990
991 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
992 for ( $i = 0; $i < $cart_count; $i++ ) {
993 $cart_row = $cart[ $i ];
994 $post_id = $cart_row['post_id'];
995 $sku = urldecode( $cart_row['sku'] );
996 $quantity = $cart_row['quantity'];
997 $itemname = $usces->getItemName( $post_id );
998 $skuprice = $cart_row['price'];
999 $cats = $usces->get_item_cat_genre_ids( $post_id );
1000 if ( is_array( $cats ) ) {
1001 sort( $cats );
1002 }
1003 $category = ( isset( $cats[0] ) ) ? get_cat_name( $cats[0] ) : '';
1004
1005 $ecommerce_reporting .= "{
1006 id: '" . $post_id . "',
1007 sku: '" . esc_js( $sku ) . "',
1008 name: '" . esc_js( $itemname ) . "',
1009 category: '" . esc_js( $category ) . "',
1010 price: '" . $skuprice . "',
1011 quantity: '" . $quantity . "'
1012 },";
1013 }
1014 $ecommerce_reporting .= "]});";
1015
1016 $monsterinsights_current_version = usces_MonsterInsights_get_version();
1017 if ( version_compare( $monsterinsights_current_version, '8.0.0', '>=' ) ) {
1018 wp_register_script( 'after-monsterinsights-frontend-script', '', array( 'monsterinsights-frontend-script' ) );
1019 wp_enqueue_script( 'after-monsterinsights-frontend-script' );
1020 wp_add_inline_script( 'after-monsterinsights-frontend-script', $ecommerce_reporting );
1021 } else {
1022 wel_esc_script_e( $ecommerce_reporting );
1023 }
1024 }
1025 }
1026
1027 /**
1028 * Universal trackPageview by monsterInsight.
1029 * monsterinsights_frontend_tracking_options_gtag_end
1030 *
1031 * @param array $push Push array.
1032 * @return array
1033 */
1034 function usces_Universal_trackPageview_by_monsterInsight( $push ) {
1035 global $usces;
1036
1037 $monsterinsights_current_version = usces_MonsterInsights_get_version();
1038 if ( version_compare( $monsterinsights_current_version, '8.0.0', '>=' ) ) {
1039 switch ( $usces->page ) {
1040 case 'cart':
1041 $push['page_path'] = 'wc_cart';
1042 break;
1043
1044 case 'customer':
1045 $push['page_path'] = 'wc_customer';
1046 break;
1047
1048 case 'delivery':
1049 $push['page_path'] = 'wc_delivery';
1050 break;
1051
1052 case 'confirm':
1053 $push['page_path'] = 'wc_confirm';
1054 break;
1055
1056 case 'ordercompletion':
1057 $push['page_path'] = 'wc_ordercompletion';
1058 break;
1059
1060 case 'error':
1061 $push['page_path'] = 'wc_error';
1062 break;
1063
1064 case 'search_item':
1065 $push['page_path'] = 'wc_search_item';
1066 break;
1067
1068 case 'maintenance':
1069 $push['page_path'] = 'wc_maintenance';
1070 break;
1071
1072 case 'login':
1073 $push['page_path'] = 'wc_login';
1074 break;
1075
1076 case 'member':
1077 $push['page_path'] = 'wc_member';
1078 break;
1079
1080 case 'newmemberform':
1081 $push['page_path'] = 'wc_newmemberform';
1082 break;
1083
1084 case 'newcompletion':
1085 $push['page_path'] = 'wc_newcompletion';
1086 break;
1087
1088 case 'editmemberform':
1089 $push['page_path'] = 'wc_editmemberform';
1090 break;
1091
1092 case 'editcompletion':
1093 $push['page_path'] = 'wc_editcompletion';
1094 break;
1095
1096 case 'lostmemberpassword':
1097 $push['page_path'] = 'wc_lostmemberpassword';
1098 break;
1099
1100 case 'lostcompletion':
1101 $push['page_path'] = 'wc_lostcompletion';
1102 break;
1103
1104 case 'changepassword':
1105 $push['page_path'] = 'wc_changepassword';
1106 break;
1107
1108 case 'changepasscompletion':
1109 $push['page_path'] = 'wc_changepasscompletion';
1110 break;
1111
1112 default: // keep origin page path.
1113 break;
1114 }
1115 } else {
1116 switch ( $usces->page ) {
1117 case 'cart':
1118 $push['page_path'] = "'/wc_cart'";
1119 break;
1120
1121 case 'customer':
1122 $push['page_path'] = "'/wc_customer'";
1123 break;
1124
1125 case 'delivery':
1126 $push['page_path'] = "'/wc_delivery'";
1127 break;
1128
1129 case 'confirm':
1130 $push['page_path'] = "'/wc_confirm'";
1131 break;
1132
1133 case 'ordercompletion':
1134 $push['page_path'] = "'/wc_ordercompletion'";
1135 break;
1136
1137 case 'error':
1138 $push['page_path'] = "'/wc_error'";
1139 break;
1140
1141 case 'search_item':
1142 $push['page_path'] = "'/wc_search_item'";
1143 break;
1144
1145 case 'maintenance':
1146 $push['page_path'] = "'/wc_maintenance'";
1147 break;
1148
1149 case 'login':
1150 $push['page_path'] = "'/wc_login'";
1151 break;
1152
1153 case 'member':
1154 $push['page_path'] = "'/wc_member'";
1155 break;
1156
1157 case 'newmemberform':
1158 $push['page_path'] = "'/wc_newmemberform'";
1159 break;
1160
1161 case 'newcompletion':
1162 $push['page_path'] = "'/wc_newcompletion'";
1163 break;
1164
1165 case 'editmemberform':
1166 $push['page_path'] = "'/wc_editmemberform'";
1167 break;
1168
1169 case 'editcompletion':
1170 $push['page_path'] = "'/wc_editcompletion'";
1171 break;
1172
1173 case 'lostmemberpassword':
1174 $push['page_path'] = "'/wc_lostmemberpassword'";
1175 break;
1176
1177 case 'lostcompletion':
1178 $push['page_path'] = "'/wc_lostcompletion'";
1179 break;
1180
1181 case 'changepassword':
1182 $push['page_path'] = "'/wc_changepassword'";
1183 break;
1184
1185 case 'changepasscompletion':
1186 $push['page_path'] = "'/wc_changepasscompletion'";
1187 break;
1188
1189 default: // keep origin page path.
1190 break;
1191 }
1192 }
1193 return $push;
1194 }
1195
1196 /**
1197 * Classic Analytics ( Yoast )
1198 * yoast-ga-push-array-ga-js
1199 *
1200 * @param array $push Push array.
1201 * @return array
1202 */
1203 function usces_Classic_trackPageview_by_Yoast( $push ) {
1204 global $usces;
1205
1206 foreach ( $push as $p_key => $p_val ) {
1207 $pos1 = strpos( (string) $p_val, "'_trackPageview" );
1208 if ( false !== $pos1 ) {
1209 unset( $push[ $p_key ] );
1210 }
1211 }
1212 switch ( $usces->page ) {
1213 case 'cart':
1214 $push[] = "'_trackPageview', '/wc_cart'";
1215 break;
1216
1217 case 'customer':
1218 $push[] = "'_trackPageview', '/wc_customer'";
1219 break;
1220
1221 case 'delivery':
1222 $push[] = "'_trackPageview', '/wc_delivery'";
1223 break;
1224
1225 case 'confirm':
1226 $push[] = "'_trackPageview', '/wc_confirm'";
1227 break;
1228
1229 case 'ordercompletion':
1230 global $usces;
1231
1232 $push[] = "'_trackPageview','/wc_ordercompletion'";
1233 $sesdata = $usces->cart->get_entry();
1234 if ( isset( $sesdata['order']['ID'] ) && ! empty( $sesdata['order']['ID'] ) ) {
1235 $order_id = $sesdata['order']['ID'];
1236 $data = $usces->get_order_data( $order_id, 'direct' );
1237 $cart = wel_safe_unserialize( $data['order_cart'] );
1238 $total_price = $usces->get_total_price( $cart ) + $data['order_discount'] - $data['order_usedpoint'];
1239 if ( $total_price < 0 ) {
1240 $total_price = 0;
1241 }
1242
1243 $push[] = "'_addTrans', '" . $order_id . "', '" . esc_js( get_option( 'blogname' ) ) . "', '" . $total_price . "', '" . $data['order_tax'] . "', '" . $data['order_shipping_charge'] . "', '" . esc_js( $data['order_address1'] . $data['order_address2'] ) . "', '" . esc_js( $data['order_pref'] ) . "', '" . get_locale() . "'";
1244 $cart_count = ( $cart && is_array( $cart ) ) ? count( $cart ) : 0;
1245 for ( $i = 0; $i < $cart_count; $i++ ) {
1246 $cart_row = $cart[ $i ];
1247 $post_id = $cart_row['post_id'];
1248 $sku = urldecode( $cart_row['sku'] );
1249 $quantity = $cart_row['quantity'];
1250 $itemname = $usces->getItemName( $post_id );
1251 $skuprice = $cart_row['price'];
1252 $cats = $usces->get_item_cat_genre_ids( $post_id );
1253 if ( is_array( $cats ) ) {
1254 sort( $cats );
1255 }
1256 $category = ( isset( $cats[0] ) ) ? get_cat_name( $cats[0] ) : '';
1257 $push[] = "'_addItem', '" . $order_id . "', '" . esc_js( $sku ) . "', '" . esc_js( $itemname ) . "', '" . esc_js( $category ) . "', '" . $skuprice . "', '" . $quantity . "'";
1258 }
1259 $push[] = "'_trackTrans'";
1260 }
1261 break;
1262
1263 case 'error':
1264 $push[] = "'_trackPageview', '/wc_error'";
1265 break;
1266
1267 case 'login':
1268 $push[] = "'_trackPageview', '/wc_login'";
1269 break;
1270
1271 case 'member':
1272 $push[] = "'_trackPageview', '/wc_member'";
1273 break;
1274
1275 case 'newmemberform':
1276 $push[] = "'_trackPageview', '/wc_newmemberform'";
1277 break;
1278
1279 case 'newcompletion':
1280 $push[] = "'_trackPageview', '/wc_newcompletion'";
1281 break;
1282
1283 case 'editmemberform':
1284 $push[] = "'_trackPageview', '/wc_editmemberform'";
1285 break;
1286
1287 case 'search_item':
1288 $push[] = "'_trackPageview', '/wc_search_item'";
1289 break;
1290
1291 case 'maintenance':
1292 $push[] = "'_trackPageview', '/wc_maintenance'";
1293 break;
1294
1295 case 'editcompletion':
1296 $push[] = "'_trackPageview', '/wc_editcompletion'";
1297 break;
1298
1299 case 'lostmemberpassword':
1300 $push[] = "'_trackPageview', '/wc_lostmemberpassword'";
1301 break;
1302
1303 case 'lostcompletion':
1304 $push[] = "'_trackPageview', '/wc_lostcompletion'";
1305 break;
1306
1307 case 'changepassword':
1308 $push[] = "'_trackPageview', '/wc_changepassword'";
1309 break;
1310
1311 case 'changepasscompletion':
1312 $push[] = "'_trackPageview', '/wc_changepasscompletion'";
1313 break;
1314
1315 default:
1316 $push[] = "'_trackPageview'";
1317 break;
1318 }
1319 return $push;
1320 }
1321
1322 /**
1323 * Using MonsterInsights.
1324 * plugins_loaded
1325 */
1326 function usces_monsterinsights() {
1327 $monsterinsights_current_version = usces_MonsterInsights_get_version();
1328 if ( null !== $monsterinsights_current_version && version_compare( $monsterinsights_current_version, '6.0.11', '>' ) ) {
1329 add_filter( 'monsterinsights_frontend_tracking_options_analytics_end', 'usces_Universal_trackPageview_by_Yoast' );
1330 add_filter( 'monsterinsights_frontend_tracking_options_gtag_end', 'usces_Universal_trackPageview_by_monsterInsight' );
1331 if ( version_compare( $monsterinsights_current_version, '8.0.0', '>=' ) ) {
1332 add_action( 'wp_enqueue_scripts', 'usces_ecommerce_reporting', 10 );
1333 } else {
1334 add_action( 'monsterinsights_frontend_tracking_gtag_after_pageview', 'usces_ecommerce_reporting' );
1335 }
1336 } else {
1337 add_filter( 'yoast-ga-push-array-universal', 'usces_Universal_trackPageview_by_Yoast' );
1338 add_filter( 'yoast-ga-push-array-ga-js', 'usces_Classic_trackPageview_by_Yoast' );
1339 }
1340 }
1341
1342 /**
1343 * Get version of MonsterInsights.
1344 *
1345 * @return string
1346 */
1347 function usces_monsterinsights_get_version() {
1348 if ( ! function_exists( 'MonsterInsights_Pro' ) && ! function_exists( 'MonsterInsights_Lite' ) ) {
1349 return;
1350 }
1351 $instance = class_exists( 'MonsterInsights' ) ? MonsterInsights_Pro() : MonsterInsights_Lite();
1352 return $instance->version;
1353 }
1354
1355 /**
1356 * Order memo area.
1357 * usces_action_order_edit_form_detail_top
1358 *
1359 * @param array $data Order data.
1360 * @param array $csod_meta Custom order data.
1361 */
1362 function usces_order_memo_form_detail_top( $data, $csod_meta ) {
1363 global $usces;
1364
1365 $order_memo = '';
1366 if ( ! empty( $data['ID'] ) ) {
1367 $order_memo = $usces->get_order_meta_value( 'order_memo', $data['ID'] );
1368 }
1369 $res = '<tr>
1370 <td class="label border">' . __( 'Administrator Note', 'usces' ) . '</td>
1371 <td colspan="5" class="col1 border memo">
1372 <textarea name="order_memo" class="order_memo">' . esc_html( $order_memo ) . '</textarea>
1373 </td>
1374 </tr>';
1375 wel_esc_script_e( $res );
1376 }
1377
1378 /**
1379 * Update order memo.
1380 * usces_action_update_orderdata
1381 *
1382 * @param object $new_orderdata New order data.
1383 */
1384 function usces_update_order_memo( $new_orderdata ) {
1385 global $usces;
1386
1387 if ( isset( $_POST['order_memo'] ) ) {
1388 $usces->set_order_meta_value( 'order_memo', $_POST['order_memo'], $new_orderdata->ID );
1389 }
1390 }
1391
1392 /**
1393 * Save order memo.
1394 * usces_action_reg_orderdata
1395 *
1396 * @param array $args {
1397 * The array of order-related data.
1398 * @type array $cart Cart data.
1399 * @type array $entry Entry data.
1400 * @type int $order_id Order ID.
1401 * @type int $member_id Member ID.
1402 * @type array $payments Payment Method.
1403 * @type array $charging_type Charge type.
1404 * @type array $results Settlement result data.
1405 * }
1406 */
1407 function usces_register_order_memo( $args ) {
1408 global $usces;
1409 extract( $args );
1410
1411 if ( isset( $_POST['order_memo'] ) && $order_id ) {
1412 $usces->set_order_meta_value( 'order_memo', $_POST['order_memo'], $order_id );
1413 }
1414 }
1415
1416 /**
1417 * Tracking number field area.
1418 * usces_action_order_edit_form_delivery_block
1419 *
1420 * @param array $data Order data.
1421 * @param array $cscs_meta Custom customer data.
1422 * @param array $action_args Order action.
1423 * @return void
1424 */
1425 function usces_add_tracking_number_field( $data, $cscs_meta, $action_args ) {
1426 global $usces;
1427 $locale = get_locale();
1428
1429 $deli_comps = array( 'クロネコヤマト', 'ヤマト運輸', '佐川急便', '日本通運', 'ゆうパック', '日本郵便', '郵便書留', '西濃運輸', '福山通運', '名鉄運輸', '新潟運輸', 'トナミ運輸', '第一貨物', '飛騨倉庫運輸', '西武運輸', 'クリックポスト', 'トールエクスプレス', 'セイノーエクスプレス', '信州名鉄運輸', '大川�
1430 �送サービス', 'その他' );
1431 $deli_comps = apply_filters( 'usces_filter_deli_comps', $deli_comps, $data );
1432
1433 $tracking_number = '';
1434 if ( ! empty( $data['ID'] ) ) {
1435 $tracking_number = $usces->get_order_meta_value( apply_filters( 'usces_filter_tracking_meta_key', 'tracking_number' ), $data['ID'] );
1436 $delivery_company = $usces->get_order_meta_value( 'delivery_company', $data['ID'] );
1437 } else {
1438 $tracking_number = '';
1439 $delivery_company = '';
1440 }
1441 $res = '';
1442 $res .= '
1443 <tr>
1444 <td class="label">' . __( 'Delivery company', 'usces' ) . '</td>
1445 <td class="col1">';
1446 if ( 'ja' === $locale ) {
1447 $res .= ' <select name="delivery_company" style="width:100%;" >' . "\n";
1448 $res .= '<option value="">' . __( '-- Select --', 'usces' ) . '</option>' . "\n";
1449 foreach ( $deli_comps as $comp ) {
1450 $res .= '<option value="' . esc_attr( $comp ) . '"' . selected( $comp, $delivery_company, false ) . '>' . esc_html( $comp ) . '</option>' . "\n";
1451 }
1452 $res .= ' </select>' . "\n";
1453 } else {
1454 $res .= ' <input name="delivery_company" type="text" style="width:100%;" value="' . esc_attr( $delivery_company ) . '">';
1455 }
1456 $res .= ' </td>
1457 </tr>';
1458 $res .= '<tr>
1459 <td class="label">' . __( 'Tracking number', 'usces' ) . '</td>
1460 <td class="col1">
1461 <input name="tracking_number" type="text" style="width:100%;" value="' . esc_attr( $tracking_number ) . '">
1462 </td>
1463 </tr>' . "\n";
1464 wel_esc_script_e( $res );
1465 }
1466
1467 /**
1468 * Update tracking number.
1469 * usces_action_update_orderdata
1470 *
1471 * @param object $new_orderdata New order data.
1472 */
1473 function usces_update_tracking_number( $new_orderdata ) {
1474 global $usces;
1475
1476 if ( isset( $_POST['tracking_number'] ) ) {
1477 $usces->set_order_meta_value( apply_filters( 'usces_filter_tracking_meta_key', 'tracking_number' ), wp_unslash( $_POST['tracking_number'] ), $new_orderdata->ID );
1478 }
1479
1480 if ( isset( $_POST['delivery_company'] ) ) {
1481 $usces->set_order_meta_value( 'delivery_company', wp_unslash( $_POST['delivery_company'] ), $new_orderdata->ID );
1482 }
1483 }
1484
1485 /**
1486 * Admin scripts.
1487 * admin_enqueue_scripts
1488 *
1489 * @param string $hook_suffix The current admin page.
1490 */
1491 function usces_admin_enqueue_scripts( $hook_suffix ) {
1492 global $wp_scripts;
1493 if ( false !== strpos( $hook_suffix, 'usc-e-shop' )
1494 || false !== strpos( $hook_suffix, 'welcart' )
1495 || false !== strpos( $hook_suffix, 'usces' )
1496 ) {
1497 $ui = $wp_scripts->query( 'jquery-ui-core' );
1498 $ui_themes = apply_filters( 'usces_filter_jquery_ui_themes_welcart', 'smoothness' );
1499 wp_enqueue_style( 'jquery-ui-welcart', "//code.jquery.com/ui/{$ui->ver}/themes/{$ui_themes}/jquery-ui.css" );
1500 }
1501 if ( 'welcart-management_page_usces_memberlist' == $hook_suffix
1502 || 'toplevel_page_usces_orderlist' == $hook_suffix
1503 ) {
1504 $path = USCES_FRONT_PLUGIN_URL . '/js/jquery/jquery-cookie.js';
1505 wp_enqueue_script( 'usces_member_cookie', $path, array( 'jquery' ), USCES_VERSION, true );
1506 }
1507 }
1508
1509 /**
1510 * Cron schedules.
1511 * cron_schedules
1512 *
1513 * @param array $schedules An array of non-default cron schedule arrays.
1514 * @return array
1515 */
1516 function usces_schedules_intervals( $schedules ) {
1517 $schedules['weekly'] = array(
1518 'interval' => 604800,
1519 'display' => 'Weekly',
1520 );
1521 return $schedules;
1522 }
1523
1524 /**
1525 * Respond to the Welcart endpoint server's site survey.
1526 *
1527 * The endpoint server periodically polls every Welcart site to refresh its site
1528 * ledger. Authentication is the fixed endpoint address plus the per-site shared
1529 * secret `usces_wcid`, and the response carries only the four values the
1530 * endpoint consumes. The raw `usces` option must never be exposed - it holds
1531 * every payment gateway credential, the bank transfer account, mail bodies and
1532 * the log output paths.
1533 *
1534 * On any mismatch this returns instead of dying, so the request falls through to
1535 * the normal page render and the endpoint's own "is Welcart active" fallback
1536 * keeps working.
1537 *
1538 * plugins_loaded
1539 */
1540 function usces_responce_wcsite() {
1541 // No nonce here by design: this is a server-to-server channel with no
1542 // browser session to bind a nonce to. The shared secret below is the check.
1543 if ( ! isset( $_POST['sname'], $_POST['wcid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1544 return;
1545 }
1546
1547 // Compared verbatim against a literal address, so no sanitization applies.
1548 $remote_addr = ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? wp_unslash( $_SERVER['REMOTE_ADDR'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1549 if ( USCES_WCSITE_ENDPOINT_IP !== $remote_addr ) {
1550 return;
1551 }
1552
1553 // is_string() guards against `wcid[]=x`, which would make hash_equals() throw.
1554 $my_wcid = (string) get_option( 'usces_wcid' );
1555 $req_wcid = is_string( $_POST['wcid'] ) ? sanitize_text_field( wp_unslash( $_POST['wcid'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
1556 if ( '' === $my_wcid || ! hash_equals( $my_wcid, $req_wcid ) ) {
1557 return;
1558 }
1559
1560 $options = get_option( 'usces', array() );
1561 $options = is_array( $options ) ? $options : array();
1562 $system = ( isset( $options['system'] ) && is_array( $options['system'] ) ) ? $options['system'] : array();
1563
1564 $data = array(
1565 'usces' => array(
1566 'company_name' => isset( $options['company_name'] ) ? (string) $options['company_name'] : '',
1567 'inquiry_mail' => isset( $options['inquiry_mail'] ) ? (string) $options['inquiry_mail'] : '',
1568 // Kept nested because the endpoint reads `usces.system.base_country`.
1569 // The key is always emitted: an empty PHP array encodes to `[]`, not
1570 // `{}`, which would break that property access on the endpoint.
1571 'system' => array(
1572 'base_country' => isset( $system['base_country'] ) ? (string) $system['base_country'] : '',
1573 ),
1574 ),
1575 'usces_settlement_selected' => get_option( 'usces_settlement_selected', array() ),
1576 );
1577
1578 header( 'Content-Type: application/json; charset=utf-8' );
1579 echo wp_json_encode( $data );
1580 exit;
1581 }
1582
1583 /**
1584 * Welcart activate.
1585 */
1586 function usces_wcsite_activate() {
1587 $usces = get_option( 'usces', array() );
1588 $circulating = wel_get_circulating_amount();
1589 $sku_num = wel_get_sku_total_num();
1590 $cat_num = wel_get_cat_total_num();
1591
1592 $metas['usces_company_name'] = $usces['company_name'];
1593 $metas['usces_company_tel'] = $usces['tel_number'];
1594 $metas['usces_company_number'] = $usces['business_registration_number'];
1595 $metas['usces_company_zip'] = $usces['zip_code'];
1596 $metas['usces_company_location'] = $usces['address1'] . ' ' . $usces['address2'];
1597 $metas['usces_inquiry_mail'] = $usces['inquiry_mail'];
1598 $metas['usces_base_country'] = $usces['system']['base_country'];
1599 $metas['usces_circulating_amount'] = $circulating['amount'];
1600 $metas['usces_circulating_count'] = $circulating['count'];
1601 $metas['usces_categories'] = wel_get_categories();
1602 $metas['usces_cat_num'] = $cat_num;
1603 $metas['usces_sku_num'] = $sku_num;
1604 $metas['usces_themes'] = wel_get_themes();
1605 $metas['usces_plugins'] = wel_get_plugins();
1606 $metas['usces_used_sett'] = wel_get_activ_pay_methd();
1607
1608 $base_metas = array(
1609 'wcid' => get_option( 'usces_wcid' ),
1610 'wchost' => $_SERVER['SERVER_NAME'],
1611 'refer' => get_option( 'home' ),
1612 'act' => 1,
1613 );
1614 $params = array_merge( $base_metas, $metas );
1615 usces_wcsite_connection( $params );
1616 }
1617
1618 /**
1619 * Welcart deactivate.
1620 */
1621 function usces_wcsite_deactivate() {
1622 $params = array(
1623 'wcid' => get_option( 'usces_wcid' ),
1624 'wchost' => $_SERVER['SERVER_NAME'],
1625 'refer' => get_option( 'home' ),
1626 'act' => 0,
1627 );
1628 usces_wcsite_connection( $params );
1629 }
1630
1631 /**
1632 * Session cache limiter.
1633 * usces_action_session_start
1634 */
1635 function usces_session_cache_limiter() {
1636 global $usces;
1637
1638 if ( $usces->is_cart_page( $_SERVER['REQUEST_URI'] ) && isset( $_REQUEST['usces_page'] ) && 'search_item' == $_REQUEST['usces_page'] ) {
1639 session_cache_limiter( 'private_no_expire' );
1640 }
1641 }
1642
1643 /**
1644 * Disused function
1645 * usces_action_login_page_footer
1646 */
1647 function usces_action_login_page_liwpp() {
1648 }
1649
1650 /**
1651 * Disused function
1652 * usces_filter_login_page_footer
1653 *
1654 * @param string $html Template form.
1655 * @return string
1656 */
1657 function usces_filter_login_page_liwpp( $html ) {
1658 return $html;
1659 }
1660
1661 /**
1662 * Disused function
1663 * usces_action_customer_page_member_inform
1664 */
1665 function usces_action_customer_page_liwpp() {
1666 }
1667
1668 /**
1669 * Disused function
1670 * usces_filter_customer_page_member_inform
1671 *
1672 * @param string $html Template form.
1673 * @return string
1674 */
1675 function usces_filter_customer_page_liwpp( $html ) {
1676 return $html;
1677 }
1678
1679 /**
1680 * Disused function
1681 * usces_filter_login_widget
1682 *
1683 * @param string $html Template form.
1684 * @return string
1685 */
1686 function usces_filter_login_widget_liwpp( $html ) {
1687 return $html;
1688 }
1689
1690 /**
1691 * Disused function
1692 * wp
1693 */
1694 function usces_login_width_paypal() {
1695 }
1696
1697 /**
1698 * Atobarai each availability area.
1699 *
1700 * @param string $second_section Second section area.
1701 * @param int $post_id Post ID.
1702 * @return string
1703 */
1704 function usces_atobarai_each_availability( $second_section, $post_id ) {
1705
1706 $product = wel_get_product( $post_id );
1707
1708 $deferred_payment_propriety = (int) $product['atobarai_propriety'];
1709
1710 $second_section .= '
1711 <tr>
1712 <th>' . __( 'Atobarai Propriety', 'usces' ) . '</th>
1713 <td>
1714 <label for="deferred_payment_propriety0"><input name="deferred_payment_propriety" id="deferred_payment_propriety0" type="radio" value="0"' . ( ! $deferred_payment_propriety ? ' checked="checked"' : '' ) . '>' . __( 'available', 'usces' ) . '</label>
1715 <label for="deferred_payment_propriety1"><input name="deferred_payment_propriety" id="deferred_payment_propriety1" type="radio" value="1"' . ( $deferred_payment_propriety ? ' checked="checked"' : '' ) . '>' . __( 'not available', 'usces' ) . '</label>
1716 </td>
1717 </tr>';
1718 return $second_section;
1719 }
1720
1721 /**
1722 * Disused function
1723 *
1724 * @param int $post_id Post ID.
1725 * @param object $post Post data.
1726 */
1727 function usces_atobarai_update_each_availability( $post_id, $post ) {
1728 // This process has moved to item_post.php 1568.
1729 }
1730
1731 /**
1732 * Setup of settlement.
1733 * plugins_loaded
1734 */
1735 function usces_instance_settlement() {
1736 global $usces;
1737
1738 $usces->settlement->setup();
1739 }
1740
1741 /**
1742 * Setup of extensions.
1743 * plugins_loaded
1744 */
1745 function usces_instance_extentions() {
1746 global $ganbare_tencho, $order_stock_linkage, $data_list_upgrade, $verify_members_email, $data_google_recaptcha, $brute_force;
1747
1748 require_once USCES_EXTENSIONS_DIR . '/GanbareTencho/GanbareTencho.class.php';
1749 $ganbare_tencho = new USCES_GANBARE_TENCHO();
1750
1751 require_once USCES_EXTENSIONS_DIR . '/OrderStockLinkage/order_stock_linkage.php';
1752 $order_stock_linkage = new USCES_STOCK_LINKAGE();
1753
1754 require_once USCES_EXTENSIONS_DIR . '/DataListUpgrade/data_list_upgrade.php';
1755 $data_list_upgrade = new USCES_DATALIST_UPGRADE();
1756
1757 require_once USCES_EXTENSIONS_DIR . '/VerifyMembersEmail/verify_members_email.php';
1758 $verify_members_email = new USCES_VERIFY_MEMBERS_EMAIL();
1759
1760 require_once USCES_EXTENSIONS_DIR . '/GoogleRecaptcha/google_recaptcha.php';
1761 $data_google_recaptcha = new USCES_GOOGLE_RECAPTCHA();
1762
1763 require_once USCES_EXTENSIONS_DIR . '/BruteForceCountermeasures/brute_force_countermeasures.php';
1764 $brute_force = new USCES_BRUTE_FORCE_COUNTER_MEASURES();
1765
1766 require_once USCES_EXTENSIONS_DIR . '/StructuredDataProduct/structured_data_product.php';
1767 $structured_data_product = new USCES_STRUCTURED_DATA_PRODUCT();
1768
1769 require_once USCES_EXTENSIONS_DIR . '/NewProductImageRegister/class-new-product-image-register.php';
1770 $new_product_image_register = new NEW_PRODUCT_IMAGE_REGISTER();
1771
1772 require_once USCES_EXTENSIONS_DIR . '/OperationLog/class-operation-log.php';
1773 $operation_log = new OPERATION_LOG();
1774
1775 require_once USCES_EXTENSIONS_DIR . '/CreditCardSecurity/class-creditcard-security.php';
1776 $card_security = new USCES_CREDITCARD_SECURITY();
1777 }
1778
1779 /**
1780 * Get list of attachment image attributes.
1781 * wp_get_attachment_image_attributes
1782 *
1783 * @param array $attr Array of attribute values for the image markup, keyed by attribute name.
1784 * @param WP_Post $attachment Image attachment post.
1785 * @param string|int $size Requested image size.
1786 * @return array
1787 */
1788 function usces_get_attachment_image_attributes( $attr, $attachment, $size ) {
1789 global $usces;
1790
1791 if ( $usces->is_cart_or_member_page( $_SERVER['REQUEST_URI'] ) || $usces->is_inquiry_page( $_SERVER['REQUEST_URI'] ) ) {
1792 if ( $usces->use_ssl && isset( $attr['srcset'] ) ) {
1793 $srcset = $attr['srcset'];
1794 $attr['srcset'] = str_replace( get_option( 'siteurl' ), USCES_SSL_URL_ADMIN, $srcset );
1795 }
1796 }
1797 return $attr;
1798 }
1799
1800 /**
1801 * SSL charm
1802 * init
1803 *
1804 * @return void
1805 */
1806 function usces_ssl_charm() {
1807 global $usces;
1808 if ( $usces->use_ssl && ( $usces->is_cart_or_member_page( $_SERVER['REQUEST_URI'] ) || $usces->is_inquiry_page( $_SERVER['REQUEST_URI'] ) ) ) {
1809 if ( function_exists( 'usces_ob_callback' ) ) {
1810 ob_start( 'usces_ob_callback' );
1811 } else {
1812 ob_start( 'usces_ob_rewrite' );
1813 }
1814 }
1815 }
1816
1817 /**
1818 * Output buffer rewrite
1819 *
1820 * @param array $buffer Rewrite buffer.
1821 * @return array
1822 */
1823 function usces_ob_rewrite( $buffer ) {
1824 $pattern = array(
1825 '|(<[^<]*)href=\"' . get_option( 'siteurl' ) . '([^>]*)\.css([^>]*>)|',
1826 '|(<[^<]*)src=\"' . get_option( 'siteurl' ) . '([^>]*>)|',
1827 );
1828 $replacement = array(
1829 '${1}href="' . USCES_SSL_URL_ADMIN . '${2}.css${3}',
1830 '${1}src="' . USCES_SSL_URL_ADMIN . '${2}',
1831 );
1832 $buffer = preg_replace( $pattern, $replacement, $buffer );
1833 return $buffer;
1834 }
1835
1836 /**
1837 * Enqueue scripts.
1838 * wp_enqueue_scripts
1839 */
1840 function usces_wp_enqueue_scripts() {
1841 global $usces;
1842
1843 $no_cart_css = isset( $usces->options['system']['no_cart_css'] ) ? $usces->options['system']['no_cart_css'] : 0;
1844
1845 wp_enqueue_style( 'usces_default_css', USCES_FRONT_PLUGIN_URL . '/css/usces_default.css', array(), USCES_VERSION );
1846 wp_enqueue_style( 'dashicons' );
1847
1848 if ( ! $no_cart_css ) {
1849 wp_enqueue_style( 'usces_cart_css', USCES_FRONT_PLUGIN_URL . '/css/usces_cart.css', array( 'usces_default_css' ), USCES_VERSION );
1850 }
1851
1852 $theme_version = defined( 'USCES_THEME_VERSION' ) ? USCES_THEME_VERSION : USCES_VERSION;
1853 if ( file_exists( get_stylesheet_directory() . '/usces_cart.css' ) ) {
1854 wp_enqueue_style( 'theme_cart_css', get_stylesheet_directory_uri() . '/usces_cart.css', array( 'usces_default_css' ), $theme_version );
1855 }
1856
1857 if ( $usces->is_cart_or_member_page( $_SERVER['REQUEST_URI'] ) ) {
1858
1859 if ( isset( $usces->options['address_search'] ) && 'activate' == $usces->options['address_search'] ) {
1860 wp_enqueue_script( 'usces_ajaxzip3', 'https://ajaxzip3.github.io/ajaxzip3.js', array(), current_time( 'timestamp' ), false );
1861 }
1862
1863 if ( isset( $usces->page ) && 'confirm' === $usces->page ) {
1864 $cart_comfirm = USCES_FRONT_PLUGIN_URL . '/js/cart_confirm.js';
1865 wp_enqueue_script( 'usces_cart_comfirm', $cart_comfirm, array( 'jquery' ), current_time( 'timestamp' ), false );
1866 }
1867
1868 if ( $usces->is_member_page( $_SERVER['REQUEST_URI'] ) ) {
1869 $member_common_js = USCES_FRONT_PLUGIN_URL . '/js/member_common.js';
1870 wp_enqueue_script( 'usces_member_page_js', $member_common_js, array( 'jquery' ), current_time( 'timestamp' ), true );
1871 }
1872 }
1873 }
1874
1875 /**
1876 * Check confirm.
1877 * wp_ajax_welcart_confirm_check
1878 */
1879 function welcart_confirm_check_ajax() {
1880 global $usces;
1881
1882 $nonce = filter_input( INPUT_POST, 'wc_nonce', FILTER_SANITIZE_SPECIAL_CHARS );
1883 $ajax = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_SPECIAL_CHARS );
1884 $condition = filter_input( INPUT_POST, 'wc_condition' );
1885
1886 if ( 'welcart_confirm_check' !== $ajax ) {
1887 $res = 'not permitted1';
1888 wp_send_json_error( $res );
1889 }
1890
1891 if ( ! wp_verify_nonce( $nonce, 'wc_confirm' ) ) {
1892 $res = 'not permitted2';
1893 wp_send_json_error( $res );
1894 }
1895 // Session fixation remediation (Layer 3 / uscesid removal, A2): reopen the shopper's shop
1896 // session via its own same-origin session cookie instead of decoding a crafted `uscesid`
1897 // through uscesdc()/session_id(). This AJAX runs under admin-ajax.php, where
1898 // usces_close_session() (admin_init) has closed the bootstrap session, so session_status()
1899 // is PHP_SESSION_NONE here and we (re)attach to the frontend shop session identified by its
1900 // cookie. Verified on trunk: the confirm-page uscesid decodes to exactly this cookie's
1901 // session id, so reading the cookie reaches the same cart (result unchanged).
1902 // See .docs/welcart2.x/security-remediation_uscesid-session-fixation.md 4.3.A A2.
1903 if ( PHP_SESSION_NONE === session_status() ) {
1904 $usces_options = get_option( 'usces' );
1905 $sess_name = defined( 'USCES_KEY' ) ? USCES_KEY : ( isset( $usces_options['usces_key'] ) ? $usces_options['usces_key'] : '' );
1906 if ( '' !== $sess_name && ! empty( $_COOKIE[ $sess_name ] ) ) {
1907 $sess_id = preg_replace( '/[^A-Za-z0-9,\-]/', '', wp_unslash( $_COOKIE[ $sess_name ] ) );
1908 session_id( $sess_id );
1909 @session_start(); // phpcs:ignore
1910 }
1911 }
1912
1913 $current['entry'] = $usces->cart->get_entry();
1914 $current['cart'] = $usces->cart->get_cart();
1915 session_write_close();
1916
1917 $condition = json_decode( urldecode( $condition ), true );
1918
1919 if ( $condition == $current ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
1920 $res = 'same';
1921 } elseif ( empty( $current['cart'] ) ) {
1922 $res = 'timeover';
1923 } elseif ( $current['cart'] == $condition['cart'] && $current['entry'] != $condition['entry'] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
1924 $res = 'entrydiff';
1925 } else {
1926 $res = 'different';
1927 }
1928 wp_send_json_success( $res );
1929 }
1930
1931 /**
1932 * Parameter uscesL10n
1933 * usces_filter_uscesL10n
1934 *
1935 * @param string $nouse Unused.
1936 * @param int $post_id Post ID.
1937 * @return string
1938 */
1939 function usces_confirm_uscesL10n( $nouse, $post_id ) {
1940 global $usces;
1941
1942 if ( 'confirm' === $usces->page ) {
1943 $condition['entry'] = $usces->cart->get_entry();
1944 $condition['cart'] = $usces->cart->get_cart();
1945
1946 $js = '';
1947 $js .= "'condition': '" . urlencode( wp_json_encode( $condition ) ) . "',\n";
1948 $js .= "'cart_url': '" . USCES_CART_URL . "',\n";
1949 $js .= "'check_mes': '" . __( 'Purchase information has been updated. Please repeat the procedure.\n\nPlease do not open and work more than one tab (window).\n', 'usces' ) . "',\n";
1950 return $js;
1951 }
1952 }
1953
1954 /**
1955 * Search zipcode
1956 * usces_filter_states_form_js
1957 *
1958 * @param string $js Script.
1959 * @return string
1960 */
1961 function usces_search_zipcode_check( $js ) {
1962 global $usces;
1963
1964 $option = get_option( 'usces', array() );
1965 if ( ! isset( $option['address_search'] ) || 'activate' != $option['address_search'] ) {
1966 return $js;
1967 }
1968
1969 if ( ( ( $usces->use_js && ( is_page( USCES_MEMBER_NUMBER ) || $usces->is_member_page( $_SERVER['REQUEST_URI'] ) ) )
1970 && ( ( true === $usces->is_member_logged_in() && WCUtils::is_blank( $usces->page ) ) || 'member' == $usces->page || 'editmemberform' == $usces->page || 'newmemberform' == $usces->page ) )
1971 ||
1972 ( ( $usces->use_js && ( is_page( USCES_CART_NUMBER ) || $usces->is_cart_page( $_SERVER['REQUEST_URI'] ) ) )
1973 && ( 'customer' == $usces->page || 'delivery' == $usces->page ) ) ) {
1974
1975 $zip_id = ( isset( $_REQUEST['usces_page'] ) && 'msa_setting' == $_REQUEST['usces_page'] ) ? 'msa_zip' : 'zipcode';
1976 $js .= '
1977 <script type="text/javascript">
1978 (function($) {
1979 $("#search_zipcode").click(function () {
1980 var str = $("#' . $zip_id . '").val();
1981 if( !str.match(/^\d{7}$|^\d{3}-\d{4}$/) ){
1982 alert("' . __( 'Please enter the zip code correctly.', 'usces' ) . '");
1983 $("#' . $zip_id . '").focus();
1984 }
1985 });
1986 })(jQuery);
1987 </script>';
1988 }
1989 return $js;
1990 }
1991
1992 /**
1993 * Admin member list page.
1994 * load-welcart-management_page_usces_memberlist
1995 */
1996 function usces_admin_member_list_hook() {
1997
1998 if ( ! isset( $_POST['member_list_options_apply'] ) ) {
1999 return;
2000 }
2001
2002 $list_option = get_option( 'usces_memberlist_option', array() );
2003 foreach ( (array) $list_option['view_column'] as $key => $value ) {
2004 if ( isset( $_POST['hide'][ $key ] ) ) {
2005 $list_option['view_column'][ $key ] = 1;
2006 } else {
2007 $list_option['view_column'][ $key ] = 0;
2008 }
2009 }
2010 $list_option['max_row'] = (int) $_POST['member_list_per_page'];
2011
2012 update_option( 'usces_memberlist_option', $list_option );
2013 }
2014
2015 /**
2016 * Admin order list page.
2017 * load-toplevel_page_usces_orderlist
2018 *
2019 * @param string $hook The current admin page.
2020 * @return void
2021 */
2022 function usces_admin_order_list_hook( $hook ) {
2023 if ( ! isset( $_POST['order_list_options_apply'] ) ) {
2024 return;
2025 }
2026
2027 $list_option = get_option( 'usces_orderlist_option', array() );
2028 foreach ( (array) $list_option['view_column'] as $key => $value ) {
2029 if ( isset( $_POST['hide'][ $key ] ) ) {
2030 $list_option['view_column'][ $key ] = 1;
2031 } else {
2032 $list_option['view_column'][ $key ] = 0;
2033 }
2034 }
2035 $list_option['max_row'] = (int) $_POST['order_list_per_page'];
2036
2037 update_option( 'usces_orderlist_option', $list_option );
2038 }
2039
2040 /**
2041 * Admin member list screen settings
2042 * screen_settings
2043 *
2044 * @param string $screen_settings Screen settings.
2045 * @param object $screen WP_Screen object.
2046 * @return string
2047 */
2048 function usces_memberlist_screen_settings( $screen_settings, $screen ) {
2049 if ( 'welcart-management_page_usces_memberlist' != $screen->id
2050 || ( isset( $_REQUEST['member_action'] ) && 'edit' == $_REQUEST['member_action'] ) ) {
2051 return $screen_settings;
2052 }
2053
2054 require_once USCES_PLUGIN_DIR . '/classes/memberList.class.php';
2055 $memberlist = new WlcMemberList();
2056 $arr_column = $memberlist->get_column();
2057 $list_option = get_option( 'usces_memberlist_option', array() );
2058 $init_view = array( 'ID', 'name1', 'name2', 'pref', 'address1', 'tel', 'email', 'entrydate', 'rank', 'point' );
2059
2060 $screen_settings = '
2061 <fieldset class="metabox-prefs">
2062 <legend>' . __( 'Columns' ) . '</legend>';
2063 foreach ( $arr_column as $key => $value ) {
2064 if ( 'ID' == $key || 'csod_' == substr( $key, 0, 5 ) ) {
2065 continue;
2066 }
2067
2068 if ( ! isset( $list_option['view_column'][ $key ] ) && in_array( $key, $init_view ) ) {
2069 $list_option['view_column'][ $key ] = 1;
2070 } elseif ( ! isset( $list_option['view_column'][ $key ] ) ) {
2071 $list_option['view_column'][ $key ] = 0;
2072 }
2073
2074 $checked = $list_option['view_column'][ $key ] ? ' checked="checked"' : '';
2075 $screen_settings .= '<label><input class="hide-column-tog" name="hide[' . $key . ']" type="checkbox" id="' . $key . '-hide" value="' . esc_attr( $value ) . '"' . $checked . ' />' . esc_html( $value ) . '</label>' . "\n";
2076 }
2077 $screen_settings .= '</fieldset>';
2078
2079 if ( ! isset( $list_option['max_row'] ) ) {
2080 $list_option['max_row'] = 50;
2081 }
2082
2083 $screen_settings .= '<fieldset class="screen-options">
2084 <legend>' . __( 'Pagination' ) . '</legend>
2085 <label for="edit_post_per_page">' . __( 'Number of items per page:' ) . '</label>
2086 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="member_list_per_page" id="member_list_per_page" maxlength="3" value="' . (int) $list_option['max_row'] . '" />
2087 </fieldset>
2088 <p class="submit"><input type="submit" name="member_list_options_apply" id="screen-options-apply" class="button button-primary" value="' . __( 'Apply' ) . '" /></p>';
2089
2090 update_option( 'usces_memberlist_option', $list_option );
2091 return $screen_settings;
2092 }
2093
2094 /**
2095 * Admin order list screen settings
2096 * screen_settings
2097 *
2098 * @param string $screen_settings Screen settings.
2099 * @param object $screen WP_Screen object.
2100 * @return string
2101 */
2102 function usces_orderlist_screen_settings( $screen_settings, $screen ) {
2103 if ( 'toplevel_page_usces_orderlist' != $screen->id
2104 || ( isset( $_REQUEST['order_action'] ) && 'edit' == $_REQUEST['order_action'] ) ) {
2105 return $screen_settings;
2106 }
2107
2108 require_once USCES_PLUGIN_DIR . '/classes/orderList2.class.php';
2109 $orderlist = new WlcOrderList();
2110 $arr_column = $orderlist->get_all_column();
2111 $list_option = get_option( 'usces_orderlist_option', array() );
2112 $init_view = array( 'deco_id', 'order_date', 'process_status', 'payment_name', 'receipt_status', 'total_price', 'deli_method', 'mem_id', 'name1', 'name2', 'pref' );
2113 if ( defined( 'WCEX_AUTO_DELIVERY' ) && version_compare( WCEX_AUTO_DELIVERY_VERSION, '1.4.0', '>=' ) ) {
2114 $init_view[] = 'reg_id';
2115 }
2116 $init_view = apply_filters( 'usces_filter_orderlist_column_init_view', $init_view );
2117
2118 $screen_settings = '
2119 <fieldset class="metabox-prefs">
2120 <legend>' . __( 'Columns' ) . '</legend>';
2121 foreach ( $arr_column as $key => $value ) {
2122
2123 if ( ! isset( $list_option['view_column'][ $key ] ) && in_array( $key, $init_view ) ) {
2124 $list_option['view_column'][ $key ] = 1;
2125 } elseif ( ! isset( $list_option['view_column'][ $key ] ) ) {
2126 $list_option['view_column'][ $key ] = 0;
2127 }
2128
2129 $checked = ( isset( $list_option['view_column'][ $key ] ) && $list_option['view_column'][ $key ] ) ? ' checked="checked"' : '';
2130 $screen_settings .= '<label><input class="hide-column-tog" name="hide[' . $key . ']" type="checkbox" id="' . $key . '-hide" value="' . $key . '"' . $checked . ' />' . esc_html( $value ) . '</label>' . "\n";
2131 }
2132 $screen_settings .= '</fieldset>';
2133
2134 if ( ! isset( $list_option['max_row'] ) ) {
2135 $list_option['max_row'] = 50;
2136 }
2137
2138 $screen_settings .= '<fieldset class="screen-options">
2139 <legend>' . __( 'Pagination' ) . '</legend>
2140 <label for="edit_post_per_page">' . __( 'Number of items per page:' ) . '</label>
2141 <input type="order" step="1" min="1" max="999" class="screen-per-page" name="order_list_per_page" id="order_list_per_page" maxlength="3" value="' . (int) $list_option['max_row'] . '" />
2142 </fieldset>
2143 <p class="submit"><input type="submit" name="order_list_options_apply" id="screen-options-apply" class="button button-primary" value="' . __( 'Apply' ) . '" /></p>';
2144
2145 update_option( 'usces_orderlist_option', $list_option );
2146 return $screen_settings;
2147 }
2148
2149 /**
2150 * New member registration anti-spam.
2151 * usces_filter_member_check
2152 *
2153 * @param string $mes Message.
2154 * @return string
2155 */
2156 function usces_memberreg_spamcheck( $mes ) {
2157
2158 if ( ! WCUtils::is_blank( $_POST['member']['name1'] ) && trim( $_POST['member']['name1'] ) == trim( $_POST['member']['name2'] ) ) {
2159 $mes .= __( 'Name is not correct', 'usces' ) . '<br />';
2160 }
2161
2162 if ( ! WCUtils::is_blank( $_POST['member']['address1'] ) && trim( $_POST['member']['address1'] ) == trim( $_POST['member']['address2'] ) ) {
2163 $mes .= __( 'Address is not correct', 'usces' ) . '<br />';
2164 }
2165
2166 if ( $mes ) {
2167 usces_log( 'memberreg_spamcheck : ' . $mes, 'acting_transaction.log' );
2168 }
2169
2170 return $mes;
2171 }
2172
2173 /**
2174 * New member registration anti-spam.
2175 * usces_filter_member_check_fromcart
2176 *
2177 * @param string $mes Message.
2178 * @return string
2179 */
2180 function usces_fromcart_memberreg_spamcheck( $mes ) {
2181
2182 if ( ! WCUtils::is_blank( $_POST['customer']['name1'] ) && trim( $_POST['customer']['name1'] ) == trim( $_POST['customer']['name2'] ) ) {
2183 $mes .= __( 'Name is not correct', 'usces' ) . '<br />';
2184 }
2185
2186 if ( ! WCUtils::is_blank( $_POST['customer']['address1'] ) && trim( $_POST['customer']['address1'] ) == trim( $_POST['customer']['address2'] ) ) {
2187 $mes .= __( 'Address is not correct', 'usces' ) . '<br />';
2188 }
2189
2190 if ( $mes ) {
2191 usces_log( 'fromcart_memberreg_spamcheck : ' . $mes, 'acting_transaction.log' );
2192 }
2193
2194 return $mes;
2195 }
2196
2197 /**
2198 * Welcart priority
2199 * pre_update_option_active_plugins
2200 *
2201 * @param mixed $active_plugins The new, unserialized option value.
2202 * @param mixed $old_value The old option value.
2203 * @return array
2204 */
2205 function usces_priority_active_plugins( $active_plugins, $old_value ) {
2206 foreach ( $active_plugins as $no => $path ) {
2207 if ( USCES_PLUGIN_BASENAME == $path ) {
2208 unset( $active_plugins[ $no ] );
2209 array_unshift( $active_plugins, USCES_PLUGIN_BASENAME );
2210 break;
2211 }
2212 }
2213 return $active_plugins;
2214 }
2215
2216 /**
2217 * Password change e-mail.
2218 * usces_action_changepass_page_inform
2219 */
2220 function usces_action_lostmail_inform() {
2221 $mem_mail = wp_unslash( $_REQUEST['mem'] );
2222 $lostkey = wp_unslash( $_REQUEST['key'] );
2223 $html = '
2224 <input type="hidden" name="mem" value="' . esc_attr( $mem_mail ) . '" />
2225 <input type="hidden" name="key" value="' . esc_attr( $lostkey ) . '" />' . "\n";
2226 wel_esc_script_e( $html );
2227 }
2228
2229 /**
2230 * Password change e-mail.
2231 * usces_filter_changepassword_inform
2232 *
2233 * @param string $html Form.
2234 * @return string
2235 */
2236 function usces_filter_lostmail_inform( $html ) {
2237 $mem_mail = wp_unslash( $_REQUEST['mem'] );
2238 $lostkey = wp_unslash( $_REQUEST['key'] );
2239 $html .= '
2240 <input type="hidden" name="mem" value="' . esc_attr( $mem_mail ) . '" />
2241 <input type="hidden" name="key" value="' . esc_attr( $lostkey ) . '" />' . "\n";
2242 return $html;
2243 }
2244
2245 /**
2246 * Purchase template form.
2247 * usces_filter_confirm_inform
2248 *
2249 * @param string $html Purchase template form.
2250 * @param array $payments Payment data.
2251 * @param string $acting_flag Payment method.
2252 * @param string $rand Settlement link key.
2253 * @param string $purchase_disabled Disabled tag.
2254 * @return string
2255 */
2256 function wc_purchase_nonce( $html, $payments, $acting_flag, $rand, $purchase_disabled ) {
2257 global $usces;
2258
2259 $nonacting_settlements = apply_filters( 'usces_filter_nonacting_settlements', $usces->nonacting_settlements );
2260 if ( strpos( $html, '_purchase_nonce' ) || in_array( $payments['settlement'], $nonacting_settlements ) ) {
2261 return $html;
2262 }
2263
2264 $noncekey = $usces->member_nonce_key( 'wc_purchase_nonce' );
2265 $html .= wp_nonce_field( $noncekey, '_purchase_nonce', false, false ) . "\n";
2266 return $html;
2267 }
2268
2269 /**
2270 * Purchase check
2271 * usces_purchase_check
2272 */
2273 function wc_purchase_nonce_check() {
2274 global $usces;
2275
2276 $entry = $usces->cart->get_entry();
2277 if ( ! isset( $entry['order']['payment_name'] ) || empty( $entry['order']['payment_name'] ) ) {
2278 wp_redirect( home_url() );
2279 exit;
2280 }
2281
2282 $nonacting_settlements = apply_filters( 'usces_filter_nonacting_settlements', $usces->nonacting_settlements );
2283 $payments = usces_get_payments_by_name( $entry['order']['payment_name'] );
2284 if ( in_array( $payments['settlement'], $nonacting_settlements ) ) {
2285 /*
2286 * Non-acting settlements (cash on delivery, bank transfer and the like)
2287 * used to return here without any nonce check at all, which let a
2288 * crafted request reach order registration without passing through the
2289 * checkout screens. The standard checkout button always emits the
2290 * 'wc_purchase_nonce' field for these settlements (see
2291 * includes/purchase_button.php), so requiring it does not reject a
2292 * legitimate order. Gateway (acting) settlements keep their previous
2293 * behaviour and are handled by the '_purchase_nonce' check below.
2294 */
2295 $purchase_nonce = isset( $_POST['wc_purchase_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['wc_purchase_nonce'] ) ) : '';
2296 if ( ! empty( $purchase_nonce ) && wp_verify_nonce( $purchase_nonce, 'wc_purchase_nonce' ) ) {
2297 return true;
2298 }
2299
2300 usces_log( 'purchase nonce check failed: missing or invalid wc_purchase_nonce for a non-acting settlement', 'acting_transaction.log' );
2301 wp_safe_redirect( USCES_CART_URL );
2302 exit;
2303 }
2304
2305 $nonce = isset( $_REQUEST['_purchase_nonce'] ) ? $_REQUEST['_purchase_nonce'] : '';
2306 if ( empty( $nonce ) || $usces->verify_member_nonce( $nonce, 'wc_purchase_nonce' ) ) {
2307 return true;
2308 }
2309
2310 wp_redirect( home_url() );
2311 exit;
2312 }
2313
2314 /**
2315 * Re-validate the session entry before an order is registered.
2316 * usces_purchase_check
2317 *
2318 * usces_purchase() does not call customer_check() or delivery_check(); the
2319 * required-field validation only runs inside confirm() when $_POST['confirm']
2320 * is set. A request that triggers confirm through the query string skips that
2321 * validation while cart::entry() still writes the posted customer, delivery
2322 * and offer values into the session, so an order could be registered with an
2323 * empty shipping address (which in turn produces a zero shipping fee because
2324 * the prefecture key is missing from the shipping charge table).
2325 *
2326 * This guard re-validates the stored entry at the purchase gate. It applies to
2327 * non-acting settlements only, so gateway payment flows are untouched.
2328 *
2329 * @param bool $result Result passed down the filter chain.
2330 * @return bool
2331 */
2332 function wc_purchase_entry_recheck( $result ) {
2333 global $usces;
2334
2335 /* Another callback already rejected the request. Do not redirect twice. */
2336 if ( false === $result ) {
2337 return $result;
2338 }
2339
2340 /* Escape hatch so a site can disable this guard without editing core. */
2341 if ( ! apply_filters( 'usces_filter_purchase_entry_recheck_enable', true ) ) {
2342 return $result;
2343 }
2344
2345 $entry = $usces->cart->get_entry();
2346 $cart = $usces->cart->get_cart();
2347
2348 /*
2349 * Extension point: let plugins re-run their own checkout validation here.
2350 * Extensions register their rules on 'usces_filter_customer_check' and
2351 * 'usces_filter_delivery_check', but those filters only run inside
2352 * confirm(), so a request that skips the confirm step skips them too --
2353 * WCEX DLSeller's terms-of-use agreement check is one example. Returning a
2354 * non-empty message from this filter rejects the purchase.
2355 *
2356 * This runs for every settlement, not just the non-acting ones, because a
2357 * rule such as the terms agreement applies regardless of how the order is
2358 * paid. The default is an empty message, so nothing changes until an
2359 * extension opts in.
2360 */
2361 $message = apply_filters( 'usces_filter_purchase_recheck', '', $entry, $cart );
2362 if ( ! WCUtils::is_blank( $message ) ) {
2363 wc_purchase_recheck_fail( 'filter' );
2364 }
2365
2366 $payment_name = isset( $entry['order']['payment_name'] ) ? $entry['order']['payment_name'] : '';
2367 $nonacting_settlements = apply_filters( 'usces_filter_nonacting_settlements', $usces->nonacting_settlements );
2368 $payments = usces_get_payments_by_name( $payment_name );
2369 $settlement = isset( $payments['settlement'] ) ? $payments['settlement'] : '';
2370
2371 /*
2372 * Gate: non-acting settlements only, and fail closed when the settlement
2373 * cannot be resolved. usces_get_payments_by_name() returns a stub array
2374 * with a null settlement for an unknown payment name. Treating that as
2375 * "not our concern" would let a forged payment name skip this guard
2376 * entirely, and usces_purchase() only rejects an unresolved payment when
2377 * the order total is greater than zero, so a zero-total order would slip
2378 * through. An unresolved settlement is therefore validated here.
2379 */
2380 $unresolved = WCUtils::is_blank( $settlement );
2381 if ( ! $unresolved && ! in_array( $settlement, $nonacting_settlements, true ) ) {
2382 return $result;
2383 }
2384
2385 /* Customer name and e-mail address are always required, as in customer_check(). */
2386 $customer = isset( $entry['customer'] ) ? $entry['customer'] : array();
2387 $name1 = isset( $customer['name1'] ) ? $customer['name1'] : '';
2388 $mail = isset( $customer['mailaddress1'] ) ? $customer['mailaddress1'] : '';
2389 if ( WCUtils::is_blank( $name1 ) || ! is_email( $mail ) ) {
2390 wc_purchase_recheck_fail( 'customer' );
2391 }
2392
2393 /*
2394 * Address fields are validated only when the cart actually ships. A
2395 * download-only or recurring-charge cart (WCEX DLSeller) never collects an
2396 * address, so an empty address is valid in that case.
2397 */
2398 if ( wc_purchase_cart_needs_shipping() ) {
2399 /* Use the separate shipping address when the buyer chose one. */
2400 $delivery_flag = isset( $entry['delivery']['delivery_flag'] ) ? (int) $entry['delivery']['delivery_flag'] : 0;
2401 $address = ( 1 === $delivery_flag && isset( $entry['delivery'] ) ) ? $entry['delivery'] : $customer;
2402
2403 /*
2404 * Whether a field is required comes from the site configuration, never
2405 * hard-coded. Only the prefecture uses a different key in the entry
2406 * ('states' as the essential-mark key, 'pref' in the entry).
2407 */
2408 $field_map = array(
2409 'name1' => 'name1',
2410 'zipcode' => 'zipcode',
2411 'states' => 'pref',
2412 'address1' => 'address1',
2413 'address2' => 'address2',
2414 'tel' => 'tel',
2415 );
2416 foreach ( $field_map as $mark_key => $entry_key ) {
2417 if ( ! usces_is_required_field( $mark_key ) ) {
2418 continue;
2419 }
2420 $value = isset( $address[ $entry_key ] ) ? $address[ $entry_key ] : '';
2421 if ( wc_purchase_recheck_value_is_empty( $mark_key, $value ) ) {
2422 wc_purchase_recheck_fail( $mark_key );
2423 }
2424 }
2425 }
2426
2427 return $result;
2428 }
2429
2430 /**
2431 * Whether a submitted field value counts as empty for the purchase recheck.
2432 *
2433 * The prefecture select renders its placeholder with the literal label as the
2434 * option value, so an unselected prefecture arrives as the '-- Select --'
2435 * string rather than an empty string. customer_check() and delivery_check()
2436 * compare against that literal, and this guard has to do the same or it would
2437 * accept an unselected prefecture, which in turn yields a zero shipping fee.
2438 * The select is translated with the 'usces_dual' text domain while the core
2439 * checks use 'usces', so both translations and the raw literal are compared.
2440 *
2441 * @param string $mark_key Essential mark key.
2442 * @param string $value Submitted value.
2443 * @return bool
2444 */
2445 function wc_purchase_recheck_value_is_empty( $mark_key, $value ) {
2446 if ( WCUtils::is_blank( $value ) ) {
2447 return true;
2448 }
2449
2450 if ( 'states' === $mark_key ) {
2451 $placeholders = array(
2452 '-- Select --',
2453 __( '-- Select --', 'usces' ),
2454 __( '-- Select --', 'usces_dual' ),
2455 );
2456 if ( in_array( $value, $placeholders, true ) ) {
2457 return true;
2458 }
2459 }
2460
2461 return false;
2462 }
2463
2464 /**
2465 * Whether the current cart contains at least one item that has to be shipped.
2466 *
2467 * Mirrors the division check used by usc_e_shop::getShippingCharge(): when
2468 * WCEX DLSeller is active its own helper is authoritative, otherwise each cart
2469 * row is inspected through getItemDivision().
2470 *
2471 * @return bool
2472 */
2473 function wc_purchase_cart_needs_shipping() {
2474 global $usces;
2475
2476 $cart = $usces->cart->get_cart();
2477 if ( empty( $cart ) ) {
2478 return false;
2479 }
2480
2481 if ( function_exists( 'dlseller_have_shipped' ) ) {
2482 return (bool) dlseller_have_shipped( $cart );
2483 }
2484
2485 foreach ( (array) $cart as $cart_row ) {
2486 if ( ! isset( $cart_row['post_id'] ) ) {
2487 /*
2488 * Fail closed: a row we cannot inspect must not be able to switch
2489 * the address validation off.
2490 */
2491 return true;
2492 }
2493
2494 $division = $usces->getItemDivision( $cart_row['post_id'] );
2495 if ( 'shipped' === $division ) {
2496 return true;
2497 }
2498
2499 /*
2500 * getItemDivision() returns null when the post is not a registered
2501 * Welcart product. Treat that as shipping required rather than as
2502 * "no shipping needed", otherwise a single unresolvable cart row
2503 * would silently skip every address check. An explicit non-shipped
2504 * division (a download item, for example) is still honoured above.
2505 */
2506 if ( null === $division ) {
2507 return true;
2508 }
2509 }
2510
2511 return false;
2512 }
2513
2514 /**
2515 * Reject a purchase request that failed the entry recheck.
2516 *
2517 * @param string $reason Field or group that failed, recorded in the log.
2518 * @return void
2519 */
2520 function wc_purchase_recheck_fail( $reason ) {
2521 usces_log( 'purchase entry recheck failed: ' . $reason, 'acting_transaction.log' );
2522 wp_safe_redirect( USCES_CART_URL );
2523 exit;
2524 }
2525
2526 /**
2527 * Checking in $usces->use_point()
2528 * usces_action_confirm_page_point_inform
2529 */
2530 function usces_use_point_nonce() {
2531 global $usces;
2532
2533 $noncekey = $usces->member_nonce_key( 'use_point' );
2534 wp_nonce_field( $noncekey, 'wc_nonce' );
2535 }
2536
2537 /**
2538 * Check post member page
2539 * usces_action_newmember_page_inform
2540 * usces_action_memberinfo_page_inform
2541 * usces_action_newpass_page_inform
2542 * usces_action_changepass_page_inform
2543 * usces_action_customer_page_inform
2544 */
2545 function usces_post_member_nonce() {
2546 global $usces;
2547
2548 $noncekey = $usces->member_nonce_key( 'post_member' );
2549 wp_nonce_field( $noncekey, 'wc_nonce' );
2550 }
2551
2552 /**
2553 * Check post member login page
2554 * usces_action_login_page_inform
2555 * usces_action_customer_page_member_inform
2556 */
2557 function usces_member_login_nonce() {
2558 global $usces;
2559
2560 $noncekey = $usces->member_nonce_key( 'post_member' );
2561 wp_nonce_field( $noncekey, 'wel_nonce' );
2562 }
2563
2564 /**
2565 * Order edit form additional information
2566 * usces_action_order_edit_form_detail_bottom
2567 *
2568 * @param array $data Order data.
2569 * @param array $cscs_meta Custom customer data.
2570 * @param array $action_args Order action.
2571 * @return void
2572 */
2573 function wel_order_edit_customer_additional_information( $data, $cscs_meta, $action_args ) {
2574 global $usces;
2575
2576 $order_id = isset( $data['ID'] ) ? (int) $data['ID'] : 0;
2577 if ( ! $order_id ) {
2578 return;
2579 }
2580
2581 $value = $usces->get_order_meta_value( 'extra_info', $order_id );
2582 if ( ! $value ) {
2583 return;
2584 }
2585
2586 $infos = wel_safe_unserialize( $value );
2587 $html = '<tr>
2588 <td colspan="2" class="label cus_note_label">' . __( 'Others', 'usces' ) . '</td>
2589 <td colspan="4" class="cus_note_label">' . "\n";
2590 $html .= '<table border="0" cellspacing="0" class="extra_info cus_info">' . "\n";
2591 foreach ( $infos as $key => $info ) {
2592 $key = str_replace( 'USER_AGANT', 'USER_AGENT', $key );
2593 $html .= '<tr><td class="label cus_note_label">' . esc_html( $key ) . '</td><td class="cus_note_label">' . esc_html( $info ) . '</td></tr>' . "\n";
2594 }
2595 $html .= '</table>' . "\n";
2596 $html .= '</td></tr>' . "\n";
2597
2598 wel_esc_script_e( $html );
2599 }
2600
2601 /**
2602 * Save additional information.
2603 * usces_post_reg_orderdata
2604 *
2605 * @param int $order_id Order ID.
2606 * @param array $results Settlement result data.
2607 * @return void
2608 */
2609 function wel_save_extra_info_to_ordermeta( $order_id, $results ) {
2610 global $usces;
2611
2612 if ( ! $order_id ) {
2613 return;
2614 }
2615
2616 $info = array(
2617 'IP' => ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? $_SERVER['REMOTE_ADDR'] : '',
2618 'USER_AGENT' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? $_SERVER['HTTP_USER_AGENT'] : '',
2619 );
2620
2621 $usces->set_order_meta_value( 'extra_info', serialize( $info ), $order_id );
2622 }
2623
2624 /**
2625 * Navigation menu arguments.
2626 * wp_nav_menu_args
2627 *
2628 * @param array $args An array containing wp_nav_menu() arguments.
2629 * @return array
2630 */
2631 function usces_wp_nav_menu_args( $args ) {
2632
2633 usces_remove_filter();
2634
2635 return $args;
2636 }
2637
2638 /**
2639 * Navigation menu.
2640 * wp_nav_menu
2641 *
2642 * @param string $nav_menu The HTML content for the navigation menu.
2643 * @param stdClass $args An object containing wp_nav_menu() arguments.
2644 * @return string
2645 */
2646 function usces_wp_nav_menu( $nav_menu, $args ) {
2647
2648 usces_reset_filter();
2649
2650 return $nav_menu;
2651 }
2652
2653 /**
2654 * Session write close.
2655 * admin_init
2656 */
2657 function usces_close_session() {
2658 if ( session_status() == PHP_SESSION_ACTIVE ) {
2659 session_write_close();
2660 }
2661 }
2662
2663 /**
2664 * Regenerate the session ID on member login (session fixation countermeasure).
2665 * usces_action_after_login
2666 *
2667 * Fires on every successful member login path (member_login() / member_just_login()
2668 * and extension logins that trigger usces_action_after_login). The uscesid mechanism
2669 * itself has been removed (A1/C) and set_cookie() now regenerates unconditionally,
2670 * but not every login path calls set_cookie() (member_just_login() does not), so this
2671 * hook is what guarantees regeneration on all of them.
2672 */
2673 function usces_regenerate_session_on_login() {
2674 if ( session_status() == PHP_SESSION_ACTIVE && ! headers_sent() ) {
2675 session_regenerate_id( true );
2676 }
2677 }
2678
2679 /**
2680 * Handles OPTIONS requests for the server.
2681 *
2682 * This is handled outside of the server code, as it doesn't obey normal route
2683 * mapping.
2684 *
2685 * @since 4.4.0
2686 *
2687 * @param mixed $response Current reponse, either response or `null` to indicate pass-through.
2688 * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server).
2689 * @param WP_REST_Request $request The request that was used to make current response.
2690 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
2691 */
2692 function usces_close_session_loopback( $response, $handler, $request ) {
2693 $router = $request->get_route();
2694 if ( ! empty( $router ) && '/wp-site-health/v1/tests/loopback-requests' == $router ) {
2695 usces_close_session();
2696 }
2697 return $response;
2698 }
2699
2700 /**
2701 * Google recaptcha script.
2702 * wp_print_footer_scripts
2703 */
2704 function usces_add_google_recaptcha_v3_script() {
2705 global $usces, $usces_entries, $usces_carts;
2706
2707 if ( $usces->is_member_page( $_SERVER['REQUEST_URI'] ) && 'newmemberform' === $usces->page ) {
2708 print_google_recaptcha_response( 'create_new_member', 'memberpages' );
2709 }
2710
2711 if ( ( $usces->is_cart_page( $_SERVER['REQUEST_URI'] ) && 'customer' === $usces->page ) ) {
2712 print_google_recaptcha_response( 'customer_order_page', 'customer-info', 'customer_form' );
2713 }
2714 if ( ( $usces->is_cart_page( $_SERVER['REQUEST_URI'] ) && 'delivery' === $usces->page ) ) {
2715 print_google_recaptcha_response( 'delivery_order_page', 'delivery-info', '' );
2716 }
2717 }
2718
2719 /**
2720 * Google recaptcha badge display.
2721 *
2722 * @param string $action Page action.
2723 * @param string $parent_element_id Parent page.
2724 * @param string $form_name Page name.
2725 * @return void
2726 */
2727 function print_google_recaptcha_response( $action, $parent_element_id, $form_name = '' ) {
2728 $option = get_option( 'usces_ex', array() );
2729 if ( isset( $option['system']['google_recaptcha']['status'] ) && $option['system']['google_recaptcha']['status'] && ! ( empty( $option['system']['google_recaptcha']['site_key'] ) ) && ! ( empty( $option['system']['google_recaptcha']['secret_key'] ) ) ) {
2730 $site_key = isset( $option['system']['google_recaptcha']['site_key'] ) ? $option['system']['google_recaptcha']['site_key'] : '';
2731 echo '<script src="https://www.google.com/recaptcha/api.js?render=' . esc_js( $site_key ) . '"></script>';
2732 echo '<script>';
2733 echo 'grecaptcha.ready(function() {';
2734 echo ' grecaptcha.execute(\'' . esc_js( $site_key ) . '\', {action: "' . esc_js( $action ) . '"}).then(function(token) {';
2735 echo ' jQuery("form").append(\'<input type="hidden" id="elm_recaptcha_response" name="recaptcha_response" value="\'+ token +\'">\');';
2736 echo ' if(!jQuery("input[type=hidden]").is("[name=recaptcha_response]")) { jQuery("input[name=member_regmode]").after(\'<input type="hidden" id="elm_recaptcha_response" name="recaptcha_response" value="\'+ token +\'">\'); }';
2737 echo ' });';
2738 echo '});';
2739 echo '</script>';
2740
2741 // run time reload google captcha token v3 key after 1 minutes.
2742 echo '<script>';
2743 echo 'var max_google_reload_times = 10;';
2744 echo 'var google_reload_times = 1;';
2745 echo 'var function_token_reload = setInterval(function(){';
2746 echo ' if (google_reload_times > max_google_reload_times) {';
2747 echo ' clearInterval(function_token_reload);';
2748 echo ' } else { ';
2749 echo ' google_reload_times = google_reload_times + 1;';
2750 echo ' grecaptcha.ready(function() {';
2751 echo ' grecaptcha.execute(\'' . esc_js( $site_key ) . '\', {action: "' . esc_js( $action ) . '"}).then(function(token) {';
2752 echo ' jQuery("form #elm_recaptcha_response").val(token);';
2753 echo ' });';
2754 echo ' });';
2755 echo ' }';
2756 echo '}, 1000*60);';
2757 echo '</script>';
2758 }
2759 }
2760
2761 /**
2762 * Upload CSV.
2763 * wp_ajax_wel_item_upload_ajax
2764 *
2765 * @return void
2766 */
2767 function wel_item_upload_ajax() {
2768 check_ajax_referer();
2769 $_REQUEST['action'] = 'upload_register';
2770 usces_item_uploadcsv();
2771 }
2772
2773 /**
2774 * Check progress.
2775 * wp_ajax_wel_item_progress_check_ajax
2776 *
2777 * @since 2.7.7
2778 * @return void
2779 */
2780 function wel_item_progress_check_ajax() {
2781 require_once USCES_PLUGIN_DIR . '/functions/progress-check.php';
2782 usces_item_progress_check();
2783 }
2784
2785 /**
2786 * Completed.
2787 * wp_ajax_wel_item_progress_completed_ajax
2788 *
2789 * @since 2.8.5
2790 * @return void
2791 */
2792 function wel_item_progress_completed_ajax() {
2793 check_ajax_referer( 'wel_progress_check_ajax', 'nonce' );
2794
2795 if ( 4 > usces_get_admin_user_level() ) {
2796 die( 'user_level' );
2797 }
2798
2799 $logfile = wp_unslash( filter_input( INPUT_POST, 'logfile', FILTER_DEFAULT ) );
2800 $logfile = WP_CONTENT_DIR . USCES_UPLOAD_TEMP . DIRECTORY_SEPARATOR . $logfile;
2801 // Make sure the file is exist.
2802 if ( usces_is_reserved_file( $logfile ) ) {
2803 // Get the content and echo it.
2804 $text = file_get_contents( $logfile );
2805 echo( $text );
2806 }
2807 exit;
2808 }
2809
2810 /**
2811 * Implementation of hook usces_filter_admin_custom_field_input_value()
2812 * Append new options from the instance of the order custom field.
2813 *
2814 * @since 2.5.3
2815 * @see usces_admin_custom_field_input()
2816 * @param string $value List of options.
2817 * @param string $key The custom field key.
2818 * @param string $entry The instance of the custom field.
2819 * @param string $custom_field The type of the custom field. Ex: order, delivery, customer, member...
2820 * @return string $value List of options appended new options from the instance.
2821 */
2822 function usces_filter_admin_custom_field_input_value( $value, $key, $entry, $custom_field ) {
2823 if ( 'order' === $custom_field ) {
2824 $means = $entry['means'];
2825 $is_choice_field_type = '0' === $means || '3' === $means || '4' === $means;
2826 if ( $is_choice_field_type ) {
2827 $options = explode( "\n", $value );
2828
2829 if ( is_array( $entry['data'] ) ) {
2830 $data = $entry['data'];
2831 } else {
2832 $data = ( ! empty( $entry['data'] ) && '#NONE#' !== $entry['data'] ) ? array( $entry['data'] ) : array();
2833 }
2834
2835 $diff_values = array_diff( $data, $options );
2836
2837 if ( ! empty( $diff_values ) ) {
2838 $options = array_merge( $options, $diff_values );
2839 $value = implode( "\n", $options );
2840 }
2841 }
2842 }
2843
2844 return $value;
2845 }
2846
2847 /**
2848 * Create Post type for Welcart product.
2849 * init
2850 *
2851 * @since 2.5.5
2852 */
2853 function usces_create_product_type() {
2854 register_post_type(
2855 'product',
2856 array(
2857 'labels' => array(
2858 'name' => __( 'Items', 'usces' ),
2859 'singular_name' => 'product',
2860 ),
2861 'public' => false,
2862 'has_archive' => false,
2863 'menu_position' => 3,
2864 'show_in_rest' => false,
2865 )
2866 );
2867 }
2868
2869 /**
2870 * Executement release card update lock.
2871 * usces_action_member_list_page
2872 *
2873 * @since 2.5.8
2874 * @param string $member_action Action.
2875 */
2876 function wel_execute_release_card_update_lock( $member_action ) {
2877 if ( 'editpost' === $member_action ) {
2878 $flag = filter_input( INPUT_POST, 'release_card_update_lock', FILTER_DEFAULT );
2879 $member_id = filter_input( INPUT_POST, 'member_id', FILTER_VALIDATE_INT );
2880 if ( 'release' === $flag && ! empty( $member_id ) ) {
2881 wel_release_card_update_lock( $member_id );
2882 }
2883 }
2884 }
2885
2886 /**
2887 * Welcart auto update control.
2888 * auto_update_plugin
2889 *
2890 * @since 2.8
2891 * @param bool|null $update Whether to update.
2892 * @param object $item The update offer.
2893 * @return boolean $update
2894 */
2895 function usces_auto_update_welcart( $update, $item ) {
2896 $targets = array( 'usc-e-shop' );
2897 if ( in_array( $item->slug ?? '', $targets ) ) {
2898
2899 $available_updates = get_site_transient( 'update_plugins' );
2900 if ( ! isset( $available_updates->response['usc-e-shop/usc-e-shop.php'] ) ) {
2901 return true;
2902 }
2903
2904 $plugin_info = $available_updates->response['usc-e-shop/usc-e-shop.php'];
2905 $current_arr = explode( '.', USCES_VERSION, 3 );
2906 $current_major = (string) $current_arr[0] . '.' . (string) $current_arr[1];
2907 $new_arr = explode( '.', $plugin_info->new_version, 3 );
2908 $new_major = (string) $new_arr[0] . '.' . (string) $new_arr[1];
2909
2910 if ( $current_major === $new_major ) {
2911 return true;
2912 } else {
2913 return false;
2914 }
2915 } else {
2916 return $update;
2917 }
2918 }
2919
2920 /**
2921 * Check if the same item code does not exist.
2922 * wp_ajax_wel_item_code_exists_ajax
2923 *
2924 * @since 2.8
2925 * @param string $item_code Item code.
2926 * @return boolean $res true if present, false otherwise.
2927 */
2928 function wel_item_code_exists_ajax() {
2929
2930 $wc_nonce = filter_input( INPUT_POST, 'wc_nonce' );
2931 $item_code = filter_input( INPUT_POST, 'item_code' );
2932
2933 $res = array(
2934 'result' => null,
2935 'item_code' => $item_code,
2936 'message' => '',
2937 );
2938
2939 if ( ! wp_verify_nonce( $wc_nonce, 'check_item_code' ) ) {
2940 wp_send_json( $res );
2941 }
2942
2943 $post_id = wel_get_id_by_item_code( $item_code, false );
2944
2945 $res['result'] = $post_id;
2946 $res['message'] = esc_js( sprintf( __( 'Product code %s has already been registered.', 'usces' ), $item_code ) );
2947
2948 wp_send_json( $res );
2949 }
2950
2951 /**
2952 * All credit security unlocked.
2953 * wp_ajax_credit_security_unlock
2954 */
2955 function wel_credit_security_unlock() {
2956
2957 $wc_nonce = filter_input( INPUT_POST, 'wc_nonce' );
2958 $res = array();
2959
2960 if ( ! wp_verify_nonce( $wc_nonce, 'credit_security_unlock' ) ) {
2961 wp_send_json( $res );
2962 }
2963
2964 wel_all_unlock();
2965
2966 wp_send_json( $res );
2967 }
2968
2969 /**
2970 * Log member data delete.
2971 *
2972 * @param array $res Result.
2973 * @param int $mem_id Member ID.
2974 * @return void
2975 */
2976 function wel_member_data_delete_log( $res, $mem_id ) {
2977 usces_log( 'Res: ' . print_r( $res, true ) . ' $_POST: ' . print_r( $_POST, true ) . ' $_GET: ' . print_r( $_GET, true ), 'db', 'member_delete', $mem_id );
2978 }
2979