Passed
Push — master ( c24352...fabb71 )
by Brian
14:05 queued 08:38
created

WPInv_Subscription::get_expiration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
// Exit if accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
9
/**
10
 * The Subscription Class
11
 *
12
 * @since  1.0.0
13
 */
14
class WPInv_Subscription {
15
16
	private $subs_db;
17
18
	public $id                = 0;
19
	public $customer_id       = 0;
20
	public $period            = '';
21
	public $initial_amount    = '';
22
	public $recurring_amount  = '';
23
	public $bill_times        = 0;
24
	public $transaction_id    = '';
25
	public $parent_payment_id = 0;
26
	public $product_id        = 0;
27
	public $created           = '0000-00-00 00:00:00';
28
	public $expiration        = '0000-00-00 00:00:00';
29
	public $trial_period      = '';
30
	public $status            = 'pending';
31
	public $profile_id        = '';
32
	public $gateway           = '';
33
	public $customer;
34
35
	/**
36
	 * Get us started
37
	 *
38
	 * @since  1.0.0
39
	 * @return void
40
	 */
41
	function __construct( $_id_or_object = 0, $_by_profile_id = false ) {
42
43
		$this->subs_db = new WPInv_Subscriptions_DB;
44
45
		if( $_by_profile_id ) {
46
47
			$_sub = $this->subs_db->get_by( 'profile_id', $_id_or_object );
48
49
			if( empty( $_sub ) ) {
50
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
51
			}
52
53
			$_id_or_object = $_sub;
54
55
		}
56
57
		return $this->setup_subscription( $_id_or_object );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setup_subscription($_id_or_object) targeting WPInv_Subscription::setup_subscription() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
	}
59
60
	/**
61
	 * Setup the subscription object
62
	 *
63
	 * @since  1.0.0
64
	 * @return void
65
	 */
66
	private function setup_subscription( $id_or_object = 0 ) {
67
68
		if( empty( $id_or_object ) ) {
69
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
70
		}
71
72
		if( is_numeric( $id_or_object ) ) {
73
74
			$sub = $this->subs_db->get( $id_or_object );
75
76
		} elseif( is_object( $id_or_object ) ) {
77
78
			$sub = $id_or_object;
79
80
		}
81
82
		if( empty( $sub ) ) {
83
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
84
		}
85
86
		foreach( $sub as $key => $value ) {
87
			$this->$key = $value;
88
		}
89
90
		$this->customer = get_userdata( $this->customer_id );
91
		$this->gateway  = wpinv_get_payment_gateway( $this->parent_payment_id );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_payment_gateway() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

91
		$this->gateway  = /** @scrutinizer ignore-deprecated */ wpinv_get_payment_gateway( $this->parent_payment_id );
Loading history...
92
93
		do_action( 'wpinv_recurring_setup_subscription', $this );
94
95
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type WPInv_Subscription which is incompatible with the documented return type void.
Loading history...
96
	}
97
98
	/**
99
	 * Magic __get function to dispatch a call to retrieve a private property
100
	 *
101
	 * @since 1.0.0
102
	 */
103
	public function __get( $key ) {
104
105
		if( method_exists( $this, 'get_' . $key ) ) {
106
107
			return call_user_func( array( $this, 'get_' . $key ) );
108
109
		} else {
110
111
			return new WP_Error( 'wpinv-subscription-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) );
112
113
		}
114
115
	}
116
117
	/**
118
	 * Creates a subscription
119
	 *
120
	 * @since  1.0.0
121
	 * @param  array  $data Array of attributes for a subscription
122
	 * @return mixed  false if data isn't passed and class not instantiated for creation
123
	 */
124
	public function create( $data = array() ) {
125
126
		if ( $this->id != 0 ) {
127
			return false;
128
		}
129
130
		$defaults = array(
131
			'customer_id'       => 0,
132
			'frequency'         => '',
133
			'period'            => '',
134
			'initial_amount'    => '',
135
			'recurring_amount'  => '',
136
			'bill_times'        => 0,
137
			'parent_payment_id' => 0,
138
			'product_id'        => 0,
139
			'created'           => '',
140
			'expiration'        => '',
141
			'status'            => '',
142
			'profile_id'        => '',
143
		);
144
145
		$args = wp_parse_args( $data, $defaults );
146
147
		if( $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) {
148
149
			if( 'active' == $args['status'] || 'trialling' == $args['status'] ) {
150
151
				// Force an active subscription to expired if expiration date is in the past
152
				$args['status'] = 'expired';
153
154
			}
155
		}
156
157
		do_action( 'wpinv_subscription_pre_create', $args );
158
159
		$id = $this->subs_db->insert( $args, 'subscription' );
160
161
		do_action( 'wpinv_subscription_post_create', $id, $args );
162
163
		return $this->setup_subscription( $id );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setup_subscription($id) targeting WPInv_Subscription::setup_subscription() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
164
165
	}
166
167
	/**
168
	 * Updates a subscription
169
	 *
170
	 * @since  1.0.0
171
	 * @param  array $args Array of fields to update
172
	 * @return bool
173
	 */
174
	public function update( $args = array() ) {
175
176
		$ret = $this->subs_db->update( $this->id, $args );
177
178
		do_action( 'wpinv_recurring_update_subscription', $this->id, $args, $this );
179
180
		if ( $ret && isset( $args['profile_id'] ) ) {
181
			update_post_meta( $this->parent_payment_id, 'subscription_id', $args['profile_id'] );
182
		}
183
184
		return $ret;
185
186
	}
187
188
	/**
189
	 * Delete the subscription
190
	 *
191
	 * @since  1.0.0
192
	 * @return bool
193
	 */
194
	public function delete() {
195
		return $this->subs_db->delete( $this->id );
196
	}
197
198
    /**
199
     * Retrieves the parent payment ID
200
     *
201
     * @since  1.0.0
202
     * @return int
203
     */
204
    public function get_original_payment_id() {
205
        return $this->parent_payment_id;
206
    }
207
208
    /**
209
     * Retrieve renewal payments for a subscription
210
     *
211
     * @since  1.0.0
212
     * @return array
213
     */
214
    public function get_child_payments() {
215
        $payments = get_posts( array(
216
            'post_parent'    => (int) $this->parent_payment_id,
217
            'posts_per_page' => '999',
218
            'post_status'    => array( 'publish', 'wpi-processing', 'wpi-renewal' ),
219
            'orderby'           => 'ID',
220
            'order'             => 'DESC',
221
            'post_type'      => 'wpi_invoice'
222
        ) );
223
224
        return $payments;
225
    }
226
227
    /**
228
     * Counts the number of payments made to the subscription
229
     *
230
     * @since  2.4
231
     * @return int
232
     */
233
    public function get_total_payments() {
234
        $child_payments = $this->get_child_payments();
235
        $total_payments = !empty( $child_payments ) ? count( $child_payments ) : 0;
236
237
        if ( 'pending' != $this->status ) {
238
                $total_payments++;
239
        }
240
241
        return $total_payments;
242
    }
243
244
    /**
245
     * Returns the number of times the subscription has been billed
246
     *
247
     * @since  1.0.2
248
     * @return int
249
     */
250
    public function get_times_billed() {
251
        $times_billed = (int)$this->get_total_payments();
252
253
        if ( ! empty( $this->trial_period ) && $times_billed > 0 ) {
254
            $times_billed--;
255
        }
256
257
        return $times_billed;
258
    }
259
260
    /**
261
     * Records a new payment on the subscription
262
     *
263
     * @since  2.4
264
     * @param  array $args Array of values for the payment, including amount and transaction ID
265
     * @return bool
266
     */
267
    public function add_payment( $args = array() ) {
268
269
		// Do we have a parent invoice?
270
        if ( ! $this->parent_payment_id || ! is_array( $args ) ) {
271
            return false;
272
        }
273
274
		// Process each payment once.
275
        if ( empty( $args['transaction_id'] ) || $this->payment_exists( $args['transaction_id'] ) ) {
276
            return false;
277
        }
278
279
		// Ensure that the parent invoice is available.
280
        $parent_invoice = wpinv_get_invoice( $this->parent_payment_id );
281
        if ( ! $parent_invoice->get_id() ) {
282
            return false;
283
        }
284
285
		// Duplicate the parent invoice.
286
		$invoice = new WPInv_Invoice( $parent_invoice );
287
		$invoice->set_id( 0 );
288
		$invoice->set_parent_id( $parent_invoice->get_parent() );
289
		$invoice->set_transaction_id( $args['transaction_id'] );
290
		$invoice->set_key( $invoice->generate_key('renewal_') );
291
		$invoice->set_number( '' );
292
		$invoice->set_completed_date( current_time( 'mysql' ) );
293
294
		if ( ! empty( $args['gateway'] ) ) {
295
			$invoice->set_gateway( $args['gateway'] );
296
		}
297
298
		$invoice->set_status( 'wpi-renewal' );
299
300
		$invoice->save();
301
302
		if ( ! $invoice->get_id() ) {
303
			return 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 0 returns the type integer which is incompatible with the documented return type boolean.
Loading history...
304
		}
305
306
		do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this );
307
		do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this );
308
        do_action( 'wpinv_recurring_record_payment', $invoice->get_id(), $this->parent_payment_id, $invoice->get_recurring_total(), $invoice->get_transaction_id() );
309
310
        update_post_meta( $invoice->get_id(), '_wpinv_subscription_id', $this->id );
311
312
        return $invoice->get_id();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $invoice->get_id() returns the type integer which is incompatible with the documented return type boolean.
Loading history...
313
    }
