| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Gateways |
| 5 |
* |
| 6 |
* @author ThimPress |
| 7 |
* @package LearnPress/Classes |
| 8 |
* @version 1.0 |
| 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 |
public 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 |
// Filter |
| 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 ] = apply_filters( 'learn-press/payment-gateway/init', $gateway ); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get all registered payments. |
| 64 |
* |
| 65 |
* @param boolean $with_order If true sort payments with the order saved in admin |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function get_gateways( $with_order = false ) { |
| 70 |
$gateways = array(); |
| 71 |
|
| 72 |
if ( count( $this->payment_gateways ) ) { |
| 73 |
foreach ( $this->payment_gateways as $gateway ) { |
| 74 |
if ( is_string( $gateway ) && class_exists( $gateway ) ) { |
| 75 |
$gateway = new $gateway(); |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! is_object( $gateway ) ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
$gateways[ $gateway->id ] = $gateway; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( $with_order && get_option( 'learn_press_payment_order' ) ) { |
| 87 |
// Sort gateways by the keys stored. |
| 88 |
usort( $gateways, array( $this, '_sort_gateways_callback' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
return $gateways; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Callback function for sorting payment gateways. |
| 97 |
* |
| 98 |
* @param LP_Gateway_Abstract $a |
| 99 |
* @param LP_Gateway_Abstract $b |
| 100 |
* |
| 101 |
* @return bool|int |
| 102 |
*/ |
| 103 |
public function _sort_gateways_callback( $a, $b ) { |
| 104 |
$ordered = get_option( 'learn_press_payment_order' ); |
| 105 |
|
| 106 |
if ( $ordered ) { |
| 107 |
return array_search( $a->id, $ordered ) > array_search( $b->id, $ordered ); |
| 108 |
} |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get payment gateways are available for use. |
| 115 |
* |
| 116 |
* @return mixed |
| 117 |
*/ |
| 118 |
public function get_available_payment_gateways() { |
| 119 |
$this->init(); |
| 120 |
$_available_gateways = array(); |
| 121 |
$is_selected = false; |
| 122 |
|
| 123 |
foreach ( $this->payment_gateways as $slug => $gateway ) { |
| 124 |
if ( ! is_object( $gateway ) ) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
if ( $slug == 'woocommerce' ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
/** |
| 131 |
* @deprecated |
| 132 |
*/ |
| 133 |
$gateway_available = apply_filters( 'learn_press_payment_gateway_available_' . $slug, true, $gateway ); |
| 134 |
|
| 135 |
if ( $gateway_available ) { |
| 136 |
// Let custom addon can define how is enable/disable |
| 137 |
if ( apply_filters( 'learn-press/payment-gateway/' . $slug . '/available', true, $gateway ) ) { |
| 138 |
|
| 139 |
// If gateway has already selected before |
| 140 |
if ( LP()->session->get( 'chosen_payment_method' ) == $gateway->id ) { |
| 141 |
$gateway->is_selected = true; |
| 142 |
$is_selected = $gateway; |
| 143 |
} |
| 144 |
$_available_gateways[ $slug ] = $gateway; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Set default payment if there is no payment is selected |
| 150 |
if ( $_available_gateways && ! $is_selected ) { |
| 151 |
$gateway = reset( $_available_gateways ); |
| 152 |
$gateway->is_selected = true; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @deprecated |
| 157 |
*/ |
| 158 |
$_available_gateways = apply_filters( 'learn_press_available_payment_gateways', $_available_gateways ); |
| 159 |
|
| 160 |
return apply_filters( 'learn-press/payment-gateways/available', $_available_gateways ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
public function get_availabe_gateways() { |
| 167 |
return $this->payment_gateways; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param string $id |
| 172 |
* |
| 173 |
* @return bool|LP_Gateway_Abstract |
| 174 |
* @since 3.0.0 |
| 175 |
* |
| 176 |
*/ |
| 177 |
public function get_gateway( $id ) { |
| 178 |
$gateways = $this->get_gateways(); |
| 179 |
|
| 180 |
if ( $gateways ) { |
| 181 |
if ( isset( $gateways[ $id ] ) ) { |
| 182 |
return $gateways[ $id ]; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Ensure that only one instance of LP_Gateways is loaded |
| 191 |
* |
| 192 |
* @return LP_Gateways|null |
| 193 |
*/ |
| 194 |
public static function instance() { |
| 195 |
if ( is_null( self::$_instance ) ) { |
| 196 |
self::$_instance = new self(); |
| 197 |
} |
| 198 |
|
| 199 |
return self::$_instance; |
| 200 |
} |
| 201 |
} |
| 202 |
|