| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * Class LP_Gateways |
| 5 | 5 | * |
| 6 | 6 | * @author ThimPress |
| 7 | 7 | * @package LearnPress/Classes |
| 8 | - * @version 1.0 | |
| 8 | + * @version 1.0.1 | |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 12 | exit; |
| @@ -29,9 +29,9 @@ | ||
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * LP_Gateways constructor. |
| 32 | 32 | */ |
| 33 | - public function __construct() { | |
| 33 | + private function __construct() { | |
| 34 | 34 | $this->init(); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
| @@ -42,9 +42,9 @@ | ||
| 42 | 42 | $gateways = array( |
| 43 | 43 | 'paypal' => 'LP_Gateway_Paypal', |
| 44 | 44 | 'offline-payment' => 'LP_Gateway_Offline_Payment', |
| 45 | 45 | ); |
| 46 | - // Filter | |
| 46 | + // @deprecated 4.2.3.5 | |
| 47 | 47 | $gateways = apply_filters( 'learn_press_payment_method', $gateways ); |
| 48 | 48 | |
| 49 | 49 | // 3.0.0 |
| 50 | 50 | $gateways = apply_filters( 'learn-press/payment-methods', $gateways ); |
| @@ -51,10 +51,13 @@ | ||
| 51 | 51 | if ( $gateways ) { |
| 52 | 52 | foreach ( $gateways as $k => $gateway ) { |
| 53 | 53 | if ( is_string( $gateway ) && class_exists( $gateway ) ) { |
| 54 | 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; | |
| 55 | 59 | } |
| 56 | - $this->payment_gateways[ $k ] = apply_filters( 'learn-press/payment-gateway/init', $gateway ); | |
| 57 | 60 | } |
| 58 | 61 | } |
| 59 | 62 | } |
| 60 | 63 | } |
| @@ -61,22 +64,29 @@ | ||
| 61 | 64 | |
| 62 | 65 | /** |
| 63 | 66 | * Get all registered payments. |
| 64 | 67 | * |
| 65 | - * @param boolean $with_order If true sort payments with the order saved in admin | |
| 66 | - * | |
| 67 | 68 | * @return array |
| 69 | + * @version 4.0.2 | |
| 68 | 70 | */ |
| 69 | - public function get_gateways( $with_order = false ) { | |
| 70 | - $gateways = array(); | |
| 71 | + public function get_gateways(): array { | |
| 72 | + $gateways = array(); | |
| 73 | + $order_payment_gateways = get_option( 'learn_press_payment_order' ); | |
| 71 | 74 | |
| 72 | 75 | 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 | + 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 | + } | |
| 76 | 81 | } |
| 82 | + } else { | |
| 83 | + $gateways = $this->payment_gateways; | |
| 84 | + } | |
| 77 | 85 | |
| 78 | - if ( ! is_object( $gateway ) ) { | |
| 86 | + // Case new array $gateways not have all gateways | |
| 87 | + foreach ( $this->payment_gateways as $gateway ) { | |
| 88 | + if ( isset( $gateways[ $gateway->id ] ) ) { | |
| 79 | 89 | continue; |
| 80 | 90 | } |
| 81 | 91 | |
| 82 | 92 | $gateways[ $gateway->id ] = $gateway; |
| @@ -82,91 +92,63 @@ | ||
| 82 | 92 | $gateways[ $gateway->id ] = $gateway; |
| 83 | 93 | } |
| 84 | 94 | } |
| 85 | 95 | |
| 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 | 96 | return $gateways; |
| 92 | 97 | } |
| 93 | 98 | |
| 94 | - | |
| 95 | 99 | /** |
| 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 | 100 | * Get payment gateways are available for use. |
| 115 | 101 | * |
| 116 | 102 | * @return mixed |
| 103 | + * @since 3.0.0 | |
| 104 | + * @version 1.0.2 | |
| 117 | 105 | */ |
| 118 | 106 | public function get_available_payment_gateways() { |
| 119 | - $this->init(); | |
| 107 | + /** | |
| 108 | + * @var LP_Gateway_Abstract[] $gateways | |
| 109 | + */ | |
| 110 | + $gateways = $this->get_gateways(); | |
| 120 | 111 | $_available_gateways = array(); |
| 121 | - $is_selected = false; | |
| 112 | + $gateway_first = null; | |
| 122 | 113 | |
| 123 | - foreach ( $this->payment_gateways as $slug => $gateway ) { | |
| 114 | + foreach ( $gateways as $slug => $gateway ) { | |
| 124 | 115 | if ( ! is_object( $gateway ) ) { |
| 125 | 116 | continue; |
| 126 | 117 | } |
| 127 | - if ( $slug == 'woocommerce' ) { | |
| 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' ) { | |
| 128 | 121 | continue; |
| 129 | 122 | } |
| 130 | - /** | |
| 131 | - * @deprecated | |
| 132 | - */ | |
| 133 | - $gateway_available = apply_filters( 'learn_press_payment_gateway_available_' . $slug, true, $gateway ); | |
| 134 | 123 | |
| 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 ) ) { | |
| 124 | + $gateway_is_available = $gateway->is_enabled(); | |
| 125 | + if ( $gateway_is_available ) { | |
| 126 | + $_available_gateways[ $slug ] = $gateway; | |
| 138 | 127 | |
| 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; | |
| 128 | + if ( is_null( $gateway_first ) ) { | |
| 129 | + $gateway_first = $gateway; | |
| 145 | 130 | } |
| 146 | 131 | } |
| 147 | 132 | } |
| 148 | 133 | |
| 149 | 134 | // 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; | |
| 135 | + if ( $_available_gateways ) { | |
| 136 | + $gateway_first->is_selected = true; | |
| 153 | 137 | } |
| 154 | 138 | |
| 155 | - /** | |
| 156 | - * @deprecated | |
| 157 | - */ | |
| 158 | - $_available_gateways = apply_filters( 'learn_press_available_payment_gateways', $_available_gateways ); | |
| 159 | - | |
| 160 | 139 | return apply_filters( 'learn-press/payment-gateways/available', $_available_gateways ); |
| 161 | 140 | } |
| 162 | 141 | |
| 163 | 142 | /** |
| 164 | 143 | * @return array |
| 144 | + * | |
| 145 | + * @deprecated 4.2.3 | |
| 146 | + * @uses LP_Request_Withdrawal::get_gateways() on Addon Commission <= 4.0.1 | |
| 165 | 147 | */ |
| 166 | - public function get_availabe_gateways() { | |
| 148 | + /*public function get_availabe_gateways() { | |
| 167 | 149 | return $this->payment_gateways; |
| 168 | - } | |
| 150 | + }*/ | |
| 169 | 151 | |
| 170 | 152 | /** |
| 171 | 153 | * @param string $id |
| 172 | 154 | * |
| @@ -173,9 +155,9 @@ | ||
| 173 | 155 | * @return bool|LP_Gateway_Abstract |
| 174 | 156 | * @since 3.0.0 |
| 175 | 157 | * |
| 176 | 158 | */ |
| 177 | - public function get_gateway( $id ) { | |
| 159 | + public function get_gateway( string $id ) { | |
| 178 | 160 | $gateways = $this->get_gateways(); |
| 179 | 161 | |
| 180 | 162 | if ( $gateways ) { |
| 181 | 163 | if ( isset( $gateways[ $id ] ) ) { |