314
315
	/**
316
	 * Retrieves the transaction ID from the subscription
317
	 *
318
	 * @since  1.0.0
319
	 * @return bool
320
	 */
321
	public function get_transaction_id() {
322
323
		if( empty( $this->transaction_id ) ) {
324
325
			$txn_id = wpinv_get_payment_transaction_id( $this->parent_payment_id );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_payment_transaction_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

325
			$txn_id = /** @scrutinizer ignore-deprecated */ wpinv_get_payment_transaction_id( $this->parent_payment_id );
Loading history...
326
327
			if( ! empty( $txn_id ) && (int) $this->parent_payment_id !== (int) $txn_id ) {
328
				$this->set_transaction_id( $txn_id );
329
			}
330
331
		}
332
333
		return $this->transaction_id;
334
335
	}
336
337
	/**
338
	 * Stores the transaction ID for the subscription purchase
339
	 *
340
	 * @since  1.0.0.4
341
	 * @return bool
342
	 */
343
	public function set_transaction_id( $txn_id = '' ) {
344
		$this->update( array( 'transaction_id' => $txn_id ) );
345
		$this->transaction_id = $txn_id;
346
	}
347
348
	/**
349
	 * Renews a subscription
350
	 *
351
	 * @since  1.0.0
352
	 * @return bool
353
	 */
