| @@ -1,183 +1,183 @@ | ||
| 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 | -} | |
| 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 | +} | |