PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.8
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / gateways / class-lp-gateways.php

class-lp-gateways.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/gateways/class-lp-gateways.php

184 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Gateways
5 *
6 * @author ThimPress
7 * @package LearnPress/Classes
8 * @version 1.0.1
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class LP_Gateways
17 */
18 class LP_Gateways {
19
20 /**
21 * @var null
22 */
23 protected static $_instance = null;
24
25 /**
26 * @var array
27 */
28 protected $payment_gateways = array();
29
30 /**
31 * LP_Gateways constructor.
32 */
33 private function __construct() {
34 $this->init();
35 }
36
37 /**
38 * Init gateways
39 */
40 public function init() {
41 if ( ! $this->payment_gateways ) {
42 $gateways = array(
43 'paypal' => 'LP_Gateway_Paypal',
44 'offline-payment' => 'LP_Gateway_Offline_Payment',
45 );
46 // @deprecated 4.2.3.5
47 $gateways = apply_filters( 'learn_press_payment_method', $gateways );
48
49 // 3.0.0
50 $gateways = apply_filters( 'learn-press/payment-methods', $gateways );
51 if ( $gateways ) {
52 foreach ( $gateways as $k => $gateway ) {
53 if ( is_string( $gateway ) && class_exists( $gateway ) ) {
54 $gateway = new $gateway();
55
56 $this->payment_gateways[ $k ] = $gateway;
57 } elseif ( $gateway instanceof LP_Gateway_Abstract ) {
58 $this->payment_gateways[ $gateway->id ] = $gateway;
59 }
60 }
61 }
62 }
63 }
64
65 /**
66 * Get all registered payments.
67 *
68 * @return array
69 * @version 4.0.2
70 */
71 public function get_gateways(): array {
72 $gateways = array();
73 $order_payment_gateways = get_option( 'learn_press_payment_order' );
74
75 if ( count( $this->payment_gateways ) ) {
76 if ( $order_payment_gateways ) {
77 foreach ( $order_payment_gateways as $id ) {
78 if ( isset( $this->payment_gateways[ $id ] ) ) {
79 $gateways[ $id ] = $this->payment_gateways[ $id ];
80 }
81 }
82 } else {
83 $gateways = $this->payment_gateways;
84 }
85
86 // Case new array $gateways not have all gateways
87 foreach ( $this->payment_gateways as $gateway ) {
88 if ( isset( $gateways[ $gateway->id ] ) ) {
89 continue;
90 }
91
92 $gateways[ $gateway->id ] = $gateway;
93 }
94 }
95
96 return $gateways;
97 }
98
99 /**
100 * Get payment gateways are available for use.
101 *
102 * @return mixed
103 * @since 3.0.0
104 * @version 1.0.2
105 */
106 public function get_available_payment_gateways() {
107 /**
108 * @var LP_Gateway_Abstract[] $gateways
109 */
110 $gateways = $this->get_gateways();
111 $_available_gateways = array();
112 $gateway_first = null;
113
114 foreach ( $gateways as $slug => $gateway ) {
115 if ( ! is_object( $gateway ) ) {
116 continue;
117 }
118
119 // Not show woo payment gateway, because when enable will be buy course on checkout of Woocommerce
120 if ( $slug == 'woocommerce' || $slug == 'woo-payment' ) {
121 continue;
122 }
123
124 $gateway_is_available = $gateway->is_enabled();
125 if ( $gateway_is_available ) {
126 $_available_gateways[ $slug ] = $gateway;
127
128 if ( is_null( $gateway_first ) ) {
129 $gateway_first = $gateway;
130 }
131 }
132 }
133
134 // Set default payment if there is no payment is selected
135 if ( $_available_gateways ) {
136 $gateway_first->is_selected = true;
137 }
138
139 return apply_filters( 'learn-press/payment-gateways/available', $_available_gateways );
140 }
141
142 /**
143 * @return array
144 *
145 * @deprecated 4.2.3
146 * @uses LP_Request_Withdrawal::get_gateways() on Addon Commission <= 4.0.1
147 */
148 /*public function get_availabe_gateways() {
149 return $this->payment_gateways;
150 }*/
151
152 /**
153 * @param string $id
154 *
155 * @return bool|LP_Gateway_Abstract
156 * @since 3.0.0
157 *
158 */
159 public function get_gateway( string $id ) {
160 $gateways = $this->get_gateways();
161
162 if ( $gateways ) {
163 if ( isset( $gateways[ $id ] ) ) {
164 return $gateways[ $id ];
165 }
166 }
167
168 return false;
169 }
170
171 /**
172 * Ensure that only one instance of LP_Gateways is loaded
173 *
174 * @return LP_Gateways|null
175 */
176 public static function instance() {
177 if ( is_null( self::$_instance ) ) {
178 self::$_instance = new self();
179 }
180
181 return self::$_instance;
182 }
183 }
184