354
	public function renew() {
355
356
		// Calculate new expiration
357
		$expires        = $this->get_expiration_time();
358
		$base_date      = $expires > current_time( 'timestamp' ) ? $expires : current_time( 'timestamp' );
359
		$frequency      = isset( $this->frequency ) ? $this->frequency : 1;
0 ignored issues
show
Bug Best Practice introduced by
The property frequency does not exist on WPInv_Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
360
		$new_expiration = strtotime( "+ {$frequency} {$this->period}", strtotime( $base_date ) );
361
		$new_expiration = apply_filters( 'wpinv_subscription_renewal_expiration', date( 'Y-m-d H:i:s', $new_expiration ), $this->id, $this );
362
363
		do_action( 'wpinv_subscription_pre_renew', $this->id, $new_expiration, $this );
364
365
		$this->status = 'active';
366
		$times_billed = $this->get_times_billed();
367
368
		// Complete subscription if applicable
369
		if ( $this->bill_times > 0 && $times_billed >= $this->bill_times ) {
370
			$this->complete();
371
			$this->status = 'completed';
372
			return;
373
		}
374
375
		$args = array(
376
			'expiration' => $new_expiration,
377
			'status'     => $this->status,
378
		);
379
380
        $this->subs_db->update( $this->id, $args );
381
382
		$this->expiration = $new_expiration;
383
384
		do_action( 'wpinv_subscription_post_renew', $this->id, $new_expiration, $this );
385
		do_action( 'wpinv_recurring_set_subscription_status', $this->id, $this->status, $this );
386
387
	}
