LLMS_Order

LifterLMS order model


Description Description

Provides CRUD operations for the llms_order post type.


Top ↑

Source Source

File: includes/models/model.llms.order.php

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
class LLMS_Order extends LLMS_Post_Model {
 
    /**
     * Database post type.
     *
     * @var string
     */
    protected $db_post_type = 'llms_order';
 
    /**
     * Model post type.
     *
     * @var string
     */
    protected $model_post_type = 'order';
 
    /**
     * Meta properties.
     *
     * @var array
     */
    protected $properties = array(
 
        'anonymized'           => 'yesno',
        'coupon_amount'        => 'float',
        'coupon_amout_trial'   => 'float',
        'coupon_value'         => 'float',
        'coupon_value_trial'   => 'float',
        'original_total'       => 'float',
        'sale_price'           => 'float',
        'sale_value'           => 'float',
        'total'                => 'float',
        'trial_original_total' => 'float',
        'trial_total'          => 'float',
 
        'access_length'        => 'absint',
        'billing_frequency'    => 'absint',
        'billing_length'       => 'absint',
        'coupon_id'            => 'absint',
        'plan_id'              => 'absint',
        'product_id'           => 'absint',
        'trial_length'         => 'absint',
        'user_id'              => 'absint',
 
        'access_expiration'    => 'text',
        'access_expires'       => 'text',
        'access_period'        => 'text',
        'billing_address_1'    => 'text',
        'billing_address_2'    => 'text',
        'billing_city'         => 'text',
        'billing_country'      => 'text',
        'billing_email'        => 'text',
        'billing_first_name'   => 'text',
        'billing_last_name'    => 'text',
        'billing_state'        => 'text',
        'billing_zip'          => 'text',
        'billing_period'       => 'text',
        'coupon_code'          => 'text',
        'coupon_type'          => 'text',
        'coupon_used'          => 'text',
        'currency'             => 'text',
        'on_sale'              => 'text',
        'order_key'            => 'text',
        'order_type'           => 'text',
        'payment_gateway'      => 'text',
        'plan_ended'           => 'yesno',
        'plan_sku'             => 'text',
        'plan_title'           => 'text',
        'product_sku'          => 'text',
        'product_type'         => 'text',
        'title'                => 'text',
        'gateway_api_mode'     => 'text',
        'gateway_customer_id'  => 'text',
        'trial_offer'          => 'text',
        'trial_period'         => 'text',
        'user_ip_address'      => 'text',
 
        'date_access_expires'  => 'text',
        'date_next_payment'    => 'text',
        'date_trial_end'       => 'text',
 
        'temp_gateway_ids'     => 'array',
 
    );
 
