LLMS_Payment_Gateways

Manage and access LifterLMS payment gateways.


Source Source

File: includes/class.llms.payment.gateways.php

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
class LLMS_Payment_Gateways {
 
    use LLMS_Trait_Singleton;
 
    /**
     * Payment Gateways
     *
     * @var LLMS_Payment_Gateway[]
     */
    public $payment_gateways = array();
 
    /**
     * Constructor.
     *
     * @since 3.0.0
     *
     * @return void
     */
    public function __construct() {
 
        add_filter( 'lifterlms_payment_gateways', array( $this, 'add_core_gateways' ) );
 
        /**
         * Filters the list of registered LifterLMS Payment Gateway classes.
         *
         * @since 3.0.0
         *
         * @param string[] $gateways Array of payment gateway class names.
         */
        $gateways = apply_filters( 'lifterlms_payment_gateways', $this->payment_gateways );
 
        foreach ( $gateways as $gateway ) {
 
            $load_gateway = new $gateway();
 
            $order = absint( $load_gateway->get_display_order() );
 
            // If the order already exists create a new order for it.
            if ( isset( $this->payment_gateways[ $order ] ) ) {
                $order = max( array_keys( $this->payment_gateways ) ) + 1;
            }
 
            $this->payment_gateways[ $order ] = $load_gateway;
        }
 
        ksort( $this->payment_gateways );
 
    }
 
    /**
     * Register core gateways.
     *
     * @since 3.0.0
     *
     * @param string[] $gateways Array of gateway class names.
     * @return string[]
     */
    public function add_core_gateways( $gateways ) {
        $gateways[] = 'LLMS_Payment_Gateway_Manual';
        return $gateways;
    }
 
    /**
     * Get only enabled payment gateways.
     *
     * @since 3.0.0
     *
     * @return array
     */
    public function get_enabled_payment_gateways() {
 
        $gateways = array();
        foreach ( $this->get_payment_gateways() as $gateway ) {
            if ( $gateway->is_enabled() ) {
                $gateways[ $gateway->get_id() ] = $gateway;
            }
        }
 
        /**
         * Filters the registered LifterLMS Payment Gateways which are explicitly enabled.
         *
         * @since 3.0.0
         *
         * @param LLMS_Payment_Gateway[] $gateways List of enabled gateways.
         */
        return apply_filters( 'lifterlms_enabled_payment_gateways', $gateways );
 
    }
 
    /**
     * Retrieves the default payment gateway ID.
     *
     * The default gateway is the first gateway in the list of enabled gateways.
     *
     * @since 3.0.0
     *
     * @return string
     */
    public function get_default_gateway() {
 
        $gateways = $this->get_enabled_payment_gateways();
        $ids      = array_keys( $gateways );
        return array_shift( $ids );
 
    }
 
    /**
     * Retrieves a payment gateway object by the gateway ID.
     *
     * @since 2.5.0
     *
     * @param string $id  id of the gateway (paypal, stripe, etc...)
     * @return LLMS_Payment_Gateway|boolean Returns the gateway if it's registered, otherwise `false`.
     */
    public function get_gateway_by_id( $id ) {
 
        $gateways = $this->get_payment_gateways();
 
        if ( array_key_exists( $id, $gateways ) ) {
            return $gateways[ $id ];
        }
 
        return false;
 
    }
 
    /**
     * Retrieves all registered payment gateways.
     *
     * @since 3.0.0
     *
     * @return LLMS_Payment_Gateway[]
     */
    public function get_payment_gateways() {
 
        $gateways = array();
        foreach ( $this->payment_gateways as $gateway ) {
            $gateways[ $gateway->id ] = $gateway;
        }
        return $gateways;
 
    }
 
    /**
     * Retrieves all enabled gateways which support the specified gateway feature.
     *
     * @since 3.10.0
     *
     * @see LLMS_Payment_Gateway::get_supported_features()
     *
     * @param string $feature A gateway feature string.
     * @return array
     */
    public function get_supporting_gateways( $feature ) {
 
        $gateways = array();
        foreach ( $this->get_enabled_payment_gateways() as $id => $gateway ) {
            if ( $gateway->supports( $feature ) ) {
                $gateways[ $id ] = $gateway;
            }
        }
 
        /**
         * Filters the list of gateways supporting the specified feature.
         *
         * Hook description.
         *
         * @since 3.10.0
         *
         * @param LLMS_Payment_Gateway[] $gateways Array of supporting gateways.
         * @param string                 $feature  The requested gateway feature string.
         */
        return apply_filters( 'lifterlms_supporting_payment_gateways', $gateways, $feature );
 
    }
 
    /**
     * Determines if any payment gateways are registered.
     *
     * @since 3.0.0
     * @since 6.5.0 Refactor for code simplicity.
     *
     * @param boolean $enabled Whether or not to check against only enabled gateways.
     * @return boolean
     */
    public function has_gateways( $enabled = false ) {
        $method = $enabled ? 'get_enabled_payment_gateways' : 'get_payment_gateways';
        return count( $this->{$method}() ) >= 1;
    }
 
}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Removed the deprecated LLMS_Payment_Gateways::$_instance property.
5.3.0 Replace singleton code with LLMS_Trait_Singleton.
3.0.0 Unknown.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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