-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathclass-pmpro-subscription.php
1396 lines (1224 loc) · 44.6 KB
/
class-pmpro-subscription.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The PMPro Subscription object.
*
* @method int get_id Get the ID of the subscription.
* @method int get_user_id Get the ID of the user the subscription belongs to.
* @method int get_membership_level_id Get the ID of the membership level that this subscription is for.
* @method string get_gateway Get the gateway used to create the subscription.
* @method string get_gateway_environment Get the gateway environment used to create the subscription.
* @method string get_subscription_transaction_id Get the ID of the subscription in the gateway.
* @method string get_status Get the status of the subscription.
* @method float get_billing_amount Get the billing amount.
* @method int get_cycle_number Get the number of cycles.
* @method string get_cycle_period Get the cycle period.
* @method int get_billing_limit Get the billing limit.
* @method float get_trial_amount Get the trial amount.
* @method int get_trial_limit Get the trial limit.
*
* @since 3.0
*/
class PMPro_Subscription {
/**
* The subscription ID.
*
* @since 3.0
*
* @var int
*/
protected $id = 0;
/**
* The subscription user ID.
*
* @since 3.0
*
* @var int
*/
protected $user_id = 0;
/**
* The subscription membership level ID.
*
* @since 3.0
*
* @var int
*/
protected $membership_level_id = 0;
/**
* The subscription gateway.
*
* @since 3.0
*
* @var string
*/
protected $gateway = '';
/**
* The subscription gateway environment.
*
* @since 3.0
*
* @var string
*/
protected $gateway_environment = '';
/**
* The subscription transaction id.
*
* @since 3.0
*
* @var string
*/
protected $subscription_transaction_id = '';
/**
* The subscription status.
*
* @since 3.0
*
* @var string
*/
protected $status = '';
/**
* The subscription start date (UTC YYYY-MM-DD HH:MM:SS).
*
* @since 3.0
*
* @var string
*/
protected $startdate = '';
/**
* The subscription end date (UTC YYYY-MM-DD HH:MM:SS).
*
* @since 3.0
*
* @var string
*/
protected $enddate = '';
/**
* The subscription next payment date (UTC YYYY-MM-DD HH:MM:SS).
*
* @since 3.0
*
* @var string
*/
protected $next_payment_date = '';
/**
* The subscription billing amount.
*
* @since 3.0
*
* @var float
*/
protected $billing_amount = 0.00;
/**
* The subscription billing cycle number.
*
* @since 3.0
*
* @var int
*/
protected $cycle_number = 0;
/**
* The subscription billing cycle period.
*
* @since 3.0
*
* @var string
*/
protected $cycle_period = 'Month';
/**
* The subscription billing limit.
*
* @since 3.0
*
* @var int
*/
protected $billing_limit = 0;
/**
* The subscription trial billing amount.
*
* @since 3.0
*
* @var float
*/
protected $trial_amount = 0.00;
/**
* The subscription trial billing cycle number.
*
* @since 3.0
*
* @var int
*/
protected $trial_limit = 0;
/**
* The initial payment amount for this subscription.
*
* This will be filled in automatically when $sub->get_initial_payment() is called.
*
* @since 3.0
*
* @var null|float|int
* @see get_initial_payment()
*/
protected $initial_payment;
/**
* The date that this subscription was last modified.
*
* @since 3.0
*
* @var string
*/
protected $modified = '';
/**
* Create a new PMPro_Subscription object.
*
* @since 3.0
*
* @param null|int|array|object $subscription The ID of the subscription to set up or the subscription data to load.
* Leave empty for a new subscription.
*/
public function __construct( $subscription = null ) {
global $wpdb;
if ( empty( $subscription ) ) {
return;
}
$subscription_data = [];
if ( is_numeric( $subscription ) ) {
// Get an existing subscription.
$subscription_data = $wpdb->get_row(
$wpdb->prepare(
"
SELECT *
FROM $wpdb->pmpro_subscriptions
WHERE id = %d
",
$subscription
),
ARRAY_A
);
} elseif ( is_array( $subscription ) ) {
$subscription_data = $subscription;
} elseif ( is_object( $subscription ) ) {
$subscription_data = get_object_vars( $subscription );
} else {
// Invalid $subscription so there's nothing we can do.
return;
}
if ( ! empty( $subscription_data ) ) {
$this->set( $subscription_data );
}
// Check if this subscription has default migration data.
$this->maybe_fix_default_migration_data();
}
/**
* Call magic methods.
*
* @since 3.0
*
* @param string $name The method that was called.
* @param array $arguments The arguments passed to the method.
*
* @return mixed|null
*/
public function __call( $name, $arguments ) {
if ( 0 === strpos( $name, 'get_' ) ) {
$property_name_arr = explode( 'get_', $name );
$property_name = $property_name_arr[1];
$supported_properties = [
'id',
'user_id',
'membership_level_id',
'gateway',
'gateway_environment',
'subscription_transaction_id',
'status',
'billing_amount',
'cycle_number',
'cycle_period',
'billing_limit',
'trial_amount',
'trial_limit',
];
if ( in_array( $property_name, $supported_properties, true ) ) {
return $this->{$property_name};
}
}
return null;
}
/**
* Get the subscription object based on query arguments.
*
* @param int|array $args The query arguments to use or the subscription ID.
*
* @return PMPro_Subscription|null The subscription objects or null if not found.
*/
public static function get_subscription( $args = [] ) {
// At least one argument is required.
if ( empty( $args ) ) {
return null;
}
if ( is_numeric( $args ) ) {
$args = [
'id' => $args,
];
}
// Invalid arguments.
if ( ! is_array( $args ) ) {
return null;
}
// Force returning of one subscription.
$args['limit'] = 1;
// Get the subscriptions using query arguments.
$subscriptions = self::get_subscriptions( $args );
// Check if we found any subscriptions.
if ( empty( $subscriptions ) ) {
return null;
}
// Get the first subscription in the array.
return reset( $subscriptions );
}
/**
* Get the list of subscription objects based on query arguments.
*
* Defaults to returning the latest 100 subscriptions.
*
* @param array $args The query arguments to use.
*
* @return PMPro_Subscription[] The list of subscription objects.
*/
public static function get_subscriptions( array $args = [] ) {
global $wpdb;
$sql_query = "SELECT `id` FROM `$wpdb->pmpro_subscriptions`";
$prepared = [];
$where = [];
$orderby = isset( $args['orderby'] ) ? $args['orderby'] : '`startdate` DESC';
$limit = isset( $args['limit'] ) ? (int) $args['limit'] : 100;
// Detect unsupported orderby usage (in the future we may support better syntax).
if ( $orderby !== preg_replace( '/[^a-zA-Z0-9\s,`]/', ' ', $orderby ) ) {
return [];
}
/*
* Now filter the query based on the arguments provided.
*/
// Filter by ID(s).
if ( isset( $args['id'] ) ) {
if ( ! is_array( $args['id'] ) ) {
$where[] = 'id = %d';
$prepared[] = $args['id'];
} else {
$where[] = 'id IN ( ' . implode( ', ', array_fill( 0, count( $args['id'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['id'] );
}
}
// Filter by user ID(s).
if ( isset( $args['user_id'] ) ) {
if ( ! is_array( $args['user_id'] ) ) {
$where[] = 'user_id = %d';
$prepared[] = $args['user_id'];
} else {
$where[] = 'user_id IN ( ' . implode( ', ', array_fill( 0, count( $args['user_id'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['user_id'] );
}
}
// Filter by membership level ID(s).
if ( isset( $args['membership_level_id'] ) ) {
if ( ! is_array( $args['membership_level_id'] ) ) {
$where[] = 'membership_level_id = %d';
$prepared[] = $args['membership_level_id'];
} else {
$where[] = 'membership_level_id IN ( ' . implode( ', ', array_fill( 0, count( $args['membership_level_id'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['membership_level_id'] );
}
}
// Filter by status(es).
if ( isset( $args['status'] ) ) {
if ( ! is_array( $args['status'] ) ) {
$where[] = 'status = %s';
$prepared[] = $args['status'];
} else {
$where[] = 'status IN ( ' . implode( ', ', array_fill( 0, count( $args['status'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['status'] );
}
}
// Filter by subscription transaction ID(s).
if ( isset( $args['subscription_transaction_id'] ) ) {
if ( ! is_array( $args['subscription_transaction_id'] ) ) {
$where[] = 'subscription_transaction_id = %s';
$prepared[] = $args['subscription_transaction_id'];
} else {
$where[] = 'subscription_transaction_id IN ( ' . implode( ', ', array_fill( 0, count( $args['subscription_transaction_id'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['subscription_transaction_id'] );
}
}
// Filter by gateway(s).
if ( isset( $args['gateway'] ) ) {
if ( ! is_array( $args['gateway'] ) ) {
$where[] = 'gateway = %s';
$prepared[] = $args['gateway'];
} else {
$where[] = 'gateway IN ( ' . implode( ', ', array_fill( 0, count( $args['gateway'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['gateway'] );
}
}
// Filter by gateway environment(s).
if ( isset( $args['gateway_environment'] ) ) {
if ( ! is_array( $args['gateway_environment'] ) ) {
$where[] = 'gateway_environment = %s';
$prepared[] = $args['gateway_environment'];
} else {
$where[] = 'gateway_environment IN ( ' . implode( ', ', array_fill( 0, count( $args['gateway_environment'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['gateway_environment'] );
}
}
// Filter by billing amount(s).
if ( isset( $args['billing_amount'] ) ) {
if ( ! is_array( $args['billing_amount'] ) ) {
$where[] = 'billing_amount = %f';
$prepared[] = $args['billing_amount'];
} else {
$where[] = 'billing_amount IN ( ' . implode( ', ', array_fill( 0, count( $args['billing_amount'] ), '%f' ) ) . ' )';
$prepared = array_merge( $prepared, $args['billing_amount'] );
}
}
// Filter by cycle number(s).
if ( isset( $args['cycle_number'] ) ) {
if ( ! is_array( $args['cycle_number'] ) ) {
$where[] = 'cycle_number = %d';
$prepared[] = $args['cycle_number'];
} else {
$where[] = 'cycle_number IN ( ' . implode( ', ', array_fill( 0, count( $args['cycle_number'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['cycle_number'] );
}
}
// Filter by cycle period(s).
if ( isset( $args['cycle_period'] ) ) {
if ( ! is_array( $args['cycle_period'] ) ) {
$where[] = 'cycle_period = %s';
$prepared[] = $args['cycle_period'];
} else {
$where[] = 'cycle_period IN ( ' . implode( ', ', array_fill( 0, count( $args['cycle_period'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['cycle_period'] );
}
}
// Filter by billing limit(s).
if ( isset( $args['billing_limit'] ) ) {
if ( ! is_array( $args['billing_limit'] ) ) {
$where[] = 'billing_limit = %d';
$prepared[] = $args['billing_limit'];
} else {
$where[] = 'billing_limit IN ( ' . implode( ', ', array_fill( 0, count( $args['billing_limit'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['billing_limit'] );
}
}
// Filter by trial amount(s).
if ( isset( $args['trial_amount'] ) ) {
if ( ! is_array( $args['trial_amount'] ) ) {
$where[] = 'trial_amount = %f';
$prepared[] = $args['trial_amount'];
} else {
$where[] = 'trial_amount IN ( ' . implode( ', ', array_fill( 0, count( $args['trial_amount'] ), '%f' ) ) . ' )';
$prepared = array_merge( $prepared, $args['trial_amount'] );
}
}
// Filter by trial limit(s).
if ( isset( $args['trial_limit'] ) ) {
if ( ! is_array( $args['trial_limit'] ) ) {
$where[] = 'trial_limit = %d';
$prepared[] = $args['trial_limit'];
} else {
$where[] = 'trial_limit IN ( ' . implode( ', ', array_fill( 0, count( $args['trial_limit'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['trial_limit'] );
}
}
// Maybe filter the data.
if ( $where ) {
$sql_query .= ' WHERE ' . implode( ' AND ', $where );
}
// Handle the order of data.
$sql_query .= ' ORDER BY ' . $orderby;
// Maybe limit the data.
if ( $limit ) {
$sql_query .= ' LIMIT %d';
$prepared[] = $limit;
}
// Maybe prepare the query.
if ( $prepared ) {
$sql_query = $wpdb->prepare( $sql_query, $prepared );
}
$subscription_ids = $wpdb->get_col( $sql_query );
if ( empty( $subscription_ids ) ) {
return [];
}
$subscriptions = [];
foreach ( $subscription_ids as $subscription_id ) {
$subscription = new PMPro_Subscription( $subscription_id );
// Make sure the subscription object is valid.
if ( ! empty( $subscription->id ) ) {
$subscriptions[] = $subscription;
}
}
return $subscriptions;
}
/**
* Get subscriptions for a user.
*
* @since 3.0
*
* @param int|null $user_id ID of the user to get subscriptions for. Defaults to current user.
* @param int|int[]|null $membership_level_id The membership level ID(s) to get subscriptions for. Defaults to all.
* @param string|string[]|null $status The status(es) of the subscription to get. Defaults to active.
*
* @return PMPro_Subscription[] The list of subscription objects.
*/
public static function get_subscriptions_for_user( $user_id = null, $membership_level_id = null, $status = [ 'active' ] ) {
// Get user_id if none passed.
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
// Check for a valid user.
if ( empty( $user_id ) ) {
return [];
}
// Filter by user ID.
$args = [
'user_id' => $user_id,
];
// Filter by membership level ID(s).
if ( $membership_level_id ) {
$args['membership_level_id'] = $membership_level_id;
}
// Filter by status(es).
if ( $status ) {
$args['status'] = $status;
}
return self::get_subscriptions( $args );
}
/**
* Get the subscription with the given subscription transaction ID.
*
* @since 3.0
*
* @param string $subscription_transaction_id Subscription transaction ID to get.
* @param string $gateway Gateway to get the subscription for.
* @param string $gateway_environment Gateway environment to get the subscription for.
*
* @return PMPro_Subscription|null PMPro_Subscription object if found, null if not found.
*/
public static function get_subscription_from_subscription_transaction_id( $subscription_transaction_id, $gateway, $gateway_environment ) {
// Require subscriptio transaction ID.
if ( empty( $subscription_transaction_id ) ) {
return null;
}
// Filter by args specified.
$args = [
'subscription_transaction_id' => $subscription_transaction_id,
'gateway' => $gateway,
'gateway_environment' => $gateway_environment,
];
return self::get_subscription( $args );
}
/**
* Create a new subscription.
*
* @since 3.0
*
* @param array $args {
* Arguments to create a new subscription.
*
* @type int $user_id ID of the user to create the subscription for. Required.
* @type int $membership_level_id ID of the membership level to create the subscription for. Required.
* @type string $gateway Gateway to create the subscription for. Required.
* @type string $gateway_environment Gateway environment to create the subscription for. Required.
* @type string $subscription_transaction_id Subscription transaction ID to create the subscription for. Required.
* @type string $status Status of the subscription.
* @type string $startdate The subscription start date (UTC YYYY-MM-DD HH:MM:SS).
* @type string $enddate The subscription end date (UTC YYYY-MM-DD HH:MM:SS).
* @type string $next_payment_date The subscription next payment date (UTC YYYY-MM-DD HH:MM:SS).
* @type float $billing_amount The subscription billing amount.
* @type int $cycle_number The subscription cycle number.
* @type string $cycle_period The subscription cycle period.
* @type int $billing_limit The subscription billing limit.
* @type float $trial_amount The subscription trial amount.
* @type int $trial_limit The subscription trial limit.
* }
*
* @return PMPro_Subscription|null PMPro_Subscription object if created, null if not.
*/
public static function create( $args ) {
global $wpdb;
// Make sure that $args is an array.
$subscription_data = array();
if ( is_array( $args ) ) {
$subscription_data = $args;
} elseif ( is_object( $args ) ) {
$subscription_data = get_object_vars( $args );
} else {
// Invalid $subscription so there's nothing we can do.
return null;
}
// At a minimum, we need a user_id, membership_level_id, subscription_transaction_id, gateway, and gateway_environment.
if (
empty( $subscription_data['user_id'] ) ||
empty( $subscription_data['membership_level_id'] ) ||
empty( $subscription_data['subscription_transaction_id'] ) ||
empty( $subscription_data['gateway'] ) ||
empty( $subscription_data['gateway_environment'] )
) {
return null;
}
// Make sure we don't already have a subscription with this transaction ID and gateway.
$existing_subscription = self::get_subscription_from_subscription_transaction_id( $subscription_data['subscription_transaction_id'], $subscription_data['gateway'], $subscription_data['gateway_environment'] );
if ( ! empty( $existing_subscription ) ) {
// Subscription already exists.
return null;
}
// Create the subscription.
$new_subscription = new PMPro_Subscription( $subscription_data );
// Save the subscription before syncing with gateway
// to avoid infinite loops if gateways load orders which
// in turn try to create this subscription again.
$saved = $new_subscription->save();
if ( ! $saved ) {
// We couldn't save the subscription.
return null;
}
// Try to pull as much info as possible directly from the gateway or from the database.
$new_subscription->update();
return $new_subscription;
}
/**
* Update the startdate and next payment date based on information
* in the database, then sync with gateway.
*
* @since 3.0
*
* @return bool True if the subscription was saved, false if not.
*/
public function update() {
// Get the gateway object.
$gateway_object = $this->get_gateway_object();
// Track update errors.
$error_message = null;
// Make sure that the gateway object is valid.
if ( $gateway_object instanceof PMProGateway ) {
// Update subscription info.
$error_message = $gateway_object->update_subscription_info( $this );
} else {
$error_message = __( 'Could not find gateway class.', 'paid-memberships-pro' );
}
// Save error in subscription meta with date to reference later.
if ( ! empty( $error_message ) ) {
update_pmpro_subscription_meta( $this->id, 'sync_error', $error_message );
update_pmpro_subscription_meta( $this->id, 'sync_error_timestamp', current_time( 'timestamp' ) );
} else {
// No error, so clear the error meta.
delete_pmpro_subscription_meta( $this->id, 'sync_error' );
delete_pmpro_subscription_meta( $this->id, 'sync_error_timestamp' );
}
pmpro_setMessage( __( 'Subscription updated.', 'paid-memberships-pro' ), 'pmpro_success' ); // Will not overwrite previous messages.
return $this->save();
}
/**
* Update a subscription when a recurring payment is made. Should only
* be used on `pmpro_subscription_payment_completed` hook.
*
* @since 3.0
*
* @param MemberOrder $order The order for the recurring payment that was just processed.
*/
public static function update_subscription_for_order( $order ) {
$subscription = $order->get_subscription();
if ( ! empty( $subscription ) ) {
$subscription->update();
}
}
/**
* Get the next payment date for this subscription.
*
* @since 3.0
*
* @param string $format Format to return the date in.
* @param bool $local_time Whether to return the date in local time or UTC.
*
* @return string|null Date in the requested format.
*/
public function get_next_payment_date( $format = 'timestamp', $local_time = true ) {
return $this->format_subscription_date( $this->next_payment_date, $format, $local_time );
}
/**
* Get the start date for this subscription.
*
* @since 3.0
*
* @param string $format Format to return the date in.
* @param bool $local_time Whether to return the date in local time or UTC.
*
* @return string|null Date in the requested format.
*/
public function get_startdate( $format = 'timestamp', $local_time = true ) {
return $this->format_subscription_date( $this->startdate, $format, $local_time );
}
/**
* Get the end date for this subscription.
*
* @since 3.0
*
* @param string $format Format to return the date in.
* @param bool $local_time Whether to return the date in local time or UTC.
*
* @return string|null Date in the requested format.
*/
public function get_enddate( $format = 'timestamp', $local_time = true ) {
return $this->format_subscription_date( $this->enddate, $format, $local_time );
}
/**
* Format a date.
*
* @since 3.0
*
* @param string $date Date to format.
* @param string $format Format to return the date in.
* @param bool $local_time Whether to return the date in local time or UTC.
*
* @return string|null Date in the requested format.
*/
private function format_subscription_date( $date, $format = 'timestamp', $local_time = true ) {
if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) {
return null;
}
if ( 'timestamp' === $format ) {
$format = 'U';
} elseif ( 'date_format' === $format ) {
$format = get_option( 'date_format' );
}
// Get date in WP local timezone.
if ( $local_time ) {
if( 'U' === $format ){
// When formatting using the epoch, the date must already consider the timezone offset.
// Then, first apply a simple format to add the timezone.
$date = get_date_from_gmt( $date );
}
return get_date_from_gmt( $date, $format );
}
// Allow timestamps.
if ( ! is_numeric( $date ) ) {
$date = strtotime( $date );
}
// Get date in GMT timezone.
return gmdate( $format, $date );
}
/**
* Returns the PMProGateway object for this subscription.
*
* @since 3.0
*
* @return null|PMProGateway The PMProGateway object, null if not set or class found.
*/
public function get_gateway_object() {
$gateway_object = null;
// The gateway was set.
if ( ! empty( $this->gateway ) ) {
// Default test gateway.
$classname = 'PMProGateway';
if ( 'free' !== $this->gateway ) {
// Adding the gateway suffix.
$classname .= '_' . $this->gateway;
}
if ( class_exists( $classname ) ) {
$gateway_object = new $classname( $this->gateway );
}
}
/**
* Allow changing the gateway object for this subscription
*
* @param PMProGateway $gateway_object Gateway object.
* @param PMPro_Subscription $this Subscription object.
*
* @since 3.0.3
*/
$gateway_object = apply_filters( 'pmpro_subscription_gateway_object', $gateway_object, $this );
return $gateway_object;
}
/**
* Get the initial payment amount for the subscription.
*
* @since 3.0
*
* @return float The initial payment amount for the subscription.
*/
public function get_initial_payment() {
if ( null !== $this->initial_payment ) {
return $this->initial_payment;
}
$this->initial_payment = 0;
// Fetch the first order for this subscription.
$orders = $this->get_orders( [
'limit' => 1,
'orderby' => '`timestamp` ASC, `id` ASC',
] );
if ( ! empty( $orders ) ) {
// Get the first order object.
$order = current( $orders );
// Use the order total as the initial payment.
$this->initial_payment = $order->total;
}
return $this->initial_payment;
}
/**
* Get the list of order objects for this subscription based on query arguments.
*
* Defaults to returning the latest 100 orders from a subscription based on the subscription transaction ID,
* the gateway, and the gateway environment.
*
* @since 3.0
*
* @param array $args The query arguments to use.
*
* @return MemberOrder[] The list of order objects.
*/
public function get_orders( array $args = [] ) {
if ( empty( $this->subscription_transaction_id ) ) {
return [];
}
global $wpdb;
$sql_query = "SELECT `id` FROM `$wpdb->pmpro_membership_orders`";
$prepared = [];
$where = [];
$orderby = isset( $args['orderby'] ) ? $args['orderby'] : '`timestamp` DESC, `id` DESC';
$limit = isset( $args['limit'] ) ? (int) $args['limit'] : 100;
// Detect unsupported orderby usage (in the future we may support better syntax).
if ( $orderby !== preg_replace( '/[^a-zA-Z0-9\s,`]/', ' ', $orderby ) ) {
return [];
}
// Filter by subscription transaction ID.
$where[] = 'subscription_transaction_id = %s';
$prepared[] = $this->subscription_transaction_id;
// Filter by gateway.
$where[] = 'gateway = %s';
$prepared[] = $this->gateway;
// Filter by gateway environment.
$where[] = 'gateway_environment = %s';
$prepared[] = $this->gateway_environment;
/*
* Now filter the query based on the arguments provided.
*/
// Filter by ID(s).
if ( isset( $args['id'] ) ) {
if ( ! is_array( $args['id'] ) ) {
$where[] = 'id = %d';
$prepared[] = $args['id'];
} else {
$where[] = 'id IN ( ' . implode( ', ', array_fill( 0, count( $args['id'] ), '%d' ) ) . ' )';
$prepared = array_merge( $prepared, $args['id'] );
}
}
// Filter by code(s).
if ( isset( $args['code'] ) ) {
if ( ! is_array( $args['code'] ) ) {
$where[] = 'code = %s';
$prepared[] = $args['code'];
} else {
$where[] = 'code IN ( ' . implode( ', ', array_fill( 0, count( $args['code'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['code'] );
}
}
// Filter by status(es).
if ( isset( $args['status'] ) ) {
if ( ! is_array( $args['status'] ) ) {
$where[] = 'status = %s';
$prepared[] = $args['status'];
} else {
$where[] = 'status IN ( ' . implode( ', ', array_fill( 0, count( $args['status'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['status'] );
}
}
// Filter by payment transaction ID(s).
if ( isset( $args['payment_transaction_id'] ) ) {
if ( ! is_array( $args['payment_transaction_id'] ) ) {
$where[] = 'payment_transaction_id = %s';
$prepared[] = $args['payment_transaction_id'];
} else {
$where[] = 'payment_transaction_id IN ( ' . implode( ', ', array_fill( 0, count( $args['payment_transaction_id'] ), '%s' ) ) . ' )';
$prepared = array_merge( $prepared, $args['payment_transaction_id'] );
}
}
// Maybe filter the data.
if ( $where ) {
$sql_query .= ' WHERE ' . implode( ' AND ', $where );
}
// Handle the order of data.
$sql_query .= ' ORDER BY ' . $orderby;
// Maybe limit the data.
if ( $limit ) {
$sql_query .= ' LIMIT %d';
$prepared[] = $limit;
}
// Maybe prepare the query.
if ( $prepared ) {
$sql_query = $wpdb->prepare( $sql_query, $prepared );
}
$order_ids = $wpdb->get_col( $sql_query );
if ( empty( $order_ids ) ) {
return [];
}
$orders = [];
foreach ( $order_ids as $order_id ) {
$order = new MemberOrder( $order_id );
// Make sure the order object is valid.
if ( ! empty( $order->id ) ) {
$orders[] = $order;
}
}
return $orders;