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
SettingsRoute.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding\Routes; |
| 4 | |
| 5 | use Give\API\RestRoute; |
| 6 | use Give\Onboarding\Helpers\Currency; |
| 7 | use Give\Onboarding\SettingsRepositoryFactory; |
| 8 | use WP_REST_Request; |
| 9 | |
| 10 | /** |
| 11 | * @since 2.8.0 |
| 12 | */ |
| 13 | class SettingsRoute implements RestRoute |
| 14 | { |
| 15 | |
| 16 | /** @var string */ |
| 17 | protected $endpoint = 'onboarding/settings/(?P<setting>\w+)'; |
| 18 | |
| 19 | /** @var SettingsRepository */ |
| 20 | protected $settingsRepository; |
| 21 | |
| 22 | /** |
| 23 | * @since 2.8.0 |
| 24 | * |
| 25 | * @param SettingsRepository $settingsRepository |
| 26 | * |
| 27 | */ |
| 28 | public function __construct(SettingsRepositoryFactory $settingsRepositoryFactory) |
| 29 | { |
| 30 | $this->settingsRepository = $settingsRepositoryFactory->make('give_settings'); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 2.8.0 |
| 35 | * |
| 36 | * @param WP_REST_Request $request |
| 37 | * |
| 38 | * @return array |
| 39 | * |
| 40 | */ |
| 41 | public function handleRequest(WP_REST_Request $request) |
| 42 | { |
| 43 | $setting = $request->get_param('setting'); |
| 44 | $value = json_decode($request->get_param('value')); |
| 45 | |
| 46 | $this->settingsRepository->set($setting, $value); |
| 47 | $this->settingsRepository->save(); |
| 48 | |
| 49 | return [ |
| 50 | 'data' => [ |
| 51 | 'setting' => $setting, |
| 52 | 'value' => $value, |
| 53 | ], |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @since 2.8.0 |
| 59 | * |
| 60 | * @param string $setting |
| 61 | * |
| 62 | * @return bool |
| 63 | * |
| 64 | */ |
| 65 | public function validateSetting($setting) |
| 66 | { |
| 67 | return in_array( |
| 68 | $setting, |
| 69 | [ |
| 70 | 'user_type', |
| 71 | 'cause_type', |
| 72 | 'usage_tracking', |
| 73 | 'base_country', |
| 74 | 'base_state', |
| 75 | 'addons', |
| 76 | 'features', |
| 77 | ] |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @inheritDoc |
| 83 | */ |
| 84 | public function registerRoute() |
| 85 | { |
| 86 | register_rest_route( |
| 87 | 'give-api/v2', |
| 88 | $this->endpoint, |
| 89 | [ |
| 90 | [ |
| 91 | 'methods' => 'POST', |
| 92 | 'callback' => [$this, 'handleRequest'], |
| 93 | 'permission_callback' => function () { |
| 94 | return current_user_can('manage_options'); |
| 95 | }, |
| 96 | 'args' => [ |
| 97 | 'setting' => [ |
| 98 | 'type' => 'string', |
| 99 | 'required' => true, |
| 100 | 'validate_callback' => [$this, 'validateSetting'], |
| 101 | 'sanitize_callback' => 'sanitize_text_field', |
| 102 | ], |
| 103 | 'value' => [ |
| 104 | 'type' => 'string', |
| 105 | 'required' => false, |
| 106 | // 'validate_callback' => [ $this, 'validateValue' ], |
| 107 | 'sanitize_callback' => 'sanitize_text_field', |
| 108 | ], |
| 109 | ], |
| 110 | ], |
| 111 | 'schema' => [$this, 'getSchema'], |
| 112 | ] |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @since 2.8.0 |
| 118 | * @return array |
| 119 | * |
| 120 | */ |
| 121 | public function getSchema() |
| 122 | { |
| 123 | return [ |
| 124 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 125 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 126 | // The title property marks the identity of the resource. |
| 127 | 'title' => 'onboarding', |
| 128 | 'type' => 'object', |
| 129 | // In JSON Schema you can specify object properties in the properties attribute. |
| 130 | 'properties' => [ |
| 131 | 'setting' => [ |
| 132 | 'description' => esc_html__('The reference name for the setting being updated.', 'give'), |
| 133 | 'type' => 'string', |
| 134 | ], |
| 135 | 'value' => [ |
| 136 | 'description' => esc_html__('The value of the setting being updated.', 'give'), |
| 137 | 'type' => 'string', |
| 138 | ], |
| 139 | ], |
| 140 | ]; |
| 141 | } |
| 142 | } |
| 143 |