Failed Conditions
Push — develop ( f473d3...859ffd )
by Remco
05:54
created

MemberPress::get_transaction_by_id()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 12
rs 10
1
<?php
2
/**
3
 * MemberPress
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress;
12
13
use MeprSubscription;
14
use MeprTransaction;
15
use MeprOptions;
16
17
/**
18
 * WordPress pay MemberPress
19
 *
20
 * @author  Remco Tolsma
21
 * @version 2.0.1
22
 * @since   1.0.0
23
 */
24
class MemberPress {
25
	/**
26
	 * MemberPress has no unambiguous way to request a transaction via an ID,
27
	 * so we have implemented a method for this.
28
	 * 
29
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L928-L933
30
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L156-L161
31
	 * @param int|string|null $id ID.
32
	 * @return MeprTransaction|null
33
	 */
34
	public static function get_transaction_by_id( $id ) {
35
		$object = MeprTransaction::get_one( $id );
36
37
		if ( ! is_object( $object ) ) {
38
			return null;
39
		}
40
41
		if ( ! isset( $object->id ) ) {
42
			return null;
43
		}
44
45
		$transaction = new MeprTransaction();
46
47
        $transaction->load_data( $object );
48
49
        return $transaction;
50
	}
51
52
	/**
53
	 * Get MemberPress subscription by ID.
54
	 * 
55
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L1468
56
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php#L211-L241
57
	 * @param int|string|null $id ID.
58
	 * @return MeprSubscription|null
59
	 */
60
	public static function get_subscription_by_id( $id ) {
61
		$result = MeprSubscription::get_one_by_subscr_id( $id );
62
63
		if ( $result instanceof MeprSubscription ) {
64
			return $result;
65
		}
66
67
		return null;
68
	}
69
70
	/**
71
	 * Transaction has status.
72
	 *
73
	 * @param MeprTransaction $transaction MemberPress transaction object.
74
	 * @param string|string[] $status      MemberPress transaction status string.
75
	 * @return bool Returns true if the transaction has the specified status, false otherwise.
76
	 */
77
	public static function transaction_has_status( MeprTransaction $transaction, $status ) {
78
		if ( is_array( $status ) ) {
79
			return in_array( $transaction->status, $status, true );
80
		}
81
82
		return ( $transaction->status === $status );
83
	}
84
85
	/**
86
	 * Get currency.
87
	 *
88
	 * @return string
89
	 */
90
	public static function get_currency() {
91
		$mepr_options = MeprOptions::fetch();
92
93
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L136-137
94
		return $mepr_options->currency_code;
95
	}
96
}
97