| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gateway manager / loader. |
| 4 |
* |
| 5 |
* Holds a registry of payment gateways and lets the checkout class |
| 6 |
* fetch enabled ones for the UI / processing. Other plugins can hook |
| 7 |
* into 'eshb_native_checkout_gateways' to register more gateways |
| 8 |
* without touching this plugin. |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class ESHB_Native_Gateway_Manager { |
| 14 |
|
| 15 |
private static $instance = null; |
| 16 |
|
| 17 |
/** @var ESHB_Native_Abstract_Gateway[] */ |
| 18 |
private $gateways = []; |
| 19 |
|
| 20 |
public static function instance() { |
| 21 |
if ( is_null( self::$instance ) ) { |
| 22 |
self::$instance = new self(); |
| 23 |
} |
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
|
| 27 |
private function __construct() { |
| 28 |
// Built-in gateways. Cash on Delivery is registered first so it |
| 29 |
// is the natural fallback default when no online gateway is set up. |
| 30 |
$this->register_gateway( new ESHB_Native_COD_Gateway() ); |
| 31 |
$this->register_gateway( new ESHB_Native_PayPal_Gateway() ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Filter the array of registered gateway instances. Third-party |
| 35 |
* gateways should be appended here as ESHB_Native_Abstract_Gateway |
| 36 |
* instances. |
| 37 |
* |
| 38 |
* @param ESHB_Native_Abstract_Gateway[] $gateways |
| 39 |
*/ |
| 40 |
$this->gateways = apply_filters( 'eshb_native_checkout_gateways', $this->gateways ); |
| 41 |
} |
| 42 |
|
| 43 |
public function register_gateway( ESHB_Native_Abstract_Gateway $gateway ) { |
| 44 |
$this->gateways[ $gateway->get_id() ] = $gateway; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return ESHB_Native_Abstract_Gateway[] |
| 49 |
*/ |
| 50 |
public function get_gateways( $only_enabled = true ) { |
| 51 |
if ( ! $only_enabled ) { |
| 52 |
return $this->gateways; |
| 53 |
} |
| 54 |
return array_filter( $this->gateways, function ( $gw ) { |
| 55 |
return $gw->is_enabled(); |
| 56 |
} ); |
| 57 |
} |
| 58 |
|
| 59 |
public function get_gateway( $id ) { |
| 60 |
return $this->gateways[ $id ] ?? null; |
| 61 |
} |
| 62 |
} |
| 63 |
|