/var/www/(Del)hsihk.com/wp-content/plugins/woocommerce/includes/class-wc-ajax.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
<?php

if ( ! defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly
}

/**
 * WooCommerce WC_AJAX
 *
 * AJAX Event Handler
 *
 * @class         WC_AJAX
 * @version        2.2.0
 * @package        WooCommerce/Classes
 * @category    Class
 * @author         WooThemes
 */
class WC_AJAX {

    
/**
     * Hook in methods
     */
    
public static function init() {

        
// woocommerce_EVENT => nopriv
        
$ajax_events = array(
            
'get_refreshed_fragments'                          => true,
            
'apply_coupon'                                     => true,
            
'update_shipping_method'                           => true,
            
'update_order_review'                              => true,
            
'add_to_cart'                                      => true,
            
'checkout'                                         => true,
            
'feature_product'                                  => false,
            
'mark_order_complete'                              => false,
            
'mark_order_processing'                            => false,
            
'add_new_attribute'                                => false,
            
'remove_variation'                                 => false,
            
'remove_variations'                                => false,
            
'save_attributes'                                  => false,
            
'add_variation'                                    => false,
            
'link_all_variations'                              => false,
            
'revoke_access_to_download'                        => false,
            
'grant_access_to_download'                         => false,
            
'get_customer_details'                             => false,
            
'add_order_item'                                   => false,
            
'add_order_fee'                                    => false,
            
'add_order_shipping'                               => false,
            
'add_order_tax'                                    => false,
            
'remove_order_item'                                => false,
            
'remove_order_tax'                                 => false,
            
'reduce_order_item_stock'                          => false,
            
'increase_order_item_stock'                        => false,
            
'add_order_item_meta'                              => false,
            
'remove_order_item_meta'                           => false,
            
'calc_line_taxes'                                  => false,
            
'save_order_items'                                 => false,
            
'load_order_items'                                 => false,
            
'add_order_note'                                   => false,
            
'delete_order_note'                                => false,
            
'json_search_products'                             => false,
            
'json_search_products_and_variations'              => false,
            
'json_search_downloadable_products_and_variations' => false,
            
'json_search_customers'                            => false,
            
'term_ordering'                                    => false,
            
'product_ordering'                                 => false,
            
'refund_line_items'                                => false,
            
'delete_refund'                                    => false
        
);

        foreach ( 
$ajax_events as $ajax_event => $nopriv ) {
            
add_action'wp_ajax_woocommerce_' $ajax_event, array( __CLASS__$ajax_event ) );

            if ( 
$nopriv ) {
                
add_action'wp_ajax_nopriv_woocommerce_' $ajax_event, array( __CLASS__$ajax_event ) );
            }
        }

        
add_action'wp_ajax_page_slurp', array( 'WC_Gateway_Mijireh''page_slurp' ) );
    }

    
/**
     * Get a refreshed cart fragment
     */
    
public static function get_refreshed_fragments() {

        
// Get mini cart
        
ob_start();

        
woocommerce_mini_cart();

        
$mini_cart ob_get_clean();

        
// Fragments and mini cart are returned
        
$data = array(
            
'fragments' => apply_filters'add_to_cart_fragments', array(
                    
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' $mini_cart '</div>'
                
)
            ),
            
'cart_hash' => WC()->cart->get_cart() ? md5json_encodeWC()->cart->get_cart() ) ) : ''
        
);

        
wp_send_json$data );

    }

    
/**
     * AJAX apply coupon on checkout page
     */
    
public static function apply_coupon() {

        
check_ajax_referer'apply-coupon''security' );

        if ( ! empty( 
$_POST['coupon_code'] ) ) {
            
WC()->cart->add_discountsanitize_text_field$_POST['coupon_code'] ) );
        } else {
            
wc_add_noticeWC_Coupon::get_generic_coupon_errorWC_Coupon::E_WC_COUPON_PLEASE_ENTER ), 'error' );
        }

        
wc_print_notices();

        die();
    }

    
/**
     * AJAX update shipping method on cart page
     */
    
public static function update_shipping_method() {

        
check_ajax_referer'update-shipping-method''security' );

        if ( ! 
defined('WOOCOMMERCE_CART') ) {
            
define'WOOCOMMERCE_CART'true );
        }

        
$chosen_shipping_methods WC()->session->get'chosen_shipping_methods' );

        if ( isset( 
$_POST['shipping_method'] ) && is_array$_POST['shipping_method'] ) ) {
            foreach ( 
$_POST['shipping_method'] as $i => $value ) {
                
$chosen_shipping_methods$i ] = wc_clean$value );
            }
        }

        
WC()->session->set'chosen_shipping_methods'$chosen_shipping_methods );

        
WC()->cart->calculate_totals();

        
woocommerce_cart_totals();

        die();
    }

    
/**
     * AJAX update order review on checkout
     */
    
public static function update_order_review() {

        
check_ajax_referer'update-order-review''security' );

        if ( ! 
defined'WOOCOMMERCE_CHECKOUT' ) ) {
            
define'WOOCOMMERCE_CHECKOUT'true );
        }

        if ( 
== sizeofWC()->cart->get_cart() ) ) {
            echo 
'<div class="woocommerce-error">' __'Sorry, your session has expired.''woocommerce' ) . ' <a href="' home_url() . '" class="wc-backward">' __'Return to homepage''woocommerce' ) . '</a></div>';
            die();
        }

        
do_action'woocommerce_checkout_update_order_review'$_POST['post_data'] );

        
$chosen_shipping_methods WC()->session->get'chosen_shipping_methods' );

        if ( isset( 
$_POST['shipping_method'] ) && is_array$_POST['shipping_method'] ) ) {
            foreach ( 
$_POST['shipping_method'] as $i => $value ) {
                
$chosen_shipping_methods$i ] = wc_clean$value );
            }
        }

        
WC()->session->set'chosen_shipping_methods'$chosen_shipping_methods );
        
WC()->session->set'chosen_payment_method', empty( $_POST['payment_method'] ) ? '' $_POST['payment_method'] );

        if ( isset( 
$_POST['country'] ) ) {
            
WC()->customer->set_country$_POST['country'] );
        }

        if ( isset( 
$_POST['state'] ) ) {
            
WC()->customer->set_state$_POST['state'] );
        }

        if ( isset( 
$_POST['postcode'] ) ) {
            
WC()->customer->set_postcode$_POST['postcode'] );
        }

        if ( isset( 
$_POST['city'] ) ) {
            
WC()->customer->set_city$_POST['city'] );
        }

        if ( isset( 
$_POST['address'] ) ) {
            
WC()->customer->set_address$_POST['address'] );
        }

        if ( isset( 
$_POST['address_2'] ) ) {
            
WC()->customer->set_address_2$_POST['address_2'] );
        }

        if ( 
wc_ship_to_billing_address_only() ) {

            if ( isset( 
$_POST['country'] ) ) {
                
WC()->customer->set_shipping_country$_POST['country'] );
            }

            if ( isset( 
$_POST['state'] ) ) {
                
WC()->customer->set_shipping_state$_POST['state'] );
            }

            if ( isset( 
$_POST['postcode'] ) ) {
                
WC()->customer->set_shipping_postcode$_POST['postcode'] );
            }

            if ( isset( 
$_POST['city'] ) ) {
                
WC()->customer->set_shipping_city$_POST['city'] );
            }

            if ( isset( 
$_POST['address'] ) ) {
                
WC()->customer->set_shipping_address$_POST['address'] );
            }

            if ( isset( 
$_POST['address_2'] ) ) {
                
WC()->customer->set_shipping_address_2$_POST['address_2'] );
            }
        } else {

            if ( isset( 
$_POST['s_country'] ) ) {
                
WC()->customer->set_shipping_country$_POST['s_country'] );
            }

            if ( isset( 
$_POST['s_state'] ) ) {
                
WC()->customer->set_shipping_state$_POST['s_state'] );
            }

            if ( isset( 
$_POST['s_postcode'] ) ) {
                
WC()->customer->set_shipping_postcode$_POST['s_postcode'] );
            }

            if ( isset( 
$_POST['s_city'] ) ) {
                
WC()->customer->set_shipping_city$_POST['s_city'] );
            }

            if ( isset( 
$_POST['s_address'] ) ) {
                
WC()->customer->set_shipping_address$_POST['s_address'] );
            }

            if ( isset( 
$_POST['s_address_2'] ) ) {
                
WC()->customer->set_shipping_address_2$_POST['s_address_2'] );
            }
        }

        
