| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) or die( 'No direct script access allowed.' ); |
| 4 |
|
| 5 |
if ( class_exists( 'Omise_Setting' ) ) { |
| 6 |
return; |
| 7 |
} |
| 8 |
|
| 9 |
class Omise_Setting { |
| 10 |
/** |
| 11 |
* The Omise_Setting Instance. |
| 12 |
* |
| 13 |
* @since 3.4 |
| 14 |
* |
| 15 |
* @var \Omise_Setting |
| 16 |
*/ |
| 17 |
protected static $the_instance = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var null | array |
| 21 |
*/ |
| 22 |
public $settings; |
| 23 |
|
| 24 |
/** |
| 25 |
* The Omise_Setting Instance. |
| 26 |
* |
| 27 |
* @since 3.4 |
| 28 |
* |
| 29 |
* @static |
| 30 |
* |
| 31 |
* @return \Omise_Setting - The instance. |
| 32 |
*/ |
| 33 |
public static function instance() { |
| 34 |
if ( is_null( self::$the_instance ) ) { |
| 35 |
self::$the_instance = new self(); |
| 36 |
} |
| 37 |
|
| 38 |
return self::$the_instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @since 3.1 |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
$this->settings = $this->get_payment_settings( 'omise' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return array |
| 50 |
* |
| 51 |
* @since 3.1 |
| 52 |
*/ |
| 53 |
protected function get_default_settings() { |
| 54 |
return array( |
| 55 |
'sandbox' => 'yes', |
| 56 |
'test_public_key' => '', |
| 57 |
'test_private_key' => '', |
| 58 |
'live_public_key' => '', |
| 59 |
'live_private_key' => '' |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return array |
| 65 |
* |
| 66 |
* @since 3.1 |
| 67 |
*/ |
| 68 |
public function get_settings() { |
| 69 |
return $this->settings; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the payment gateway settings option name |
| 74 |
* |
| 75 |
* @param string $payment_id Payment ID can be found at each of gateway classes (includes/gateway). |
| 76 |
* |
| 77 |
* @return string The payment gateway settings option name. |
| 78 |
* |
| 79 |
* @since 3.1 |
| 80 |
*/ |
| 81 |
protected function get_payment_method_settings_name( $payment_id = 'omise' ) { |
| 82 |
return 'woocommerce_' . $payment_id . '_settings'; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get Omise settings from 'wp_options' table. |
| 87 |
* |
| 88 |
* @param string $payment_id |
| 89 |
* |
| 90 |
* @return array |
| 91 |
* |
| 92 |
* @since 3.1 |
| 93 |
*/ |
| 94 |
public function get_payment_settings( $payment_id ) { |
| 95 |
if ( $options = get_option( $this->get_payment_method_settings_name( $payment_id ) ) ) { |
| 96 |
return array_merge( |
| 97 |
$this->get_default_settings(), |
| 98 |
$options |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
return $this->get_default_settings(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param array $data |
| 107 |
* |
| 108 |
* @return array |
| 109 |
* |
| 110 |
* @since 3.1 |
| 111 |
*/ |
| 112 |
public function update_settings( $data ) { |
| 113 |
$data = array_intersect_key( $data, $this->get_default_settings() ); |
| 114 |
$data['sandbox'] = isset( $data['sandbox'] ) && ! is_null( $data['sandbox'] ) ? 'yes' : 'no'; |
| 115 |
|
| 116 |
array_walk( $data, function( &$input, $key ) { |
| 117 |
$input = esc_html( sanitize_text_field( $input ) ); |
| 118 |
} ); |
| 119 |
|
| 120 |
$this->settings = array_merge( |
| 121 |
$this->settings, |
| 122 |
$data |
| 123 |
); |
| 124 |
|
| 125 |
update_option( $this->get_payment_method_settings_name( 'omise' ), $this->settings ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Whether Sandbox (test) mode is enabled or not. |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
* |
| 133 |
* @since 3.1 |
| 134 |
*/ |
| 135 |
public function is_test() { |
| 136 |
$sandbox = $this->settings['sandbox']; |
| 137 |
|
| 138 |
return isset( $sandbox ) && $sandbox == 'yes'; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Return Omise public key. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
* |
| 146 |
* @since 3.1 |
| 147 |
*/ |
| 148 |
public function public_key() { |
| 149 |
if ( $this->is_test() ) { |
| 150 |
return $this->settings['test_public_key']; |
| 151 |
} |
| 152 |
|
| 153 |
return $this->settings['live_public_key']; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return Omise secret key. |
| 158 |
* |
| 159 |
* @return string |
| 160 |
* |
| 161 |
* @since 3.1 |
| 162 |
*/ |
| 163 |
public function secret_key() { |
| 164 |
if ( $this->is_test() ) { |
| 165 |
return $this->settings['test_private_key']; |
| 166 |
} |
| 167 |
|
| 168 |
return $this->settings['live_private_key']; |
| 169 |
} |
| 170 |
} |
| 171 |
|