    /**
     * Add an admin-only note to the order visible on the admin panel
     * notes are recorded using the wp comments API & DB
     *
     * @since 3.0.0
     * @since 3.35.0 Sanitize $_SERVER data.
     *
     * @param string  $note          Note content.
     * @param boolean $added_by_user Optional. If this is an admin-submitted note adds user info to note meta. Default is false.
     * @return null|int Null on error or WP_Comment ID of the note.
     */
    public function add_note( $note, $added_by_user = false ) {
 
        if ( ! $note ) {
            return;
        }
 
        // Added by a user from the admin panel.
        if ( $added_by_user && is_user_logged_in() && current_user_can( apply_filters( 'lifterlms_admin_order_access', 'manage_options' ) ) ) {
 
            $user_id      = get_current_user_id();
            $user         = get_user_by( 'id', $user_id );
            $author       = $user->display_name;
            $author_email = $user->user_email;
 
        } else {
 
            $user_id       = 0;
            $author        = _x( 'LifterLMS', 'default order note author', 'lifterlms' );
            $author_email  = strtolower( _x( 'LifterLms', 'default order note author', 'lifterlms' ) ) . '@';
            $author_email .= isset( $_SERVER['HTTP_HOST'] ) ? str_replace( 'www.', '', sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : 'noreply.com';
            $author_email  = sanitize_email( $author_email );
 
        }
 
        $note_id = wp_insert_comment(
            apply_filters(
                'llms_add_order_note_content',
                array(
                    'comment_post_ID'      => $this->get( 'id' ),
                    'comment_author'       => $author,
                    'comment_author_email' => $author_email,
                    'comment_author_url'   => '',
                    'comment_content'      => $note,
                    'comment_type'         => 'llms_order_note',
                    'comment_parent'       => 0,
                    'user_id'              => $user_id,
                    'comment_approved'     => 1,
                    'comment_agent'        => 'LifterLMS',
                    'comment_date'         => current_time( 'mysql' ),
                )
            )
        );
 
        do_action( 'llms_new_order_note_added', $note_id, $this );
 
        return $note_id;
 
    }
 
    /**
     * Called after inserting a new order into the database
     *
     * @since 3.0.0
     *
     * @return void
     */
    protected function after_create() {
        // Add a random key that can be passed in the URL and whatever.
        $this->set( 'order_key', $this->generate_order_key() );
    }
 
    /**
     * Calculate the next payment due date
     *
     * @since 3.10.0
     * @since 3.12.0 Unknown.
     * @since 3.37.6 Now uses the last successful transaction time to calculate from when the previously
     *               stored next payment date is in the future.
     * @since 4.9.0 Fix comparison for PHP8 compat.
     * @since 5.3.0 Determine if a limited order has ended based on number of remaining payments in favor of current date/time.
     *
     * @param string $format PHP date format used to format the returned date string.
     * @return string The formatted next payment due date or an empty string when there is no next payment.
     */
    private function calculate_next_payment_date( $format = 'Y-m-d H:i:s' ) {
 
        // If the limited plan has already ended return early.
        $remaining = $this->get_remaining_payments();
        if ( 0 === $remaining ) {
            // This filter is documented below.
            return apply_filters( 'llms_order_calculate_next_payment_date', '', $format, $this );
        }
 
        $start_time        = $this->get_date( 'date', 'U' );
        $next_payment_time = $this->get_date( 'date_next_payment', 'U' );
        $last_txn_time     = $this->get_last_transaction_date( 'llms-txn-succeeded', 'recurring', 'U' );
 
        // If were on a trial and the trial hasn't ended yet next payment date is the date the trial ends.
        if ( $this->has_trial() && ! $this->has_trial_ended() ) {
 
            $next_payment_time = $this->get_trial_end_date( 'U' );
 
        } else {
 
            /**
             * Calculate next payment date from the saved `date_next_payment` calculated during
             * the previous recurring transaction or during order initialization.
             *
             * This condition will be encountered during the 2nd, 3rd, 4th, etc... recurring payments.
             */
            if ( $next_payment_time && $next_payment_time < llms_current_time( 'timestamp' ) ) {
 
                $from_time = $next_payment_time;
 
                /**
                 * Use the order's last successful transaction date.
                 *
                 * This will be encountered when any amount of "chaos" is
                 * introduced causing the previously stored `date_next_payment`
                 * to be GREATER than the current time.
                 *
                 * Orders created
                 */
            } elseif ( $last_txn_time && $last_txn_time > $start_time ) {
 
                $from_time = $last_txn_time;
 
                /**
                 * Use the order's creation time.
                 *
                 * This condition will be encountered for the 1st recurring payment only.
                 */
            } else {
 
                $from_time = $start_time;
 
            }
 
            $period            = $this->get( 'billing_period' );
            $frequency         = $this->get( 'billing_frequency' );
            $next_payment_time = strtotime( '+' . $frequency . ' ' . $period, $from_time );
 
            /**
             * Make sure the next payment is more than 2 hours in the future
             *
             * This ensures changes to the site's timezone because of daylight savings
             * will never cause a 2nd renewal payment to be processed on the same day.
             */
            $i = 1;
            while ( $next_payment_time < ( llms_current_time( 'timestamp', true ) + 2 * HOUR_IN_SECONDS ) && $i < 3000 ) {
                $next_payment_time = strtotime( '+' . $frequency . ' ' . $period, $next_payment_time );
                $i++;
            }
        }
 
        /**
         * Filter the calculated next payment date
         *
         * @since 3.10.0
         *
         * @param string     $ret    The formatted next payment due date or an empty string when there is no next payment.
         * @param string     $format The requested date format.
         * @param LLMS_Order $order  The order object.
         */
        return apply_filters( 'llms_order_calculate_next_payment_date', date( $format, $next_payment_time ), $format, $this );
 
    }
 
    /**
     * Calculate the end date of the trial
     *
     * @since 3.10.0
     *
     * @param string $format Optional. Desired return format of the date. Defalt is 'Y-m-d H:i:s'.
     * @return string
     */
    private function calculate_trial_end_date( $format = 'Y-m-d H:i:s' ) {
 
        $start = $this->get_date( 'date', 'U' ); // Start with the date the order was initially created.
 
        $length = $this->get( 'trial_length' );
        $period = $this->get( 'trial_period' );
 
        $end = strtotime( '+' . $length . ' ' . $period, $start );
 
        $ret = date_i18n( $format, $end );
 
        return apply_filters( 'llms_order_calculate_trial_end_date', $ret, $format, $this );
 
    }
 
    /**
     * Determines if an order can be confirmed.
     *
     * An order can be confirmed only when the order's status is pending.
     *
     * Additional requirements can be introduced via the filter `llms_order_can_be_confirmed`.
     *
     * @since 7.0.0
     *
     * @return boolean
     */
    public function can_be_confirmed() {
 
        /**
         * Determine if the order can be confirmed.
         *
         * @since 3.34.4
         *
         * @param boolean    $can_be_confirmed Whether or not the order can be confirmed.
         * @param LLMS_Order $order            Order object.
         * @param string     $gateway_id       Payment gateway ID.
         */
        return apply_filters(
            'llms_order_can_be_confirmed',
            ( 'llms-pending' === $this->get( 'status' ) ),
            $this,
            $this->get( 'payment_gateway' )
        );
 
    }
 
    /**
     * Determine if the order can be retried for recurring payments
     *
     * @since 3.10.0
     * @since 5.2.0 Use strict type comparison.
     * @since 5.2.1 Combine conditions that return `false`.
     *
     * @return boolean
     */
    public function can_be_retried() {
 
        $can_retry = true;
 
        if (
            // Only recurring orders can be retried.
            ! $this->is_recurring() ||
            // Recurring rety feature is disabled.
            ! llms_parse_bool( get_option( 'lifterlms_recurring_payment_retry', 'yes' ) ) ||
            // Only active & on-hold orders qualify for a retry.
            ! in_array( $this->get( 'status' ), array( 'llms-active', 'llms-on-hold' ), true )
        ) {
            $can_retry = false;
        } else {
 
            // If the gateway isn't active or the gateway doesn't support recurring retries.
            $gateway = $this->get_gateway();
            if ( is_wp_error( $gateway ) || ! $gateway->supports( 'recurring_retry' ) ) {
                $can_retry = false;
            }
        }
 
        /**
         * Filters whether or not a recurring order can be retried
         *
         * @since 5.2.1
         *
         * @param boolean    $can_retry Whether or not the order can be retried.
         * @param LLMS_Order $order     Order object.
         */
        return apply_filters( 'llms_order_can_be_retried', $can_retry, $this );
 
    }
 
    /**
     * Determines if the order can be resubscribed to.
     *
     * @since 3.19.0
     * @since 5.2.0 Use strict type comparison.
     *
     * @return bool
     */
    public function can_resubscribe() {
 
        $can_resubscribe = false;
 
        if ( $this->is_recurring() ) {
 
            /**
             * Filters the order statuses from which an order can be reactivated.
             *
             * @since 7.0.0
             *
             * @param string[] $allowed_statuses The list of allowed order statuses.
             */
            $allowed_statuses = apply_filters(
                'llms_order_status_can_resubscribe_from',
                array(
                    'llms-on-hold',
                    'llms-pending',
                    'llms-pending-cancel',
                )
            );
            $can_resubscribe  = in_array( $this->get( 'status' ), $allowed_statuses, true );
 
        }
 
        /**
         * Determines whether or not a user can resubscribe to an inactive recurring payment order.
         *
         * @since 3.19.0
         *
         * @param boolean    $can_resubscribe Whether or not a user can resubscribe.
         * @param LLMS_Order $order           The order object.
         */
        return apply_filters( 'llms_order_can_resubscribe', $can_resubscribe, $this );
 
    }
 
    /**
     * Determines if the order's payment source can be changed.
     *
     * @since 7.0.0
     *
     * @return boolean
     */
    public function can_switch_source() {
 
        $can_switch = 'llms-active' === $this->get( 'status' ) || $this->can_resubscribe();
 
        /**
         * Filters whether or not the order's payment source can be changed.
         *
         * @since 7.0.0
         *
         * @param boolean    $can_switch Whether or not the order's source can be switched.
         * @param LLMS_Order $order      The order object.
         */
        return apply_filters( 'llms_order_can_switch_source', $can_switch, $this );
 
    }
 
    /**
     * Generate an order key for the order
     *
     * @since 3.0.0
     *
     * @return string
     */
    public function generate_order_key() {
        /**
         * Modify the generated order key for the order.
         *
         * @since 3.0.0
         * @since 5.2.1 Added the `$order` parameter.
         *
         * @param string     $order_key The generated order key.
         * @param LLMS_Order $order_key Order object.
         */
        return apply_filters( 'lifterlms_generate_order_key', uniqid( 'order-' ), $this );
    }
 
    /**
     * Determine the date when access will expire
     *
     * Based on the access settings of the access plan
     * at the `$start_date` of access.
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     *
     * @param string $format Optional. Date format. Default is 'Y-m-d'.
     * @return string Date string.
     *                "Lifetime Access" for plans with lifetime access.
     *                "To be Determined" for limited date when access hasn't started yet.
     */
    public function get_access_expiration_date( $format = 'Y-m-d' ) {
 
        $type = $this->get( 'access_expiration' );
 
        $ret = $this->get_date( 'date_access_expires', $format );
        if ( ! $ret ) {
            switch ( $type ) {
                case 'lifetime':
                    $ret = __( 'Lifetime Access', 'lifterlms' );
                    break;
 
                case 'limited-date':
                    $ret = date_i18n( $format, ( $this->get_date( 'access_expires', 'U' ) + ( DAY_IN_SECONDS - 1 ) ) );
                    break;
 
                case 'limited-period':
                    if ( $this->get( 'start_date' ) ) {
                        $time = strtotime( '+' . $this->get( 'access_length' ) . ' ' . $this->get( 'access_period' ), $this->get_date( 'start_date', 'U' ) ) + ( DAY_IN_SECONDS - 1 );
                        $ret  = date_i18n( $format, $time );
                    } else {
                        $ret = __( 'To be Determined', 'lifterlms' );
                    }
                    break;
 
                default:
                    $ret = apply_filters( 'llms_order_' . $type . '_access_expiration_date', $type, $this, $format );
 
            }
        }
 
        return apply_filters( 'llms_order_get_access_expiration_date', $ret, $this, $format );
 
    }
 
    /**
     * Get the current status of a student's access
     *
     * Based on the access plan data stored on the order at the time of purchase.
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     * @since 5.2.0 Use stric type comparison.
     *
     * @return string 'inactive' If the order is refunded, failed, pending, etc...
     *                'expired'  If access has expired according to $this->get_access_expiration_date()
     *                'active'   Otherwise.
     */
    public function get_access_status() {
 
        $statuses = apply_filters(
            'llms_order_allow_access_stasuses',
            array(
                'llms-active',
                'llms-completed',
                'llms-pending-cancel',
                /**
                 * Recurring orders can expire but still grant access
                 * eg: 3monthly payments grants 1 year of access
                 * on the 4th month the order will be marked as expired
                 * but the access has not yet expired based on the data below.
                 */
                'llms-expired',
            )
        );
 
        // If the order doesn't have one of the allowed statuses.
        // Return 'inactive' and don't bother checking expiration data.
        if ( ! in_array( $this->get( 'status' ), $statuses, true ) ) {
 
            return 'inactive';
 
        }
 
        // Get the expiration date as a timestamp.
        $expires = $this->get_access_expiration_date( 'U' );
 
        /**
         * A translated non-numeric string will be returned for lifetime access
         * so if we have a timestamp we should compare it against the current time
         * to determine if access has expired.
         */
        if ( is_numeric( $expires ) ) {
 
            $now = llms_current_time( 'timestamp' );
 
            // Expiration date is in the past
            // eg: the access has already expired.
            if ( $expires < $now ) {
 
                return 'expired';
 
            }
        }
 
        // We're active.
        return 'active';
 
    }
 
    /**
     * Retrieve arguments passed to order-related events processed by the action scheduler
     *
     * @since 3.19.0
     *
     * @return array
     */
    protected function get_action_args() {
        return array(
            'order_id' => $this->get( 'id' ),
        );
    }
 
    /**
     * Get the formatted coupon amount with a currency symbol or percentage
     *
     * @since 3.0.0
     *
     * @param string $payment Coupon discount type, either 'regular' or 'trial'.
     * @return string
     */
    public function get_coupon_amount( $payment = 'regular' ) {
 
        if ( 'regular' === $payment ) {
            $amount = $this->get( 'coupon_amount' );
        } elseif ( 'trial' === $payment ) {
            $amount = $this->get( 'coupon_amount_trial' );
        }
 
        $type = $this->get( 'coupon_type' );
        if ( 'percent' === $type ) {
            $amount = $amount . '%';
        } elseif ( 'dollar' === $type ) {
            $amount = llms_price( $amount );
        }
        return $amount;
 
    }
 
    /**
     * Retrieve the customer's full name
     *
     * @since 3.0.0
     * @since 3.18.0 Unknown.
     *
     * @return string
     */
    public function get_customer_name() {
        if ( 'yes' === $this->get( 'anonymized' ) ) {
            return __( 'Anonymous', 'lifterlms' );
        }
        return trim( $this->get( 'billing_first_name' ) . ' ' . $this->get( 'billing_last_name' ) );
    }
 
    /**
     * Retrieve the customer's full billing address
     *
     * @since 5.2.0
     *
     * @return string
     */
    public function get_customer_full_address() {
 
        $billing_address_1 = $this->get( 'billing_address_1' );
        if ( empty( $billing_address_1 ) ) {
            return '';
        }
 
        $address   = array(
            trim( $billing_address_1 . ' ' . $this->get( 'billing_address_2' ) ),
        );
        $address[] = trim( $this->get( 'billing_city' ) . ' ' . $this->get( 'billing_state' ) );
        $address[] = $this->get( 'billing_zip' );
        $address[] = llms_get_country_name( $this->get( 'billing_country' ) );
 
        return implode( ', ', array_filter( $address ) );
    }
 
    /**
     * An array of default arguments to pass to $this->create() when creating a new post
     *
     * @since 3.0.0
     * @since 3.10.0 Unknown.
     * @since 5.3.1 Set the `post_date` property using `llms_current_time()`.
     * @since 5.9.0 Remove usage of deprecated `strftime()`.
     *
     * @param string $title Title to create the post with.
     * @return array
     */
    protected function get_creation_args( $title = '' ) {
 
        $date = llms_current_time( 'mysql' );
 
        if ( empty( $title ) ) {
 
            $title = sprintf(
                // Translators: %1$s = Transaction creation date.
                __( 'Order &ndash; %1$s', 'lifterlms' ),
                date_format( date_create( $date ), 'M d, Y @ h:i A' )
            );
 
        }
 
        return apply_filters(
            "llms_{$this->model_post_type}_get_creation_args",
            array(
                'comment_status' => 'closed',
                'ping_status'    => 'closed',
                'post_author'    => 1,
                'post_content'   => '',
                'post_date'      => $date,
                'post_excerpt'   => '',
                'post_password'  => uniqid( 'order_' ),
                'post_status'    => 'llms-' . apply_filters( 'llms_default_order_status', 'pending' ),
                'post_title'     => $title,
                'post_type'      => $this->get( 'db_post_type' ),
            ),
            $this
        );
    }
 
    /**
     * Retrieve the payment gateway instance for the order's selected payment gateway
     *
     * @since 1.0.0
     *
     * @return LLMS_Payment_Gateway|WP_Error Instance of the LLMS_Payment_Gateway extending class used for the payment.
     *                                       WP_Error if the gateway cannot be located, e.g. because it's no longer enabled.
     */
    public function get_gateway() {
        $gateways = llms()->payment_gateways();
        $gateway  = $gateways->get_gateway_by_id( $this->get( 'payment_gateway' ) );
        if ( $gateway && ( $gateway->is_enabled() || is_admin() ) ) {
            return $gateway;
        } else {
            return new WP_Error( 'error', sprintf( __( 'Payment gateway %s could not be located or is no longer enabled', 'lifterlms' ), $this->get( 'payment_gateway' ) ) );
        }
    }
 
    /**
     * Get the initial payment amount due on checkout
     *
     * This will always be the value of "total" except when the product has a trial.
     *
     * @since 3.0.0
     *
     * @return mixed
     */
    public function get_initial_price( $price_args = array(), $format = 'html' ) {
 
        if ( $this->has_trial() ) {
            $price = 'trial_total';
        } else {
            $price = 'total';
        }
 
        return $this->get_price( $price, $price_args, $format );
    }
 
 
    /**
     * Get an array of the order notes
     *
     * Each note is actually a WordPress comment.
     *
     * @since 3.0.0
     *
     * @param integer $number Number of comments to return.
     * @param integer $page   Page number for pagination.
     * @return array
     */
    public function get_notes( $number = 10, $page = 1 ) {
 
        $comments = get_comments(
            array(
                'status'  => 'approve',
                'number'  => $number,
                'offset'  => ( $page - 1 ) * $number,
                'post_id' => $this->get( 'id' ),
            )
        );
 
        return $comments;
 
    }
 
    /**
     * Retrieve an LLMS_Post_Model object for the associated product
     *
     * @since 3.8.0
     *
     * @return LLMS_Post_Model|WP_Post|null|false LLMS_Post_Model extended object (LLMS_Course|LLMS_Membership),
     *                                            null if WP get_post() fails,
     *                                            false if LLMS_Post_Model extended class isn't found.
     */
    public function get_product() {
        return llms_get_post( $this->get( 'product_id' ) );
    }
 
    /**
     * Retrieve the last (most recent) transaction processed for the order.
     *
     * @since 3.0.0
     * @since 7.1.0 Skip counting the total rows found when retrieving the last transaction.
     *
     * @param array|string $status Filter by status (see transaction statuses). By default looks for any status.
     * @param array|string $type   Filter by type [recurring|single|trial]. By default looks for any type.
     * @return LLMS_Transaction|false instance of the LLMS_Transaction or false if none found
     */
    public function get_last_transaction( $status = 'any', $type = 'any' ) {
        $txns = $this->get_transactions(
            array(
                'per_page'      => 1,
                'status'        => $status,
                'type'          => $type,
                'no_found_rows' => true,
            )
        );
        if ( $txns['count'] ) {
            return array_pop( $txns['transactions'] );
        }
        return false;
    }
 
    /**
     * Retrieve the date of the last (most recent) transaction
     *
     * @since 3.0.0
     *
     * @param array|string $status Optional. Filter by status (see transaction statuses). Default is 'llms-txn-succeeded'.
     * @param array|string $type   Optional. Filter by type [recurring|single|trial]. By default looks for any type.
     * @param string       $format Optional. Date format of the return. Default is 'Y-m-d H:i:s'.
     * @return string|false Date or false if none found.
     */
    public function get_last_transaction_date( $status = 'llms-txn-succeeded', $type = 'any', $format = 'Y-m-d H:i:s' ) {
        $txn = $this->get_last_transaction( $status, $type );
        if ( $txn ) {
            return $txn->get_date( 'date', $format );
        } else {
            return false;
        }
    }
 
    /**
     * Retrieve the due date of the next payment according to access plan terms
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     * @since 5.2.0 Use stric type comparisons.
     *
     * @param string $format Optional. Date return format. Default is 'Y-m-d H:i:s'.
     * @return string
     */
    public function get_next_payment_due_date( $format = 'Y-m-d H:i:s' ) {
 
        // Single payments will never have a next payment date.
        if ( ! $this->is_recurring() ) {
            return new WP_Error( 'not-recurring', __( 'Order is not recurring', 'lifterlms' ) );
        } elseif ( ! in_array( $this->get( 'status' ), array( 'llms-active', 'llms-failed', 'llms-on-hold', 'llms-pending', 'llms-pending-cancel' ), true ) ) {
            return new WP_Error( 'invalid-status', __( 'Invalid order status', 'lifterlms' ), $this->get( 'status' ) );
        }
 
        // Retrieve the saved due date.
        $next_payment_time = $this->get_date( 'date_next_payment', 'U' );
        // Calculate it if not saved.
        if ( ! $next_payment_time ) {
            $next_payment_time = $this->calculate_next_payment_date( 'U' );
            if ( ! $next_payment_time ) {
                return new WP_Error( 'plan-ended', __( 'No more payments due', 'lifterlms' ) );
            }
        }
 
        /**
         * Filter the next payment due date.
         *
         * A timestamp should always be returned as the conversion to the requested format
         * will be performed on the returned value.
         *
         * @since 3.0.0
         *
         * @param int        $next_payment_time Unix timestamp for the next payment due date.
         * @param LLMS_Order $order             Order object.
         * @param string     $format            Requested date format.
         */
        $next_payment_time = apply_filters( 'llms_order_get_next_payment_due_date', $next_payment_time, $this, $format );
 
        return date_i18n( $format, $next_payment_time );
 
    }
 
    /**
     * Retrieve the timestamp of the next scheduled event for a given action
     *
     * @since 4.6.0
     *
     * @param string $action Action hook ID. Core actions are "llms_charge_recurring_payment", "llms_access_plan_expiration".
     * @return int|false Returns the timestamp of the next action as an integer or `false` when no action exist.
     */
    public function get_next_scheduled_action_time( $action ) {
        return as_next_scheduled_action( $action, $this->get_action_args() );
    }
 
    /**
     * Retrieves the number of payments remaining for a recurring plan with a limited number of payments
     *
     * @since 5.3.0
     *
     * @return bool|int Returns `false` for invalid order types (single-payment orders or recurring orders
     *                  without a billing length). Otherwise returns the number of remaining payments as an integer.
     */
    public function get_remaining_payments() {
 
        $remaining = false;
 
        if ( $this->has_plan_expiration() ) {
            $len  = $this->get( 'billing_length' );
            $txns = $this->get_transactions(
                array(
                    'status'   => array( 'llms-txn-succeeded', 'llms-txn-refunded' ),
                    'per_page' => 1,
                    'type'     => array( 'recurring', 'single' ), // If a manual payment is recorded it's counted a single payment and that should count.
                )
            );
 
            $remaining = $len - $txns['total'];
        }
 
        /**
         * Filters the number of payments remaining for a recurring plan with a limited number of payments.
         *
         * @since 5.3.0
         *
         * @param bool|int   $remaining Number of remaining payments or `false` when called against invalid order types.
         * @param LLMS_Order $order     Order object.
         */
        return apply_filters( 'llms_order_remaining_payments', $remaining, $this );
 
    }
 
    /**
     * Get configured payment retry rules
     *
     * @since 3.10.0
     *
     * @return array[] {
     *     An array of retry rule arrays.
     *
     *     @type int    $delay         The number of seconds to delay to use when scheduling the retry attempt.
     *     @type string $status        The status of the order while awaiting the next retry.
     *     @type bool   $notifications Whether or not to trigger notifications to the student/user.
     * }
     */
    private function get_retry_rules() {
 
        $rules = array(
            array(
                'delay'         => HOUR_IN_SECONDS * 12,
                'status'        => 'on-hold',
                'notifications' => false,
            ),
            array(
                'delay'         => DAY_IN_SECONDS,
                'status'        => 'on-hold',
                'notifications' => true,
            ),
            array(
                'delay'         => DAY_IN_SECONDS * 2,
                'status'        => 'on-hold',
                'notifications' => true,
            ),
            array(
                'delay'         => DAY_IN_SECONDS * 3,
                'status'        => 'on-hold',
                'notifications' => true,
            ),
        );
 
        /**
         * Filters the automatic payment recurring retry rules.
         *
         * @since 7.0.0
         *
         * @param array      $rules Array of retry rule arrays {@see LLMS_Order::get_retry_rules()}.
         * @param LLMS_Order $rules The order object.
         */
        return apply_filters( 'llms_order_automatic_retry_rules', $rules, $this );
 
    }
 
    /**
     * SQL query to retrieve total amounts for transactions by type
     *
     * @since 3.0.0
     * @since 3.35.0 Prepare SQL query properly.
     *
     * @param string $type Optional. Type can be 'amount' or 'refund_amount'. Default is 'amount'.
     * @return float
     */
    public function get_transaction_total( $type = 'amount' ) {
 
        $statuses = array( 'llms-txn-refunded' );
 
        if ( 'amount' === $type ) {
            $statuses[] = 'llms-txn-succeeded';
        }
 
        $post_statuses = '';
        foreach ( $statuses as $i => $status ) {
            $post_statuses .= " p.post_status = '$status'";
            if ( $i + 1 < count( $statuses ) ) {
                $post_statuses .= 'OR';
            }
        }
 
        global $wpdb;
        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $post_statuses is prepared above.
        $grosse = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT SUM( m2.meta_value )
             FROM $wpdb->posts AS p
             LEFT JOIN $wpdb->postmeta AS m1 ON m1.post_id = p.ID -- Join for the ID.
             LEFT JOIN $wpdb->postmeta AS m2 ON m2.post_id = p.ID -- Get the actual amounts.
             WHERE p.post_type = 'llms_transaction'
               AND ( $post_statuses )
               AND m1.meta_key = %s
               AND m1.meta_value = %d
               AND m2.meta_key = %s
            ;",
                array(
                    "{$this->meta_prefix}order_id",
                    $this->get( 'id' ),
                    "{$this->meta_prefix}{$type}",
                )
            )
        ); // db call ok; no-cache ok.
        // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
 