WC()->cart->calculate_totals();

        
do_action'woocommerce_checkout_order_review'true ); // Display review order table

        
die();
    }

    
/**
     * AJAX add to cart
     */
    
public static function add_to_cart() {
        
ob_start();

        
$product_id        apply_filters'woocommerce_add_to_cart_product_id'absint$_POST['product_id'] ) );
        
$quantity          = empty( $_POST['quantity'] ) ? wc_stock_amount$_POST['quantity'] );
        
$passed_validation apply_filters'woocommerce_add_to_cart_validation'true$product_id$quantity );

        if ( 
$passed_validation && WC()->cart->add_to_cart$product_id$quantity ) ) {

            
do_action'woocommerce_ajax_added_to_cart'$product_id );

            if ( 
get_option'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
                
wc_add_to_cart_message$product_id );
            }

            
// Return fragments
            
self::get_refreshed_fragments();

        } else {

            
// If there was an error adding to the cart, redirect to the product page to show any errors
            
$data = array(
                
'error' => true,
                
'product_url' => apply_filters'woocommerce_cart_redirect_after_error'get_permalink$product_id ), $product_id )
            );

            
wp_send_json$data );

        }

        die();
    }

    
/**
     * Process ajax checkout form
     */
    
public static function checkout() {
        if ( ! 
defined'WOOCOMMERCE_CHECKOUT' ) ) {
            
define'WOOCOMMERCE_CHECKOUT'true );
        }

        
WC()->checkout()->process_checkout();

        die(
0);
    }

    
/**
     * Feature a product from admin
     */
    
public static function feature_product() {
        if ( ! 
current_user_can'edit_products' ) ) {
            
wp_die__'You do not have sufficient permissions to access this page.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        if ( ! 
check_admin_referer'woocommerce-feature-product' ) ) {
            
wp_die__'You have taken too long. Please go back and retry.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        
$post_id = ! empty( $_GET['product_id'] ) ? (int) $_GET['product_id'] : '';

        if ( ! 
$post_id || get_post_type$post_id ) !== 'product' ) {
            die;
        }

        
$featured get_post_meta$post_id'_featured'true );

        if ( 
'yes' === $featured ) {
            
update_post_meta$post_id'_featured''no' );
        } else {
            
update_post_meta$post_id'_featured''yes' );
        }

        
delete_transient'wc_featured_products' );

        
wp_safe_redirectwp_get_referer() ? remove_query_arg( array( 'trashed''untrashed''deleted''ids' ), wp_get_referer() ) : admin_url'edit.php?post_type=shop_order' ) );

        die();
    }

    
/**
     * Mark an order as complete
     */
    
public static function mark_order_complete() {
        if ( ! 
current_user_can'edit_shop_orders' ) ) {
            
wp_die__'You do not have sufficient permissions to access this page.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        if ( ! 
check_admin_referer'woocommerce-mark-order-complete' ) ) {
            
wp_die__'You have taken too long. Please go back and retry.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        
$order_id = isset( $_GET['order_id'] ) && (int) $_GET['order_id'] ? (int) $_GET['order_id'] : '';
        if ( ! 
$order_id ) {
            die();
        }

        
$order wc_get_order$order_id );
        
$order->update_status'completed' );

        
wp_safe_redirectwp_get_referer() ? wp_get_referer() : admin_url'edit.php?post_type=shop_order' ) );

        die();
    }

    
/**
     * Mark an order as processing
     */
    
public static function mark_order_processing() {
        if ( ! 
current_user_can'edit_shop_orders' ) ) {
            
wp_die__'You do not have sufficient permissions to access this page.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        if ( ! 
check_admin_referer'woocommerce-mark-order-processing' ) ) {
            
wp_die__'You have taken too long. Please go back and retry.''woocommerce' ), '', array( 'response' => 403 ) );
        }

        
$order_id = isset( $_GET['order_id'] ) && (int) $_GET['order_id'] ? (int) $_GET['order_id'] : '';
        if ( ! 
$order_id ) {
            die();
        }

        
$order wc_get_order$order_id );
        
$order->update_status'processing' );

        
wp_safe_redirectwp_get_referer() ? wp_get_referer() : admin_url'edit.php?post_type=shop_order' ) );

        die();
    }

    
/**
     * Add a new attribute via ajax function
     */
    
public static function add_new_attribute() {
        
ob_start();

        
check_ajax_referer'add-attribute''security' );

        
$taxonomy esc_attr$_POST['taxonomy'] );
        
$term     stripslashes$_POST['term'] );

        if ( 
taxonomy_exists$taxonomy ) ) {

            
$result wp_insert_term$term$taxonomy );

            if ( 
is_wp_error$result ) ) {
                
wp_send_json( array(
                    
'error' => $result->get_error_message()
                ) );
            } else {
                
wp_send_json( array(
                    
'term_id' => $result['term_id'],
                    
'name'    => $term,
                    
'slug'    => sanitize_title$term ),
                ) );
            }
        }

        die();
    }

    
/**
     * Delete variation via ajax function
     */
    
public static function remove_variation() {

        
check_ajax_referer'delete-variation''security' );

        
$variation_id intval$_POST['variation_id'] );
        
$variation get_post$variation_id );

        if ( 
$variation && 'product_variation' == $variation->post_type ) {
            
wp_delete_post$variation_id );
        }

        die();
    }

    
/**
     * Delete variations via ajax function
     */
    
public static function remove_variations() {

        
check_ajax_referer'delete-variations''security' );

        
$variation_ids = (array) $_POST['variation_ids'];

        foreach ( 
$variation_ids as $variation_id ) {
            
$variation get_post$variation_id );

            if ( 
$variation && 'product_variation' == $variation->post_type ) {
                
wp_delete_post$variation_id );
            }
        }

        die();
    }

    
/**
     * Save attributes via ajax
     */
    
public static function save_attributes() {

        
check_ajax_referer'save-attributes''security' );

        
// Get post data
        
parse_str$_POST['data'], $data );
        
$post_id absint$_POST['post_id'] );

        
// Save Attributes
        
$attributes = array();

        if ( isset( 
$data['attribute_names'] ) ) {

            
$attribute_names  array_map'stripslashes'$data['attribute_names'] );
            
$attribute_values = isset( $data['attribute_values'] ) ? $data['attribute_values'] : array();

            if ( isset( 
$data['attribute_visibility'] ) ) {
                
$attribute_visibility $data['attribute_visibility'];
            }

            if ( isset( 
$data['attribute_variation'] ) ) {
                
$attribute_variation $data['attribute_variation'];
            }

            
$attribute_is_taxonomy $data['attribute_is_taxonomy'];
            
$attribute_position    $data['attribute_position'];
            
$attribute_names_count sizeof$attribute_names );

            for ( 
$i 0$i $attribute_names_count$i++ ) {
                if ( ! 
$attribute_names$i ] ) {
                    continue;
                }

                
$is_visible   = isset( $attribute_visibility$i ] ) ? 0;
                
