class-wc-subscriptions-manager.php
101 KB
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
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
<?php
/**
* Subscriptions Management Class
*
* An API of Subscription utility functions and Account Management functions.
*
* Subscription activation and cancellation functions are hooked directly to order status changes
* so your payment gateway only needs to work with WooCommerce APIs. You can however call other
* management functions directly when necessary.
*
* @package WooCommerce Subscriptions
* @subpackage WC_Subscriptions_Manager
* @category Class
* @author Brent Shepherd
* @since 1.0
*/
class WC_Subscriptions_Manager {
/**
* The database key for user's subscriptions.
*
* @since 1.0
*/
public static $users_meta_key = 'woocommerce_subscriptions';
/**
* Set up the class, including it's hooks & filters, when the file is loaded.
*
* @since 1.0
**/
public static function init() {
// When an order's status is changed, run the appropriate subscription function
add_action( 'woocommerce_order_status_cancelled', __CLASS__ . '::cancel_subscriptions_for_order' );
add_action( 'woocommerce_order_status_failed', __CLASS__ . '::failed_subscription_sign_ups_for_order' );
add_action( 'woocommerce_order_status_on-hold', __CLASS__ . '::put_subscription_on_hold_for_order' );
// Expire a user's subscription
add_action( 'woocommerce_scheduled_subscription_expiration', __CLASS__ . '::expire_subscription', 10, 1 );
// Expire a user's subscription
add_action( 'woocommerce_scheduled_subscription_end_of_prepaid_term', __CLASS__ . '::subscription_end_of_prepaid_term', 10, 1 );
// Check if the subscription needs to use the failed payment process to repair its status
add_action( 'woocommerce_scheduled_subscription_payment', __CLASS__ . '::maybe_process_failed_renewal_for_repair', 0, 1 );
// Whenever a renewal payment is due, put the subscription on hold and create a renewal order before anything else, in case things don't go to plan
add_action( 'woocommerce_scheduled_subscription_payment', __CLASS__ . '::prepare_renewal', 1, 1 );
// Order is trashed, trash subscription
add_action( 'wp_trash_post', __CLASS__ . '::maybe_trash_subscription', 10 );
// When a user is being deleted from the site, via standard WordPress functions, make sure their subscriptions are cancelled
add_action( 'delete_user', __CLASS__ . '::trash_users_subscriptions' );
// Do the same thing for WordPress networks
add_action( 'wpmu_delete_user', __CLASS__ . '::trash_users_subscriptions_for_network' );
// make sure a subscription is cancelled before it is trashed/deleted
add_action( 'wp_trash_post', __CLASS__ . '::maybe_cancel_subscription', 10, 1 );
add_action( 'before_delete_post', __CLASS__ . '::maybe_cancel_subscription', 10, 1 );
// set correct status to restore after a subscription is trashed/deleted
add_action( 'trashed_post', __CLASS__ . '::fix_trash_meta_status' );
// call special hooks when a subscription is trashed/deleted
add_action( 'trashed_post', __CLASS__ . '::trigger_subscription_trashed_hook' );
add_action( 'deleted_post', __CLASS__ . '::trigger_subscription_deleted_hook' );
}
/**
* Sets up renewal for subscriptions managed by Subscriptions.
*
* This function is hooked early on the scheduled subscription payment hook and will:
* - place the subscription on-hold
* - create a renewal order, with the pending status if it requires payment, or processing/complete
* if the recurring total is $0.
*
* @param int $subscription_id The ID of a 'shop_subscription' post
* @since 2.0
*/
public static function prepare_renewal( $subscription_id ) {
$subscription = wcs_get_subscription( $subscription_id );
if ( empty( $subscription ) || ! $subscription->has_status( 'active' ) ) {
return false;
}
// If the subscription is using manual payments, the gateway isn't active or it manages scheduled payments
if ( 0 == $subscription->get_total() || $subscription->is_manual() || empty( $subscription->payment_method ) || ! $subscription->payment_method_supports( 'gateway_scheduled_payments' ) ) {
// Always put the subscription on hold in case something goes wrong while trying to process renewal
$subscription->update_status( 'on-hold', _x( 'Subscription renewal payment due:', 'used in order note as reason for why subscription status changed', 'woocommerce-subscriptions' ) );
// Generate a renewal order for payment gateways to use to record the payment (and determine how much is due)
$renewal_order = wcs_create_renewal_order( $subscription );
if ( is_wp_error( $renewal_order ) ) {
// let's try this again
$renewal_order = wcs_create_renewal_order( $subscription );
if ( is_wp_error( $renewal_order ) ) {
throw new Exception( __( 'Error: Unable to create renewal order from scheduled payment. Please try again.', 'woocommerce-subscriptions' ) );
}
}
if ( 0 == $subscription->get_total() ) {
$renewal_order->payment_complete();
$subscription->update_status( 'active' ); // don't call payment_complete() because technically, no payment was received
} else {
if ( $subscription->is_manual() ) {
do_action( 'woocommerce_generated_manual_renewal_order', $renewal_order->id );
} else {
$renewal_order->set_payment_method( $subscription->payment_gateway );
}
}
}
}
/**
* Expires a single subscription on a users account.
*
* @param int $subscription_id The ID of a 'shop_subscription' post
* @since 1.0
*/
public static function expire_subscription( $subscription_id, $deprecated = null ) {
if ( null !== $deprecated ) {
_deprecated_argument( __METHOD__, '2.0', 'The subscription key is deprecated. Use a subscription post ID' );
$subscription = wcs_get_subscription_from_key( $deprecated );
} else {
$subscription = wcs_get_subscription( $subscription_id );
}
$subscription->update_status( 'expired' );
}
/**
* Fires when a cancelled subscription reaches the end of its prepaid term.
*
* @param int $subscription_id The ID of a 'shop_subscription' post
* @since 1.3
*/
public static function subscription_end_of_prepaid_term( $subscription_id, $deprecated = null ) {
if ( null !== $deprecated ) {
_deprecated_argument( __METHOD__, '2.0', 'The subscription key is deprecated. Use a subscription post ID' );
$subscription = wcs_get_subscription_from_key( $deprecated );
} else {
$subscription = wcs_get_subscription( $subscription_id );
}
$subscription->update_status( 'cancelled' );
}
/**
* Records a payment on a subscription.
*
* @param int $user_id The id of the user who owns the subscription.
* @param string $subscription_key A subscription key of the form obtained by @see get_subscription_key( $order_id, $product_id )
* @since 1.0
*/
public static function process_subscription_payment( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::payment_complete()' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$subscription->payment_complete();
// Reset failed payment count & suspension count
$subscription = array(); // we only want to reset the failed payments and susp count
$subscription['failed_payments'] = $subscription['suspension_count'] = 0;
self::update_users_subscriptions( $user_id, array( $subscription_key => $subscription ) );
}
/**
* Processes a failed payment on a subscription by recording the failed payment and cancelling the subscription if it exceeds the
* maximum number of failed payments allowed on the site.
*
* @param int $user_id The id of the user who owns the expiring subscription.
* @param string $subscription_key A subscription key of the form obtained by @see get_subscription_key( $order_id, $product_id )
* @since 1.0
*/
public static function process_subscription_payment_failure( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::payment_failed()' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
// Allow a short circuit for plugins & payment gateways to force max failed payments exceeded
if ( apply_filters( 'woocommerce_subscriptions_max_failed_payments_exceeded', false, $user_id, $subscription_key ) ) {
$new_status = 'cancelled';
} else {
$new_status = 'on-hold';
}
$subscription->payment_failed( $new_status );
// Reset failed payment count & suspension count
$subscription = array(); // we only want to reset the failed payments and susp count
$subscription['failed_payments'] = $subscription['failed_payments'] + 1;
self::update_users_subscriptions( $user_id, array( $subscription_key => $subscription ) );
}
/**
* This function should be called whenever a subscription payment is made on an order. This includes
* when the subscriber signs up and for a recurring payment.
*
* The function is a convenience wrapper for @see self::process_subscription_payment(), so if calling that
* function directly, do not call this function also.
*
* @param WC_Order|int $order The order or ID of the order for which subscription payments should be marked against.
* @since 1.0
*/
public static function process_subscription_payments_on_order( $order, $product_id = '' ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
$subscription->payment_complete();
}
do_action( 'processed_subscription_payments_for_order', $order );
}
}
/**
* This function should be called whenever a subscription payment has failed.
*
* The function is a convenience wrapper for @see self::process_subscription_payment_failure(), so if calling that
* function directly, do not call this function also.
*
* @param int|WC_Order $order The order or ID of the order for which subscription payments should be marked against.
* @since 1.0
*/
public static function process_subscription_payment_failure_on_order( $order, $product_id = '' ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
$subscription->payment_failed();
}
do_action( 'processed_subscription_payment_failure_for_order', $order );
}
}
/**
* Activates all the subscriptions created by a given order.
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as activated.
* @since 1.0
*/
public static function activate_subscriptions_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
$subscription->update_status( 'active' );
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to activate subscription status for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'subscriptions_activated_for_order', $order );
}
}
/**
* Suspends all the subscriptions on an order by changing their status to "on-hold".
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as activated.
* @since 1.0
*/
public static function put_subscription_on_hold_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, array( 'order_type' => 'parent' ) );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
if ( ! $subscription->has_status( wcs_get_subscription_ended_statuses() ) ) {
$subscription->update_status( 'on-hold' );
}
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to update subscription status after order #%1$s was put on-hold: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'subscriptions_put_on_hold_for_order', $order );
}
}
/**
* Mark all subscriptions in an order as cancelled on the user's account.
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as cancelled.
* @since 1.0
*/
public static function cancel_subscriptions_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, array( 'order_type' => 'parent' ) );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
if ( ! $subscription->has_status( wcs_get_subscription_ended_statuses() ) ) {
$subscription->cancel_order();
}
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to cancel subscription after order #%1$s was cancelled: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'subscriptions_cancelled_for_order', $order );
}
}
/**
* Marks all the subscriptions in an order as expired
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as expired.
* @since 1.0
*/
public static function expire_subscriptions_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
try {
if ( ! $subscription->has_status( wcs_get_subscription_ended_statuses() ) ) {
$subscription->update_status( 'expired' );
}
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to set subscription as expired for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'subscriptions_expired_for_order', $order );
}
}
/**
* Called when a sign up fails during the payment processing step.
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as failed.
* @since 1.0
*/
public static function failed_subscription_sign_ups_for_order( $order ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, array( 'order_type' => 'parent' ) );
if ( ! empty( $subscriptions ) ) {
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
// Set subscription status to failed and log failure
if ( $order->has_status( 'failed' ) ) {
$order->update_status( 'failed', __( 'Subscription sign up failed.', 'woocommerce-subscriptions' ) );
}
foreach ( $subscriptions as $subscription ) {
try {
$subscription->payment_failed();
} catch ( Exception $e ) {
// translators: $1: order number, $2: error message
$subscription->add_order_note( sprintf( __( 'Failed to process failed payment on subscription for order #%1$s: %2$s', 'woocommerce-subscriptions' ), is_object( $order ) ? $order->get_order_number() : $order, $e->getMessage() ) );
}
}
do_action( 'failed_subscription_sign_ups_for_order', $order );
}
}
/**
* Uses the details of an order to create a pending subscription on the customers account
* for a subscription product, as specified with $product_id.
*
* @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
* @param int $product_id The ID of the subscription product on the order, if a variation, it must be the variation's ID.
* @param array $args An array of name => value pairs to customise the details of the subscription, including:
* 'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
* 'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
* @since 1.1
*/
public static function create_pending_subscription_for_order( $order, $product_id, $args = array() ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_create_subscription()' );
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
if ( ! WC_Subscriptions_Product::is_subscription( $product_id ) ) {
return;
}
$args = wp_parse_args( $args, array(
'start_date' => get_gmt_from_date( $order->order_date ),
'expiry_date' => '',
) );
$billing_period = WC_Subscriptions_Product::get_period( $product_id );
$billing_interval = WC_Subscriptions_Product::get_interval( $product_id );
// Support passing timestamps
$args['start_date'] = is_numeric( $args['start_date'] ) ? date( 'Y-m-d H:i:s', $args['start_date'] ) : $args['start_date'];
$product = wc_get_product( $product_id );
// Check if there is already a subscription for this product and order
$subscriptions = wcs_get_subscriptions( array( 'order_id' => $order->id, 'product_id' => $product_id ) );
if ( ! empty( $subscriptions ) ) {
$subscription = array_pop( $subscriptions );
// Make sure the subscription is pending and start date is set correctly
wp_update_post( array(
'ID' => $subscription->id,
'post_status' => 'wc-' . apply_filters( 'woocommerce_default_subscription_status', 'pending' ),
'post_date' => get_date_from_gmt( $args['start_date'] ),
) );
} else {
$subscription = wcs_create_subscription( array(
'start_date' => get_date_from_gmt( $args['start_date'] ),
'order_id' => $order->id,
'customer_id' => $order->get_user_id(),
'billing_period' => $billing_period,
'billing_interval' => $billing_interval,
'customer_note' => $order->customer_note,
) );
if ( is_wp_error( $subscription ) ) {
throw new Exception( __( 'Error: Unable to create subscription. Please try again.', 'woocommerce-subscriptions' ) );
}
$item_id = $subscription->add_product(
$product,
1,
array(
'variation' => ( method_exists( $product, 'get_variation_attributes' ) ) ? $product->get_variation_attributes() : array(),
'totals' => array(
'subtotal' => $product->get_price(),
'subtotal_tax' => 0,
'total' => $product->get_price(),
'tax' => 0,
'tax_data' => array( 'subtotal' => array(), 'total' => array() ),
),
)
);
if ( ! $item_id ) {
throw new Exception( __( 'Error: Unable to add product to created subscription. Please try again.', 'woocommerce-subscriptions' ) );
}
}
// Make sure some of the meta is copied form the order rather than the store's defaults
update_post_meta( $subscription->id, '_order_currency', $order->order_currency );
update_post_meta( $subscription->id, '_prices_include_tax', $order->prices_include_tax );
// Adding a new subscription so set the expiry date/time from the order date
if ( ! empty( $args['expiry_date'] ) ) {
if ( is_numeric( $args['expiry_date'] ) ) {
$args['expiry_date'] = date( 'Y-m-d H:i:s', $args['expiry_date'] );
}
$expiration = $args['expiry_date'];
} else {
$expiration = WC_Subscriptions_Product::get_expiration_date( $product_id, $args['start_date'] );
}
// Adding a new subscription so set the expiry date/time from the order date
$trial_expiration = WC_Subscriptions_Product::get_trial_expiration_date( $product_id, $args['start_date'] );
$dates_to_update = array();
if ( $trial_expiration > 0 ) {
$dates_to_update['trial_end'] = $trial_expiration;
}
if ( $expiration > 0 ) {
$dates_to_update['end'] = $expiration;
}
if ( ! empty( $dates_to_update ) ) {
$subscription->update_dates( $dates_to_update );
}
// Set the recurring totals on the subscription
$subscription->set_total( 0, 'tax' );
$subscription->set_total( $product->get_price(), 'total' );
$subscription->add_order_note( __( 'Pending subscription created.', 'woocommerce-subscriptions' ) );
do_action( 'pending_subscription_created_for_order', $order, $product_id );
}
/**
* Creates subscriptions against a users account with a status of pending when a user creates
* an order containing subscriptions.
*
* @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
* @since 1.0
*/
public static function process_subscriptions_on_checkout( $order ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions_Checkout::process_checkout()' );
if ( ! empty( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) {
WC_Subscriptions_Checkout::process_checkout( $order, $_POST );
}
}
/**
* Updates a user's subscriptions for each subscription product in the order.
*
* @param WC_Order $order The order to get subscriptions and user details from.
* @param string $status (optional) A status to change the subscriptions in an order to. Default is 'active'.
* @since 1.0
*/
public static function update_users_subscriptions_for_order( $order, $status = 'pending' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions::update_status()' );
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
if ( 'suspend' === $status ) {
$status = 'on-hold';
_deprecated_argument( __METHOD__, '1.2', 'The "suspend" status value is deprecated. Use "on-hold"' );
}
foreach ( wcs_get_subscriptions_for_order( $order->id, array( 'order_type' => 'parent' ) ) as $subscription_id => $subscription ) {
switch ( $status ) {
case 'cancelled' :
$subscription->cancel_order();
break;
case 'active' :
case 'expired' :
case 'on-hold' :
$subscription->update_status( $status );
break;
case 'failed' :
_deprecated_argument( __METHOD__, '2.0', 'The "failed" status value is deprecated.' );
self::failed_subscription_signup( $order->user_id, $subscription_id );
break;
case 'pending' :
_deprecated_argument( __METHOD__, '2.0', 'The "pending" status value is deprecated.' );
default :
self::create_pending_subscription_for_order( $order );
break;
}
}
do_action( 'updated_users_subscriptions_for_order', $order, $status );
}
/**
* Takes a user ID and array of subscription details and updates the users subscription details accordingly.
*
* @uses wp_parse_args To allow only part of a subscription's details to be updated, like status.
* @param int $user_id The ID of the user for whom subscription details should be updated
* @param array $subscriptions An array of arrays with a subscription key and corresponding 'detail' => 'value' pair. Can alter any of these details:
* 'start_date' The date the subscription was activated
* 'expiry_date' The date the subscription expires or expired, false if the subscription will never expire
* 'failed_payments' The date the subscription's trial expires or expired, false if the subscription has no trial period
* 'end_date' The date the subscription ended, false if the subscription has not yet ended
* 'status' Subscription status can be: cancelled, active, expired or failed
* 'completed_payments' An array of MySQL formatted dates for all payments that have been made on the subscription
* 'failed_payments' An integer representing a count of failed payments
* 'suspension_count' An integer representing a count of the number of times the subscription has been suspended for this billing period
* @since 1.0
*/
public static function update_users_subscriptions( $user_id, $subscriptions ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions API methods' );
foreach ( $subscriptions as $subscription_key => $new_subscription_details ) {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( isset( $new_subscription_details['status'] ) && 'deleted' == $new_subscription_details['status'] ) {
wp_delete_post( $subscription->id );
} else {
// There is no direct analog for this in WC_Subscription, so we need to call the deprecated method
self::update_subscription( $subscription_key, $new_subscription_details );
}
}
do_action( 'updated_users_subscriptions', $user_id, $subscriptions );
return self::get_users_subscriptions( $user_id ); // We need to call this deprecated method to preserve the return value in the deprecated array structure
}
/**
* Takes a subscription key and array of subscription details and updates the users subscription details accordingly.
*
* @uses wp_parse_args To allow only part of a subscription's details to be updated, like status.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param array $new_subscription_details An array of arrays with a subscription key and corresponding 'detail' => 'value' pair. Can alter any of these details:
* 'start_date' The date the subscription was activated
* 'expiry_date' The date the subscription expires or expired, false if the subscription will never expire
* 'failed_payments' The date the subscription's trial expires or expired, false if the subscription has no trial period
* 'end_date' The date the subscription ended, false if the subscription has not yet ended
* 'status' Subscription status can be: cancelled, active, expired or failed
* 'completed_payments' An array of MySQL formatted dates for all payments that have been made on the subscription
* 'failed_payments' An integer representing a count of failed payments
* 'suspension_count' An integer representing a count of the number of times the subscription has been suspended for this billing period
* @since 1.4
*/
public static function update_subscription( $subscription_key, $new_subscription_details ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions API methods' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( isset( $new_subscription_details['status'] ) && 'deleted' == $new_subscription_details['status'] ) {
wp_delete_post( $subscription->id );
} else {
foreach ( $new_subscription_details as $meta_key => $meta_value ) {
switch ( $meta_key ) {
case 'start_date' :
$subscription->update_dates( array( 'start' => $meta_value ) );
break;
case 'trial_expiry_date' :
$subscription->update_dates( array( 'trial_end' => $meta_value ) );
break;
case 'expiry_date' :
$subscription->update_dates( array( 'end' => $meta_value ) );
break;
case 'failed_payments' :
_deprecated_argument( __METHOD__, '2.0', 'The "failed_payments" meta value is deprecated. Create a renewal order with "failed" status instead.' );
break;
case 'completed_payments' :
_deprecated_argument( __METHOD__, '2.0', 'The "completed_payments" meta value is deprecated. Create a renewal order with completed payment instead.' );
break;
case 'suspension_count' :
$subscription->update_suspension_count( $subscription->suspension_count + 1 );
break;
}
}
}
do_action( 'updated_users_subscription', $subscription_key, $new_subscription_details );
return wcs_get_subscription_in_deprecated_structure( $subscription );
}
/**
* Takes a user ID and cancels any subscriptions that user has.
*
* @uses wp_parse_args To allow only part of a subscription's details to be updated, like status.
* @param int $user_id The ID of the user for whom subscription details should be updated
* @since 1.3.8
*/
public static function cancel_users_subscriptions( $user_id ) {
$subscriptions = wcs_get_users_subscriptions( $user_id );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
if ( $subscription->can_be_updated_to( 'cancelled' ) ) {
$subscription->update_status( 'cancelled' );
}
}
do_action( 'cancelled_users_subscriptions', $user_id );
}
}
/**
* Takes a user ID and cancels any subscriptions that user has on any site in a WordPress network
*
* @uses wp_parse_args To allow only part of a subscription's details to be updated, like status.
* @param int $user_id The ID of the user for whom subscription details should be updated
* @since 1.3.8
*/
public static function cancel_users_subscriptions_for_network( $user_id ) {
$sites = get_blogs_of_user( $user_id );
if ( ! empty( $sites ) ) {
foreach ( $sites as $site ) {
switch_to_blog( $site->userblog_id );
self::cancel_users_subscriptions( $user_id );
restore_current_blog();
}
}
do_action( 'cancelled_users_subscriptions_for_network', $user_id );
}
/**
* Clear all subscriptions for a given order.
*
* @param WC_Order $order The order for which subscriptions should be cleared.
* @since 1.0
*/
public static function clear_users_subscriptions_from_order( $order ) {
foreach ( wcs_get_subscriptions_for_order( $order, array( 'order_type' => 'parent' ) ) as $subscription_id => $subscription ) {
wp_delete_post( $subscription->id );
}
do_action( 'cleared_users_subscriptions_from_order', $order );
}
/**
* Clear all subscriptions attached to an order when it's deleted. Also make sure
* all related scheduled actions are cancelled when deleting a susbcription.
*
* @param int $post_id The post ID of the WC Subscription or WC Order being trashed
* @since 1.0
*/
public static function maybe_trash_subscription( $post_id ) {
if ( 'shop_order' == get_post_type( $post_id ) ) {
// delete subscription
foreach ( wcs_get_subscriptions_for_order( $post_id, array( 'order_type' => 'parent' ) ) as $subscription ) {
wp_trash_post( $subscription->id );
}
}
}
/**
* Make sure a subscription is cancelled before it is trashed or deleted
*
* @param int $post_id
* @since 2.0
*/
public static function maybe_cancel_subscription( $post_id ) {
if ( 'shop_subscription' == get_post_type( $post_id ) ) {
$subscription = wcs_get_subscription( $post_id );
if ( ! $subscription->has_status( wcs_get_subscription_ended_statuses() ) ) {
$subscription->update_status( 'cancelled' );
}
}
}
/**
* When WordPress trashes a post, it sets a '_wp_trash_meta_status' post meta value so that the post can
* be restored to its original status. However, when setting that value, it uses the 'post_status' of a
* $post variable in memory. If that status is changed on the 'wp_trash_post' or 'wp_delete_post' hooks,
* as is the case with a subscription, which is cancelled before being trashed if it is active or on-hold,
* then the '_wp_trash_meta_status' value will be incorrectly set to its status before being trashed.
*
* This function fixes that by setting '_wp_trash_meta_status' to 'wc-cancelled' whenever its former status
* is something that can not be restored.
*
* @param int $post_id
* @since 2.0
*/
public static function fix_trash_meta_status( $post_id ) {
if ( 'shop_subscription' == get_post_type( $post_id ) && ! in_array( get_post_meta( $post_id,'_wp_trash_meta_status', true ), array( 'wc-pending', 'wc-expired', 'wc-cancelled' ) ) ) {
update_post_meta( $post_id,'_wp_trash_meta_status', 'wc-cancelled' );
}
}
/**
* Trigger action hook after a subscription has been trashed.
*
* @param int $post_id
* @since 2.0
*/
public static function trigger_subscription_trashed_hook( $post_id ) {
if ( 'shop_subscription' == get_post_type( $post_id ) ) {
do_action( 'woocommerce_subscription_trashed', $post_id );
}
}
/**
* Takes a user ID and trashes any subscriptions that user has.
*
* @param int $user_id The ID of the user whose subscriptions will be trashed
* @since 2.0
*/
public static function trash_users_subscriptions( $user_id ) {
$subscriptions = wcs_get_users_subscriptions( $user_id );
if ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
wp_delete_post( $subscription->id );
}
}
}
/**
* Takes a user ID and trashes any subscriptions that user has on any site in a WordPress network
*
* @param int $user_id The ID of the user whose subscriptions will be trashed
* @since 2.0
*/
public static function trash_users_subscriptions_for_network( $user_id ) {
$sites = get_blogs_of_user( $user_id );
if ( ! empty( $sites ) ) {
foreach ( $sites as $site ) {
switch_to_blog( $site->userblog_id );
self::trash_users_subscriptions( $user_id );
restore_current_blog();
}
}
}
/**
* Trigger action hook after a subscription has been deleted.
*
* @param int $post_id
* @since 2.0
*/
public static function trigger_subscription_deleted_hook( $post_id ) {
if ( 'shop_subscription' == get_post_type( $post_id ) ) {
do_action( 'woocommerce_subscription_deleted', $post_id );
}
}
/**
* Checks if the current request is by a user to change the status of their subscription, and if it is
* validate the subscription cancellation request and maybe processes the cancellation.
*
* @since 1.0
* @deprecated 2.0
*/
public static function maybe_change_users_subscription() {
_deprecated_function( __METHOD__, '2.0', 'WCS_User_Change_Status_Handler::maybe_change_users_subscription()' );
WCS_User_Change_Status_Handler::maybe_change_users_subscription();
}
/**
* Check if a given subscription can be changed to a given a status.
*
* The function checks the subscription's current status and if the payment gateway used to purchase the
* subscription allows for the given status to be set via its API.
*
* @param string $new_status_or_meta The status or meta data you want to change th subscription to. Can be 'active', 'on-hold', 'cancelled', 'expired', 'trash', 'deleted', 'failed', 'new-payment-date' or some other value attached to the 'woocommerce_can_subscription_be_changed_to' filter.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @since 1.0
*/
public static function can_subscription_be_changed_to( $new_status_or_meta, $subscription_key, $user_id = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::can_be_updated_to( $new_status_or_meta )' );
if ( 'new-payment-date' == $new_status_or_meta ) {
_deprecated_argument( __METHOD__, '2.0', 'The "new-payment-date" parameter value is deprecated. Use WC_Subscription::can_date_be_updated( "next_payment" ) method instead.' );
} elseif ( 'suspended' == $new_status_or_meta ) {
_deprecated_argument( __METHOD__, '2.0', 'The "suspended" parameter value is deprecated. Use "on-hold" instead.' );
$new_status_or_meta = 'on-hold';
}
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
switch ( $new_status_or_meta ) {
case 'new-payment-date' :
$subscription_can_be_changed = $subscription->can_date_be_updated( 'next_payment' );
break;
case 'active' :
case 'on-hold' :
case 'cancelled' :
case 'expired' :
case 'trash' :
case 'deleted' :
case 'failed' :
default :
$subscription_can_be_changed = $subscription->can_be_updated_to( $new_status_or_meta );
break;
}
} catch ( Exception $e ) {
$subscription_can_be_changed = false;
}
return $subscription_can_be_changed;
}
/*
* Subscription Getters & Property functions
*/
/**
* Return an associative array of a given subscriptions details (if it exists).
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param deprecated don't use
* @return array Subscription details
* @since 1.1
*/
public static function get_subscription( $subscription_key, $deprecated = null ) {
if ( null != $deprecated ) {
_deprecated_argument( __METHOD__, '1.4', 'Second parameter is deprecated' );
}
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription( $subscription_id )' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
$subscription = wcs_get_subscription_in_deprecated_structure( $subscription );
} catch ( Exception $e ) {
$subscription = array();
}
return apply_filters( 'woocommerce_get_subscription', $subscription, $subscription_key, $deprecated );
}
/**
* Return an i18n'ified string for a given subscription status.
*
* @param string $status An subscription status of it's internal form.
* @return string A translated subscription status string for display.
* @since 1.2.3
*/
public static function get_status_to_display( $status, $subscription_key = '', $user_id = 0 ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_statuses()' );
switch ( $status ) {
case 'active' :
$status_string = _x( 'Active', 'Subscription status', 'woocommerce-subscriptions' );
break;
case 'cancelled' :
$status_string = _x( 'Cancelled', 'Subscription status', 'woocommerce-subscriptions' );
break;
case 'expired' :
$status_string = _x( 'Expired', 'Subscription status', 'woocommerce-subscriptions' );
break;
case 'pending' :
$status_string = _x( 'Pending', 'Subscription status', 'woocommerce-subscriptions' );
break;
case 'failed' :
$status_string = _x( 'Failed', 'Subscription status', 'woocommerce-subscriptions' );
break;
case 'on-hold' :
case 'suspend' : // Backward compatibility
$status_string = _x( 'On-hold', 'Subscription status', 'woocommerce-subscriptions' );
break;
default :
$status_string = apply_filters( 'woocommerce_subscriptions_custom_status_string', ucfirst( $status ), $subscription_key, $user_id );
}
return apply_filters( 'woocommerce_subscriptions_status_string', $status_string, $status, $subscription_key, $user_id );
}
/**
* Return an i18n'ified associative array of all possible subscription periods.
*
* @since 1.1
* @deprecated 2.0
*/
public static function get_subscription_period_strings( $number = 1, $period = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_period_strings()' );
return wcs_get_subscription_period_strings( $number, $period );
}
/**
* Return an i18n'ified associative array of all possible subscription periods.
*
* @since 1.0
* @deprecated 2.0
*/
public static function get_subscription_period_interval_strings( $interval = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_period_interval_strings()' );
return wcs_get_subscription_period_interval_strings( $interval );
}
/**
* Returns an array of subscription lengths.
*
* PayPal Standard Allowable Ranges
* D – for days; allowable range is 1 to 90
* W – for weeks; allowable range is 1 to 52
* M – for months; allowable range is 1 to 24
* Y – for years; allowable range is 1 to 5
*
* @param subscription_period string (optional) One of day, week, month or year. If empty, all subscription ranges are returned.
* @since 1.0
* @deprecated 2.0
*/
public static function get_subscription_ranges( $subscription_period = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_ranges()' );
return wcs_get_subscription_ranges( $subscription_period );
}
/**
* Returns an array of allowable trial periods.
*
* @see self::get_subscription_ranges()
* @param subscription_period string (optional) One of day, week, month or year. If empty, all subscription ranges are returned.
* @since 1.1
* @deprecated 2.0
*/
public static function get_subscription_trial_lengths( $subscription_period = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_trial_lengths( $subscription_period )' );
return wcs_get_subscription_trial_lengths( $subscription_period );
}
/**
* Return an i18n'ified associative array of all possible subscription trial periods.
*
* @since 1.2
* @deprecated 2.0
*/
public static function get_subscription_trial_period_strings( $number = 1, $period = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscription_trial_period_strings( $number, $period )' );
return wcs_get_subscription_trial_period_strings( $number, $period );
}
/**
* Return an i18n'ified associative array of all time periods allowed for subscriptions.
*
* @param string $form Either 'singular' for singular trial periods or 'plural'.
* @since 1.2
* @deprecated 2.0
*/
public static function get_available_time_periods( $form = 'singular' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_available_time_periods( $form )' );
return wcs_get_available_time_periods( $form );
}
/**
* Returns the string key for a subscription purchased in an order specified by $order_id
*
* @param order_id int The ID of the order in which the subscription was purchased.
* @param product_id int The ID of the subscription product.
* @return string The key representing the given subscription.
* @since 1.0
*/
public static function get_subscription_key( $order_id, $product_id = '' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_old_subscription_key( WC_Subscription $subscription )' );
// If we have a child renewal order, we need the parent order's ID
if ( wcs_order_contains_renewal( $order_id ) ) {
$order_id = WC_Subscriptions_Renewal_Order::get_parent_order_id( $order_id );
}
// Get the ID of the first order item in a subscription created by this order
if ( empty( $product_id ) ) {
$subscriptions = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'parent' ) );
foreach ( $subscriptions as $subscription ) {
$subscription_items = $subscription->get_items();
if ( ! empty( $subscription_items ) ) {
break;
}
}
if ( ! empty( $subscription_items ) ) {
$first_item = reset( $subscription_items );
$product_id = WC_Subscriptions_Order::get_items_product_id( $first_item );
} else {
$product_id = '';
}
}
$subscription_key = $order_id . '_' . $product_id;
return apply_filters( 'woocommerce_subscription_key', $subscription_key, $order_id, $product_id );
}
/**
* Returns the number of failed payments for a given subscription.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @return int The number of outstanding failed payments on the subscription, if any.
* @since 1.0
* @deprecated 2.0
*/
public static function get_subscriptions_failed_payment_count( $subscription_key, $user_id = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_failed_payment_count()' );
return apply_filters( 'woocommerce_subscription_failed_payment_count', wcs_get_subscription_from_key( $subscription_key )->get_failed_payment_count(), $user_id, $subscription_key );
}
/**
* Returns the number of completed payments for a given subscription (including the intial payment).
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @return int The number of outstanding failed payments on the subscription, if any.
* @since 1.4
* @deprecated 2.0
*/
public static function get_subscriptions_completed_payment_count( $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_completed_payment_count()' );
return apply_filters( 'woocommerce_subscription_completed_payment_count', wcs_get_subscription_from_key( $subscription_key )->get_completed_payment_count(), $subscription_key );
}
/**
* Takes a subscription key and returns the date on which the subscription is scheduled to expire
* or 0 if it is cancelled, expired, or never going to expire.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @since 1.1
* @deprecated 2.0
*/
public static function get_subscription_expiration_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_date( "end" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$expiration_date = ( 'mysql' == $type ) ? $subscription->get_date( 'end' ) : $subscription->get_time( 'end' );
return apply_filters( 'woocommerce_subscription_expiration_date' , $expiration_date, $subscription_key, $user_id );
}
/**
* Updates a subscription's expiration date as scheduled in WP-Cron and in the subscription details array.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id (optional) The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param (optional) $next_payment string | int The date and time the next payment is due, either as MySQL formatted datetime string or a Unix timestamp. If empty, @see self::calculate_subscription_expiration_date() will be called.
* @return mixed If the expiration does not get set, returns false, otherwise it will return a MySQL datetime formatted string for the new date when the subscription will expire
* @since 1.2.4
* @deprecated 2.0
*/
public static function set_expiration_date( $subscription_key, $user_id = '', $expiration_date = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_dates( array( "end" => $expiration_date ) )' );
if ( is_int( $expiration_date ) ) {
$expiration_date = date( 'Y-m-d H:i:s', $expiration_date );
}
$subscription = wcs_get_subscription_from_key( $subscription_key );
return apply_filters( 'woocommerce_subscriptions_set_expiration_date', $subscription->update_dates( array( 'end' => $expiration_date ) ), $subscription->get_date( 'end' ), $subscription_key, $user_id );
}
/**
* A subscription now either has an end date or it doesn't, there is no way to calculate it based on the original subsciption
* product (because a WC_Subscription object can have more than one product and syncing length with expiration date was both
* cumbersome and error prone).
*
* Takes a subscription key and calculates the date on which the subscription is scheduled to expire
* or 0 if it will never expire.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @since 1.1
* @deprecated 2.0
*/
public static function calculate_subscription_expiration_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_date( "end" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$expiration_date = ( 'mysql' == $type ) ? $subscription->get_date( 'end' ) : $subscription->get_time( 'end' );
return apply_filters( 'woocommerce_subscription_calculated_expiration_date', $expiration_date, $subscription_key, $user_id );
}
/**
* Takes a subscription key and returns the date on which the next recurring payment is to be billed, if any.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @return mixed If there is no future payment set, returns 0, otherwise it will return a date of the next payment in the form specified by $type
* @since 1.2
* @deprecated 2.0
*/
public static function get_next_payment_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_date( "next_payment" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$next_payment = ( 'mysql' == $type ) ? $subscription->get_date( 'next_payment' ) : $subscription->get_time( 'next_payment' );
return apply_filters( 'woocommerce_subscription_next_payment_date', $next_payment, $subscription_key, $user_id, $type );
}
/**
* Clears the payment schedule for a subscription and schedules a new date for the next payment.
*
* If updating the an existing next payment date (instead of setting a new date, you should use @see self::update_next_payment_date() instead
* as it will validate the next payment date and update the WP-Cron lock.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id (optional) The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param (optional) $next_payment string | int The date and time the next payment is due, either as MySQL formatted datetime string or a Unix timestamp. If empty, @see self::calculate_next_payment_date() will be called.
* @return mixed If there is no future payment set, returns 0, otherwise it will return a MySQL datetime formatted string for the date of the next payment
* @since 1.2
* @deprecated 2.0
*/
public static function set_next_payment_date( $subscription_key, $user_id = '', $next_payment = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_dates( array( "next_payment" => $next_payment ) )' );
if ( is_int( $next_payment ) ) {
$next_payment = date( 'Y-m-d H:i:s', $next_payment );
}
$subscription = wcs_get_subscription_from_key( $subscription_key );
return apply_filters( 'woocommerce_subscription_set_next_payment_date', $subscription->update_dates( array( 'next_payment' => $next_payment ) ), $subscription->get_date( 'next_payment' ), $subscription_key, $user_id );
}
/**
* Takes a subscription key and returns the date on which the next recurring payment is to be billed, if any.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @return mixed If there is no future payment set, returns 0, otherwise it will return a date of the next payment in the form specified by $type
* @since 1.2
* @deprecated 2.0
*/
public static function get_last_payment_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_date( "last_payment" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$last_payment_date = ( 'mysql' == $type ) ? $subscription->get_date( 'last_payment' ) : $subscription->get_time( 'last_payment' );
return apply_filters( 'woocommerce_subscription_last_payment_date', $last_payment_date, $subscription_key, $user_id, $type );
}
/**
* Changes the transient used to safeguard against firing scheduled_subscription_payments during a payment period.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $lock_time The amount of time to lock for in seconds from now, the lock will be set 1 hour before this time
* @param int $user_id (optional) The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @since 1.2
* @deprecated 2.0
*/
public static function update_wp_cron_lock( $subscription_key, $lock_time, $user_id = '' ) {
_deprecated_function( __METHOD__, '2.0' );
}
/**
* Clears the payment schedule for a subscription and sets a net date
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id (optional) The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @return mixed If there is no future payment set, returns 0, otherwise it will return a date of the next payment of the type specified with $type
* @since 1.2
* @deprecated 2.0
*/
public static function calculate_next_payment_date( $subscription_key, $user_id = '', $type = 'mysql', $from_date = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::calculate_date( "next_payment" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$next_payment = $subscription->calculate_date( 'next_payment' );
return ( 'mysql' == $type ) ? $next_payment : strtotime( $next_payment );
}
/**
* Takes a subscription key and returns the date on which the trial for the subscription ended or is going to end, if any.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @return mixed If the subscription has no trial period, returns 0, otherwise it will return the date the trial period ends or ended in the form specified by $type
* @since 1.2
*/
public static function get_trial_expiration_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_date( "trial_end" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$trial_end_date = ( 'mysql' == $type ) ? $subscription->get_date( 'trial_end' ) : $subscription->get_time( 'trial_end' );
return apply_filters( 'woocommerce_subscription_trial_expiration_date', $trial_end_date, $subscription_key, $user_id, $type );
}
/**
* Updates the trial expiration date as scheduled in WP-Cron and in the subscription details array.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id (optional) The ID of the user who owns the subscription. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param (optional) $next_payment string | int The date and time the next payment is due, either as MySQL formatted datetime string or a Unix timestamp. If empty, @see self::calculate_next_payment_date() will be called.
* @return mixed If the trial expiration does not get set, returns false, otherwise it will return a MySQL datetime formatted string for the new date when the trial will expire
* @since 1.2.4
*/
public static function set_trial_expiration_date( $subscription_key, $user_id = '', $trial_expiration_date = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_dates( array( "trial_end" => $expiration_date ) )' );
if ( is_int( $trial_expiration_date ) ) {
$trial_expiration_date = date( 'Y-m-d H:i:s', $trial_expiration_date );
}
$subscription = wcs_get_subscription_from_key( $subscription_key );
return apply_filters( 'woocommerce_subscriptions_set_trial_expiration_date', $subscription->update_dates( array( 'trial_end' => $trial_expiration_date ) ), $subscription->get_date( 'trial_end' ), $subscription_key, $user_id );
}
/**
* Takes a subscription key and calculates the date on which the subscription's trial should end
* or 0 if no trial is set.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @param string $type (optional) The format for the Either 'mysql' or 'timestamp'.
* @since 1.1
*/
public static function calculate_trial_expiration_date( $subscription_key, $user_id = '', $type = 'mysql' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::calculate_date( "trial_end" )' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
$trial_end = $subscription->calculate_date( 'trial_end' );
$trial_end = ( 'mysql' == $type ) ? $trial_end : strtotime( $trial_end );
return apply_filters( 'woocommerce_subscription_calculated_trial_expiration_date' , $trial_end, $subscription_key, $user_id );
}
/**
* Takes a subscription key and returns the user who owns the subscription (based on the order ID in the subscription key).
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @return int The ID of the user who owns the subscriptions, or 0 if no user can be found with the subscription
* @since 1.2
*/
public static function get_user_id_from_subscription_key( $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::get_user_id()' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
return $subscription->get_user_id();
}
/**
* Checks if a subscription requires manual payment because the payment gateway used to purchase the subscription
* did not support automatic payments at the time of the subscription sign up.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @return bool | null True if the subscription exists and requires manual payments, false if the subscription uses automatic payments, null if the subscription doesn't exist.
* @since 1.2
* @deprecated 2.0
*/
public static function requires_manual_renewal( $subscription_key, $user_id = '' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::is_manual()' );
return wcs_get_subscription_from_key( $subscription_key )->is_manual();
}
/**
* Checks if a subscription has an unpaid renewal order.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance.
* @return bool True if the subscription has an unpaid renewal order, false if the subscription has no unpaid renewal orders.
* @since 1.2
*/
public static function subscription_requires_payment( $subscription_key, $user_id ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::needs_payment()' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
return apply_filters( 'woocommerce_subscription_requires_payment', $subscription->needs_payment(), wcs_get_subscription_in_deprecated_structure( $subscription ), $subscription_key, $user_id );
}
/*
* User API Functions
*/
/**
* Check if a user owns a subscription, as specified with $subscription_key.
*
* If no user is specified, the currently logged in user will be used.
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id (optional) int The ID of the user to check against. Defaults to the currently logged in user.
* @return bool True if the user has the subscription (or any subscription if no subscription specified), otherwise false.
* @since 1.3
* @deprecated 2.0
*/
public static function user_owns_subscription( $subscription_key, $user_id = 0 ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions::get_user_id()' );
if ( 0 === $user_id || empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->get_user_id() == $user_id ) {
$owns_subscription = true;
} else {
$owns_subscription = false;
}
return apply_filters( 'woocommerce_user_owns_subscription', $owns_subscription, $subscription_key, $user_id );
}
/**
* Check if a user has a subscription, optionally specified with $product_id.
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @param product_id int (optional) The ID of a subscription product.
* @param status string (optional) A subscription status to check against. For example, for a $status of 'active', a subscriber must have an active subscription for a return value of true.
* @return bool True if the user has the subscription (or any subscription if no subscription specified), otherwise false.
* @version 1.3.5
*/
public static function user_has_subscription( $user_id = 0, $product_id = '', $status = 'any' ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_user_has_subscription()' );
return wcs_user_has_subscription( $user_id, $product_id, $status );
}
/**
* Gets all the active and inactive subscriptions for all users.
*
* @return array An associative array containing all users with subscriptions and the details of their subscriptions: 'user_id' => $subscriptions
* @since 1.0
*/
public static function get_all_users_subscriptions() {
_deprecated_function( __METHOD__, '2.0' );
foreach ( get_users() as $user ) {
foreach ( wcs_get_users_subscriptions( $user->ID ) as $subscription ) {
$subscriptions_in_old_format[ wcs_get_old_subscription_key( $subscription ) ] = wcs_get_subscription_in_deprecated_structure( $subscription );
}
}
return apply_filters( 'woocommerce_all_users_subscriptions', $subscriptions_in_old_format );
}
/**
* Gets all the active and inactive subscriptions for a user, as specified by $user_id
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @param array $order_ids (optional) An array of post_ids of WC_Order objects as a way to get only subscriptions for certain orders. Defaults to null, which will return subscriptions for all orders.
* @since 1.0
*/
public static function get_users_subscriptions( $user_id = 0, $order_ids = array() ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_users_subscriptions( $user_id )' );
$subscriptions_in_old_format = array();
foreach ( wcs_get_users_subscriptions( $user_id ) as $subscription ) {
$subscriptions_in_old_format[ wcs_get_old_subscription_key( $subscription ) ] = wcs_get_subscription_in_deprecated_structure( $subscription );
}
return apply_filters( 'woocommerce_users_subscriptions', $subscriptions_in_old_format, $user_id );
}
/**
* Gets all the subscriptions for a user that have been trashed, as specified by $user_id
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @since 1.0
*/
public static function get_users_trashed_subscriptions( $user_id = '' ) {
$subscriptions = self::get_users_subscriptions( $user_id );
foreach ( $subscriptions as $key => $subscription ) {
if ( 'trash' != $subscription['status'] ) {
unset( $subscriptions[ $key ] );
}
}
return apply_filters( 'woocommerce_users_trashed_subscriptions', $subscriptions, $user_id );
}
/**
* A convenience wrapper to assign the inactive subscriber role to a user.
*
* @param int $user_id The id of the user whose role should be changed
* @since 1.2
* @deprecated 2.0
*/
public static function make_user_inactive( $user_id ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_make_user_inactive()' );
wcs_make_user_inactive( $user_id );
}
/**
* A convenience wrapper to assign the cancelled subscriber role to a user.
*
* Hooked to 'subscription_end_of_prepaid_term' hook.
*
* @param int $user_id The id of the user whose role should be changed
* @since 1.3.2
* @deprecated 2.0
*/
public static function maybe_assign_user_cancelled_role( $user_id ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_maybe_make_user_inactive()' );
wcs_maybe_make_user_inactive( $user_id );
}
/**
* A convenience wrapper for changing a users role.
*
* @param int $user_id The id of the user whose role should be changed
* @param string $role_name Either a WordPress role or one of the WCS keys: 'default_subscriber_role' or 'default_cancelled_role'
* @since 1.0
* @deprecated 2.0
*/
public static function update_users_role( $user_id, $role_name ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_update_users_role()' );
wcs_update_users_role( $user_id, $role_name );
}
/**
* Marks a customer as a paying customer when their subscription is activated.
*
* A wrapper for the @see woocommerce_paying_customer() function.
*
* @param int $order_id The id of the order for which customers should be pulled from and marked as paying.
* @since 1.0
* @deprecated 2.0
*/
public static function mark_paying_customer( $order ) {
_deprecated_function( __METHOD__, '2.0' );
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
update_user_meta( $order->user_id, 'paying_customer', 1 );
}
/**
* Unlike someone making a once-off payment, a subscriber can cease to be a paying customer. This function
* changes a user's status to non-paying.
*
* Deprecated as orders now take care of the customer's status as paying or not paying
*
* @param object $order The order for which a customer ID should be pulled from and marked as paying.
* @since 1.0
* @deprecated 2.0
*/
public static function mark_not_paying_customer( $order ) {
_deprecated_function( __METHOD__, '2.0' );
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
if ( $order->user_id > 0 ) {
update_user_meta( $order->user_id, 'paying_customer', 0 );
}
}
/**
* Return a link for subscribers to change the status of their subscription, as specified with $status parameter
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
* @deprecated 2.0
*/
public static function get_users_change_status_link( $subscription_key, $status ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_users_change_status_link( $subscription_id, $status )' );
if ( 'suspended' == $status ) {
_deprecated_argument( __METHOD__, '2.0', 'The "suspended" parameter value is deprecated. Use "on-hold" instead.' );
$status = 'on-hold';
}
$subscription_id = wcs_get_subscription_id_from_key( $subscription_key );
$current_status = '';
$subscription = wcs_get_subscription( $subscription_id );
if ( $subscription instanceof WC_Subscription ) {
$current_status = $subscription->get_status();
}
return apply_filters( 'woocommerce_subscriptions_users_action_link', wcs_get_users_change_status_link( $subscription_id, $status, $current_status ), $subscription_key, $status );
}
/**
* Change a subscription's next payment date.
*
* @param mixed $new_payment_date Either a MySQL formatted Date/time string or a Unix timestamp.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @param int $user_id The id of the user who purchased the subscription
* @param string $timezone Either 'server' or 'user' to describe the timezone of the $new_payment_date.
* @since 1.2
* @deprecated 2.0
*/
public static function update_next_payment_date( $new_payment_date, $subscription_key, $user_id = '', $timezone = 'server' ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_dates( array( "next_payment" => $new_payment_date ) )' );
$new_payment_timestamp = ( is_numeric( $new_payment_date ) ) ? $new_payment_date : strtotime( $new_payment_date );
// The date needs to be converted to GMT/UTC
if ( 'server' != $timezone ) {
$new_payment_timestamp = $new_payment_timestamp - ( get_option( 'gmt_offset' ) * 3600 );
}
$new_payment_date = date( 'Y-m-d H:i:s', $new_payment_timestamp );
$subscription = wcs_get_subscription_from_key( $subscription_key );
try {
$subscription->update_dates( array( 'next_payment' => $new_payment_date ) );
$response = $subscription->get_time( 'next_payment' );
} catch ( Exception $e ) {
$response = new WP_Error( 'invalid-date', $e->getMessage() );
}
return $response;
}
/*
* Helper Functions
*/
/**
* Because neither PHP nor WP include a real array merge function that works recursively.
*
* @since 1.0
*/
public static function array_merge_recursive_for_real( $first_array, $second_array ) {
$merged = $first_array;
if ( is_array( $second_array ) ) {
foreach ( $second_array as $key => $val ) {
if ( is_array( $second_array[ $key ] ) ) {
$merged[ $key ] = ( isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) ? self::array_merge_recursive_for_real( $merged[ $key ], $second_array[ $key ] ) : $second_array[ $key ];
} else {
$merged[ $key ] = $val;
}
}
}
return $merged;
}
/**
* Takes a total and calculates the recurring proportion of that based on $proportion and then fixes any rounding bugs to
* make sure the totals add up.
*
* Used mainly to calculate the recurring amount from a total which may also include a sign up fee.
*
* @param float $total The total amount
* @since 1.2
* @return float $proportion A proportion of the total (e.g. 0.5 is half of the total)
*/
public static function get_amount_from_proportion( $total, $proportion ) {
$sign_up_fee_proprotion = 1 - $proportion;
$sign_up_total = round( $total * $sign_up_fee_proprotion, 2 );
$recurring_amount = round( $total * $proportion, 2 );
// Handle any rounding bugs
if ( $sign_up_total + $recurring_amount != $total ) {
$recurring_amount = $recurring_amount - ( $sign_up_total + $recurring_amount - $total );
}
return $recurring_amount;
}
/**
* Creates a subscription price string from an array of subscription details. For example, ""$5 / month for 12 months".
*
* @param array $subscription_details A set of name => value pairs for the subscription details to include in the string. Available keys:
* 'initial_amount': The upfront payment for the subscription, including sign up fees, as a string from the @see woocommerce_price(). Default empty string (no initial payment)
* 'initial_description': The word after the initial payment amount to describe the amount. Examples include "now" or "initial payment". Defaults to "up front".
* 'recurring_amount': The amount charged per period. Default 0 (no recurring payment).
* 'subscription_interval': How regularly the subscription payments are charged. Default 1, meaning each period e.g. per month.
* 'subscription_period': The temporal period of the subscription. Should be one of {day|week|month|year} as used by @see self::get_subscription_period_strings()
* 'subscription_length': The total number of periods the subscription should continue for. Default 0, meaning continue indefinitely.
* 'trial_length': The total number of periods the subscription trial period should continue for. Default 0, meaning no trial period.
* 'trial_period': The temporal period for the subscription's trial period. Should be one of {day|week|month|year} as used by @see self::get_subscription_period_strings()
* @since 1.2
* @deprecated 2.0
* @return float $proportion A proportion of the total (e.g. 0.5 is half of the total)
*/
public static function get_subscription_price_string( $subscription_details ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_price_string()' );
return wcs_price_string( $subscription_details );
}
/**
* Copy of the WordPress "touch_time" template function for use with a variety of different times
*
* @param array $args A set of name => value pairs to customise how the function operates. Available keys:
* 'date': (string) the date to display in the selector in MySQL format ('Y-m-d H:i:s'). Required.
* 'tab_index': (int) the tab index for the element. Optional. Default 0.
* 'multiple': (bool) whether there will be multiple instances of the element on the same page (determines whether to include an ID or not). Default false.
* 'echo': (bool) whether to return and print the element or simply return it. Default true.
* 'include_time': (bool) whether to include a specific time for the selector. Default true.
* 'include_year': (bool) whether to include a the year field. Default true.
* 'include_buttons': (bool) whether to include submit buttons on the selector. Default true.
* @since 1.2
*/
public static function touch_time( $args = array() ) {
global $wp_locale;
$args = wp_parse_args( $args, array(
'date' => true,
'tab_index' => 0,
'multiple' => false,
'echo' => true,
'include_time' => true,
'include_buttons' => true,
)
);
if ( empty( $args['date'] ) ) {
return;
}
$tab_index_attribute = ( (int) $args['tab_index'] > 0 ) ? ' tabindex="' . $args['tab_index'] . '"' : '';
$month = mysql2date( 'n', $args['date'], false );
$month_input = '<select ' . ( $args['multiple'] ? '' : 'id="edit-month" ' ) . 'name="edit-month"' . $tab_index_attribute . '>';
for ( $i = 1; $i < 13; $i = $i + 1 ) {
$month_numeral = zeroise( $i, 2 );
$month_input .= '<option value="' . $month_numeral . '"';
$month_input .= ( $i == $month ) ? ' selected="selected"' : '';
// translators: 1$: month number (e.g. "01"), 2$: month abbreviation (e.g. "Jan")
$month_input .= '>' . sprintf( _x( '%1$s-%2$s', 'used in a select box', 'woocommerce-subscriptions' ), $month_numeral, $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) ) . "</option>\n";
}
$month_input .= '</select>';
$day_input = '<input type="text" ' . ( $args['multiple'] ? '' : 'id="edit-day" ' ) . 'name="edit-day" value="' . mysql2date( 'd', $args['date'], false ) . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
$year_input = '<input type="text" ' . ( $args['multiple'] ? '' : 'id="edit-year" ' ) . 'name="edit-year" value="' . mysql2date( 'Y', $args['date'], false ) . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
if ( $args['include_time'] ) {
$hour_input = '<input type="text" ' . ( $args['multiple'] ? '' : 'id="edit-hour" ' ) . 'name="edit-hour" value="' . mysql2date( 'H', $args['date'], false ) . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
$minute_input = '<input type="text" ' . ( $args['multiple'] ? '' : 'id="edit-minute" ' ) . 'name="edit-minute" value="' . mysql2date( 'i', $args['date'], false ) . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
// translators: all fields are full html nodes: 1$: month input, 2$: day input, 3$: year input, 4$: hour input, 5$: minute input. Change the order if you'd like
$touch_time = sprintf( __( '%1$s%2$s, %3$s @ %4$s : %5$s', 'woocommerce-subscriptions' ), $month_input, $day_input, $year_input, $hour_input, $minute_input );
} else {
// translators: all fields are full html nodes: 1$: month input, 2$: day input, 3$: year input. Change the order if you'd like
$touch_time = sprintf( __( '%1$s%2$s, %3$s', 'woocommerce-subscriptions' ), $month_input, $day_input, $year_input );
}
if ( $args['include_buttons'] ) {
$touch_time .= '<p>';
$touch_time .= '<a href="#edit_timestamp" class="save-timestamp hide-if-no-js button">' . __( 'Change', 'woocommerce-subscriptions' ) . '</a>';
$touch_time .= '<a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js">' . _x( 'Cancel', 'an action on a subscription', 'woocommerce-subscriptions' ) . '</a>';
$touch_time .= '</p>';
}
$allowed_html = array(
'select' => array(
'id' => array(),
'name' => array(),
'tabindex' => array(),
),
'option' => array(
'value' => array(),
'selected' => array(),
),
'input' => array(
'type' => array(),
'id' => array(),
'name' => array(),
'value' => array(),
'size' => array(),
'tabindex' => array(),
'maxlength' => array(),
'autocomplete' => array(),
),
'p' => array(),
'a' => array(
'href' => array(),
'title' => array(),
'class' => array(),
),
);
if ( $args['echo'] ) {
echo wp_kses( $touch_time, $allowed_html );
}
return $touch_time;
}
/**
* If a gateway doesn't manage payment schedules, then we should suspend the subscription until it is paid (i.e. for manual payments
* or token gateways like Stripe). If the gateway does manage the scheduling, then we shouldn't suspend the subscription because a
* gateway may use batch processing on the time payments are charged and a subscription could end up being incorrectly suspended.
*
* @param int $user_id The id of the user whose subscription should be put on-hold.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.2.5
* @deprecated 2.0
*/
public static function maybe_put_subscription_on_hold( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_status()' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( 'on-hold' ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
// If the subscription is using manual payments, the gateway isn't active or it manages scheduled payments
if ( 0 == $subscription->get_total() || $subscription->is_manual() || empty( $subscription->payment_method ) || ! $subscription->payment_method_supports( 'gateway_scheduled_payments' ) ) {
$subscription->update_status( 'on-hold', _x( 'Subscription renewal payment due:', 'used in order note as reason for why subscription status changed', 'woocommerce-subscriptions' ) );
}
}
/**
* Check if the subscription needs to use the failed payment process to repair its status after it incorrectly expired due to a date migration
* bug in upgrade process for 2.0.0 of Subscriptions (i.e. not 2.0.1 or newer). See WCS_Repair_2_0_2::maybe_repair_status() for more details.
*
* @param int $subscription_id The ID of a 'shop_subscription' post
* @since 2.0.2
*/
public static function maybe_process_failed_renewal_for_repair( $subscription_id ) {
if ( 'true' == get_post_meta( $subscription_id, '_wcs_repaired_2_0_2_needs_failed_payment', true ) ) {
$subscription = wcs_get_subscription( $subscription_id );
// Always put the subscription on hold in case something goes wrong while trying to process renewal
$subscription->update_status( 'on-hold', _x( 'Subscription renewal payment due:', 'used in order note as reason for why subscription status changed', 'woocommerce-subscriptions' ) );
// Create a renewal order to record the failed payment which can then be used by the customer to reactivate the subscription
$renewal_order = wcs_create_renewal_order( $subscription );
// Mark the payment as failed so the customer can login to fix up the failed payment
$subscription->payment_failed();
// Only force the failed payment once
update_post_meta( $subscription_id, '_wcs_repaired_2_0_2_needs_failed_payment', 'false' );
// We've already processed the renewal
remove_action( 'woocommerce_scheduled_subscription_payment', __CLASS__ . '::prepare_renewal' );
remove_action( 'woocommerce_scheduled_subscription_payment', 'WC_Subscriptions_Payment_Gateways::gateway_scheduled_subscription_payment', 10, 1 );
}
}
/* Deprecated Functions */
/**
* @deprecated 1.1
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function can_subscription_be_cancelled( $subscription_key, $user_id = '' ) {
_deprecated_function( __METHOD__, '1.1', __CLASS__ . '::can_subscription_be_changed_to( "cancelled", $subscription_key, $user_id )' );
$subscription_can_be_cancelled = self::can_subscription_be_changed_to( 'cancelled', $subscription_key, $user_id );
return apply_filters( 'woocommerce_subscription_can_be_cancelled', $subscription_can_be_cancelled, $subscription, $order );
}
/**
* @deprecated 1.1
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function get_users_cancellation_link( $subscription_key ) {
_deprecated_function( __METHOD__, '1.1', __CLASS__ . '::get_users_cancellation_link( $subscription_key, "cancel" )' );
return apply_filters( 'woocommerce_subscriptions_users_cancellation_link', self::get_users_change_status_link( $subscription_key, 'cancel' ), $subscription_key );
}
/**
* @deprecated 1.1
* @since 1.0
*/
public static function maybe_cancel_users_subscription() {
_deprecated_function( __METHOD__, '1.1', __CLASS__ . '::maybe_change_users_subscription()' );
self::maybe_change_users_subscription();
}
/**
* @deprecated 1.1
* @param int $user_id The ID of the user who owns the subscriptions.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function get_failed_payment_count( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '1.1', __CLASS__ . '::get_subscriptions_failed_payment_count( $subscription_key, $user_id )' );
return self::get_subscriptions_failed_payment_count( $subscription_key, $user_id );
}
/**
* Deprecated in favour of a more correctly named @see maybe_reschedule_subscription_payment()
*
* @deprecated 1.1.5
* @since 1.0
*/
public static function reschedule_subscription_payment( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '1.1.5', __CLASS__ . '::maybe_reschedule_subscription_payment( $user_id, $subscription_key )' );
self::maybe_reschedule_subscription_payment( $user_id, $subscription_key );
}
/**
* Suspended a single subscription on a users account by placing it in the "suspended" status.
*
* Subscriptions version 1.2 replaced the "suspended" status with the "on-hold" status to match WooCommerce core.
*
* @param int $user_id The id of the user whose subscription should be suspended.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @deprecated 1.2
* @since 1.0
*/
public static function suspend_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '1.2', __CLASS__ . '::put_subscription_on_hold( $user_id, $subscription_key )' );
self::put_subscription_on_hold( $user_id, $subscription_key );
}
/**
* Suspended all the subscription products in an order.
*
* Subscriptions version 1.2 replaced the "suspended" status with the "on-hold" status to match WooCommerce core.
*
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as activated.
* @deprecated 1.2
* @since 1.0
*/
public static function suspend_subscriptions_for_order( $order ) {
_deprecated_function( __METHOD__, '1.2', __CLASS__ . '::put_subscription_on_hold_for_order( $order )' );
self::put_subscription_on_hold_for_order( $order );
}
/**
* Gets a specific subscription for a user, as specified by $subscription_key
*
* Subscriptions version 1.4 moved subscription details out of user meta and into item meta, meaning it can be accessed
* efficiently without a user ID.
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @param string $subscription_key A subscription key of the form created by @see self::subscription_key()
* @deprecated 1.4
* @since 1.0
*/
public static function get_users_subscription( $user_id = 0, $subscription_key ) {
_deprecated_function( __METHOD__, '1.4', __CLASS__ . '::get_subscription( $subscription_key )' );
return apply_filters( 'woocommerce_users_subscription', self::get_subscription( $subscription_key ), $user_id, $subscription_key );
}
/**
* Removed a specific subscription for a user, as specified by $subscription_key, but as subscriptions are no longer stored
* against a user and are instead stored against the order, this is no longer required (changing the user on the order effectively
* performs the same thing without requiring the subscription to have any changes).
*
* @param int $user_id (optional) The id of the user whose subscriptions you want. Defaults to the currently logged in user.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @deprecated 1.4
* @since 1.0
*/
public static function remove_users_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '1.4' );
}
/**
* When a scheduled subscription payment hook is fired, automatically process the subscription payment
* if the amount is for $0 (and therefore, there is no payment to be processed by a gateway, and likely
* no gateway used on the initial order).
*
* If a subscription has a $0 recurring total and is not already active (after being actived by something else
* handling the 'scheduled_subscription_payment' with the default priority of 10), then this function will call
* @see self::process_subscription_payment() to reactive the subscription, generate a renewal order etc.
*
* @param int $user_id The id of the user who the subscription belongs to
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.3.2
* @deprecated 2.0
*/
public static function maybe_process_subscription_payment( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', __CLASS__ . '::prepare_renewal( $subscription_id )' );
self::prepare_renewal( wcs_get_subscription_id_from_key( $subscription_key ) );
}
/**
* Return a link for subscribers to change the status of their subscription, as specified with $status parameter
*
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function current_user_can_suspend_subscription( $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_can_user_put_subscription_on_hold( $subscription, $user )' );
return wcs_can_user_put_subscription_on_hold( wcs_get_subscription_from_key( $subscription_key ) );
}
/**
* Return a multi-dimensional associative array of subscriptions with a certain value, grouped by user ID.
*
* A slow PHP based search routine which can't use the speed of MySQL because subscription details. If you
* know the key for the value you are search by, use @see self::get_subscriptions() for better performance.
*
* @param string $search_query The query to search the database for.
* @return array Subscription details
* @since 1.1
* @deprecated 2.0
*/
public static function search_subscriptions( $search_query ) {
_deprecated_function( __METHOD__, '2.0', 'wcs_get_subscriptions()' );
global $wpdb;
$subscriptions_to_search = self::get_all_users_subscriptions();
$subscriptions_found = array();
$search_terms = explode( ' ', $search_query );
foreach ( $subscriptions_to_search as $user_id => $subscriptions ) {
$user = get_user_by( 'id', $user_id );
if ( false === $user || ! is_object( $user ) ) {
continue;
}
$user = $user->data;
foreach ( $search_terms as $search_term ) {
// If the search query is found in the user's details, add all of their subscriptions, otherwise add only subscriptions with a matching item
if ( false !== stripos( $user->user_nicename, $search_term ) || false !== stripos( $user->display_name, $search_term ) ) {
$subscriptions_found[ $user_id ] = $subscriptions;
} elseif ( false !== stripos( $user->user_login, $search_term ) || false !== stripos( $user->user_email, $search_term ) ) {
$subscriptions_found[ $user_id ] = $subscriptions;
} else {
foreach ( $subscriptions as $subscription_key => $subscription ) {
$product_title = get_the_title( $subscription['product_id'] );
if ( in_array( $search_term, $subscription, true ) || false != preg_match( "/$search_term/i", $product_title ) ) {
$subscriptions_found[ $user_id ][ $subscription_key ] = $subscription;
}
}
}
}
}
return apply_filters( 'woocommerce_search_subscriptions', $subscriptions_found, $search_query );
}
/**
* Marks a single subscription as active on a users account.
*
* @param int $user_id The id of the user whose subscription is to be activated.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function activate_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( 'active' ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
if ( ! $subscription->has_status( 'pending' ) && ! $subscription->can_be_updated_to( 'active' ) ) {
do_action( 'unable_to_activate_subscription', $user_id, $subscription_key );
$activated_subscription = false;
} else {
$subscription->update_status( 'active' );
do_action( 'activated_subscription', $user_id, $subscription_key );
$activated_subscription = true;
}
return $activated_subscription;
}
/**
* Changes a single subscription from on-hold to active on a users account.
*
* @param int $user_id The id of the user whose subscription is to be activated.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function reactivate_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0' );
if ( false !== self::activate_subscription( $user_id, $subscription_key ) ) {
do_action( 'reactivated_subscription', $user_id, $subscription_key );
}
}
/**
* Suspends a single subscription on a users account by placing it in the "on-hold" status.
*
* @param int $user_id The id of the user whose subscription should be put on-hold.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function put_subscription_on_hold( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscription::update_status( "on-hold" )' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( 'on-hold' ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
if ( ! $subscription->can_be_updated_to( 'on-hold' ) ) {
do_action( 'unable_to_put_subscription_on-hold', $user_id, $subscription_key );
do_action( 'unable_to_suspend_subscription', $user_id, $subscription_key );
} else {
$subscription->update_status( 'on-hold' );
do_action( 'subscription_put_on-hold', $user_id, $subscription_key );
// Backward, backward compatibility
do_action( 'suspended_subscription', $user_id, $subscription_key );
}
}
/**
* Cancels a single subscription on a users account.
*
* @param int $user_id The id of the user whose subscription should be cancelled.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function cancel_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'WC_Subscriptions::cancel_order()' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( array( 'pending-cancel', 'cancelled' ) ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
if ( ! $subscription->can_be_updated_to( 'cancelled' ) ) {
do_action( 'unable_to_cancel_subscription', $user_id, $subscription_key );
} else {
$subscription->update_status( 'cancelled' );
do_action( 'cancelled_subscription', $user_id, $subscription_key );
}
}
/**
* Sets a single subscription on a users account to be 'on-hold' and keeps a record of the failed sign up on an order.
*
* @param int $user_id The id of the user whose subscription should be cancelled.
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function failed_subscription_signup( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( 'on-hold' ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
// Place the subscription on-hold
$subscription->update_status( 'on-hold' );
// Log failure on order
// translators: placeholder is subscription ID
$subscription->order->add_order_note( sprintf( __( 'Failed sign-up for subscription %s.', 'woocommerce-subscriptions' ), $subscription->id ) );
do_action( 'subscription_sign_up_failed', $user_id, $subscription_key );
}
/**
* Trashes a single subscription on a users account.
*
* @param int $user_id The ID of the user who the subscription belongs to
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.0
*/
public static function trash_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'wp_trash_post()' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
if ( $subscription->has_status( 'trash' ) ) {
return false;
}
} catch ( Exception $e ) {
return false;
}
if ( ! $subscription->can_be_updated_to( 'cancelled' ) ) {
do_action( 'unable_to_trash_subscription', $user_id, $subscription_key );
} else {
// Run all cancellation related functions on the subscription
if ( ! $subscription->has_status( array( 'cancelled', 'expired', 'trash' ) ) ) {
$subscription->update_status( 'cancelled' );
}
wp_trash_post( $subscription->id, true );
do_action( 'subscription_trashed', $user_id, $subscription_key );
}
}
/**
* Permanently deletes a single subscription on a users account.
*
* @param int $user_id The ID of the user who the subscription belongs to
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.2
*/
public static function delete_subscription( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0', 'wp_delete_post()' );
try {
$subscription = wcs_get_subscription_from_key( $subscription_key );
} catch ( Exception $e ) {
return false;
}
if ( ! $subscription->can_be_updated_to( 'deleted' ) && ! $subscription->can_be_updated_to( 'cancelled' ) ) {
do_action( 'unable_to_delete_subscription', $user_id, $subscription_key );
} else {
// Run all cancellation related functions on the subscription
if ( ! $subscription->has_status( array( 'cancelled', 'expired', 'trash' ) ) ) {
$subscription->update_status( 'cancelled' );
}
wp_delete_post( $subscription->id, true );
do_action( 'subscription_deleted', $user_id, $subscription_key, $subscription, $item );
}
}
/**
* Processes an ajax request to change a subscription's next payment date.
*
* Deprecated because editing a subscription's next payment date is now done from the Edit Subscription screen.
*
* @since 1.2
* @deprecated 2.0
*/
public static function ajax_update_next_payment_date() {
_deprecated_function( __METHOD__, '2.0', 'wp_delete_post()' );
$response = array( 'status' => 'error' );
if ( ! wp_verify_nonce( $_POST['wcs_nonce'], 'woocommerce-subscriptions' ) ) {
$response['message'] = '<div class="error">' . __( 'Invalid security token, please reload the page and try again.', 'woocommerce-subscriptions' ) . '</div>';
} elseif ( ! current_user_can( 'manage_woocommerce' ) ) {
$response['message'] = '<div class="error">' . __( 'Only store managers can edit payment dates.', 'woocommerce-subscriptions' ) . '</div>';
} elseif ( empty( $_POST['wcs_day'] ) || empty( $_POST['wcs_month'] ) || empty( $_POST['wcs_year'] ) ) {
$response['message'] = '<div class="error">' . __( 'Please enter all date fields.', 'woocommerce-subscriptions' ) . '</div>';
} else {
$new_payment_date = sprintf( '%s-%s-%s %s', (int) $_POST['wcs_year'], zeroise( (int) $_POST['wcs_month'], 2 ), zeroise( (int) $_POST['wcs_day'], 2 ), date( 'H:i:s', current_time( 'timestamp' ) ) );
$new_payment_timestamp = self::update_next_payment_date( $new_payment_date, $_POST['wcs_subscription_key'], self::get_user_id_from_subscription_key( $_POST['wcs_subscription_key'] ), 'user' );
if ( is_wp_error( $new_payment_timestamp ) ) {
$response['message'] = sprintf( '<div class="error">%s</div>', $new_payment_timestamp->get_error_message() );
} else {
$new_payment_timestamp_user_time = $new_payment_timestamp + ( get_option( 'gmt_offset' ) * 3600 ); // The timestamp is returned in server time
$time_diff = $new_payment_timestamp - gmdate( 'U' );
if ( $time_diff > 0 && $time_diff < 7 * 24 * 60 * 60 ) {
// translators: placeholder is human time diff (e.g. "3 weeks")
$date_to_display = sprintf( __( 'In %s', 'woocommerce-subscriptions' ), human_time_diff( gmdate( 'U' ), $new_payment_timestamp ) );
} else {
$date_to_display = date_i18n( wc_date_format(), $new_payment_timestamp_user_time );
}
$response['status'] = 'success';
$response['message'] = '<div class="updated">' . __( 'Date Changed', 'woocommerce-subscriptions' ) . '</div>';
$response['dateToDisplay'] = $date_to_display;
$response['timestamp'] = $new_payment_timestamp_user_time;
}
}
echo wcs_json_encode( $response );
exit();
}
/**
* WP-Cron occasionally gets itself into an infinite loop on scheduled events, this function is
* designed to create a non-cron related safeguard against payments getting caught up in such a loop.
*
* When the scheduled subscription payment hook is fired by WP-Cron, this function is attached before
* any other to make sure the hook hasn't already fired for this period.
*
* A transient is used to keep a record of any payment for each period. The transient expiration is
* set to one billing period in the future, minus 1 hour, if there is a future payment due, otherwise,
* it is set to 23 hours in the future. This later option provides a safeguard in case a subscription's
* data is corrupted and the @see self::calculate_next_payment_date() is returning an
* invalid value. As no subscription can charge a payment more than once per day, the 23 hours is a safe
* throttle period for billing that still removes the possibility of a catastrophic failure (payments
* firing every few seconds until a credit card is maxed out).
*
* The transient keys use both the user ID and subscription key to ensure it is unique per subscription
* (even on multisite)
*
* @param int $user_id The id of the user who purchased the subscription
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.1.2
* @deprecated 2.0
*/
public static function safeguard_scheduled_payments( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0' );
}
/**
* When a subscription payment hook is fired, reschedule the hook to run again on the
* time/date of the next payment (if any).
*
* WP-Cron's built in wp_schedule_event() function can not be used because the recurrence
* must be a timestamp, which creates inaccurate schedules for month and year billing periods.
*
* @param int $user_id The id of the user who the subscription belongs to
* @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
* @since 1.1.5
* @deprecated 2.0
*/
public static function maybe_reschedule_subscription_payment( $user_id, $subscription_key ) {
_deprecated_function( __METHOD__, '2.0' );
$subscription = wcs_get_subscription_from_key( $subscription_key );
// Don't reschedule for cancelled, suspended or expired subscriptions
if ( ! $subscription->has_status( 'expired', 'cancelled', 'on-hold' ) ) {
// Reschedule the 'scheduled_subscription_payment' hook
if ( $subscription->can_date_be_updated( 'next_payment' ) ) {
$subscription->update_dates( array( 'next_payment' => $subscription->calculate_date( 'next_payment' ) ) );
do_action( 'rescheduled_subscription_payment', $user_id, $subscription_key );
}
}
}
/**
* Fires when the trial period for a subscription has completed.
*
* @param int $subscription_id The ID of a 'shop_subscription' post
* @since 1.0
* @deprecated 2.0
*/
public static function subscription_trial_end( $subscription_id, $deprecated = null ) {
_deprecated_function( __METHOD__, '2.0' );
}
}
WC_Subscriptions_Manager::init();