| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Gateway Settings UI. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Gateway_Settings |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2019, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Gateway_Settings' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Gateway_Settings |
| 22 |
* |
| 23 |
* @final |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
final class Charitable_Gateway_Settings { |
| 27 |
|
| 28 |
/** |
| 29 |
* The single instance of this class. |
| 30 |
* |
| 31 |
* @var Charitable_Gateway_Settings|null |
| 32 |
*/ |
| 33 |
private static $instance = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Create object instance. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
private function __construct() { |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns and/or create the single instance of this class. |
| 45 |
* |
| 46 |
* @since 1.2.0 |
| 47 |
* |
| 48 |
* @return Charitable_Gateway_Settings |
| 49 |
*/ |
| 50 |
public static function get_instance() { |
| 51 |
if ( is_null( self::$instance ) ) { |
| 52 |
self::$instance = new self(); |
| 53 |
} |
| 54 |
|
| 55 |
return self::$instance; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns all the payment gateway settings fields. |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function add_gateway_fields() { |
| 66 |
if ( ! charitable_is_settings_view( 'gateways' ) ) { |
| 67 |
return array(); |
| 68 |
} |
| 69 |
|
| 70 |
return array( |
| 71 |
'section' => array( |
| 72 |
'title' => '', |
| 73 |
'type' => 'hidden', |
| 74 |
'priority' => 10000, |
| 75 |
'value' => 'gateways', |
| 76 |
'save' => false, |
| 77 |
), |
| 78 |
'section_gateways' => array( |
| 79 |
'title' => __( 'Available Payment Gateways', 'charitable' ), |
| 80 |
'type' => 'heading', |
| 81 |
'priority' => 5, |
| 82 |
), |
| 83 |
'gateways' => array( |
| 84 |
'title' => false, |
| 85 |
'callback' => array( $this, 'render_gateways_table' ), |
| 86 |
'priority' => 10, |
| 87 |
), |
| 88 |
'test_mode' => array( |
| 89 |
'title' => __( 'Turn on Test Mode', 'charitable' ), |
| 90 |
'type' => 'checkbox', |
| 91 |
'priority' => 15, |
| 92 |
), |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Add settings for each individual payment gateway. |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @return array[] |
| 102 |
*/ |
| 103 |
public function add_individual_gateway_fields( $fields ) { |
| 104 |
foreach ( charitable_get_helper( 'gateways' )->get_active_gateways() as $gateway ) { |
| 105 |
if ( ! class_exists( $gateway ) ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
$gateway = new $gateway; |
| 110 |
$fields[ 'gateways_' . $gateway->get_gateway_id() ] = apply_filters( 'charitable_settings_fields_gateways_gateway', array(), $gateway ); |
| 111 |
} |
| 112 |
|
| 113 |
return $fields; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Add gateway keys to the settings groups. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @param string[] $groups |
| 122 |
* @return string[] |
| 123 |
*/ |
| 124 |
public function add_gateway_settings_dynamic_groups( $groups ) { |
| 125 |
foreach ( charitable_get_helper( 'gateways' )->get_active_gateways() as $gateway_key => $gateway ) { |
| 126 |
if ( ! class_exists( $gateway ) ) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$groups[ 'gateways_' . $gateway_key ] = apply_filters( 'charitable_gateway_settings_fields_gateways_gateway', array(), new $gateway ); |
| 131 |
} |
| 132 |
|
| 133 |
return $groups; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Display table with available payment gateways. |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function render_gateways_table( $args ) { |
| 144 |
charitable_admin_view( 'settings/gateways', $args ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
endif; |
| 149 |
|