$is_variation = isset( $attribute_variation$i ] ) ? 0;
                
$is_taxonomy  $attribute_is_taxonomy$i ] ? 0;

                if ( 
$is_taxonomy ) {

                    if ( isset( 
$attribute_values$i ] ) ) {

                        
// Select based attributes - Format values (posted values are slugs)
                        
if ( is_array$attribute_values$i ] ) ) {
                            
$values array_map'sanitize_title'$attribute_values$i ] );

                        
// Text based attributes - Posted values are term names - don't change to slugs
                        
} else {
                            
$values array_map'stripslashes'array_map'strip_tags'explodeWC_DELIMITER$attribute_values$i ] ) ) );
                        }

                        
// Remove empty items in the array
                        
$values array_filter$values'strlen' );

                    } else {
                        
$values = array();
                    }

                    
// Update post terms
                    
if ( taxonomy_exists$attribute_names$i ] ) ) {
                        
wp_set_object_terms$post_id$values$attribute_names$i ] );
                    }

                    if ( 
$values ) {
                        
// Add attribute to array, but don't set values
                        
$attributessanitize_title$attribute_names$i ] ) ] = array(
                            
'name'             => wc_clean$attribute_names$i ] ),
                            
'value'         => '',
                            
'position'         => $attribute_position$i ],
                            
'is_visible'     => $is_visible,
                            
'is_variation'     => $is_variation,
                            
'is_taxonomy'     => $is_taxonomy
                        
);
                    }

                } elseif ( isset( 
$attribute_values$i ] ) ) {

                    
// Text based, separate by pipe
                    
$values implode' ' WC_DELIMITER ' 'array_map'wc_clean'array_map'stripslashes'explodeWC_DELIMITER$attribute_values$i ] ) ) ) );

                    
// Custom attribute - Add attribute to array and set the values
                    
$attributessanitize_title$attribute_names$i ] ) ] = array(
                        
'name'             => wc_clean$attribute_names$i ] ),
                        
'value'         => $values,
                        
'position'         => $attribute_position$i ],
                        
'is_visible'     => $is_visible,
                        
'is_variation'     => $is_variation,
                        
'is_taxonomy'     => $is_taxonomy
                    
);
                }

             }
        }

        if ( ! 
function_exists'attributes_cmp' ) ) {
            function 
attributes_cmp$a$b ) {
                if ( 
$a['position'] == $b['position'] ) {
                    return 
0;
                }

                return ( 
$a['position'] < $b['position'] ) ? -1;
            }
        }
        
uasort$attributes'attributes_cmp' );

        
update_post_meta$post_id'_product_attributes'$attributes );

        die();
    }

    
/**
     * Add variation via ajax function
     */
    
public static function add_variation() {

        
check_ajax_referer'add-variation''security' );

        
$post_id intval$_POST['post_id'] );
        
$loop intval$_POST['loop'] );

        
$variation = array(
            
'post_title'   => 'Product #' $post_id ' Variation',
            
'post_content' => '',
            
'post_status'  => 'publish',
            
'post_author'  => get_current_user_id(),
            
'post_parent'  => $post_id,
            
'post_type'    => 'product_variation'
        
);

        
$variation_id wp_insert_post$variation );

        
do_action'woocommerce_create_product_variation'$variation_id );

        if ( 
$variation_id ) {

            
$variation_post_status 'publish';
            
$variation_data get_post_meta$variation_id );
            
$variation_data['variation_post_id'] = $variation_id;

            
// Get attributes
            
$attributes = (array) maybe_unserializeget_post_meta$post_id'_product_attributes'true ) );

            
// Get tax classes
            
$tax_classes                 array_filter(array_map('trim'explode("\n"get_option('woocommerce_tax_classes'))));
            
$tax_class_options           = array();
            
$tax_class_options['parent'] =__'Same as parent''woocommerce' );
            
$tax_class_options['']       = __'Standard''woocommerce' );

            if ( 
$tax_classes ) {
                foreach ( 
$tax_classes as $class ) {
                    
$tax_class_optionssanitize_title$class ) ] = $class;
                }
            }

            
$backorder_options = array(
                
'no'     => __'Do not allow''woocommerce' ),
                
'notify' => __'Allow, but notify customer''woocommerce' ),
                
'yes'    => __'Allow''woocommerce' )
            );

            
$stock_status_options = array(
                
'instock'    => __'In stock''woocommerce' ),
                
'outofstock' => __'Out of stock''woocommerce' )
            );

            
// Get parent data
            
$parent_data = array(
                
'id'                   => $post_id,
                
'attributes'           => $attributes,
                
'tax_class_options'    => $tax_class_options,
                
'sku'                  => get_post_meta$post_id'_sku'true ),
                
'weight'               => get_post_meta$post_id'_weight'true ),
                
'length'               => get_post_meta$post_id'_length'true ),
                
'width'                => get_post_meta$post_id'_width'true ),
                
'height'               => get_post_meta$post_id'_height'true ),
                
'tax_class'            => get_post_meta$post_id'_tax_class'true ),
                
'backorder_options'    => $backorder_options,
                
'stock_status_options' => $stock_status_options
            
);

            if ( ! 
$parent_data['weight'] ) {
                
$parent_data['weight'] = '0.00';
            }

            if ( ! 
$parent_data['length'] ) {
                
$parent_data['length'] = '0';
            }

            if ( ! 
$parent_data['width'] ) {
                
$parent_data['width'] = '0';
            }

            if ( ! 
$parent_data['height'] ) {
                
$parent_data['height'] = '0';
            }

            
$_tax_class          '';
            
$_downloadable_files '';
            
$_stock_status       '';
            
$_backorders         '';
            
$image_id            0;
            
$variation           get_post$variation_id ); // Get the variation object

            
include( 'admin/meta-boxes/views/html-variation-admin.php' );
        }

        die();
    }

    
/**
     * Link all variations via ajax function
     */
    
public static function link_all_variations() {

        if ( ! 
defined'WC_MAX_LINKED_VARIATIONS' ) ) {
            
define'WC_MAX_LINKED_VARIATIONS'49 );
        }

        
check_ajax_referer'link-variations''security' );

        @
set_time_limit(0);

        
$post_id intval$_POST['post_id'] );

        if ( ! 
$post_id ) {
            die();
        }

        
$variations = array();
        
$_product   wc_get_product$post_id, array( 'product_type' => 'variable' ) );

        
// Put variation attributes into an array
        
foreach ( $_product->get_attributes() as $attribute ) {

            if ( ! 
$attribute['is_variation'] ) {
                continue;
            }

            
$attribute_field_name 'attribute_' sanitize_title$attribute['name'] );

            if ( 
$attribute['is_taxonomy'] ) {
                
$options wc_get_product_terms$post_id$attribute['name'], array( 'fields' => 'names' ) );
            } else {
                
$options explodeWC_DELIMITER$attribute['value'] );
            }

            
$options array_map'sanitize_title'array_map'trim'$options ) );

            
$variations$attribute_field_name ] = $options;
        }

        
// Quit out if none were found
        
if ( sizeof$variations ) == ) {
            die();
        }

        
// Get existing variations so we don't create duplicates
        
$available_variations = array();

        foreach( 
$_product->get_children() as $child_id ) {
            
$child $_product->get_child$child_id );

            if ( ! empty( 
$child->variation_id ) ) {
                
$available_variations[] = $child->get_variation_attributes();
            }
        }

        
// Created posts will all have the following data
        
$variation_post_data = array(
            
'post_title'   => 'Product #' $post_id ' Variation',
            
'post_content' => '',
            
'post_status'  => 'publish',
            
'post_author'  => get_current_user_id(),
            
'post_parent'  => $post_id,
            
'post_type'    => 'product_variation'
        
);

        
// Now find all combinations and create posts
        
