AddonsRoute.php
4 years ago
CurrencyRoute.php
3 years ago
FeaturesRoute.php
1 year ago
FormRoute.php
4 years ago
LocationRoute.php
4 years ago
SettingsRoute.php
4 years ago
CurrencyRoute.php
98 lines
| 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 |