PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Onboarding / Routes / CurrencyRoute.php

CurrencyRoute.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Onboarding/Routes/CurrencyRoute.php

98 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Onboarding\Routes;
4
5 use Give\API\RestRoute;
6 use Give\Onboarding\SettingsRepositoryFactory;
7 use WP_REST_Request;
8
9 /**
10 * @since 2.8.0
11 */
12 class CurrencyRoute implements RestRoute
13 {
14
15 /** @var string */
16 protected $endpoint = 'onboarding/settings/currency';
17
18 /**
19 * @var SettingsRepositoryFactory
20 */
21 protected $settingsRepository;
22
23 /**
24 * @since 2.8.0
25 */
26 public function __construct(SettingsRepositoryFactory $settingsRepositoryFactory)
27 {
28 $this->settingsRepository = $settingsRepositoryFactory->make('give_settings');
29 }
30
31 /**
32 * @since 2.8.0
33 *
34 * @param WP_REST_Request $request
35 *
36 * @return array
37 *
38 */
39 public function handleRequest(WP_REST_Request $request)
40 {
41 $currencyCode = json_decode($request->get_param('value'));
42
43 $currencyList = give_get_currencies_list();
44 $currencyConfiguration = $currencyList[$currencyCode]['setting'];
45
46 $this->settingsRepository->set('currency', $currencyCode);
47 $this->settingsRepository->set('currency_position', $currencyConfiguration['currency_position']);
48 $this->settingsRepository->set('thousands_separator', $currencyConfiguration['thousands_separator']);
49 $this->settingsRepository->set('decimal_separator', $currencyConfiguration['decimal_separator']);
50 $this->settingsRepository->set('number_decimals', $currencyConfiguration['number_decimals']);
51 $this->settingsRepository->save();
52
53 return [
54 'data' => [
55 'setting' => 'currency',
56 'value' => $currencyCode,
57 ],
58 ];
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public function registerRoute()
65 {
66 register_rest_route(
67 'give-api/v2',
68 $this->endpoint,
69 [
70 [
71 'methods' => 'POST',
72 'callback' => [$this, 'handleRequest'],
73 'permission_callback' => function () {
74 return current_user_can('manage_options');
75 },
76 'args' => [
77 'value' => [
78 'type' => 'string',
79 'required' => true,
80 'validate_callback' => [$this, 'validateSetting'],
81 ],
82 ],
83 ],
84 ]
85 );
86 }
87
88 /**
89 * Limits the symbol to a 3-letter currency code
90 *
91 * @since 2.21.3
92 */
93 public function validateSetting($value): bool
94 {
95 return array_key_exists(json_decode($value, false), give_get_currencies_list());
96 }
97 }
98