if ( ! function_exists'array_cartesian' ) ) {

            
/**
             * @param array $input
             * @return array
             */
            
function array_cartesian$input ) {
                
$result = array();

                while ( list( 
$key$values ) = each$input ) ) {
                    
// If a sub-array is empty, it doesn't affect the cartesian product
                    
if ( empty( $values ) ) {
                        continue;
                    }

                    
// Special case: seeding the product array with the values from the first sub-array
                    
if ( empty( $result ) ) {
                        foreach ( 
$values as $value ) {
                            
$result[] = array( $key => $value );
                        }
                    }
                    else {
                        
// Second and subsequent input sub-arrays work like this:
                        //   1. In each existing array inside $product, add an item with
                        //      key == $key and value == first item in input sub-array
                        //   2. Then, for each remaining item in current input sub-array,
                        //      add a copy of each existing array inside $product with
                        //      key == $key and value == first item in current input sub-array

                        // Store all items to be added to $product here; adding them on the spot
                        // inside the foreach will result in an infinite loop
                        
$append = array();
                        foreach ( 
$result as &$product ) {
                            
// Do step 1 above. array_shift is not the most efficient, but it
                            // allows us to iterate over the rest of the items with a simple
                            // foreach, making the code short and familiar.
                            
$product$key ] = array_shift$values );

                            
// $product is by reference (that's why the key we added above
                            // will appear in the end result), so make a copy of it here
                            
$copy $product;

                            
// Do step 2 above.
                            
foreach ( $values as $item ) {
                                
$copy$key ] = $item;
                                
$append[] = $copy;
                            }

                            
// Undo the side effecst of array_shift
                            
array_unshift$values$product$key ] );
                        }

                        
// Out of the foreach, we can add to $results now
                        
$result array_merge$result$append );
                    }
                }

                return 
$result;
            }
        }

        
$variation_ids       = array();
        
$added               0;
        
$possible_variations array_cartesian$variations );

        foreach ( 
$possible_variations as $variation ) {

            
// Check if variation already exists
            
if ( in_array$variation$available_variations ) ) {
                continue;
            }

            
$variation_id wp_insert_post$variation_post_data );

            
$variation_ids[] = $variation_id;

            foreach ( 
$variation as $key => $value ) {
                
update_post_meta$variation_id$key$value );
            }

            
$added++;

            
do_action'product_variation_linked'$variation_id );

            if ( 
$added WC_MAX_LINKED_VARIATIONS ) {
                break;
            }
        }

        
delete_transient'wc_product_children_ids_' $post_id );

        echo 
$added;

        die();
    }

    
/**
     * Delete download permissions via ajax function
     */
    
public static function revoke_access_to_download() {

        
check_ajax_referer'revoke-access''security' );

        global 
$wpdb;

        
$download_id $_POST['download_id'];
        
$product_id  intval$_POST['product_id'] );
        
$order_id    intval$_POST['order_id'] );

        
$wpdb->query$wpdb->prepare"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %s;"$order_id$product_id$download_id ) );

        
do_action'woocommerce_ajax_revoke_access_to_product_download'$download_id$product_id$order_id );

        die();
    }

    
/**
     * Grant download permissions via ajax function
     */
    
public static function grant_access_to_download() {

        
check_ajax_referer'grant-access''security' );

        global 
$wpdb;

        
$wpdb->hide_errors();

        
$order_id     intval$_POST['order_id'] );
        
$product_ids  $_POST['product_ids'];
        
$loop         intval$_POST['loop'] );
        
$file_counter 0;
        
$order        wc_get_order$order_id );

        if ( ! 
is_array$product_ids ) ) {
            
$product_ids = array( $product_ids );
        }

        foreach ( 
$product_ids as $product_id ) {
            
$product wc_get_product$product_id );
            
$files   $product->get_files();

            if ( ! 
$order->billing_email ) {
                die();
            }

            if ( 
$files ) {
                foreach ( 
$files as $download_id => $file ) {
                    if ( 
$inserted_id wc_downloadable_file_permission$download_id$product_id$order ) ) {

                        
// insert complete - get inserted data
                        
$download $wpdb->get_row$wpdb->prepare"SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d"$inserted_id ) );

                        
$loop ++;
                        
$file_counter ++;

                        if ( isset( 
$file['name'] ) ) {
                            
$file_count $file['name'];
                        } else {
                            
$file_count sprintf__'File %d''woocommerce' ), $file_counter );
                        }
                        include( 
'admin/meta-boxes/views/html-order-download-permission.php' );
                    }
                }
            }
        }

        die();
    }

    
/**
     * Get customer details via ajax
     */
    
public static function get_customer_details() {
        
ob_start();

        
check_ajax_referer'get-customer-details''security' );

        
$user_id      = (int) trim(stripslashes($_POST['user_id']));
        
$type_to_load esc_attr(trim(stripslashes($_POST['type_to_load'])));

        
$customer_data = array(
            
$type_to_load '_first_name' => get_user_meta$user_id$type_to_load '_first_name'true ),
            
$type_to_load '_last_name'  => get_user_meta$user_id$type_to_load '_last_name'true ),
            
$type_to_load '_company'    => get_user_meta$user_id$type_to_load '_company'true ),
            
$type_to_load '_address_1'  => get_user_meta$user_id$type_to_load '_address_1'true ),
            
$type_to_load '_address_2'  => get_user_meta$user_id$type_to_load '_address_2'true ),
            
$type_to_load '_city'       => get_user_meta$user_id$type_to_load '_city'true ),
            
$type_to_load '_postcode'   => get_user_meta$user_id$type_to_load '_postcode'true ),
            
$type_to_load '_country'    => get_user_meta$user_id$type_to_load '_country'true ),
            
$type_to_load '_state'      => get_user_meta$user_id$type_to_load '_state'true ),
            
$type_to_load '_email'      => get_user_meta$user_id$type_to_load '_email'true ),
            
$type_to_load '_phone'      => get_user_meta$user_id$type_to_load '_phone'true ),
        );

        
$customer_data apply_filters'woocommerce_found_customer_details'$customer_data );

        
wp_send_json$customer_data );

    }

    
/**
     * Add order item via ajax
     */
    
public static function add_order_item() {
        
check_ajax_referer'order-item''security' );

        
$item_to_add sanitize_text_field$_POST['item_to_add'] );
        
$order_id    absint$_POST['order_id'] );

        
// Find the item
        
if ( ! is_numeric$item_to_add ) ) {
            die();
        }

        
$post get_post$item_to_add );

        if ( ! 
$post || ( 'product' !== $post->post_type && 'product_variation' !== $post->post_type ) ) {
            die();
        }

        
$_product    wc_get_product$post->ID );
        
$order       wc_get_order$order_id );
        
$order_taxes $order->get_taxes();
        
$class       'new_row';

        
// Set values
        
$item = array();

        
$item['product_id']        = $_product->id;
        
$item['variation_id']      = isset( $_product->variation_id ) ? $_product->variation_id '';
        
$item['variation_data']    = $item['variation_id'] ? $_product->get_variation_attributes() : '';
        
$item['name']              = $_product->get_title();
        
$item['tax_class']         = $_product->get_tax_class();
        
$item['qty']               = 1;
        
$item['line_subtotal']     = wc_format_decimal$_product->get_price_excluding_tax() );
        
$item['line_subtotal_tax'] = '';
        
$item['line_total']        = wc_format_decimal$_product->get_price_excluding_tax() );
        
$item['line_tax']          = '';

        
// Add line item
        
$item_id wc_add_order_item$order_id, array(
            
'order_item_name'         => $item['name'],
            
'order_item_type'         => 'line_item'
        
) );

        
// Add line item meta
        
