forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-core-functions.php
1422 lines (1275 loc) · 43 KB
/
wc-core-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* WooCommerce Core Functions
*
* General core functions available on both the front-end and admin.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Include core functions (available in both admin and frontend).
include( 'wc-conditional-functions.php' );
include( 'wc-coupon-functions.php' );
include( 'wc-user-functions.php' );
include( 'wc-deprecated-functions.php' );
include( 'wc-formatting-functions.php' );
include( 'wc-order-functions.php' );
include( 'wc-page-functions.php' );
include( 'wc-product-functions.php' );
include( 'wc-account-functions.php' );
include( 'wc-term-functions.php' );
include( 'wc-attribute-functions.php' );
include( 'wc-rest-functions.php' );
/**
* Filters on data used in admin and frontend.
*/
add_filter( 'woocommerce_coupon_code', 'html_entity_decode' );
add_filter( 'woocommerce_coupon_code', 'sanitize_text_field' );
add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default
add_filter( 'woocommerce_stock_amount', 'intval' ); // Stock amounts are integers by default
add_filter( 'woocommerce_shipping_rate_label', 'sanitize_text_field' ); // Shipping rate label
/**
* Short Description (excerpt).
*/
add_filter( 'woocommerce_short_description', 'wptexturize' );
add_filter( 'woocommerce_short_description', 'convert_smilies' );
add_filter( 'woocommerce_short_description', 'convert_chars' );
add_filter( 'woocommerce_short_description', 'wpautop' );
add_filter( 'woocommerce_short_description', 'shortcode_unautop' );
add_filter( 'woocommerce_short_description', 'prepend_attachment' );
add_filter( 'woocommerce_short_description', 'do_shortcode', 11 ); // AFTER wpautop()
/**
* Create a new order programmatically.
*
* Returns a new order object on success which can then be used to add additional data.
*
* @param array $args
* @return WC_Order|WP_Error
*/
function wc_create_order( $args = array() ) {
$default_args = array(
'status' => null,
'customer_id' => null,
'customer_note' => null,
'parent' => null,
'created_via' => null,
'cart_hash' => null,
'order_id' => 0,
);
try {
$args = wp_parse_args( $args, $default_args );
$order = new WC_Order( $args['order_id'] );
// Update props that were set (not null)
if ( ! is_null( $args['parent'] ) ) {
$order->set_parent_id( absint( $args['parent'] ) );
}
if ( ! is_null( $args['status'] ) ) {
$order->set_status( $args['status'] );
}
if ( ! is_null( $args['customer_note'] ) ) {
$order->set_customer_note( $args['customer_note'] );
}
if ( ! is_null( $args['customer_id'] ) ) {
$order->set_customer_id( is_numeric( $args['customer_id'] ) ? absint( $args['customer_id'] ) : 0 );
}
if ( ! is_null( $args['created_via'] ) ) {
$order->set_created_via( sanitize_text_field( $args['created_via'] ) );
}
if ( ! is_null( $args['cart_hash'] ) ) {
$order->set_cart_hash( sanitize_text_field( $args['cart_hash'] ) );
}
// Update other order props set automatically
$order->set_currency( get_woocommerce_currency() );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$order->set_customer_ip_address( WC_Geolocation::get_ip_address() );
$order->set_customer_user_agent( wc_get_user_agent() );
$order->save();
} catch ( Exception $e ) {
return new WP_Error( 'error', $e->getMessage() );
}
return $order;
}
/**
* Update an order. Uses wc_create_order.
*
* @param array $args
* @return string | WC_Order
*/
function wc_update_order( $args ) {
if ( ! $args['order_id'] ) {
return new WP_Error( __( 'Invalid order ID', 'woocommerce' ) );
}
return wc_create_order( $args );
}
/**
* Get template part (for templates like the shop-loop).
*
* WC_TEMPLATE_DEBUG_MODE will prevent overrides in themes from taking priority.
*
* @access public
* @param mixed $slug
* @param string $name (default: '')
*/
function wc_get_template_part( $slug, $name = '' ) {
$template = '';
// Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
}
// Get default slug-name.php
if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
$template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
}
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) {
$template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
}
// Allow 3rd party plugins to filter template file from their plugin.
$template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
if ( $template ) {
load_template( $template, false );
}
}
/**
* Get other templates (e.g. product attributes) passing attributes and including the file.
*
* @access public
* @param string $template_name
* @param array $args (default: array())
* @param string $template_path (default: '')
* @param string $default_path (default: '')
*/
function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
if ( ! empty( $args ) && is_array( $args ) ) {
extract( $args );
}
$located = wc_locate_template( $template_name, $template_path, $default_path );
if ( ! file_exists( $located ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
return;
}
// Allow 3rd party plugin filter template file from their plugin.
$located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path );
do_action( 'woocommerce_before_template_part', $template_name, $template_path, $located, $args );
include( $located );
do_action( 'woocommerce_after_template_part', $template_name, $template_path, $located, $args );
}
/**
* Like wc_get_template, but returns the HTML instead of outputting.
* @see wc_get_template
* @since 2.5.0
* @param string $template_name
*/
function wc_get_template_html( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
ob_start();
wc_get_template( $template_name, $args, $template_path, $default_path );
return ob_get_clean();
}
/**
* Locate a template and return the path for inclusion.
*
* This is the load order:
*
* yourtheme / $template_path / $template_name
* yourtheme / $template_name
* $default_path / $template_name
*
* @access public
* @param string $template_name
* @param string $template_path (default: '')
* @param string $default_path (default: '')
* @return string
*/
function wc_locate_template( $template_name, $template_path = '', $default_path = '' ) {
if ( ! $template_path ) {
$template_path = WC()->template_path();
}
if ( ! $default_path ) {
$default_path = WC()->plugin_path() . '/templates/';
}
// Look within passed path within the theme - this is priority.
$template = locate_template(
array(
trailingslashit( $template_path ) . $template_name,
$template_name,
)
);
// Get default template/
if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
$template = $default_path . $template_name;
}
// Return what we found.
return apply_filters( 'woocommerce_locate_template', $template, $template_name, $template_path );
}
/**
* Get Base Currency Code.
*
* @return string
*/
function get_woocommerce_currency() {
return apply_filters( 'woocommerce_currency', get_option( 'woocommerce_currency' ) );
}
/**
* Get full list of currency codes.
*
* @return array
*/
function get_woocommerce_currencies() {
return array_unique(
apply_filters( 'woocommerce_currencies',
array(
'AED' => __( 'United Arab Emirates dirham', 'woocommerce' ),
'AFN' => __( 'Afghan afghani', 'woocommerce' ),
'ALL' => __( 'Albanian lek', 'woocommerce' ),
'AMD' => __( 'Armenian dram', 'woocommerce' ),
'ANG' => __( 'Netherlands Antillean guilder', 'woocommerce' ),
'AOA' => __( 'Angolan kwanza', 'woocommerce' ),
'ARS' => __( 'Argentine peso', 'woocommerce' ),
'AUD' => __( 'Australian dollar', 'woocommerce' ),
'AWG' => __( 'Aruban florin', 'woocommerce' ),
'AZN' => __( 'Azerbaijani manat', 'woocommerce' ),
'BAM' => __( 'Bosnia and Herzegovina convertible mark', 'woocommerce' ),
'BBD' => __( 'Barbadian dollar', 'woocommerce' ),
'BDT' => __( 'Bangladeshi taka', 'woocommerce' ),
'BGN' => __( 'Bulgarian lev', 'woocommerce' ),
'BHD' => __( 'Bahraini dinar', 'woocommerce' ),
'BIF' => __( 'Burundian franc', 'woocommerce' ),
'BMD' => __( 'Bermudian dollar', 'woocommerce' ),
'BND' => __( 'Brunei dollar', 'woocommerce' ),
'BOB' => __( 'Bolivian boliviano', 'woocommerce' ),
'BRL' => __( 'Brazilian real', 'woocommerce' ),
'BSD' => __( 'Bahamian dollar', 'woocommerce' ),
'BTC' => __( 'Bitcoin', 'woocommerce' ),
'BTN' => __( 'Bhutanese ngultrum', 'woocommerce' ),
'BWP' => __( 'Botswana pula', 'woocommerce' ),
'BYR' => __( 'Belarusian ruble', 'woocommerce' ),
'BZD' => __( 'Belize dollar', 'woocommerce' ),
'CAD' => __( 'Canadian dollar', 'woocommerce' ),
'CDF' => __( 'Congolese franc', 'woocommerce' ),
'CHF' => __( 'Swiss franc', 'woocommerce' ),
'CLP' => __( 'Chilean peso', 'woocommerce' ),
'CNY' => __( 'Chinese yuan', 'woocommerce' ),
'COP' => __( 'Colombian peso', 'woocommerce' ),
'CRC' => __( 'Costa Rican colón', 'woocommerce' ),
'CUC' => __( 'Cuban convertible peso', 'woocommerce' ),
'CUP' => __( 'Cuban peso', 'woocommerce' ),
'CVE' => __( 'Cape Verdean escudo', 'woocommerce' ),
'CZK' => __( 'Czech koruna', 'woocommerce' ),
'DJF' => __( 'Djiboutian franc', 'woocommerce' ),
'DKK' => __( 'Danish krone', 'woocommerce' ),
'DOP' => __( 'Dominican peso', 'woocommerce' ),
'DZD' => __( 'Algerian dinar', 'woocommerce' ),
'EGP' => __( 'Egyptian pound', 'woocommerce' ),
'ERN' => __( 'Eritrean nakfa', 'woocommerce' ),
'ETB' => __( 'Ethiopian birr', 'woocommerce' ),
'EUR' => __( 'Euro', 'woocommerce' ),
'FJD' => __( 'Fijian dollar', 'woocommerce' ),
'FKP' => __( 'Falkland Islands pound', 'woocommerce' ),
'GBP' => __( 'Pound sterling', 'woocommerce' ),
'GEL' => __( 'Georgian lari', 'woocommerce' ),
'GGP' => __( 'Guernsey pound', 'woocommerce' ),
'GHS' => __( 'Ghana cedi', 'woocommerce' ),
'GIP' => __( 'Gibraltar pound', 'woocommerce' ),
'GMD' => __( 'Gambian dalasi', 'woocommerce' ),
'GNF' => __( 'Guinean franc', 'woocommerce' ),
'GTQ' => __( 'Guatemalan quetzal', 'woocommerce' ),
'GYD' => __( 'Guyanese dollar', 'woocommerce' ),
'HKD' => __( 'Hong Kong dollar', 'woocommerce' ),
'HNL' => __( 'Honduran lempira', 'woocommerce' ),
'HRK' => __( 'Croatian kuna', 'woocommerce' ),
'HTG' => __( 'Haitian gourde', 'woocommerce' ),
'HUF' => __( 'Hungarian forint', 'woocommerce' ),
'IDR' => __( 'Indonesian rupiah', 'woocommerce' ),
'ILS' => __( 'Israeli new shekel', 'woocommerce' ),
'IMP' => __( 'Manx pound', 'woocommerce' ),
'INR' => __( 'Indian rupee', 'woocommerce' ),
'IQD' => __( 'Iraqi dinar', 'woocommerce' ),
'IRR' => __( 'Iranian rial', 'woocommerce' ),
'ISK' => __( 'Icelandic króna', 'woocommerce' ),
'JEP' => __( 'Jersey pound', 'woocommerce' ),
'JMD' => __( 'Jamaican dollar', 'woocommerce' ),
'JOD' => __( 'Jordanian dinar', 'woocommerce' ),
'JPY' => __( 'Japanese yen', 'woocommerce' ),
'KES' => __( 'Kenyan shilling', 'woocommerce' ),
'KGS' => __( 'Kyrgyzstani som', 'woocommerce' ),
'KHR' => __( 'Cambodian riel', 'woocommerce' ),
'KMF' => __( 'Comorian franc', 'woocommerce' ),
'KPW' => __( 'North Korean won', 'woocommerce' ),
'KRW' => __( 'South Korean won', 'woocommerce' ),
'KWD' => __( 'Kuwaiti dinar', 'woocommerce' ),
'KYD' => __( 'Cayman Islands dollar', 'woocommerce' ),
'KZT' => __( 'Kazakhstani tenge', 'woocommerce' ),
'LAK' => __( 'Lao kip', 'woocommerce' ),
'LBP' => __( 'Lebanese pound', 'woocommerce' ),
'LKR' => __( 'Sri Lankan rupee', 'woocommerce' ),
'LRD' => __( 'Liberian dollar', 'woocommerce' ),
'LSL' => __( 'Lesotho loti', 'woocommerce' ),
'LYD' => __( 'Libyan dinar', 'woocommerce' ),
'MAD' => __( 'Moroccan dirham', 'woocommerce' ),
'MDL' => __( 'Moldovan leu', 'woocommerce' ),
'MGA' => __( 'Malagasy ariary', 'woocommerce' ),
'MKD' => __( 'Macedonian denar', 'woocommerce' ),
'MMK' => __( 'Burmese kyat', 'woocommerce' ),
'MNT' => __( 'Mongolian tögrög', 'woocommerce' ),
'MOP' => __( 'Macanese pataca', 'woocommerce' ),
'MRO' => __( 'Mauritanian ouguiya', 'woocommerce' ),
'MUR' => __( 'Mauritian rupee', 'woocommerce' ),
'MVR' => __( 'Maldivian rufiyaa', 'woocommerce' ),
'MWK' => __( 'Malawian kwacha', 'woocommerce' ),
'MXN' => __( 'Mexican peso', 'woocommerce' ),
'MYR' => __( 'Malaysian ringgit', 'woocommerce' ),
'MZN' => __( 'Mozambican metical', 'woocommerce' ),
'NAD' => __( 'Namibian dollar', 'woocommerce' ),
'NGN' => __( 'Nigerian naira', 'woocommerce' ),
'NIO' => __( 'Nicaraguan córdoba', 'woocommerce' ),
'NOK' => __( 'Norwegian krone', 'woocommerce' ),
'NPR' => __( 'Nepalese rupee', 'woocommerce' ),
'NZD' => __( 'New Zealand dollar', 'woocommerce' ),
'OMR' => __( 'Omani rial', 'woocommerce' ),
'PAB' => __( 'Panamanian balboa', 'woocommerce' ),
'PEN' => __( 'Peruvian nuevo sol', 'woocommerce' ),
'PGK' => __( 'Papua New Guinean kina', 'woocommerce' ),
'PHP' => __( 'Philippine peso', 'woocommerce' ),
'PKR' => __( 'Pakistani rupee', 'woocommerce' ),
'PLN' => __( 'Polish złoty', 'woocommerce' ),
'PRB' => __( 'Transnistrian ruble', 'woocommerce' ),
'PYG' => __( 'Paraguayan guaraní', 'woocommerce' ),
'QAR' => __( 'Qatari riyal', 'woocommerce' ),
'RON' => __( 'Romanian leu', 'woocommerce' ),
'RSD' => __( 'Serbian dinar', 'woocommerce' ),
'RUB' => __( 'Russian ruble', 'woocommerce' ),
'RWF' => __( 'Rwandan franc', 'woocommerce' ),
'SAR' => __( 'Saudi riyal', 'woocommerce' ),
'SBD' => __( 'Solomon Islands dollar', 'woocommerce' ),
'SCR' => __( 'Seychellois rupee', 'woocommerce' ),
'SDG' => __( 'Sudanese pound', 'woocommerce' ),
'SEK' => __( 'Swedish krona', 'woocommerce' ),
'SGD' => __( 'Singapore dollar', 'woocommerce' ),
'SHP' => __( 'Saint Helena pound', 'woocommerce' ),
'SLL' => __( 'Sierra Leonean leone', 'woocommerce' ),
'SOS' => __( 'Somali shilling', 'woocommerce' ),
'SRD' => __( 'Surinamese dollar', 'woocommerce' ),
'SSP' => __( 'South Sudanese pound', 'woocommerce' ),
'STD' => __( 'São Tomé and Príncipe dobra', 'woocommerce' ),
'SYP' => __( 'Syrian pound', 'woocommerce' ),
'SZL' => __( 'Swazi lilangeni', 'woocommerce' ),
'THB' => __( 'Thai baht', 'woocommerce' ),
'TJS' => __( 'Tajikistani somoni', 'woocommerce' ),
'TMT' => __( 'Turkmenistan manat', 'woocommerce' ),
'TND' => __( 'Tunisian dinar', 'woocommerce' ),
'TOP' => __( 'Tongan paʻanga', 'woocommerce' ),
'TRY' => __( 'Turkish lira', 'woocommerce' ),
'TTD' => __( 'Trinidad and Tobago dollar', 'woocommerce' ),
'TWD' => __( 'New Taiwan dollar', 'woocommerce' ),
'TZS' => __( 'Tanzanian shilling', 'woocommerce' ),
'UAH' => __( 'Ukrainian hryvnia', 'woocommerce' ),
'UGX' => __( 'Ugandan shilling', 'woocommerce' ),
'USD' => __( 'United States dollar', 'woocommerce' ),
'UYU' => __( 'Uruguayan peso', 'woocommerce' ),
'UZS' => __( 'Uzbekistani som', 'woocommerce' ),
'VEF' => __( 'Venezuelan bolívar', 'woocommerce' ),
'VND' => __( 'Vietnamese đồng', 'woocommerce' ),
'VUV' => __( 'Vanuatu vatu', 'woocommerce' ),
'WST' => __( 'Samoan tālā', 'woocommerce' ),
'XAF' => __( 'Central African CFA franc', 'woocommerce' ),
'XCD' => __( 'East Caribbean dollar', 'woocommerce' ),
'XOF' => __( 'West African CFA franc', 'woocommerce' ),
'XPF' => __( 'CFP franc', 'woocommerce' ),
'YER' => __( 'Yemeni rial', 'woocommerce' ),
'ZAR' => __( 'South African rand', 'woocommerce' ),
'ZMW' => __( 'Zambian kwacha', 'woocommerce' ),
)
)
);
}
/**
* Get Currency symbol.
*
* @param string $currency (default: '')
* @return string
*/
function get_woocommerce_currency_symbol( $currency = '' ) {
if ( ! $currency ) {
$currency = get_woocommerce_currency();
}
$symbols = apply_filters( 'woocommerce_currency_symbols', array(
'AED' => 'د.إ',
'AFN' => '؋',
'ALL' => 'L',
'AMD' => 'AMD',
'ANG' => 'ƒ',
'AOA' => 'Kz',
'ARS' => '$',
'AUD' => '$',
'AWG' => 'ƒ',
'AZN' => 'AZN',
'BAM' => 'KM',
'BBD' => '$',
'BDT' => '৳ ',
'BGN' => 'лв.',
'BHD' => '.د.ب',
'BIF' => 'Fr',
'BMD' => '$',
'BND' => '$',
'BOB' => 'Bs.',
'BRL' => 'R$',
'BSD' => '$',
'BTC' => '฿',
'BTN' => 'Nu.',
'BWP' => 'P',
'BYR' => 'Br',
'BZD' => '$',
'CAD' => '$',
'CDF' => 'Fr',
'CHF' => 'CHF',
'CLP' => '$',
'CNY' => '¥',
'COP' => '$',
'CRC' => '₡',
'CUC' => '$',
'CUP' => '$',
'CVE' => '$',
'CZK' => 'Kč',
'DJF' => 'Fr',
'DKK' => 'DKK',
'DOP' => 'RD$',
'DZD' => 'د.ج',
'EGP' => 'EGP',
'ERN' => 'Nfk',
'ETB' => 'Br',
'EUR' => '€',
'FJD' => '$',
'FKP' => '£',
'GBP' => '£',
'GEL' => 'ლ',
'GGP' => '£',
'GHS' => '₵',
'GIP' => '£',
'GMD' => 'D',
'GNF' => 'Fr',
'GTQ' => 'Q',
'GYD' => '$',
'HKD' => '$',
'HNL' => 'L',
'HRK' => 'Kn',
'HTG' => 'G',
'HUF' => 'Ft',
'IDR' => 'Rp',
'ILS' => '₪',
'IMP' => '£',
'INR' => '₹',
'IQD' => 'ع.د',
'IRR' => '﷼',
'ISK' => 'Kr.',
'JEP' => '£',
'JMD' => '$',
'JOD' => 'د.ا',
'JPY' => '¥',
'KES' => 'KSh',
'KGS' => 'лв',
'KHR' => '៛',
'KMF' => 'Fr',
'KPW' => '₩',
'KRW' => '₩',
'KWD' => 'د.ك',
'KYD' => '$',
'KZT' => 'KZT',
'LAK' => '₭',
'LBP' => 'ل.ل',
'LKR' => 'රු',
'LRD' => '$',
'LSL' => 'L',
'LYD' => 'ل.د',
'MAD' => 'د. م.',
'MAD' => 'د.م.',
'MDL' => 'L',
'MGA' => 'Ar',
'MKD' => 'ден',
'MMK' => 'Ks',
'MNT' => '₮',
'MOP' => 'P',
'MRO' => 'UM',
'MUR' => '₨',
'MVR' => '.ރ',
'MWK' => 'MK',
'MXN' => '$',
'MYR' => 'RM',
'MZN' => 'MT',
'NAD' => '$',
'NGN' => '₦',
'NIO' => 'C$',
'NOK' => 'kr',
'NPR' => '₨',
'NZD' => '$',
'OMR' => 'ر.ع.',
'PAB' => 'B/.',
'PEN' => 'S/.',
'PGK' => 'K',
'PHP' => '₱',
'PKR' => '₨',
'PLN' => 'zł',
'PRB' => 'р.',
'PYG' => '₲',
'QAR' => 'ر.ق',
'RMB' => '¥',
'RON' => 'lei',
'RSD' => 'дин.',
'RUB' => '₽',
'RWF' => 'Fr',
'SAR' => 'ر.س',
'SBD' => '$',
'SCR' => '₨',
'SDG' => 'ج.س.',
'SEK' => 'kr',
'SGD' => '$',
'SHP' => '£',
'SLL' => 'Le',
'SOS' => 'Sh',
'SRD' => '$',
'SSP' => '£',
'STD' => 'Db',
'SYP' => 'ل.س',
'SZL' => 'L',
'THB' => '฿',
'TJS' => 'ЅМ',
'TMT' => 'm',
'TND' => 'د.ت',
'TOP' => 'T$',
'TRY' => '₺',
'TTD' => '$',
'TWD' => 'NT$',
'TZS' => 'Sh',
'UAH' => '₴',
'UGX' => 'UGX',
'USD' => '$',
'UYU' => '$',
'UZS' => 'UZS',
'VEF' => 'Bs F',
'VND' => '₫',
'VUV' => 'Vt',
'WST' => 'T',
'XAF' => 'Fr',
'XCD' => '$',
'XOF' => 'Fr',
'XPF' => 'Fr',
'YER' => '﷼',
'ZAR' => 'R',
'ZMW' => 'ZK',
) );
$currency_symbol = isset( $symbols[ $currency ] ) ? $symbols[ $currency ] : '';
return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency );
}
/**
* Send HTML emails from WooCommerce.
*
* @param mixed $to
* @param mixed $subject
* @param mixed $message
* @param string $headers (default: "Content-Type: text/html\r\n")
* @param string $attachments (default: "")
*/
function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
$mailer = WC()->mailer();
$mailer->send( $to, $subject, $message, $headers, $attachments );
}
/**
* Get an image size.
*
* Variable is filtered by woocommerce_get_image_size_{image_size}.
*
* @param mixed $image_size
* @return array
*/
function wc_get_image_size( $image_size ) {
if ( is_array( $image_size ) ) {
$width = isset( $image_size[0] ) ? $image_size[0] : '300';
$height = isset( $image_size[1] ) ? $image_size[1] : '300';
$crop = isset( $image_size[2] ) ? $image_size[2] : 1;
$size = array(
'width' => $width,
'height' => $height,
'crop' => $crop,
);
$image_size = $width . '_' . $height;
} elseif ( in_array( $image_size, array( 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ) ) {
$size = get_option( $image_size . '_image_size', array() );
$size['width'] = isset( $size['width'] ) ? $size['width'] : '300';
$size['height'] = isset( $size['height'] ) ? $size['height'] : '300';
$size['crop'] = isset( $size['crop'] ) ? $size['crop'] : 0;
} else {
$size = array(
'width' => '300',
'height' => '300',
'crop' => 1,
);
}
return apply_filters( 'woocommerce_get_image_size_' . $image_size, $size );
}
/**
* Queue some JavaScript code to be output in the footer.
*
* @param string $code
*/
function wc_enqueue_js( $code ) {
global $wc_queued_js;
if ( empty( $wc_queued_js ) ) {
$wc_queued_js = '';
}
$wc_queued_js .= "\n" . $code . "\n";
}
/**
* Output any queued javascript code in the footer.
*/
function wc_print_js() {
global $wc_queued_js;
if ( ! empty( $wc_queued_js ) ) {
// Sanitize.
$wc_queued_js = wp_check_invalid_utf8( $wc_queued_js );
$wc_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $wc_queued_js );
$wc_queued_js = str_replace( "\r", '', $wc_queued_js );
$js = "<!-- WooCommerce JavaScript -->\n<script type=\"text/javascript\">\njQuery(function($) { $wc_queued_js });\n</script>\n";
/**
* woocommerce_queued_js filter.
*
* @since 2.6.0
* @param string $js JavaScript code.
*/
echo apply_filters( 'woocommerce_queued_js', $js );
unset( $wc_queued_js );
}
}
/**
* Set a cookie - wrapper for setcookie using WP constants.
*
* @param string $name Name of the cookie being set.
* @param string $value Value of the cookie.
* @param integer $expire Expiry of the cookie.
* @param string $secure Whether the cookie should be served only over https.
*/
function wc_setcookie( $name, $value, $expire = 0, $secure = false ) {
if ( ! headers_sent() ) {
setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure );
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
headers_sent( $file, $line );
trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE );
}
}
/**
* Get the URL to the WooCommerce REST API.
*
* @since 2.1
* @param string $path an endpoint to include in the URL.
* @return string the URL.
*/
function get_woocommerce_api_url( $path ) {
$version = defined( 'WC_API_REQUEST_VERSION' ) ? WC_API_REQUEST_VERSION : substr( WC_API::VERSION, 0, 1 );
$url = get_home_url( null, "wc-api/v{$version}/", is_ssl() ? 'https' : 'http' );
if ( ! empty( $path ) && is_string( $path ) ) {
$url .= ltrim( $path, '/' );
}
return $url;
}
/**
* Get a log file path.
*
* @since 2.2
* @param string $handle name.
* @return string the log file path.
*/
function wc_get_log_file_path( $handle ) {
return trailingslashit( WC_LOG_DIR ) . $handle . '-' . sanitize_file_name( wp_hash( $handle ) ) . '.log';
}
/**
* Recursively get page children.
* @param int $page_id
* @return int[]
*/
function wc_get_page_children( $page_id ) {
$page_ids = get_posts( array(
'post_parent' => $page_id,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any',
'fields' => 'ids',
) );
if ( ! empty( $page_ids ) ) {
foreach ( $page_ids as $page_id ) {
$page_ids = array_merge( $page_ids, wc_get_page_children( $page_id ) );
}
}
return $page_ids;
}
/**
* Flushes rewrite rules when the shop page (or it's children) gets saved.
*/
function flush_rewrite_rules_on_shop_page_save( $post_id ) {
$shop_page_id = wc_get_page_id( 'shop' );
if ( $shop_page_id === $post_id || in_array( $post_id, wc_get_page_children( $shop_page_id ) ) ) {
flush_rewrite_rules();
}
}
add_action( 'save_post', 'flush_rewrite_rules_on_shop_page_save' );
/**
* Various rewrite rule fixes.
*
* @since 2.2
* @param array $rules
* @return array
*/
function wc_fix_rewrite_rules( $rules ) {
global $wp_rewrite;
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
// Fix the rewrite rules when the product permalink have %product_cat% flag.
if ( preg_match( '`/(.+)(/%product_cat%)`' , $product_permalink, $matches ) ) {
foreach ( $rules as $rule => $rewrite ) {
if ( preg_match( '`^' . preg_quote( $matches[1], '`' ) . '/\(`', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) {
unset( $rules[ $rule ] );
}
}
}
// If the shop page is used as the base, we need to handle shop page subpages to avoid 404s.
if ( ! empty( $permalinks['use_verbose_page_rules'] ) && ( $shop_page_id = wc_get_page_id( 'shop' ) ) ) {
$page_rewrite_rules = array();
$subpages = wc_get_page_children( $shop_page_id );
// Subpage rules
foreach ( $subpages as $subpage ) {
$uri = get_page_uri( $subpage );
$page_rewrite_rules[ $uri . '/?$' ] = 'index.php?pagename=' . $uri;
$wp_generated_rewrite_rules = $wp_rewrite->generate_rewrite_rules( $uri, EP_PAGES, true, true, false, false );
foreach ( $wp_generated_rewrite_rules as $key => $value ) {
$wp_generated_rewrite_rules[ $key ] = $value . '&pagename=' . $uri;
}
$page_rewrite_rules = array_merge( $page_rewrite_rules, $wp_generated_rewrite_rules );
}
// Merge with rules
$rules = array_merge( $page_rewrite_rules, $rules );
}
return $rules;
}
add_filter( 'rewrite_rules_array', 'wc_fix_rewrite_rules' );
/**
* Prevent product attachment links from breaking when using complex rewrite structures.
*
* @param string $link
* @param id $post_id
* @return string
*/
function wc_fix_product_attachment_link( $link, $post_id ) {
global $wp_rewrite;
$post = get_post( $post_id );
if ( 'product' === get_post_type( $post->post_parent ) ) {
$permalinks = get_option( 'woocommerce_permalinks' );
$product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base'];
if ( preg_match( '/\/(.+)(\/%product_cat%)$/' , $product_permalink, $matches ) ) {
$link = home_url( '/?attachment_id=' . $post->ID );
}
}
return $link;
}
add_filter( 'attachment_link', 'wc_fix_product_attachment_link', 10, 2 );
/**
* Protect downloads from ms-files.php in multisite.
*
* @param mixed $rewrite
* @return string
*/
function wc_ms_protect_download_rewite_rules( $rewrite ) {
if ( ! is_multisite() || 'redirect' == get_option( 'woocommerce_file_download_method' ) ) {
return $rewrite;
}
$rule = "\n# WooCommerce Rules - Protect Files from ms-files.php\n\n";
$rule .= "<IfModule mod_rewrite.c>\n";
$rule .= "RewriteEngine On\n";
$rule .= "RewriteCond %{QUERY_STRING} file=woocommerce_uploads/ [NC]\n";
$rule .= "RewriteRule /ms-files.php$ - [F]\n";
$rule .= "</IfModule>\n\n";
return $rule . $rewrite;
}
add_filter( 'mod_rewrite_rules', 'wc_ms_protect_download_rewite_rules' );
/**
* WooCommerce Core Supported Themes.
*
* @since 2.2
* @return string[]
*/
function wc_get_core_supported_themes() {
return array( 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' );
}
/**
* Wrapper function to execute the `woocommerce_deliver_webhook_async` cron.
* hook, see WC_Webhook::process().
*
* @since 2.2
* @param int $webhook_id webhook ID to deliver.
* @param mixed $arg hook argument.
*/
function wc_deliver_webhook_async( $webhook_id, $arg ) {
$webhook = new WC_Webhook( $webhook_id );
$webhook->deliver( $arg );
}
add_action( 'woocommerce_deliver_webhook_async', 'wc_deliver_webhook_async', 10, 2 );
/**
* Formats a string in the format COUNTRY:STATE into an array.
*
* @since 2.3.0
* @param string $country_string
* @return array
*/
function wc_format_country_state_string( $country_string ) {
if ( strstr( $country_string, ':' ) ) {
list( $country, $state ) = explode( ':', $country_string );
} else {
$country = $country_string;
$state = '';
}
return array(
'country' => $country,
'state' => $state,
);
}
/**
* Get the store's base location.
*
* @todo should the woocommerce_default_country option be renamed to contain 'base'?
* @since 2.3.0
* @return array
*/
function wc_get_base_location() {
$default = apply_filters( 'woocommerce_get_base_location', get_option( 'woocommerce_default_country' ) );
return wc_format_country_state_string( $default );
}
/**
* Get the customer's default location.
*
* Filtered, and set to base location or left blank. If cache-busting,
* this should only be used when 'location' is set in the querystring.
*
* @todo should the woocommerce_default_country option be renamed to contain 'base'?
* @todo deprecate woocommerce_customer_default_location and support an array filter only to cover all cases.
* @since 2.3.0
* @return array
*/
function wc_get_customer_default_location() {
$location = array();
switch ( get_option( 'woocommerce_default_customer_address' ) ) {
case 'geolocation_ajax' :
case 'geolocation' :
// Exclude common bots from geolocation by user agent.
$ua = wc_get_user_agent();
if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) {
$location = WC_Geolocation::geolocate_ip( '', true, false );
}
// Base fallback.
if ( empty( $location['country'] ) ) {
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) );
}
break;
case 'base' :
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) );
break;
default :
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', '' ) );
break;
}
return apply_filters( 'woocommerce_customer_default_location_array', $location );
}
/**
* Get user agent string.
* @since 2.7.0
* @return string
*/
function wc_get_user_agent() {
return isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
}
// This function can be removed when WP 3.9.2 or greater is required.
if ( ! function_exists( 'hash_equals' ) ) :
/**
* Compare two strings in constant time.
*
* This function was added in PHP 5.6.
* It can leak the length of a string.
*
* @since 3.9.2
*
* @param string $a Expected string.
* @param string $b Actual string.
* @return bool Whether strings are equal.
*/
function hash_equals( $a, $b ) {
$a_length = strlen( $a );
if ( $a_length !== strlen( $b ) ) {
return false;
}