388
389
	/**
390
	 * Marks a subscription as completed
391
	 *
392
	 * Subscription is completed when the number of payments matches the billing_times field
393
	 *
394
	 * @since  1.0.0
395
	 * @return void
396
	 */
397
	public function complete() {
398
399
		// Only mark a subscription as complete if it's not already cancelled.
400
		if ( 'cancelled' === $this->status ) {
401
			return;
402
		}
403
404
		$args = array(
405
			'status' => 'completed'
406
		);
407
408
		if( $this->subs_db->update( $this->id, $args ) ) {
409
410
			$this->status = 'completed';
411
412
			do_action( 'wpinv_subscription_completed', $this->id, $this );
413
414
		}
415
416
	}
417
418
	/**
419
	 * Marks a subscription as expired
420
	 *
421
	 * Subscription is completed when the billing times is reached
422
	 *
423
	 * @since  1.0.0
424
	 * @param  $check_expiration bool True if expiration date should be checked with merchant processor before expiring
425
	 * @return void
426
	 */
427
	public function expire( $check_expiration = false ) {
428
429
		$expiration = $this->expiration;
430
431
		if( $check_expiration ) {
432
433
			// check_expiration() updates $this->expiration so compare to $expiration above
434
435
			if( $expiration < $this->get_expiration() && current_time( 'timestamp' ) < $this->get_expiration_time() ) {
436
437
				return false; // Do not mark as expired since real expiration date is in the future
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
438
			}
439
440
		}
441
442
		$args = array(
443
			'status' => 'expired'
444
		);
445
446
		if( $this->subs_db->update( $this->id, $args ) ) {
447
448
			$this->status = 'expired';
449
450
			do_action( 'wpinv_subscription_expired', $this->id, $this );
451
452
		}
453
454
	}
455
456
	/**
457
	 * Marks a subscription as failing
458
	 *
459
	 * @since  2.4.2
460
	 * @return void
461
	 */
462
	public function failing() {
463
464
		$args = array(
465
			'status' => 'failing'
466
		);
467
468
		if( $this->subs_db->update( $this->id, $args ) ) {
469
470
			$this->status = 'failing';
471
472
			do_action( 'wpinv_subscription_failing', $this->id, $this );
473
			do_action( 'wpinv_recurring_payment_failed', $this );
474
475
		}
476
477
	}
478
479
    /**
480
     * Marks a subscription as cancelled
481
     *
482
     * @since  1.0.0
483
     * @return void
484
     */
485
    public function cancel() {
486
        if ( 'cancelled' === $this->status ) {
487
            return; // Already cancelled
488
        }
489
490
        $args = array(
491
            'status' => 'cancelled'
492
        );
493
494
        if ( $this->subs_db->update( $this->id, $args ) ) {
495
            if ( is_user_logged_in() ) {
496
                $userdata = get_userdata( get_current_user_id() );
497
                $user     = $userdata->display_name;
498
            } else {
499
                $user = __( 'gateway', 'invoicing' );
500
            }
501
502
            $note = sprintf( __( 'Subscription has been cancelled by %s', 'invoicing' ), $user );
503
            wpinv_insert_payment_note( $this->parent_payment_id, $note, '', '', true );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_insert_payment_note() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

503
            /** @scrutinizer ignore-deprecated */ wpinv_insert_payment_note( $this->parent_payment_id, $note, '', '', true );
Loading history...
504
505
            $this->status = 'cancelled';
506
507
            do_action( 'wpinv_subscription_cancelled', $this->id, $this );
508
        }
509
    }
510
511
	/**
512
	 * Determines if subscription can be cancelled
513
	 *
514
	 * This method is filtered by payment gateways in order to return true on subscriptions
515
	 * that can be cancelled with a profile ID through the merchant processor
516
	 *
517
	 * @since  1.0.0
518
	 * @return bool
519
	 */
520
	public function can_cancel() {
521
        $ret = false;
522
	    if( $this->gateway === 'manual' || in_array( $this->status, $this->get_cancellable_statuses() ) ) {
523
            $ret = true;
524
        }
525
		return apply_filters( 'wpinv_subscription_can_cancel', $ret, $this );
526
	}