if ( $item_id ) {
            
wc_add_order_item_meta$item_id'_qty'$item['qty'] );
            
wc_add_order_item_meta$item_id'_tax_class'$item['tax_class'] );
            
wc_add_order_item_meta$item_id'_product_id'$item['product_id'] );
            
wc_add_order_item_meta$item_id'_variation_id'$item['variation_id'] );
            
wc_add_order_item_meta$item_id'_line_subtotal'$item['line_subtotal'] );
            
wc_add_order_item_meta$item_id'_line_subtotal_tax'$item['line_subtotal_tax'] );
            
wc_add_order_item_meta$item_id'_line_total'$item['line_total'] );
            
wc_add_order_item_meta$item_id'_line_tax'$item['line_tax'] );

            
// Since 2.2
            
wc_add_order_item_meta$item_id'_line_tax_data', array( 'total' => array(), 'subtotal' => array() ) );

            
// Store variation data in meta
            
if ( $item['variation_data'] && is_array$item['variation_data'] ) ) {
                foreach ( 
$item['variation_data'] as $key => $value ) {
                    
wc_add_order_item_meta$item_idstr_replace'attribute_'''$key ), $value );
                }
            }

            
do_action'woocommerce_ajax_add_order_item_meta'$item_id$item );
        }

        
$item          apply_filters'woocommerce_ajax_order_item'$item$item_id );

        include( 
'admin/meta-boxes/views/html-order-item.php' );

        
// Quit out
        
die();
    }

    
/**
     * Add order fee via ajax
     */
    
public static function add_order_fee() {

        
check_ajax_referer'order-item''security' );

        
$order_id      absint$_POST['order_id'] );
        
$order         wc_get_order$order_id );
        
$order_taxes   $order->get_taxes();
        
$item          = array();

        
// Add new fee
        
$fee            = new stdClass();
        
$fee->name      '';
        
$fee->tax_class '';
        
$fee->taxable   $fee->tax_class !== '0';
        
$fee->amount    '';
        
$fee->tax       '';
        
$fee->tax_data  = array();
        
$item_id        $order->add_fee$fee );

        include( 
'admin/meta-boxes/views/html-order-fee.php' );

        
// Quit out
        
die();
    }

    
/**
     * Add order shipping cost via ajax
     */
    
public static function add_order_shipping() {

        
check_ajax_referer'order-item''security' );

        
$order_id         absint$_POST['order_id'] );
        
$order            wc_get_order$order_id );
        
$order_taxes      $order->get_taxes();
        
$shipping_methods WC()->shipping() ? WC()->shipping->load_shipping_methods() : array();
        
$item             = array();

        
// Add new shipping
        
$shipping        = new stdClass();
        
$shipping->label '';
        
$shipping->id    '';
        
$shipping->cost  '';
        
$shipping->taxes = array();
        
$item_id         $order->add_shipping$shipping );

        include( 
'admin/meta-boxes/views/html-order-shipping.php' );

        
// Quit out
        
die();
    }

    
/**
     * Add order tax column via ajax
     */
    
public static function add_order_tax() {
        global 
$wpdb;

        
check_ajax_referer'order-item''security' );

        
$order_id absint$_POST['order_id'] );
        
$rate_id  absint$_POST['rate_id'] );
        
$order    = new WC_Order$order_id );
        
$data     get_post_meta$order_id );

        
// Add new tax
        
$order->add_tax$rate_id0);

        
// Return HTML items
        
include( 'admin/meta-boxes/views/html-order-items.php' );

        die();
    }

    
/**
     * Remove an order item
     */
    
public static function remove_order_item() {
        
check_ajax_referer'order-item''security' );

        
$order_item_ids $_POST['order_item_ids'];

        if ( ! 
is_array$order_item_ids ) && is_numeric$order_item_ids ) ) {
            
$order_item_ids = array( $order_item_ids );
        }

        if ( 
sizeof$order_item_ids ) > ) {
            foreach( 
$order_item_ids as $id ) {
                
wc_delete_order_itemabsint$id ) );
            }
        }

        die();
    }

    
/**
     * Remove an order tax
     */
    
public static function remove_order_tax() {

        
check_ajax_referer'order-item''security' );

        
$order_id absint$_POST['order_id'] );
        
$rate_id  absint$_POST['rate_id'] );

        
wc_delete_order_item$rate_id );

        
// Return HTML items
        
$order = new WC_Order$order_id );
        
$data  get_post_meta$order_id );
        include( 
'admin/meta-boxes/views/html-order-items.php' );

        die();
    }

    
/**
     * Reduce order item stock
     */
    
public static function reduce_order_item_stock() {
        
check_ajax_referer'order-item''security' );

        
$order_id       absint$_POST['order_id'] );
        
$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
        
$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
        
$order          wc_get_order$order_id );
        
$order_items    $order->get_items();
        
$return         = array();

        if ( 
$order && ! empty( $order_items ) && sizeof$order_item_ids ) > ) {

            foreach ( 
$order_items as $item_id => $order_item ) {

                
// Only reduce checked items
                
if ( ! in_array$item_id$order_item_ids ) ) {
                    continue;
                }

                
$_product $order->get_product_from_item$order_item );

                if ( 
$_product->exists() && $_product->managing_stock() && isset( $order_item_qty$item_id ] ) && $order_item_qty$item_id ] > ) {
                    
$stock_change apply_filters'woocommerce_reduce_order_stock_quantity'$order_item_qty$item_id ], $item_id );
                    
$new_stock    $_product->reduce_stock$stock_change );

                    
$return[] = sprintf__'Item #%s stock reduced from %s to %s.''woocommerce' ), $order_item['product_id'], $new_stock $stock_change$new_stock );
                    
$order->add_order_notesprintf__'Item #%s stock reduced from %s to %s.''woocommerce' ), $order_item['product_id'], $new_stock $stock_change$new_stock ) );
                    
$order->send_stock_notifications$_product$new_stock$order_item_qty$item_id ] );
                }
            }

            
do_action'woocommerce_reduce_order_stock'$order );

            if ( empty( 
$return ) ) {
                
$return[] = __'No products had their stock reduced - they may not have stock management enabled.''woocommerce' );
            }

            echo 
implode', '$return );
        }

        die();
    }

    
/**
     * Increase order item stock
     */
    
public static function increase_order_item_stock() {
        
check_ajax_referer'order-item''security' );

        
$order_id       absint$_POST['order_id'] );
        
$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
        
$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
        
$order          wc_get_order$order_id );
        
$order_items    $order->get_items();
        
$return         = array();

        if ( 
$order && ! empty( $order_items ) && sizeof$order_item_ids ) > ) {

            foreach ( 
$order_items as $item_id => $order_item ) {

                
// Only reduce checked items
                
if ( ! in_array$item_id$order_item_ids ) ) {
                    continue;
                }

                
$_product $order->get_product_from_item$order_item );

                if ( 
$_product->exists() && $_product->managing_stock() && isset( $order_item_qty$item_id ] ) && $order_item_qty$item_id ] > ) {

                    
$old_stock    $_product->stock;
                    
$stock_change apply_filters'woocommerce_restore_order_stock_quantity'$order_item_qty$item_id ], $item_id );
                    
$new_quantity $_product->increase_stock$stock_change );

                    
$return[] = sprintf__'Item #%s stock increased from %s to %s.''woocommerce' ), $order_item['product_id'], $old_stock$new_quantity );
                    
$order->add_order_notesprintf__'Item #%s stock increased from %s to %s.''woocommerce' ), $order_item['product_id'], $old_stock$new_quantity ) );
                }
            }

            
do_action'woocommerce_restore_order_stock'$order );

            if ( empty( 
$return ) ) {
                
$return[] = __'No products had their stock increased - they may not have stock management enabled.''woocommerce' );
            }

            echo 
