AddonsRoute.php
4 years ago
CurrencyRoute.php
4 years ago
FeaturesRoute.php
4 years ago
FormRoute.php
4 years ago
LocationRoute.php
4 years ago
SettingsRoute.php
4 years ago
CurrencyRoute.php
116 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 SettingsRepository |
| 20 | */ |
| 21 | protected $settingsRepository; |
| 22 | |
| 23 | /** |
| 24 | * @since 2.8.0 |
| 25 | * |
| 26 | * @param SettingsRepository $settingsRepository |
| 27 | * |
| 28 | */ |
| 29 | public function __construct(SettingsRepositoryFactory $settingsRepositoryFactory) |
| 30 | { |
| 31 | $this->settingsRepository = $settingsRepositoryFactory->make('give_settings'); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @since 2.8.0 |
| 36 | * |
| 37 | * @param WP_REST_Request $request |
| 38 | * |
| 39 | * @return array |
| 40 | * |
| 41 | */ |
| 42 | public function handleRequest(WP_REST_Request $request) |
| 43 | { |
| 44 | $currencyCode = json_decode($request->get_param('value')); |
| 45 | |
| 46 | $currencyList = give_get_currencies_list(); |
| 47 | $currencyConfiguration = $currencyList[$currencyCode]['setting']; |
| 48 | |
| 49 | $this->settingsRepository->set('currency', $currencyCode); |
| 50 | $this->settingsRepository->set('currency_position', $currencyConfiguration['currency_position']); |
| 51 | $this->settingsRepository->set('thousands_separator', $currencyConfiguration['thousands_separator']); |
| 52 | $this->settingsRepository->set('decimal_separator', $currencyConfiguration['decimal_separator']); |
| 53 | $this->settingsRepository->set('number_decimals', $currencyConfiguration['number_decimals']); |
| 54 | $this->settingsRepository->save(); |
| 55 | |
| 56 | return [ |
| 57 | 'data' => [ |
| 58 | 'setting' => 'currency', |
| 59 | 'value' => $currencyCode, |
| 60 | ], |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritDoc |
| 66 | */ |
| 67 | public function registerRoute() |
| 68 | { |
| 69 | register_rest_route( |
| 70 | 'give-api/v2', |
| 71 | $this->endpoint, |
| 72 | [ |
| 73 | [ |
| 74 | 'methods' => 'POST', |
| 75 | 'callback' => [$this, 'handleRequest'], |
| 76 | 'permission_callback' => function () { |
| 77 | return current_user_can('manage_options'); |
| 78 | }, |
| 79 | 'args' => [ |
| 80 | 'value' => [ |
| 81 | 'type' => 'string', |
| 82 | 'required' => true, |
| 83 | // 'validate_callback' => [ $this, 'validateSetting' ], |
| 84 | 'sanitize_callback' => 'sanitize_text_field', |
| 85 | ], |
| 86 | ], |
| 87 | ], |
| 88 | 'schema' => [$this, 'getSchema'], |
| 89 | ] |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 2.8.0 |
| 95 | * @return array |
| 96 | * |
| 97 | */ |
| 98 | public function getSchema() |
| 99 | { |
| 100 | return [ |
| 101 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 102 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 103 | // The title property marks the identity of the resource. |
| 104 | 'title' => 'onboarding', |
| 105 | 'type' => 'object', |
| 106 | // In JSON Schema you can specify object properties in the properties attribute. |
| 107 | 'properties' => [ |
| 108 | 'currencyCode' => [ |
| 109 | 'description' => esc_html__('Two letter code representing a country.', 'give'), |
| 110 | 'type' => 'string', |
| 111 | ], |
| 112 | ], |
| 113 | ]; |
| 114 | } |
| 115 | } |
| 116 |