Passed
Push — develop ( e0c6d1...743bf9 )
by Reüel
04:44
created

Subscription::get_expiry_date()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Subscription
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Subscriptions
9
 */
10
11
namespace Pronamic\WordPress\Pay\Subscriptions;
12
13
use DateInterval;
14
use Pronamic\WordPress\DateTime\DateTime;
15
use Pronamic\WordPress\Money\Currency;
16
use Pronamic\WordPress\Money\Money;
17
use Pronamic\WordPress\Pay\Core\Statuses;
18
use Pronamic\WordPress\Pay\Payments\Payment;
19
use WP_Post;
20
21
/**
22
 * Subscription.
23
 */
24
class Subscription {
25
	/**
26
	 * The ID of this subscription.
27
	 *
28
	 * @var int
29
	 */
30
	protected $id;
31
32
	/**
33
	 * The date of this subscription.
34
	 *
35
	 * @var DateTime
36
	 */
37
	public $date;
38
39
	/**
40
	 * The key of this subscription, used in URL's for security.
41
	 *
42
	 * @var string
43
	 */
44
	public $key;
45
46
	/**
47
	 * The frequency of this subscription, for example: `daily`, `weekly`, `monthly` or `annually`.
48
	 *
49
	 * @var string
50
	 */
51
	public $frequency;
52
53
	/**
54
	 * The interval of this subscription, for example: 1, 2, 3, etc.
55
	 *
56
	 * @todo Improve documentation?
57
	 * @var  int
58
	 */
59
	public $interval;
60
61
	/**
62
	 * The interval period of this subscription.
63
	 *
64
	 * @todo Improve documentation?
65
	 * @var  int
66
	 */
67
	public $interval_period;
68
69
	/**
70
	 * The transaction ID of this subscription.
71
	 *
72
	 * @todo Is this required within a transaction?
73
	 * @var string
74
	 */
75
	public $transaction_id;
76
77
	/**
78
	 * The description of this subscription.
79
	 *
80
	 * @todo Is this required within a transaction?
81
	 * @var string
82
	 */
83
	public $description;
84
85
	/**
86
	 * The amount of this subscription, for example 18.95.
87
	 *
88
	 * @var Money
89
	 */
90
	protected $amount;
91
92
	/**
93
	 * The status of this subscription, for example 'Success'.
94
	 *
95
	 * @todo How to reference to a class constant?
96
	 * @see  Statuses
97
	 * @var  string
98
	 */
99
	public $status;
100
101
	/**
102
	 * Identifier for the source which started this subsription.
103
	 * For example: 'woocommerce', 'gravityforms', 'easydigitaldownloads', etc.
104
	 *
105
	 * @var string
106
	 */
107
	public $source;
108
109
	/**
110
	 * Unique ID at the source which started this subscription, for example:
111
	 * - WooCommerce order ID.
112
	 * - Easy Digital Downloads payment ID.
113
	 * - Gravity Forms entry ID.
114
	 *
115
	 * @var string
116
	 */
117
	public $source_id;
118
119
	/**
120
	 * The name of the consumer of this subscription.
121
	 *
122
	 * @todo Is this required and should we add the 'consumer' part?
123
	 * @var  string
124
	 */
125
	public $consumer_name;
126
127
	/**
128
	 * The IBAN of the consumer of this subscription.
129
	 *
130
	 * @todo Is this required and should we add the 'consumer' part?
131
	 * @var  string
132
	 */
133
	public $consumer_iban;
134
135
	/**
136
	 * The BIC of the consumer of this subscription.
137
	 *
138
	 * @todo Is this required and should we add the 'consumer' part?
139
	 * @var  string
140
	 */
141
	public $consumer_bic;
142
143
	/**
144
	 * The order ID of this subscription.
145
	 *
146
	 * @todo Is this required?
147
	 * @var  string
148
	 */
149
	public $order_id;
150
151
	/**
152
	 * The address of the consumer of this subscription.
153
	 *
154
	 * @todo Is this required?
155
	 * @var  string
156
	 */
157
	public $address;
158
159
	/**
160
	 * The city of the consumer of this subscription.
161
	 *
162
	 * @todo Is this required?
163
	 * @var  string
164
	 */
165
	public $city;
166
167
	/**
168
	 * The ZIP of the consumer of this subscription.
169
	 *
170
	 * @todo Is this required?
171
	 * @var  string
172
	 */
173
	public $zip;
174
175
	/**
176
	 * The country of the consumer of this subscription.
177
	 *
178
	 * @todo Is this required?
179
	 * @var  string
180
	 */
181
	public $country;
182
183
	/**
184
	 * The telephone number of the consumer of this subscription.
185
	 *
186
	 * @todo Is this required?
187
	 * @var  string
188
	 */
189
	public $telephone_number;
190
191
	/**
192
	 * The gateway configuration ID to use with this subscription.
193
	 *
194
	 * @todo Should we improve the name of this var?
195
	 * @var  integer
196
	 */
197
	public $config_id;
198
199
	/**
200
	 * The email of the consumer of this subscription.
201
	 *
202
	 * @todo Is this required?
203
	 * @var  string
204
	 */
205
	public $email;
206
207
	/**
208
	 * The customer name of the consumer of this subscription.
209
	 *
210
	 * @todo Is this required?
211
	 * @var  string
212
	 */
213
	public $customer_name;
214
215
	/**
216
	 * The payment method which was used to create this subscription.
217
	 *
218
	 * @var  string
219
	 */
220
	public $payment_method;
221
222
	/**
223
	 * The date when this subscirption started.
224
	 *
225
	 * @var DateTime|null
226
	 */
227
	public $start_date;
228
229
	/**
230
	 * The date when this subscirption will end, can be `null`.
231
	 *
232
	 * @var DateTime|null
233
	 */
234
	public $end_date;
235
236
	/**
237
	 * The end date of the last succesfull payment.
238
	 *
239
	 * @var DateTime|null
240
	 */
241
	public $expiry_date;
242
243
	/**
244
	 * The next payment date.
245
	 *
246
	 * @var DateTime|null
247
	 */
248
	public $next_payment;
249
250
	/**
251
	 * Array for extra meta data to store with this subscription.
252
	 *
253
	 * @var array
254
	 */
255
	public $meta;
256
257
	/**
258
	 * WordPress post object related to this subscription.
259
	 *
260
	 * @var WP_Post|array
261
	 */
262
	public $post;
263
264
	/**
265
	 * Construct and initialize subscription object.
266
	 *
267
	 * @param int $post_id A subscription post ID or null.
268
	 */
269
	public function __construct( $post_id = null ) {
270
		$this->id   = $post_id;
271
		$this->date = new DateTime();
272
		$this->meta = array();
273
274
		$this->set_amount( new Money() );
275
276
		if ( ! empty( $post_id ) ) {
277
			pronamic_pay_plugin()->subscriptions_data_store->read( $this );
278
		}
279
	}
280
281
	/**
282
	 * Get the ID of this subscription.
283
	 *
284
	 * @return int
285
	 */
286
	public function get_id() {
287
		return $this->id;
288
	}
289
290
	/**
291
	 * Set the ID of this subscription.
292
	 *
293
	 * @param int $id The ID of this subscription.
294
	 */
295
	public function set_id( $id ) {
296
		$this->id = $id;
297
	}
298
299
	/**
300
	 * Get the unique key of this subscription.
301
	 *
302
	 * @return string
303
	 */
304
	public function get_key() {
305
		return $this->key;
306
	}
307
308
	/**
309
	 * Get the config ID of this subscription.
310
	 *
311
	 * @return string
312
	 */
313
	public function get_config_id() {
314
		return $this->config_id;
315
	}
316
317
	/**
318
	 * Get the source identifier of this subscription, for example: 'woocommerce', 'gravityforms', etc.
319
	 *
320
	 * @return string
321
	 */
322
	public function get_source() {
323
		return $this->source;
324
	}
325
326
	/**
327
	 * Get the source ID of this subscription, for example a WooCommerce order ID or a Gravity Forms entry ID.
328
	 *
329
	 * @return string
330
	 */
331
	public function get_source_id() {
332
		return $this->source_id;
333
	}
334
335
	/**
336
	 * Get the frequency of this subscription, for example: 'daily', 'weekly', 'monthly' or 'annually'.
337
	 *
338
	 * @return string
339
	 */
340
	public function get_frequency() {
341
		return $this->frequency;
342
	}
343
344
	/**
345
	 * Get the interval, for example: 1, 2, 3, 4, etc., this specifies for example:
346
	 * - Repeat every *2* days
347
	 * - Repeat every *1* months
348
	 * - Repeat every *2* year
349
	 *
350
	 * @return int
351
	 */
352
	public function get_interval() {
353
		return $this->interval;
354
	}
355
356
	/**
357
	 * Get the interval period, for example 'D', 'M', 'Y', etc.
358
	 *
359
	 * @see    http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
360
	 * @return string
361
	 */
362
	public function get_interval_period() {
363
		return $this->interval_period;
364
	}
365
366
	/**
367
	 * Get date interval.
368
	 *
369
	 * @see http://php.net/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
370
	 * @return \DateInterval
371
	 * @throws \Exception    Throws an Exception when the `interval_spec` cannot be parsed as an interval.
372
	 */
373
	public function get_date_interval() {
374
		$interval_spec = 'P' . $this->interval . $this->interval_period;
375
376
		$interval = new DateInterval( $interval_spec );
377
378
		return $interval;
379
	}
380
381
	/**
382
	 * Get the description of this subscription.
383
	 *
384
	 * @return string
385
	 */
386
	public function get_description() {
387
		return $this->description;
388
	}
389
390
	/**
391
	 * Get the currency of this subscription.
392
	 *
393
	 * @return Currency
394
	 */
395
	public function get_currency() {
396
		return $this->get_amount()->get_currency()->get_alphabetic_code();
397
	}
398
399
	/**
400
	 * Get the amount of this subscription.
401
	 *
402
	 * @return Money
403
	 */
404
	public function get_amount() {
405
		return $this->amount;
406
	}
407
408
	/**
409
	 * Set the amount of this subscription.
410
	 *
411
	 * @param Money $amount Money object.
412
	 *
413
	 * @return void
414
	 */
415
	public function set_amount( Money $amount ) {
416
		$this->amount = $amount;
417
	}
418
419
	/**
420
	 * Get the transaction ID of this subscription.
421
	 *
422
	 * @return string
423
	 */
424
	public function get_transaction_id() {
425
		return $this->transaction_id;
426
	}
427
428
	/**
429
	 * Set the transaction ID of this subscription.
430
	 *
431
	 * @param string $transaction_id A transaction ID.
432
	 */
433
	public function set_transaction_id( $transaction_id ) {
434
		$this->transaction_id = $transaction_id;
435
	}
436
437
	/**
438
	 * Get the status of this subscription.
439
	 *
440
	 * @todo   Check constant?
441
	 * @return string
442
	 */
443
	public function get_status() {
444
		return $this->status;
445
	}
446
447
	/**
448
	 * Set the status of this subscription.
449
	 *
450
	 * @todo  Check constant?
451
	 * @param string $status A status string.
452
	 */
453
	public function set_status( $status ) {
454
		$this->status = $status;
455
	}
456
457
	/**
458
	 * Set consumer name.
459
	 *
460
	 * @param string $name A name.
461
	 */
462
	public function set_consumer_name( $name ) {
463
		$this->consumer_name = $name;
464
	}
465
466
	/**
467
	 * Set consumer IBAN.
468
	 *
469
	 * @param string $iban A IBAN.
470
	 */
471
	public function set_consumer_iban( $iban ) {
472
		$this->consumer_iban = $iban;
473
	}
474
475
	/**
476
	 * Set consumer BIC.
477
	 *
478
	 * @param string $bic A BIC.
479
	 */
480
	public function set_consumer_bic( $bic ) {
481
		$this->consumer_bic = $bic;
482
	}
483
484
	/**
485
	 * Add the specified note to this subscription.
486
	 *
487
	 * @param string $note A Note.
488
	 */
489
	public function add_note( $note ) {
490
		$commentdata = array(
491
			'comment_post_ID'  => $this->id,
492
			'comment_content'  => $note,
493
			'comment_type'     => 'subscription_note',
494
			'user_id'          => get_current_user_id(),
495
			'comment_approved' => true,
496
		);
497
498
		$comment_id = wp_insert_comment( $commentdata );
499
500
		return $comment_id;
501
	}
502
503
	/**
504
	 * Get meta by the specified meta key.
505
	 *
506
	 * @param string $key A meta key.
507
	 * @return string
508
	 */
509
	public function get_meta( $key ) {
510
		$key = '_pronamic_subscription_' . $key;
511
512
		return get_post_meta( $this->id, $key, true );
513
	}
514
515
	/**
516
	 * Set meta data.
517
	 *
518
	 * @param  string $key   A meta key.
519
	 * @param  mixed  $value A meta value.
520
	 *
521
	 * @return boolean        True on successful update, false on failure.
522
	 */
523
	public function set_meta( $key, $value = false ) {
524
		$key = '_pronamic_subscription_' . $key;
525
526
		if ( $value instanceof \DateTime ) {
527
			$value = $value->format( 'Y-m-d H:i:s' );
528
		}
529
530
		if ( empty( $value ) ) {
531
			return delete_post_meta( $this->id, $key );
532
		}
533
534
		return update_post_meta( $this->id, $key, $value );
535
	}
536
537
	/**
538
	 * Get source text.
539
	 *
540
	 * @return string
541
	 */
542
	public function get_source_text() {
543
		$text = $this->get_source() . '<br />' . $this->get_source_id();
544
545
		$payment = $this->get_first_payment();
546
547
		if ( null !== $payment ) {
548
			$text = $payment->get_source_text();
549
		}
550
551
		return $text;
552
	}
553
554
	/**
555
	 * Get source description.
556
	 *
557
	 * @return string
558
	 */
559
	public function get_source_description() {
560
		$text = $this->get_source();
561
562
		$payment = $this->get_first_payment();
563
564
		if ( $payment ) {
565
			$text = $payment->get_source_description();
566
		}
567
568
		return $text;
569
	}
570
571
	/**
572
	 * Get source link for this subscription.
573
	 *
574
	 * @return string|null
575
	 */
576
	public function get_source_link() {
577
		$url = null;
578
579
		$payment = $this->get_first_payment();
580
581
		if ( $payment ) {
582
			$url = apply_filters( 'pronamic_payment_source_url', $url, $payment );
583
			$url = apply_filters( 'pronamic_payment_source_url_' . $this->source, $url, $payment );
584
		}
585
586
		return $url;
587
	}
588
589
	/**
590
	 * Get cancel URL for this subscription.
591
	 *
592
	 * @return string
593
	 */
594
	public function get_cancel_url() {
595
		$cancel_url = add_query_arg(
596
			array(
597
				'subscription' => $this->get_id(),
598
				'key'          => $this->get_key(),
599
				'action'       => 'cancel',
600
			), home_url()
601
		);
602
603
		return $cancel_url;
604
	}
605
606
	/**
607
	 * Get renewal URL for this subscription.
608
	 *
609
	 * @return string
610
	 */
611
	public function get_renewal_url() {
612
		$renewal_url = add_query_arg(
613
			array(
614
				'subscription' => $this->get_id(),
615
				'key'          => $this->get_key(),
616
				'action'       => 'renew',
617
			), home_url()
618
		);
619
620
		return $renewal_url;
621
	}
622
623
	/**
624
	 * Get all the payments for this subscription.
625
	 *
626
	 * @return array
627
	 */
628
	public function get_payments() {
629
		return get_pronamic_payments_by_meta( '_pronamic_payment_subscription_id', $this->id );
630
	}
631
632
	/**
633
	 * Get the first payment of this subscription.
634
	 *
635
	 * @return Payment|null
636
	 */
637
	public function get_first_payment() {
638
		$payments = $this->get_payments();
639
640
		if ( count( $payments ) > 0 ) {
641
			return $payments[0];
642
		}
643
644
		return null;
645
	}
646
647
	/**
648
	 * Get the start date of this subscription.
649
	 *
650
	 * @return DateTime|null
651
	 */
652
	public function get_start_date() {
653
		return $this->start_date;
654
	}
655
656
	/**
657
	 * Set the start date of this subscription.
658
	 *
659
	 * @param DateTime $date Start date.
660
	 */
661
	public function set_start_date( DateTime $date ) {
662
		$this->start_date = $date;
663
	}
664
665
	/**
666
	 * Get the end date of this subscription.
667
	 *
668
	 * @return DateTime|null
669
	 */
670
	public function get_end_date() {
671
		return $this->end_date;
672
	}
673
674
	/**
675
	 * Set the end date of this subscription.
676
	 *
677
	 * @param DateTime|null $date End date.
678
	 */
679
	public function set_end_date( DateTime $date ) {
680
		$this->end_date = $date;
681
	}
682
683
	/**
684
	 * Get the expiry date of this subscription.
685
	 *
686
	 * @return DateTime
687
	 */
688
	public function get_expiry_date() {
689
		return $this->expiry_date;
690
	}
691
692
	/**
693
	 * Set the expiry date of this subscription.
694
	 *
695
	 * @param DateTime $date Expiry date.
696
	 */
697
	public function set_expiry_date( DateTime $date ) {
698
		$this->expiry_date = $date;
699
	}
700
701
	/**
702
	 * Set the next payment date of this subscription.
703
	 *
704
	 * @param DateTime $date Next payment date.
705
	 */
706
	public function set_next_payment_date( DateTime $date ) {
707
		$this->next_payment = $date;
708
	}
709
710
	/**
711
	 * Get the next payment date of this subscription.
712
	 *
713
	 * @return DateTime
714
	 */
715
	public function get_next_payment_date() {
716
		return $this->next_payment;
717
	}
718
719
	/**
720
	 * Update meta.
721
	 *
722
	 * @todo  Not sure how and when this function is used.
723
	 * @param array $meta The meta data to update.
724
	 */
725
	public function update_meta( $meta ) {
726
		if ( ! is_array( $meta ) || count( $meta ) === 0 ) {
0 ignored issues
show
introduced by
The condition is_array($meta) is always true.
Loading history...
727
			return;
728
		}
729
730
		$note = sprintf(
731
			'<p>%s:</p>',
732
			__( 'Subscription changed', 'pronamic_ideal' )
733
		);
734
735
		$note .= '<dl>';
736
737
		foreach ( $meta as $key => $value ) {
738
			$this->set_meta( $key, $value );
739
740
			if ( $value instanceof DateTime ) {
741
				$value = date_i18n( __( 'l jS \o\f F Y, h:ia', 'pronamic_ideal' ), $value->getTimestamp() );
742
			}
743
744
			$note .= sprintf( '<dt>%s</dt>', esc_html( $key ) );
745
			$note .= sprintf( '<dd>%s</dd>', esc_html( $value ) );
746
		}
747
748
		$note .= '</dl>';
749
750
		$this->add_note( $note );
751
	}
752
753
	/**
754
	 * Save subscription.
755
	 *
756
	 * @return void
757
	 */
758
	public function save() {
759
		pronamic_pay_plugin()->subscriptions_data_store->update( $this );
760
	}
761
}
762