implode', '$return );
        }

        die();
    }

    
/**
     * Add some meta to a line item
     */
    
public static function add_order_item_meta() {
        
check_ajax_referer'order-item''security' );

        
$meta_id wc_add_order_item_metaabsint$_POST['order_item_id'] ), __'Name''woocommerce' ), __'Value''woocommerce' ) );

        if ( 
$meta_id ) {
            echo 
'<tr data-meta_id="' esc_attr$meta_id ) . '"><td><input type="text" name="meta_key[' $meta_id ']" /><textarea name="meta_value[' $meta_id ']"></textarea></td><td width="1%"><button class="remove_order_item_meta button">&times;</button></td></tr>';
        }

        die();
    }

    
/**
     * Remove meta from a line item
     */
    
public static function remove_order_item_meta() {
        global 
$wpdb;

        
check_ajax_referer'order-item''security' );

        
$meta_id absint$_POST['meta_id'] );

        
$wpdb->query$wpdb->prepare"DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_id = %d"$meta_id ) );

        die();
    }

    
/**
     * Calc line tax
     */
    
public static function calc_line_taxes() {
        global 
$wpdb;

        
check_ajax_referer'calc-totals''security' );

        
$tax            = new WC_Tax();
        
$order_id       absint$_POST['order_id'] );
        
$items          = array();
        
$country        strtoupperesc_attr$_POST['country'] ) );
        
$state          strtoupperesc_attr$_POST['state'] ) );
        
$postcode       strtoupperesc_attr$_POST['postcode'] ) );
        
$city           sanitize_titleesc_attr$_POST['city'] ) );
        
$order          wc_get_order$order_id );
        
$taxes          = array();
        
$shipping_taxes = array();

        
// Parse the jQuery serialized items
        
parse_str$_POST['items'], $items );

        
// Prevent undefined warnings
        
if ( ! isset( $items['line_tax'] ) ) {
            
$items['line_tax'] = array();
        }
        if ( ! isset( 
$items['line_subtotal_tax'] ) ) {
            
$items['line_subtotal_tax'] = array();
        }
        
$items['order_taxes'] = array();

        
// Get items and fees taxes
        
if ( isset( $items['order_item_id'] ) ) {

            
$get_values = array( 'order_item_id''line_subtotal''line_total''order_item_tax_class' );

            foreach ( 
$get_values as $value ) {
                $
$value = isset( $items$value ] ) ? $items$value ] : array();
            }

            foreach ( 
$order_item_id as $item_id ) {
                
$item_id                          absint$item_id );
                
$line_total$item_id ]           = isset( $line_total$item_id ] ) ? wc_format_decimal$line_total$item_id ] ) : 0;
                
$line_subtotal$item_id ]        = isset( $line_subtotal$item_id ] ) ? wc_format_decimal$line_subtotal$item_id ] ) : $line_total$item_id ];
                
$order_item_tax_class$item_id ] = isset( $order_item_tax_class$item_id ] ) ? sanitize_text_field$order_item_tax_class$item_id ] ) : '';
                
$product_id                       $order->get_item_meta$item_id'_product_id'true );

                
// Get product details
                
if ( get_post_type$product_id ) == 'product' ) {
                    
$_product        wc_get_product$product_id );
                    
$item_tax_status $_product->get_tax_status();
                } else {
                    
$item_tax_status 'taxable';
                }

                if ( 
'0' !== $order_item_tax_class$item_id ] && 'taxable' === $item_tax_status ) {
                    
$tax_rates WC_Tax::find_rates( array(
                        
'country'   => $country,
                        
'state'     => $state,
                        
'postcode'  => $postcode,
                        
'city'      => $city,
                        
'tax_class' => $order_item_tax_class$item_id ]
                    ) );

                    
$line_taxes          WC_Tax::calc_tax$line_total$item_id ], $tax_ratesfalse );
                    
$line_subtotal_taxes WC_Tax::calc_tax$line_subtotal$item_id ], $tax_ratesfalse );

                    
// Set the new line_tax
                    
foreach ( $line_taxes as $_tax_id => $_tax_value ) {
                        
$items['line_tax'][ $item_id ][ $_tax_id ] = $_tax_value;
                    }

                    
// Set the new line_subtotal_tax
                    
foreach ( $line_subtotal_taxes as $_tax_id => $_tax_value ) {
                        
$items['line_subtotal_tax'][ $item_id ][ $_tax_id ] = $_tax_value;
                    }

                    
// Sum the item taxes
                    
foreach ( array_keys$taxes $line_taxes ) as $key ) {
                        
$taxes$key ] = ( isset( $line_taxes$key ] ) ? $line_taxes$key ] : ) + ( isset( $taxes$key ] ) ? $taxes$key ] : );
                    }
                }
            }
        }

        
// Get shipping taxes
        
if ( isset( $items['shipping_method_id'] ) ) {
            
$matched_tax_rates = array();

            
$tax_rates WC_Tax::find_rates( array(
                
'country'   => $country,
                
'state'     => $state,
                
'postcode'  => $postcode,
                
'city'      => $city,
                
'tax_class' => ''
            
) );

            if ( 
$tax_rates ) {
                foreach ( 
$tax_rates as $key => $rate ) {
                    if ( isset( 
$rate['shipping'] ) && 'yes' == $rate['shipping'] ) {
                        
$matched_tax_rates$key ] = $rate;
                    }
                }
            }

            
$get_values = array( 'shipping_method_id''shipping_cost''shipping_taxes' );

            foreach ( 
$get_values as $value ) {
                $
$value = isset( $items$value ] ) ? $items$value ] : array();
            }

            foreach ( 
$shipping_method_id as $item_id ) {
                
$item_id                   absint$item_id );
                
$shipping_cost$item_id ] = isset( $shipping_cost$item_id ] ) ? wc_format_decimal$shipping_cost$item_id ] ) : 0;
                
$shipping_taxes            WC_Tax::calc_shipping_tax$shipping_cost$item_id ], $matched_tax_rates );

                
// Set the new shipping_taxes
                
foreach ( $shipping_taxes as $_tax_id => $_tax_value ) {
                    
$items['shipping_taxes'][ $item_id ][ $_tax_id ] = $_tax_value;
                }
            }
        }

        
// Remove old tax rows
        
$order->remove_order_items'tax' );

        
// Add tax rows
        
foreach ( array_keys$taxes $shipping_taxes ) as $tax_rate_id ) {
            
$order->add_tax$tax_rate_id, isset( $taxes$tax_rate_id ] ) ? $taxes$tax_rate_id ] : 0, isset( $shipping_taxes$tax_rate_id ] ) ? $shipping_taxes$tax_rate_id ] : );
        }

        
// Create the new order_taxes
        
foreach ( $order->get_taxes() as $tax_id => $tax_item ) {
            
$items['order_taxes'][ $tax_id ] = absint$tax_item['rate_id'] );
        }

        
// Save order items
        
wc_save_order_items$order_id$items );

        
// Return HTML items
        
$order = new WC_Order$order_id );
        
$data  get_post_meta$order_id );
        include( 
'admin/meta-boxes/views/html-order-items.php' );

        die();
    }

    
/**
     * Save order items via ajax
     */
    
public static function save_order_items() {
        
check_ajax_referer'order-item''security' );

        if ( isset( 
$_POST['order_id'] ) && isset( $_POST['items'] ) ) {
            
$order_id absint$_POST['order_id'] );

            
// Parse the jQuery serialized items
            
$items = array();
            
parse_str$_POST['items'], $items );

            
// Save order items
            
wc_save_order_items$order_id$items );

            
// Return HTML items
            
$order = new WC_Order$order_id );
            
$data  get_post_meta$order_id );
            include( 
'admin/meta-boxes/views/html-order-items.php' );
        }

        die();
    }

    