527
528
    /**
529
     * Returns an array of subscription statuses that can be cancelled
530
     *
531
     * @access      public
532
     * @since       1.0.0
533
     * @return      array
534
     */
535
    public function get_cancellable_statuses() {
536
        return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) );
537
    }
538
539
	/**
540
	 * Retrieves the URL to cancel subscription
541
	 *
542
	 * @since  1.0.0
543
	 * @return string
544
	 */
545
	public function get_cancel_url() {
546
547
		$url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'cancel_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-cancel' );
548
549
		return apply_filters( 'wpinv_subscription_cancel_url', $url, $this );
550
	}
551
552
	/**
553
	 * Determines if subscription can be manually renewed
554
	 *
555
	 * This method is filtered by payment gateways in order to return true on subscriptions
556
	 * that can be renewed manually
557
	 *
558
	 * @since  2.5
559
	 * @return bool
560
	 */
561
	public function can_renew() {
562
563
		return apply_filters( 'wpinv_subscription_can_renew', true, $this );
564
	}
565
566
	/**
567
	 * Retrieves the URL to renew a subscription
568
	 *
569
	 * @since  2.5
570
	 * @return string
571
	 */
572
	public function get_renew_url() {
573
574
		$url = wp_nonce_url( add_query_arg( array( 'wpinv_action' => 'renew_subscription', 'sub_id' => $this->id ) ), 'wpinv-recurring-renew' );
575
576
		return apply_filters( 'wpinv_subscription_renew_url', $url, $this );
577
	}
578
579
	/**
580
	 * Determines if subscription can have their payment method updated
581
	 *
582
	 * @since  1.0.0
583
	 * @return bool
584
	 */
585
	public function can_update() {
586
		return apply_filters( 'wpinv_subscription_can_update', false, $this );
587
	}
588
589
	/**
590
	 * Retrieves the URL to update subscription
591
	 *
592
	 * @since  1.0.0
593
	 * @return void
594
	 */
595
	public function get_update_url() {
596
597
		$url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->id ) );
598
599
		return apply_filters( 'wpinv_subscription_update_url', $url, $this );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('wp...date_url', $url, $this) also could return the type string which is incompatible with the documented return type void.
Loading history...
600
	}
601
602
	/**
603
	 * Determines if subscription is active
604
	 *
605
	 * @since  1.0.0
606
	 * @return void
607
	 */
608
	public function is_active() {
609
610
		$ret = false;
611
612
		if( ! $this->is_expired() && ( $this->status == 'active' || $this->status == 'cancelled' || $this->status == 'trialling' ) ) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->is_expired() targeting WPInv_Subscription::is_expired() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
613
			$ret = true;
614
		}
615
616
		return apply_filters( 'wpinv_subscription_is_active', $ret, $this->id, $this );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('wp...$ret, $this->id, $this) also could return the type boolean which is incompatible with the documented return type void.
Loading history...
617
618
	}
619
620
	/**
621
	 * Determines if subscription is expired
622
	 *
623
	 * @since  1.0.0
624
	 * @return void
625
	 */