        return floatval( $grosse );
    }
 
    /**
     * Get the start date for the order.
     *
     * Gets the date of the first initially successful transaction
     * if none found, uses the created date of the order.
     *
     * @since 3.0.0
     * @since 7.1.0 Skip counting the total rows found when retrieving the first transaction.
     *
     * @param string $format Desired return format of the date.
     * @return string
     */
    public function get_start_date( $format = 'Y-m-d H:i:s' ) {
        /**
         * Get the first recorded transaction.
         * Refunds are okay b/c that would have initially given the user access.
         */
        $txns = $this->get_transactions(
            array(
                'order'         => 'ASC',
                'orderby'       => 'date',
                'per_page'      => 1,
                'status'        => array( 'llms-txn-succeeded', 'llms-txn-refunded' ),
                'type'          => 'any',
                'no_found_rows' => true,
            )
        );
        if ( $txns['count'] ) {
            $txn  = array_pop( $txns['transactions'] );
            $date = $txn->get_date( 'date', $format );
        } else {
            $date = $this->get_date( 'date', $format );
        }
 
        /**
         * Filter the order start date.
         *
         * @since 3.0.0
         * @since 7.1.0 Added the `$format` parameter.
         *
         * @param string     $date   The formatted start date for the order.
         * @param LLMS_Order $order  The order object.
         * @param string     $format The requested format of the date.
         */
        return apply_filters( 'llms_order_get_start_date', $date, $this, $format );
    }
 
    /**
     * Retrieves the user action required when changing the order's payment source.
     *
     * @since 7.0.0
     *
     * @return null|string Returns `switch` when the payment source can be switched and `pay` when payment on the new source
     *                     is required before switching. A `null` return indicates that the order's payment source cannot be switched.
     */
    public function get_switch_source_action() {
 
        $action = null;
        if ( $this->can_switch_source() ) {
            $action = in_array( $this->get( 'status' ), array( 'llms-active', 'llms-pending-cancel' ), true ) ? 'switch' : 'pay';
        }
 
        /**
         * Filters the required user action for the order when switching the order's payment source.
         *
         * @since 7.0.0
         *
         * @param null|string $action The switch action ID or `null` when the payment source cannot be switched.
         * @param LLMS_Order  $order  The order object.
         */
        return apply_filters( 'llms_order_switch_source_action', $action, $this );
 
    }
 
    /**
     * Retrieve an array of transactions associated with the order according to supplied arguments.
     *
     * @since 3.0.0
     * @since 3.10.0 Unknown.
     * @since 3.37.6 Add additional return property, `total`, which returns the total number of found transactions.
     * @since 5.2.0 Use stric type comparisons.
     * @since 7.1.0 Added `no_found_rows` parameter.
     *
     * @param array $args {
     *     Hash of query argument data, ultimately passed to a WP_Query.
     *
     *     @type string|string[] $status        Transaction post status or array of transaction post status. Defaults to "any".
     *     @type string|string[] $type          Transaction types or array of transaction types. Defaults to "any".
     *                                          Accepts "recurring", "single", or "trial".
     *     @type int             $per_page      Number of transactions to include in the return. Default `50`.
     *     @type int             $paged         Result set page number.
     *     @type string          $order         Result set order. Default "DESC". Accepts "DESC" or "ASC".
     *     @type string          $orderby       Result set ordering field. Default "date".
     *     @type bool            $no_found_rows Whether to skip counting the total rows found. Enabling can improve
     *                                          performance. Default `false`.
     * }
     * @return array
     */
    public function get_transactions( $args = array() ) {
 
        extract(
            wp_parse_args(
                $args,
                array(
                    'status'        => 'any', // String or array or post statuses.
                    'type'          => 'any', // String or array of transaction types [recurring|single|trial].
                    'per_page'      => 50, // Int, number of transactions to return.
                    'paged'         => 1, // Int, page number of transactions to return.
                    'order'         => 'DESC',
                    'orderby'       => 'date', // Field to order results by.
                    'no_found_rows' => false,
                )
            )
        );
 
        // Assume any and use this to check for valid statuses.
        $statuses = llms_get_transaction_statuses();
 
        // Check statuses.
        if ( 'any' !== $statuses ) {
 
            // If status is a string, ensure it's a valid status.
            if ( is_string( $status ) && in_array( $status, $statuses, true ) ) {
                $statuses = array( $status );
            } elseif ( is_array( $status ) ) {
                $temp = array();
                foreach ( $status as $stat ) {
                    if ( in_array( (string) $stat, $statuses, true ) ) {
                        $temp[] = $stat;
                    }
                }
                $statuses = $temp;
            }
        }
 
        // Setup type meta query.
        $types = array(
            'relation' => 'OR',
        );
 
        if ( 'any' === $type ) {
            $types[] = array(
                'key'   => $this->meta_prefix . 'payment_type',
                'value' => 'recurring',
            );
            $types[] = array(
                'key'   => $this->meta_prefix . 'payment_type',
                'value' => 'single',
            );
            $types[] = array(
                'key'   => $this->meta_prefix . 'payment_type',
                'value' => 'trial',
            );
        } elseif ( is_string( $type ) ) {
            $types[] = array(
                'key'   => $this->meta_prefix . 'payment_type',
                'value' => $type,
            );
        } elseif ( is_array( $type ) ) {
            foreach ( $type as $t ) {
                $types[] = array(
                    'key'   => $this->meta_prefix . 'payment_type',
                    'value' => $t,
                );
            }
        }
 
        // Execute the query.
        $query = new WP_Query(
            /**
             * Filters the order's transactions query aguments.
             *
             * @since 3.0.0
             * @since 7.1.0 Added `$no_found_rows` arg.
             *
             * @param array $query_args {
             *     Hash of query argument data passed to a WP_Query.
             *
             *     @type string|string[] $status        Transaction post status or array of transaction post status.
             *                                          Defaults to "any".
             *     @type string|string[] $type          Transaction types or array of transaction types.
             *                                          Defaults to "any".
             *                                          Accepts "recurring", "single", or "trial".
             *     @type int             $per_page      Number of transactions to include in the return. Default `50`.
             *     @type int             $paged         Result set page number.
             *     @type string          $order         Result set order. Default "DESC". Accepts "DESC" or "ASC".
             *     @type string          $orderby       Result set ordering field. Default "date".
             *     @type bool            $no_found_rows Whether to skip counting the total rows found.
             *                                          Enabling can improve performance. Default false.
             * }
             */
            apply_filters(
                'llms_order_get_transactions_query',
                array(
                    'meta_query'     => array(
                        'relation' => 'AND',
                        array(
                            'key'   => $this->meta_prefix . 'order_id',
                            'value' => $this->get( 'id' ),
                        ),
                        $types,
                    ),
                    'order'          => $order,
                    'orderby'        => $orderby,
                    'post_status'    => $statuses,
                    'post_type'      => 'llms_transaction',
                    'posts_per_page' => $per_page,
                    'paged'          => $paged,
                    'no_found_rows'  => $no_found_rows,
                )
            ),
            $this,
            $status
        );
 
        $transactions = array();
 
        foreach ( $query->posts as $post ) {
            $transactions[ $post->ID ] = llms_get_post( $post );
        }
 
        return array(
            'total'        => $query->found_posts,
            'count'        => $query->post_count,
            'page'         => $paged,
            'pages'        => $query->max_num_pages,
            'transactions' => $transactions,
        );
 
    }
 
    /**
     * Retrieve the date when a trial will end
     *
     * @since 3.0.0
     *
     * @param string $format Optional. Date return format. Default is 'Y-m-d H:i:s'.
     * @return string
     */
    public function get_trial_end_date( $format = 'Y-m-d H:i:s' ) {
 
        if ( ! $this->has_trial() ) {
 
            $trial_end_date = '';
 
        } else {
 
            // Retrieve the saved end date.
            $trial_end_date = $this->get_date( 'date_trial_end', $format );
 
            // If not saved, calculate it.
            if ( ! $trial_end_date ) {
 
                $trial_end_date = $this->calculate_trial_end_date( $format );
 
            }
        }
 
        return apply_filters( 'llms_order_get_trial_end_date', $trial_end_date, $this );
 
    }
 
    /**
     * Gets the total revenue of an order
     *
     * @since 3.0.0
     * @since 3.1.3 Handle legacy orders.
     *
     * @param string $type Optional. Revenue type [grosse|net]. Default is 'net'.
     * @return float
     */
    public function get_revenue( $type = 'net' ) {
 
        if ( $this->is_legacy() ) {
 
            $amount = $this->get( 'total' );
 
        } else {
 
            $amount = $this->get_transaction_total( 'amount' );
 
            if ( 'net' === $type ) {
 
                $refunds = $this->get_transaction_total( 'refund_amount' );
 
                $amount = $amount - $refunds;
 
            }
        }
 
        return apply_filters( 'llms_order_get_revenue', $amount, $type, $this );
 
    }
 
    /**
     * Get a link to view the order on the student dashboard
     *
     * @since 3.0.0
     * @since 3.8.0 Unknown.
     *
     * @return string
     */
    public function get_view_link() {
 
        $link = llms_get_endpoint_url( 'orders', $this->get( 'id' ), llms_get_page_url( 'myaccount' ) );
        return apply_filters( 'llms_order_get_view_link', $link, $this );
 
    }
 
    /**
     * Determine if the student associated with this order has access
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function has_access() {
        return ( 'active' === $this->get_access_status() ) ? true : false;
    }
 
    /**
     * Determine if a coupon was used
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function has_coupon() {
        return ( 'yes' === $this->get( 'coupon_used' ) );
    }
 
    /**
     * Determine if there was a discount applied to this order via either a sale or a coupon
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function has_discount() {
        return ( $this->has_coupon() || $this->has_sale() );
    }
 
    /**
     * Determine if a recurring order has a limited number of payments
     *
     * @since 5.3.0
     *
     * @return boolean Returns `true` for recurring orders with a billing length and `false` otherwise.
     */
    public function has_plan_expiration() {
        return ( $this->is_recurring() && ( $this->get( 'billing_length' ) > 0 ) );
    }
 
    /**
     * Determine if the access plan was on sale during the purchase
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function has_sale() {
        return ( 'yes' === $this->get( 'on_sale' ) );
    }
 
    /**
     * Determine if there's a payment scheduled for the order
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function has_scheduled_payment() {
        $date = $this->get_next_payment_due_date();
        return is_wp_error( $date ) ? false : true;
    }
 
    /**
     * Determine if the order has a trial
     *
     * @since 3.0.0
     *
     * @return boolean True if has a trial, false if it doesn't.
     */
    public function has_trial() {
        return ( $this->is_recurring() && 'yes' === $this->get( 'trial_offer' ) );
    }
 
    /**
     * Determine if the trial period has ended for the order
     *
     * @since 3.0.0
     * @since 3.10.0 Unknown.
     *
     * @return boolean True if ended, false if not ended.
     */
    public function has_trial_ended() {
        return ( llms_current_time( 'timestamp' ) >= $this->get_trial_end_date( 'U' ) );
    }
 
    /**
     * Initializes a new order with user, plan, gateway, and coupon metadata.
     *
     * Assumes all data passed in has already been validated.
     *
     * @since 3.8.0
     * @since 3.10.0 Unknown.
     * @since 5.3.0 Don't set unused legacy property `date_billing_end`.
     * @since 7.0.0 Use `LLMS_Order::set_user_data()` to update user data.
     *
     * @param array|LLMS_Student|WP_User|integer $user_data User info for the person placing the order. See
     *                                                      {@see LLMS_Order::set_user_data()} for more info.
     * @param LLMS_Access_Plan                   $plan      The purchase access plan.
     * @param LLMS_Payment_Gateway               $gateway   Gateway being used.
     * @param LLMS_Coupon                        $coupon    Coupon object or `false` if no coupon used.
     * @return LLMS_Order
     */
    public function init( $user_data, $plan, $gateway, $coupon = false ) {
 
        $this->set_user_data( $user_data );
 
        // Access plan data.
        $this->set( 'plan_id', $plan->get( 'id' ) );
        $this->set( 'plan_title', $plan->get( 'title' ) );
        $this->set( 'plan_sku', $plan->get( 'sku' ) );
 
        // Product data.
        $product = $plan->get_product();
        $this->set( 'product_id', $product->get( 'id' ) );
        $this->set( 'product_title', $product->get( 'title' ) );
        $this->set( 'product_sku', $product->get( 'sku' ) );
        $this->set( 'product_type', $plan->get_product_type() );
 
        $this->set( 'payment_gateway', $gateway->get_id() );
        $this->set( 'gateway_api_mode', $gateway->get_api_mode() );
 
        // Trial data.
        if ( $plan->has_trial() ) {
            $this->set( 'trial_offer', 'yes' );
            $this->set( 'trial_length', $plan->get( 'trial_length' ) );
            $this->set( 'trial_period', $plan->get( 'trial_period' ) );
            $trial_price = $plan->get_price( 'trial_price', array(), 'float' );
            $this->set( 'trial_original_total', $trial_price );
            $trial_total = $coupon ? $plan->get_price_with_coupon( 'trial_price', $coupon, array(), 'float' ) : $trial_price;
            $this->set( 'trial_total', $trial_total );
            $this->set( 'date_trial_end', $this->calculate_trial_end_date() );
        } else {
            $this->set( 'trial_offer', 'no' );
        }
 
        $price = $plan->get_price( 'price', array(), 'float' );
        $this->set( 'currency', get_lifterlms_currency() );
 
        // Price data.
        if ( $plan->is_on_sale() ) {
            $price_key = 'sale_price';
            $this->set( 'on_sale', 'yes' );
            $sale_price = $plan->get( 'sale_price', array(), 'float' );
            $this->set( 'sale_price', $sale_price );
            $this->set( 'sale_value', $price - $sale_price );
        } else {
            $price_key = 'price';
            $this->set( 'on_sale', 'no' );
        }
 
        // Store original total before any discounts.
        $this->set( 'original_total', $price );
 
        // Get the actual total due after discounts if any are applicable.
        $total = $coupon ? $plan->get_price_with_coupon( $price_key, $coupon, array(), 'float' ) : $$price_key;
        $this->set( 'total', $total );
 
        // Coupon data.
        if ( $coupon ) {
            $this->set( 'coupon_id', $coupon->get( 'id' ) );
            $this->set( 'coupon_amount', $coupon->get( 'coupon_amount' ) );
            $this->set( 'coupon_code', $coupon->get( 'title' ) );
            $this->set( 'coupon_type', $coupon->get( 'discount_type' ) );
            $this->set( 'coupon_used', 'yes' );
            $this->set( 'coupon_value', $$price_key - $total );
            if ( $plan->has_trial() && $coupon->has_trial_discount() ) {
                $this->set( 'coupon_amount_trial', $coupon->get( 'trial_amount' ) );
                $this->set( 'coupon_value_trial', $trial_price - $trial_total );
            }
        } else {
            $this->set( 'coupon_used', 'no' );
        }
 
        // Get all billing schedule related information.
        $this->set( 'billing_frequency', $plan->get( 'frequency' ) );
        if ( $plan->is_recurring() ) {
            $this->set( 'billing_length', $plan->get( 'length' ) );
            $this->set( 'billing_period', $plan->get( 'period' ) );
            $this->set( 'order_type', 'recurring' );
            $this->set( 'date_next_payment', $this->calculate_next_payment_date() );
        } else {
            $this->set( 'order_type', 'single' );
        }
 
        $this->set( 'access_expiration', $plan->get( 'access_expiration' ) );
 
        // Get access related data so when payment is complete we can calculate the actual expiration date.
        if ( $plan->can_expire() ) {
            $this->set( 'access_expires', $plan->get( 'access_expires' ) );
            $this->set( 'access_length', $plan->get( 'access_length' ) );
            $this->set( 'access_period', $plan->get( 'access_period' ) );
        }
 
        /**
         * Action triggered after the order is initialized.
         *
         * @since Unknown.
         * @since 7.0.0 Added `$user_data` parameter.
         *                 The `$student` parameter returns an "empty" student object
         *                 if the method's input data is an array instead of an existing
         *                 user object.
         *
         * @param LLMS_Order                         $order     The order object.
         * @param LLMS_Student                       $student   The student object. If an array of data is passed
         *                                                      to `LLMS_Order::init()` then an empty student object
         *                                                      will be passed.
         * @param array|LLMS_Student|WP_User|integer $user_data User data.
         */
        do_action(
            'lifterlms_new_pending_order',
            $this,
            is_array( $user_data ) ? new LLMS_Student( null, false ) : llms_get_student( $user_data ),
            $user_data
        );
 
        return $this;
 
    }
 
    /**
     * Determine if the order is a legacy order migrated from 2.x
     *
     * @since 3.0.0
     *
     * @return boolean
     */
    public function is_legacy() {
        return ( 'publish' === $this->get( 'status' ) );
    }
 
    /**
     * Determine if the order is recurring or singular
     *
     * @since 3.0.0
     *
     * @return boolean True if recurring, false if not.
     */
    public function is_recurring() {
        return $this->get( 'order_type' ) === 'recurring';
    }
 
    /**
     * Schedule access expiration
     *
     * @since 3.19.0
     * @since 3.32.0 Update to use latest action-scheduler functions.
     *
     * @return void
     */
    public function maybe_schedule_expiration() {
 
        // Get expiration date based on setting.
        $expires = $this->get_access_expiration_date( 'U' );
 
        // Will return a timestamp or "Lifetime Access as a string".
        if ( is_numeric( $expires ) ) {
            $this->unschedule_expiration();
            as_schedule_single_action( $expires, 'llms_access_plan_expiration', $this->get_action_args() );
        }
 
    }
 
    /**
     * Schedules the next payment due on a recurring order
     *
     * Can be called without consequence on a single payment order.
     * Will always unschedule the scheduled action (if one exists) before scheduling another.
     *
     * @since 3.0.0
     * @since 3.32.0 Update to use latest action-scheduler functions.
     * @since 4.7.0 Add `plan_ended` metadata when a plan ends.
     * @since 5.2.0 Move scheduling recurring payment into a proper method.
     *
     * @return void
     */
    public function maybe_schedule_payment( $recalc = true ) {
 
        if ( ! $this->is_recurring() ) {
            return;
        }
 
        if ( $recalc ) {
            $this->set( 'date_next_payment', $this->calculate_next_payment_date() );
        }
 
        $date = $this->get_next_payment_due_date();
 
        // Unschedule and reschedule.
        if ( $date && ! is_wp_error( $date ) ) {
 
            $this->schedule_recurring_payment( $date );
 
        } elseif ( is_wp_error( $date ) ) {
 
            if ( 'plan-ended' === $date->get_error_code() ) {
 
                // Unschedule the next action (does nothing if no action scheduled).
                $this->unschedule_recurring_payment();
 
                // Add a note that the plan has completed.
                $this->add_note( __( 'Order payment plan completed.', 'lifterlms' ) );
                $this->set( 'plan_ended', 'yes' );
 
            }
        }
 
    }
 
    /**
     * Handles scheduling recurring payment retries when the gateway supports them
     *
     * @since 3.10.0
     * @since 7.0.0 Added return value.
     *
     * @return null|boolean Returns `null` if the order cannot be retried, `false` when all retry rules have been tried (or none exist), and `true`
     *                      when a retry is scheduled.
     */
    public function maybe_schedule_retry() {
 
        if ( ! $this->can_be_retried() ) {
            return null;
        }
 
        // Get the index of the rule to use for this retry.
        $current_rule_index = $this->get( 'last_retry_rule' );
        if ( '' === $current_rule_index ) {
            $current_rule_index = 0;
        } else {
            ++$current_rule_index;
        }
 
        $rules        = $this->get_retry_rules();
        $current_rule = $rules[ $current_rule_index ] ?? false;
 
        // No rule to run.
        if ( ! $current_rule ) {
 
            $this->set_status( 'failed' );
            $this->set( 'last_retry_rule', '' );
 
            $this->add_note( esc_html__( 'Maximum retry attempts reached.', 'lifterlms' ) );
 
            /**
             * Action triggered when there are not more recurring payment retry rules.
             *
             * @since 3.10.0
             *
             * @param LLMS_Order $order The order object.
             */
            do_action( 'llms_automatic_payment_maximum_retries_reached', $this );
 
            return false;
 
        }
 
        $timestamp = current_time( 'timestamp' ) + $current_rule['delay'];
 
        $this->set_date( 'next_payment', date_i18n( 'Y-m-d H:i:s', $timestamp ) );
        $this->set_status( $current_rule['status'] );
        $this->set( 'last_retry_rule', $current_rule_index );
 
        $this->add_note(
            sprintf(
                // Translators: %s = next attempt date.
                esc_html__( 'Automatic retry attempt scheduled for %s', 'lifterlms' ),
                date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp )
            )
        );
 
        // If notifications should be sent, trigger them.
        if ( $current_rule['notifications'] ) {
            /**
             * Triggers the "Payment Retry Scheduled" notification.
             *
             * @since 3.10.0
             *
             * @param LLMS_Order $order The order object.
             */
            do_action( 'llms_send_automatic_payment_retry_notification', $this );
        }
 
        /**
         * Action triggered after a recurring payment retry is successfully scheduled.
         *
         * @since 3.10.0
         *
         * @param LLMS_Order $order The order object.
         */
        do_action( 'llms_automatic_payment_retry_scheduled', $this );
 
        return true;
 
    }
 
    /**
     * Record a transaction for the order
     *
     * @since 3.0.0
     *
     * @param array $data Optional array of additional data to store for the transaction.
     * @return LLMS_Transaction Instance of LLMS_Transaction for the created transaction.
     */
    public function record_transaction( $data = array() ) {
 
        extract(
            array_merge(
                array(
                    'amount'             => 0,
                    'completed_date'     => current_time( 'mysql' ),
                    'customer_id'        => '',
                    'fee_amount'         => 0,
                    'source_id'          => '',
                    'source_description' => '',
                    'transaction_id'     => '',
                    'status'             => 'llms-txn-succeeded',
                    'payment_gateway'    => $this->get( 'payment_gateway' ),
                    'payment_type'       => 'single',
                ),
                $data
            )
        );
 
        $txn = new LLMS_Transaction( 'new', $this->get( 'id' ) );
 
        $txn->set( 'api_mode', $this->get( 'gateway_api_mode' ) );
        $txn->set( 'amount', $amount );
        $txn->set( 'currency', $this->get( 'currency' ) );
        $txn->set( 'gateway_completed_date', date_i18n( 'Y-m-d h:i:s', strtotime( $completed_date ) ) );
        $txn->set( 'gateway_customer_id', $customer_id );
        $txn->set( 'gateway_fee_amount', $fee_amount );
        $txn->set( 'gateway_source_id', $source_id );
        $txn->set( 'gateway_source_description', $source_description );
        $txn->set( 'gateway_transaction_id', $transaction_id );
        $txn->set( 'order_id', $this->get( 'id' ) );
        $txn->set( 'payment_gateway', $payment_gateway );
        $txn->set( 'payment_type', $payment_type );
        $txn->set( 'status', $status );
 
        return $txn;
 
    }
 
    /**
     * Date field setter for date fields that require things to be updated when their value changes
     *
     * This is mainly used to allow updating dates which are editable from the admin panel which
     * should trigger additional actions when updated.
     *
     * Settable dates: date_next_payment, date_trial_end, date_access_expires.
     *
     * @since 3.10.0
     * @since 3.19.0 Unknown.
     *
     * @param string $date_key Date field to set.
     * @param string $date_val Date string or a unix time stamp.
     */
    public function set_date( $date_key, $date_val ) {
 
        // Convert to timestamp if not already a timestamp.
        if ( ! is_numeric( $date_val ) ) {
            $date_val = strtotime( $date_val );
        }
 
        $this->set( 'date_' . $date_key, date( 'Y-m-d H:i:s', $date_val ) );
 
        switch ( $date_key ) {
 
            // Reschedule access expiration.
            case 'access_expires':
                $this->maybe_schedule_expiration();
                break;
 
            // Additionally update the next payment date & don't break because we want to reschedule payments too.
            case 'trial_end':
                $this->set_date( 'next_payment', $this->calculate_next_payment_date( 'U' ) );
 
                // Everything else reschedule's payments.
            default:
                $this->maybe_schedule_payment( false );
 
        }
 
    }
 
    /**
     * Update the status of an order
     *
     * @since 3.8.0
     * @since 3.10.0 Unknown.
     * @since 5.2.0 Prefer `array_key_exists( $key, $keys )` over `in_array( $key, array_keys( $assoc_array ) )`.
     *
     * @param string $status Status name, accepts unprefixed statuses.
     * @return void
     */
    public function set_status( $status ) {
 
        if ( false === strpos( $status, 'llms-' ) ) {
            $status = 'llms-' . $status;
        }
 
        if ( array_key_exists( $status, llms_get_order_statuses( $this->get( 'order_type' ) ) ) ) {
            $this->set( 'status', $status );
        }
 
    }
 
    /**
     * Sets user-related metadata for the order.
     *
     * @since 7.0.0
     *
     * @param array|LLMS_Student|WP_User|integer $user_or_data Accepts a raw array user meta-data or
     *                                                         an input string accepted by `llms_get_student()`.
     *                                                         When passing an existing user the data will be pulled
     *                                                         from the user metadata and saved to the order.
     * @return array {
     *     Returns an associative array representing the user metadata that was stored on the order.
     *
     *     @type integer $user_id            User's WP_User id.
     *     @type string  $user_ip_address    User's ip address.
     *     @type string  $billing_email      User's email.
     *     @type string  $billing_first_name User's first name.
     *     @type string  $billing_last_name  User's last name.
     *     @type string  $billing_address_1  User's address line 1.
     *     @type string  $billing_address_2  User's address line 2.
     *     @type string  $billing_city       User's city.
     *     @type string  $billing_state      User's state.
     *     @type string  $billing_zip        User's zip.
     *     @type string  $billing_country    User's country.
     *     @type string  $billing_phone      User's phone.
     * }
     */
    public function set_user_data( $user_or_data ) {
 
        $to_set = array(
            'user_id'            => '',
            'billing_email'      => '',
            'billing_first_name' => '',
            'billing_last_name'  => '',
            'billing_address_1'  => '',
            'billing_address_2'  => '',
            'billing_city'       => '',
            'billing_state'      => '',
            'billing_zip'        => '',
            'billing_country'    => '',
            'billing_phone'      => '',
        );
 
        $user = ! is_array( $user_or_data ) ? llms_get_student( $user_or_data ) : false;
        if ( $user ) {
 
            $user_or_data = array();
 
            $map = array(
                'user_id'            => 'id',
                'billing_email'      => 'user_email',
                'billing_phone'      => 'phone',
                'billing_first_name' => 'first_name',
                'billing_last_name'  => 'last_name',
            );
 
            foreach ( array_keys( $to_set ) as $order_key ) {
                $to_set[ $order_key ] = $user->get( $map[ $order_key ] ?? $order_key );
            }
        }
 
        // Only use the default IP address if it wasn't specified in the input array.
        $to_set['user_ip_address'] = $user_or_data['user_ip_address'] ?? llms_get_ip_address();
 
        // Merge the data and remove excess keys.
        $to_set = array_intersect_key(
            array_merge( $to_set, $user_or_data ),
            $to_set
        );
 
        $this->set_bulk( $to_set );
        return $to_set;
 
    }
 
    /**
     * Record the start date of the access plan and schedule expiration if expiration is required in the future
     *
     * @since 3.0.0
     * @since 3.19.0 Unknown.
     * @since 5.2.0 Use strict type comparision.
     *
     * @return void
     */
    public function start_access() {
 
        // Only start access if access isn't already started.
        $date = $this->get( 'start_date' );
        if ( ! $date ) {
 
            // Set the start date to now.
            $date = llms_current_time( 'mysql' );
            $this->set( 'start_date', $date );
 
        }
 
        $this->unschedule_expiration();
 
        // Setup expiration.
        if ( in_array( $this->get( 'access_expiration' ), array( 'limited-date', 'limited-period' ), true ) ) {
 
            $expires_date = $this->get_access_expiration_date( 'Y-m-d H:i:s' );
            $this->set( 'date_access_expires', $expires_date );
            $this->maybe_schedule_expiration();
 
        }
 
    }
 
    /**
     * Cancels a scheduled expiration action
     *
     * Does nothing if no expiration is scheduled
     *
     * @since 3.19.0
     * @since 3.32.0 Update to use latest action-scheduler functions.
     * @since 4.6.0 Use `$this->get_next_scheduled_action_time()` to determine if the action is currently scheduled.
     *
     * @return void
     */
    public function unschedule_expiration() {
 
        if ( $this->get_next_scheduled_action_time( 'llms_access_plan_expiration' ) ) {
            as_unschedule_action( 'llms_access_plan_expiration', $this->get_action_args() );
        }
 
    }
 
    /**
     * Cancels a scheduled recurring payment action
     *
     * Does nothing if no payments are scheduled
     *
     * @since 3.0.0
     * @since 3.32.0 Update to use latest action-scheduler functions.
     * @since 4.6.0 Use `$this->get_next_scheduled_action_time()` to determine if the action is currently scheduled.
     *
     * @return void
     */
    public function unschedule_recurring_payment() {
 
        if ( $this->get_next_scheduled_action_time( 'llms_charge_recurring_payment' ) ) {
 
            $action_args = $this->get_action_args();
 
            as_unschedule_action( 'llms_charge_recurring_payment', $action_args );
 
            /**
             * Fired after a recurring payment is unscheduled
             *
             * @since 5.2.0
             *
             * @param LLMS_Order $order       LLMS_Order instance.
             * @param int        $date        Timestamp of the recurring payment date UTC.
             * @param array      $action_args Arguments passed to the scheduler.
             */
            do_action( 'llms_charge_recurring_payment_unscheduled', $this, $action_args );
 
        }
 
    }
 
    /**
     * Schedule recurring payment
     *
     * It will unschedule the next recurring payment action, if any, before scheduling.
     *
     * @since 5.2.0
     *
     * @param string  $next_payment_date Optional. Next payment date. If not provided it'll be retrieved using `$this->get_next_payment_due_date()`.
     * @param boolean $gmt               Optional. Whether the provided `$next_payment_date` date is gmt. Default is `false`.
     *                                   Only applies when the `$next_payment_date` is provided.
     * @return WP_Error|integer WP_Error if the plan ended. Otherwise returns the return value of `as_schedule_single_action`: the action's ID.
     */
    public function schedule_recurring_payment( $next_payment_date = false, $gmt = false ) {
 
        // Unschedule the next action (does nothing if no action scheduled).
        $this->unschedule_recurring_payment();
 
        $date = $this->get_recurring_payment_due_date_for_scheduler( $next_payment_date, $gmt );
 
        if ( is_wp_error( $date ) ) {
            return $date;
        }
 
        $action_args = $this->get_action_args();
 
        // Schedule the payment.
        $action_id = as_schedule_single_action(
            $date,
            'llms_charge_recurring_payment',
            $action_args
        );
 
        /**
         * Fired after a recurring payment is scheduled
         *
         * @since 5.2.0
         *
         * @param LLMS_Order $order       LLMS_Order instance.
         * @param integer    $date        Timestamp of the recurring payment date UTC.
         * @param array      $action_args Arguments passed to the scheduler.
         * @param integer    $action_id   Scheduled action ID.
         */
        do_action( 'llms_charge_recurring_payment_scheduled', $this, $date, $action_args, $action_id );
 
        return $action_id;
 
    }
 
    /**
     * Returns the recurring payment due date in a suitable format for the scheduler.
     *
     * @since 5.2.0
     *


Top ↑

Properties Properties

The following post and post meta properties are accessible for this class. See LLMS_Post_Model::get() and LLMS_Post_Model::set() for more information.

$access_expiration

(string) Access expiration type, accepts: lifetime (default), limited-period, or limited-date.

$access_expires

(string) Date on which access expires in m/d/Y format. Only applicable when the $access_expiration property is set to "limited-date".

$access_length

(int) Length of access from time of purchase, combine with the $access_period. Only applicable when the $access_expiration property is set to "limited-period".

$access_period

(string) Time period of access from time of purchase, combine with $access_length. Only applicable when the $access_expiration property is set to "limited-period". Accepts: year, month, week, or day.

$anonymized

(string) Determines if the order has been anonymized due to a personal information erasure request. Accepts "yes" or "no".

$billing_address_1

(string) Customer billing address line 1.

$billing_address_2

(string) Customer billing address line 2.

$billing_city

(string) Customer billing city.

$billing_country

(string) Customer billing country, two character ISO code.

$billing_email

(string) Customer email address.

$billing_first_name

(string) Customer first name.

$billing_last_name

(string) Customer last name.

$billing_phone

(string) Customer phone number.

$billing_state

(string) Customer billing state.

$billing_zip

(string) Customer billing zip/postal code.

$billing_frequency

(int) The billing frequency interval. A value of 0 indicates a one-time payment. Accepts integers <= 6.

$billing_length

(int) Number of intervals to run payment for, combine with $billing_period & $billing_frequency. A value of 0 indicates that recurring payments run indefinitely (until cancelled). Only applicable if $billing_frequency is not 0.

$billing_period

(string) The billing period. Combine with $length. Only applicable if $billing_frequency is not 0. Accepts: year, month, week, or day.

$coupon_amount

(float) Amount of the coupon (flat/percentage) in relation to the plan amount.

$coupon_amout_trial

(float) Amount of the coupon (flat/percentage) in relation to the plan trial amount where applicable.

$coupon_code

(string) Coupon code applied to the order.

$coupon_id

(int) The WP_Post ID of the used coupon.

$coupon_type

(string) Type of coupon used, either percent or dollar.

$coupon_used

(string) Whether or not a coupon was used for the order. Accepts yes or no.

$coupon_value

(float) Value of the coupon. When on sale, $sale_price minus $total; when not on sale $original_total minus $total.

$coupon_value_trial

(float) Value of the coupon applied to the trial. The $trial_original_total minus $trial_total.

$currency

(string) Transaction's currency code.

$date_access_expires

(string) Date when access should expire as a datetime string: Y-m-d H:i:s.

$date_next_payment

(string) Date when the next recurring payment is due as a datemtime string: Y-m-d H:i:s. Use function LLMS_Order::get_next_payment_due_date() instead of accessing directly!

$date_trial_end

(string) Date when the trial ends for orders with a trial as a datemtime string: Y-m-d H:i:s. Use function LLMS_Order::get_trial_end_date() instead of accessing directly!

$gateway_api_mode

(string) API Mode of the gateway when the transaction was made, either "test" or "live".

$gateway_customer_id

(string) Gateway's unique ID for the customer who placed the order (if supported by the gateway).

$gateway_source_id

(string) Gateway's unique ID for the card or source to be used for recurring subscriptions (if supported by gateway).

$gateway_subscription_id

(string) Gateway's unique ID for the recurring subscription (if supported by the gateway).

$id

(int) The WP_Post ID of the order.

$last_retry_rule

(int) Rule number for current retry step for the order.

$on_sale

(string) Whether or not sale pricing was used for the plan, either "yes" or "no".

$order_key

(string) A unique identifier for the order that can be passed safely in URLs.

$order_type

(string) Single or recurring order, either "single" or "recurring".

$original_total

(float) Price of the order before applicable sale and coupon adjustments.

$payment_gateway

(string) LifterLMS Payment Gateway ID (eg "paypal" or "stripe").

$plan_id

(int) WP_Post ID of the purchased access plan.

$plan_sku

(string) SKU of the purchased access plan.

$plan_title

(string) Title / Name of the purchased access plan.

$plan_ended

(string) Whether or not the payment plan has ended. Only applicable when the plan is not "unlimited". Accepts "yes" or "no".

$product_id

(int) WP_Post ID of the purchased course or membership product.

$product_sku

(string) SKU of the purchased product.

$product_title

(string) Title / Name of the purchased product.

$product_type

(string) Type of product purchased (course or membership).

$sale_price

(float) Sale price before coupon adjustments.

$sale_value

(float) The value of the sale, $original_total - $sale_price.

$start_date

(string) Date when access was initially granted; this is used to determine when access expires.

$temp_gateway_ids

(array) { An associative array containing gateway ids. The gateway IDs are cached in this meta property while the source is being switched. Any gateway running actions when a source is switched may need to know the previous source IDs which might be cleared or overwritten by other gateways during the switch. @type string customer The value of the gateway_customer_id property when the source switch starts. @type string source The value of the gateway_source_id property when the source switch starts. @type string subscription The value of the gateway_subscription_id property when the source switch starts. }

$total

(float) Actual price of the order, after applicable sale & coupon adjustments.

$trial_length

(int) Length of the trial. Combined with $trial_period to determine the actual length of the trial.

$trial_offer

(string) Whether or not there was a trial offer applied to the order, either yes or no.

$trial_original_total

(float) Total price of the trial before applicable coupon adjustments.

$trial_period

(string) Period for the trial period. Accepts: year, month, week, or day.

$trial_total

(float) Total price of the trial after applicable coupon adjustments/

$user_id

(int) Customer WP_User ID.

$user_ip_address

(string) Customer's IP address at time of purchase.


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
5.3.0 Removed usage of the meta property date_billing_end and removed private method calculate_billing_end_date().
4.7.0 Added plan_ended meta property.
3.35.0 Prepare transaction revenue SQL query properly; Sanitize $_SERVER data.
3.32.0 Update to use latest action-scheduler functions.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.