/**
     * Load order items via ajax
     */
    
public static function load_order_items() {
        
check_ajax_referer'order-item''security' );

        
// Return HTML items
        
$order_id absint$_POST['order_id'] );
        
$order    = new WC_Order$order_id );
        
$data     get_post_meta$order_id );
        include( 
'admin/meta-boxes/views/html-order-items.php' );

        die();
    }

    
/**
     * Add order note via ajax
     */
    
public static function add_order_note() {

        
check_ajax_referer'add-order-note''security' );

        
$post_id   = (int) $_POST['post_id'];
        
$note      wp_kses_posttrimstripslashes$_POST['note'] ) ) );
        
$note_type $_POST['note_type'];

        
$is_customer_note $note_type == 'customer' 0;

        if ( 
$post_id ) {
            
$order      wc_get_order$post_id );
            
$comment_id $order->add_order_note$note$is_customer_note );

            echo 
'<li rel="' esc_attr$comment_id ) . '" class="note ';
            if ( 
$is_customer_note ) {
                echo 
'customer-note';
            }
            echo 
'"><div class="note_content">';
            echo 
wpautopwptexturize$note ) );
            echo 
'</div><p class="meta"><a href="#" class="delete_note">'.__'Delete note''woocommerce' ).'</a></p>';
            echo 
'</li>';
        }

        
// Quit out
        
die();
    }

    
/**
     * Delete order note via ajax
     */
    
public static function delete_order_note() {

        
check_ajax_referer'delete-order-note''security' );

        
$note_id = (int) $_POST['note_id'];

        if ( 
$note_id ) {
            
wp_delete_comment$note_id );
        }

        
// Quit out
        
die();
    }

    
/**
     * Search for products and echo json
     *
     * @param string $x (default: '')
     * @param string $post_types (default: array('product'))
     */
    
public static function json_search_products$x ''$post_types = array('product') ) {
        
ob_start();

        
check_ajax_referer'search-products''security' );

        
$term = (string) wc_cleanstripslashes$_GET['term'] ) );

        if ( empty( 
$term ) ) {
            die();
        }

        if ( 
is_numeric$term ) ) {

            
$args = array(
                
'post_type'      => $post_types,
                
'post_status'    => 'publish',
                
'posts_per_page' => -1,
                
'post__in'       => array(0$term),
                
'fields'         => 'ids'
            
);

            
$args2 = array(
                
'post_type'      => $post_types,
                
'post_status'    => 'publish',
                
'posts_per_page' => -1,
                
'post_parent'    => $term,
                
'fields'         => 'ids'
            
);

            
$args3 = array(
                
'post_type'      => $post_types,
                
'post_status'    => 'publish',
                
'posts_per_page' => -1,
                
'meta_query'     => array(
                    array(
                        
'key'     => '_sku',
                        
'value'   => $term,
                        
'compare' => 'LIKE'
                    
)
                ),
                
'fields'         => 'ids'
            
);

            
$posts array_uniquearray_mergeget_posts$args ), get_posts$args2 ), get_posts$args3 ) ) );

        } else {

            
$args = array(
                
'post_type'      => $post_types,
                
'post_status'    => 'publish',
                
'posts_per_page' => -1,
                
's'              => $term,
                
'fields'         => 'ids'
            
);

            
$args2 = array(
                
'post_type'      => $post_types,
                
'post_status'    => 'publish',
                
'posts_per_page' => -1,
                
'meta_query'     => array(
                    array(
                    
'key'     => '_sku',
                    
'value'   => $term,
                    
'compare' => 'LIKE'
                    
)
                ),
                
'fields'         => 'ids'
            
);

            
$posts array_uniquearray_mergeget_posts$args ), get_posts$args2 ) ) );

        }

        
$found_products = array();

        if ( 
$posts ) {
            foreach ( 
$posts as $post ) {
                
$product wc_get_product$post );

                
$found_products$post ] = $product->get_formatted_name();
            }
        }

        
$found_products apply_filters'woocommerce_json_search_found_products'$found_products );

        
wp_send_json$found_products );

    }

    
/**
     * Search for product variations and return json
     *
     * @access public
     * @return void
     * @see WC_AJAX::json_search_products()
     */
    
public static function json_search_products_and_variations() {
        
self::json_search_products'', array('product''product_variation') );
    }

    
/**
     * Search for customers and return json
     */
    
public static function json_search_customers() {
        
ob_start();

        
check_ajax_referer'search-customers''security' );

        
$term wc_cleanstripslashes$_GET['term'] ) );

        if ( empty( 
$term ) ) {
            die();
        }

        
$default = isset( $_GET['default'] ) ? $_GET['default'] : __'Guest''woocommerce' );

        
$found_customers = array( '' => $default );

        
add_action'pre_user_query', array( __CLASS__'json_search_customer_name' ) );

        
$customers_query = new WP_User_Queryapply_filters'woocommerce_json_search_customers_query', array(
            
'fields'         => 'all',
            
'orderby'        => 'display_name',
            
'search'         => '*' $term '*',
            
'search_columns' => array( 'ID''user_login''user_email''user_nicename' )
        ) ) );

        
remove_action'pre_user_query', array( __CLASS__'json_search_customer_name' ) );

        
$customers $customers_query->get_results();

        if ( 
$customers ) {
            foreach ( 
$customers as $customer ) {
                
$found_customers$customer->ID ] = $customer->display_name ' (#' $customer->ID ' &ndash; ' sanitize_email$customer->user_email ) . ')';
            }
        }

        
wp_send_json$found_customers );

    }

    
/**
     * Search for downloadable product variations and return json
     *
     * @access public
     * @return void
     * @see WC_AJAX::json_search_products()
     */
    
public static function json_search_downloadable_products_and_variations() {
        
ob_start();

        
$term = (string) wc_cleanstripslashes$_GET['term'] ) );

        
$args = array(
            
'post_type'      => array( 'product''product_variation' ),
            
'posts_per_page' => -1,
            
'post_status'    => 'publish',
            
'order'          => 'ASC',
            
'orderby'        => 'parent title',
            
'meta_query'     => array(
                array(
                    
'key'   => '_downloadable',
                    
'value' => 'yes'
                
)
            ),
            
's'              => $term
        
);

        
$posts get_posts$args );
        
$found_products = array();

        if ( 
$posts ) {
            foreach ( 
$posts as $post ) {
                
$product wc_get_product$post->ID );
                
$found_products$post->ID ] = $product->get_formatted_name();
            }
        }

        
wp_send_json$found_products );

    }

    
/**
     * When searching using the WP_User_Query, search names (user meta) too
     * @param  object $query
     * @return object
     */
    
public static function json_search_customer_name$query ) {
        global 
$wpdb;

        
$term wc_cleanstripslashes$_GET['term'] ) );
        if ( 
method_exists$wpdb'esc_like' ) ) {
            
$term $wpdb->esc_like$term );
        } else {
            
$term like_escape$term );
        }

        
$query->query_from  .= " INNER JOIN {$wpdb->usermeta} AS user_name ON {$wpdb->users}.ID = user_name.user_id AND ( user_name.meta_key = 'first_name' OR user_name.meta_key = 'last_name' ) ";
        
$query->query_where .= $wpdb->prepare" OR user_name.meta_value LIKE %s "'%' $term '%' );
    }

    
/**
     * Ajax request handling for categories ordering
     */
    
public static function term_ordering() {
        
$id       = (int) $_POST['id'];
        
$next_id  = isset( $_POST['nextid'] ) && (int) $_POST['nextid'] ? (int) $_POST['nextid'] : null;
        
$taxonomy = isset( $_POST['thetaxonomy'] ) ? esc_attr$_POST['thetaxonomy'] ) : null;
        