626
	public function is_expired() {
627
628
		$ret = false;
629
630
		if ( $this->status == 'expired' ) {
631
632
			$ret = true;
633
634
		} elseif( 'active' === $this->status || 'cancelled' === $this->status || $this->status == 'trialling'  ) {
635
636
			$ret        = false;
637
			$expiration = $this->get_expiration_time();
638
639
			if( $expiration && strtotime( 'NOW', current_time( 'timestamp' ) ) > $expiration ) {
640
				$ret = true;
641
642
				if ( 'active' === $this->status || $this->status == 'trialling'  ) {
643
					$this->expire();
644
				}
645
			}
646
647
		}
648
649
		return apply_filters( 'wpinv_subscription_is_expired', $ret, $this->id, $this );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('wp...$ret, $this->id, $this) also could return the type boolean which is incompatible with the documented return type void.
Loading history...
650
651
	}
652
653
	/**
654
	 * Retrieves the expiration date
655
	 *
656
	 * @since  1.0.0
657
	 * @return string
658
	 */
659
	public function get_expiration() {
660
		return $this->expiration;
661
	}
662
663
	/**
664
	 * Retrieves the expiration date in a timestamp
665
	 *
666
	 * @since  1.0.0
667
	 * @return int
668
	 */
669
	public function get_expiration_time() {
670
		return strtotime( $this->expiration, current_time( 'timestamp' ) );
671
	}
672
673
	/**
674
	 * Retrieves the subscription status
675
	 *
676
	 * @since  1.0.0
677
	 * @return int
678
	 */
679
	public function get_status() {
680
681
		// Monitor for page load delays on pages with large subscription lists (IE: Subscriptions table in admin)
682
		$this->is_expired();
683
		return $this->status;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->status returns the type string which is incompatible with the documented return type integer.
Loading history...
684
	}
685
686
	/**
687
	 * Retrieves the subscription status label
688
	 *
689
	 * @since  1.0.0
690
	 * @return int
691
	 */
692
	public function get_status_label() {
693
694
		switch( $this->get_status() ) {
695
			case 'active' :
696
				$status = __( 'Active', 'invoicing' );
697
				break;
698
699
			case 'cancelled' :
700
				$status = __( 'Cancelled', 'invoicing' );
701
				break;
702
703
			case 'expired' :
704
				$status = __( 'Expired', 'invoicing' );
705
				break;
706
707
			case 'pending' :
708
				$status = __( 'Pending', 'invoicing' );
709
				break;
710
711
			case 'failing' :
712
				$status = __( 'Failing', 'invoicing' );
713
				break;
714
715
			case 'trialling' :
716
				$status = __( 'Trialling', 'invoicing' );
717
				break;
718
719
			case 'completed' :
720
				$status = __( 'Completed', 'invoicing' );
721
				break;
722
723
			default:
724
				$status = ucfirst( $this->get_status() );
725
				break;
726
		}
727
728
		return $status;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $status returns the type string which is incompatible with the documented return type integer.
Loading history...
729
	}
730
731
    /**
732
     * Retrieves the subscription status label
733
     *
734
     * @since  1.0.0
735
     * @return int
736
     */
737
    public function get_status_label_html() {
738
739
        switch( $get_status = $this->get_status() ) {
740
            case 'active' :
741
                $status = __( 'Active', 'invoicing' );
742
                $class = 'label-info';
743
                break;
744
745
            case 'cancelled' :
746
                $status = __( 'Cancelled', 'invoicing' );
747
                $class = 'label-danger';
748
                break;
749
750
            case 'expired' :
751
                $status = __( 'Expired', 'invoicing' );
752
                $class = 'label-default';
753
                break;
754
755
            case 'pending' :
756
                $status = __( 'Pending', 'invoicing' );
757
                $class = 'label-primary';
758
                break;
759
760
            case 'failing' :
761
                $status = __( 'Failing', 'invoicing' );
762
                $class = 'label-danger';
763
                break;
764
765
            case 'trialling' :
766
                $status = __( 'Trialling', 'invoicing' );
767
                $class = 'label-info';
768
                break;
769
770
            case 'completed' :
771
                $status = __( 'Completed', 'invoicing' );
772
                $class = 'label-success';
773
                break;
774
775
            default:
776
                $status = ucfirst( $this->get_status() );
777
                $class = 'label-default';
778
                break;
779
        }
780
781
        $label = '<span class="sub-status label label-sub-' . $get_status . ' ' . $class . '">' . $status . '</span>';
782
783
        return apply_filters( 'wpinv_subscription_status_label_html', $label, $get_status, $status );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('wp..., $get_status, $status) also could return the type string which is incompatible with the documented return type integer.
Loading history...
784
    }
785
786
    /**
787
     * Determines if a payment exists with the specified transaction ID
788
     *
789
     * @since  2.4
790
     * @param  string $txn_id The transaction ID from the merchant processor
791
     * @return bool
792
     */
793
    public function payment_exists( $txn_id = '' ) {
794
        global $wpdb;
795
796
        if ( empty( $txn_id ) ) {
797
            return false;
798
        }
799
800
        $txn_id = esc_sql( $txn_id );
801
802
        $purchase = $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpinv_transaction_id' AND meta_value = '{$txn_id}' LIMIT 1" );
803
804
        if ( $purchase != null ) {
805
            return true;
806
        }
807
808
        return false;
809
    }
810
811
}
812