RestController.php
5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Class WC_REST_Controller
*
* @package WooCommerce\Payments\MultiCurrency
*/
namespace WCPay\MultiCurrency;
defined( 'ABSPATH' ) || exit;
/**
* REST controller for multi-currency.
*/
class RestController extends \WC_Payments_REST_Controller {
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/multi-currency';
/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/currencies',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_store_currencies' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/update-enabled-currencies',
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_enabled_currencies' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/currencies/(?P<currency_code>[A-Za-z]{3})',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_currency_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/get-settings',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/currencies/(?P<currency_code>[A-Za-z]{3})',
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_currency_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/update-settings',
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [ $this, 'update_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Retrieve currencies for the store.
*
* @return array The store currencies structure.
*/
public function get_store_currencies() {
return [
'available' => WC_Payments_Multi_Currency()->get_available_currencies(),
'enabled' => WC_Payments_Multi_Currency()->get_enabled_currencies(),
'default' => WC_Payments_Multi_Currency()->get_default_currency(),
];
}
/**
* Update enabled currencies based on posted data.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return array The store currencies structure.
*/
public function update_enabled_currencies( $request ) {
$params = $request->get_params();
WC_Payments_Multi_Currency()->set_enabled_currencies( $params['enabled'] );
return $this->get_store_currencies();
}
/**
* Gets the currency settings for a single currency.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return array The currency settings.
*/
public function get_currency_settings( $request ) {
$currency_code = sanitize_key( strtolower( $request['currency_code'] ) );
return [
'exchange_rate_type' => get_option( 'wcpay_multi_currency_exchange_rate_' . $currency_code, 'automatic' ),
'manual_rate' => get_option( 'wcpay_multi_currency_manual_rate_' . $currency_code, null ),
'price_rounding' => get_option( 'wcpay_multi_currency_price_rounding_' . $currency_code, null ),
'price_charm' => get_option( 'wcpay_multi_currency_price_charm_' . $currency_code, null ),
];
}
/**
* Updates the currency settings for a single currency.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return array The currency settings.
*/
public function update_currency_settings( $request ) {
$currency_code = sanitize_key( strtolower( $request['currency_code'] ) );
$params = $request->get_params();
$available_currencies = WC_Payments_Multi_Currency()->get_available_currencies();
if ( array_key_exists( strtoupper( $currency_code ), $available_currencies ) ) {
if ( isset( $params['exchange_rate_type'] ) && in_array( $params['exchange_rate_type'], [ 'automatic', 'manual' ], true ) ) {
update_option( 'wcpay_multi_currency_exchange_rate_' . $currency_code, esc_attr( $params['exchange_rate_type'] ) );
}
if ( 'manual' === $params['exchange_rate_type'] && isset( $params['manual_rate'] ) ) {
update_option( 'wcpay_multi_currency_manual_rate_' . $currency_code, (float) $params['manual_rate'] );
}
if ( isset( $params['price_rounding'] ) ) {
update_option( 'wcpay_multi_currency_price_rounding_' . $currency_code, (float) $params['price_rounding'] );
}
if ( isset( $params['price_charm'] ) ) {
update_option( 'wcpay_multi_currency_price_charm_' . $currency_code, (float) $params['price_charm'] );
}
}
return $this->get_currency_settings( $request );
}
/**
* Gets the store settings for Multi-Currency.
*
* @return array The store settings.
*/
public function get_settings() {
return WC_Payments_Multi_Currency()->get_settings();
}
/**
* Updates Multi-Currency store settings parameters.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return array The store settings.
*/
public function update_settings( $request ) {
$params = $request->get_params();
WC_Payments_Multi_Currency()->update_settings( $params );
return WC_Payments_Multi_Currency()->get_settings();
}
}