$term     get_term_by('id'$id$taxonomy);

        if ( ! 
$id || ! $term || ! $taxonomy ) {
            die(
0);
        }

        
wc_reorder_terms$term$next_id$taxonomy );

        
$children get_terms$taxonomy"child_of=$id&menu_order=ASC&hide_empty=0" );

        if ( 
$term && sizeof$children ) ) {
            echo 
'children';
            die();
        }
    }

    
/**
     * Ajax request handling for product ordering
     *
     * Based on Simple Page Ordering by 10up (http://wordpress.org/extend/plugins/simple-page-ordering/)
     */
    
public static function product_ordering() {
        global 
$wpdb;

        
ob_start();

        
// check permissions again and make sure we have what we need
        
if ( ! current_user_can('edit_products') || empty( $_POST['id'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) ) {
            die(-
1);
        }

        
// real post?
        
if ( ! $post get_post$_POST['id'] ) ) {
            die(-
1);
        }

        
$previd  = isset( $_POST['previd'] ) ? $_POST['previd'] : false;
        
$nextid  = isset( $_POST['nextid'] ) ? $_POST['nextid'] : false;
        
$new_pos = array(); // store new positions for ajax

        
$siblings $wpdb->get_results$wpdb->prepare('
            SELECT ID, menu_order FROM %1$s AS posts
            WHERE     posts.post_type     = \'product\'
            AND     posts.post_status     IN ( \'publish\', \'pending\', \'draft\', \'future\', \'private\' )
            AND     posts.ID            NOT IN (%2$d)
            ORDER BY posts.menu_order ASC, posts.ID DESC
        '
$wpdb->posts$post->ID) );

        
$menu_order 0;

        foreach ( 
$siblings as $sibling ) {

            
// if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order
            
if ( $nextid == $sibling->ID ) {
                
$wpdb->update(
                    
$wpdb->posts,
                    array(
                        
'menu_order' => $menu_order
                    
),
                    array( 
'ID' => $post->ID ),
                    array( 
'%d' ),
                    array( 
'%d' )
                );
                
$new_pos$post->ID ] = $menu_order;
                
$menu_order++;
            }

            
// if repositioned post has been set, and new items are already in the right order, we can stop
            
if ( isset( $new_pos$post->ID ] ) && $sibling->menu_order >= $menu_order ) {
                break;
            }

            
// set the menu order of the current sibling and increment the menu order
            
$wpdb->update(
                
$wpdb->posts,
                array(
                    
'menu_order' => $menu_order
                
),
                array( 
'ID' => $sibling->ID ),
                array( 
'%d' ),
                array( 
'%d' )
            );
            
$new_pos$sibling->ID ] = $menu_order;
            
$menu_order++;

            if ( ! 
$nextid && $previd == $sibling->ID ) {
                
$wpdb->update(
                    
$wpdb->posts,
                    array(
                        
'menu_order' => $menu_order
                    
),
                    array( 
'ID' => $post->ID ),
                    array( 
'%d' ),
                    array( 
'%d' )
                );
                
$new_pos[$post->ID] = $menu_order;
                
$menu_order++;
            }

        }

        
do_action'woocommerce_after_product_ordering' );

        
wp_send_json$new_pos );
    }

    
/**
     * Handle a refund via the edit order screen
     */
    
public static function refund_line_items() {
        
ob_start();

        
check_ajax_referer'order-item''security' );

        
$order_id               absint$_POST['order_id'] );
        
$refund_amount          wc_format_decimalsanitize_text_field$_POST['refund_amount'] ) );
        
$refund_reason          sanitize_text_field$_POST['refund_reason'] );
        
$line_item_qtys         json_decodesanitize_text_fieldstripslashes$_POST['line_item_qtys'] ) ), true );
        
$line_item_totals       json_decodesanitize_text_fieldstripslashes$_POST['line_item_totals'] ) ), true );
        
$line_item_tax_totals   json_decodesanitize_text_fieldstripslashes$_POST['line_item_tax_totals'] ) ), true );
        
$api_refund             $_POST['api_refund'] === 'true' true false;
        
$restock_refunded_items $_POST['restock_refunded_items'] === 'true' true false;
        
$refund                 false;

        try {
            
// Validate that the refund can occur
            
$order       wc_get_order$order_id );
            
$order_items $order->get_items();
            
$max_refund  $order->get_total() - $order->get_total_refunded();

            if ( ! 
$refund_amount || $max_refund $refund_amount ) {
                throw new 
exception__'Invalid refund amount''woocommerce' ) );
            }

            
// Prepare line items which we are refunding
            
$line_items = array();
            
$item_ids   array_uniquearray_mergearray_keys$line_item_qtys$line_item_totals ) ) );

            foreach ( 
$item_ids as $item_id ) {
                
$line_items$item_id ] = array( 'qty' => 0'refund_total' => 0'refund_tax' => array() );
            }
            foreach ( 
$line_item_qtys as $item_id => $qty ) {
                
$line_items$item_id ]['qty'] = max$qty);

                if ( 
$restock_refunded_items && $qty && isset( $order_items$item_id ] ) ) {
                    
$order_item $order_items$item_id ];
                    
$_product   $order->get_product_from_item$order_item );

                    if ( 
$_product && $_product->exists() && $_product->managing_stock() ) {
                        
$old_stock    $_product->stock;
                        
$new_quantity $_product->increase_stock$qty );

                        
$order->add_order_notesprintf__'Item #%s stock increased from %s to %s.''woocommerce' ), $order_item['product_id'], $old_stock$new_quantity ) );
                    }
                }
            }
            foreach ( 
$line_item_totals as $item_id => $total ) {
                
$line_items$item_id ]['refund_total'] = wc_format_decimal$total );
            }
            foreach ( 
$line_item_tax_totals as $item_id => $tax_totals ) {
                
$line_items$item_id ]['refund_tax'] = array_map'wc_format_decimal'$tax_totals );
            }

            
// Create the refund object
            
$refund wc_create_refund( array(
                
'amount'     => $refund_amount,
                
'reason'     => $refund_reason,
                
'order_id'   => $order_id,
                
'line_items' => $line_items
            
) );

            if ( 
is_wp_error$refund ) ) {
                throw new 
Exception$refund->get_error_message() );
            }

            
// Refund via API
            
if ( $api_refund ) {
                if ( 
WC()->payment_gateways() ) {
                    
$payment_gateways WC()->payment_gateways->payment_gateways();
                }
                if ( isset( 
$payment_gateways$order->payment_method ] ) && $payment_gateways$order->payment_method ]->supports'refunds' ) ) {
                    
$result $payment_gateways$order->payment_method ]->process_refund$order_id$refund_amount$refund_reason );

                    if ( 
is_wp_error$result ) ) {
                        throw new 
Exception$result->get_error_message() );
                    } elseif ( ! 
$result ) {
                        throw new 
Exception__'Refund failed''woocommerce' ) );
                    }
                }
            }

            
// Clear transients
            
wc_delete_shop_order_transients$order_id );

            
wp_send_jsontrue );

        } catch ( 
Exception $e ) {
            if ( 
$refund && is_a$refund'WC_Order_Refund' ) ) {
                
wp_delete_post$refund->idtrue );
            }
            
wp_send_json( array( 'error' => $e->getMessage() ) );
        }
    }

    
/**
     * Delete a refund
     */
    
public static function delete_refund() {
        
check_ajax_referer'order-item''security' );

        
$refund_id absint$_POST['refund_id'] );

        if ( 
$refund_id && 'shop_order_refund' === get_post_type$refund_id ) ) {
            
wc_delete_shop_order_transientswp_get_post_parent_id$refund_id ) );
            
wp_delete_post$refund_id );
        }

        die();
    }
}

WC_AJAX::init();