← All changes
|
backend/src/Controllers/Settings/GetSettingController.php
+72
-0
0.6.1
→
1.5
View file →
| @@ -1,0 +1,72 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * @copyright © Melograno Venture Studio. All rights reserved. | |
| 5 | + * @licence See COPYING.md for license details. | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace IvyForms\Controllers\Settings; | |
| 9 | + | |
| 10 | +use IvyForms\Common\Exceptions\ForbiddenException; | |
| 11 | +use IvyForms\Common\Exceptions\InvalidArgumentException; | |
| 12 | +use IvyForms\Common\Sanitizer\Sanitizer; | |
| 13 | +use IvyForms\Controllers\Controller; | |
| 14 | +use IvyForms\Services\Settings\SettingsService; | |
| 15 | +use IvyForms\Services\Translations\BackendStrings; | |
| 16 | +use IvyForms\Services\Usage\UsageTrackingService; | |
| 17 | +use WP_REST_Request; | |
| 18 | +use WP_REST_Response; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Class GetSettingController | |
| 22 | + * | |
| 23 | + * @package IvyForms\Controllers\Settings | |
| 24 | + */ | |
| 25 | +class GetSettingController extends Controller | |
| 26 | +{ | |
| 27 | + private SettingsService $settingsService; | |
| 28 | + private UsageTrackingService $usageTrackingService; | |
| 29 | + | |
| 30 | + public function __construct( | |
| 31 | + SettingsService $settingsService, | |
| 32 | + UsageTrackingService $usageTrackingService | |
| 33 | + ) { | |
| 34 | + $this->settingsService = $settingsService; | |
| 35 | + $this->usageTrackingService = $usageTrackingService; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @param WP_REST_Request $data | |
| 40 | + * | |
| 41 | + * @return WP_REST_Response | |
| 42 | + * | |
| 43 | + * @throws InvalidArgumentException | |
| 44 | + * @throws ForbiddenException | |
| 45 | + */ | |
| 46 | + public function handle(WP_REST_Request $data): WP_REST_Response | |
| 47 | + { | |
| 48 | + // Verify the nonce | |
| 49 | + Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); | |
| 50 | + | |
| 51 | + // Get URL parameters (category and option) | |
| 52 | + $settingsCategory = Sanitizer::sanitizeText($data->get_url_params()['category']); | |
| 53 | + $settingsOption = Sanitizer::sanitizeText($data->get_url_params()['option']); | |
| 54 | + | |
| 55 | + if (empty($settingsCategory) || empty($settingsOption)) { | |
| 56 | + throw new InvalidArgumentException( | |
| 57 | + BackendStrings::getSettingsStrings()['settings_category_or_option_missing'] | |
| 58 | + ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $settingValue = $settingsOption === 'usageTrackingEnabled' ? | |
| 62 | + $this->usageTrackingService->isSettingEnabled() : | |
| 63 | + $this->settingsService->getSetting($settingsCategory, $settingsOption); | |
| 64 | + | |
| 65 | + return new WP_REST_Response([ | |
| 66 | + 'message' => BackendStrings::getSettingsStrings()['setting_retrieved'], | |
| 67 | + 'data' => [ | |
| 68 | + 'value' => $settingValue, | |
| 69 | + ] | |
| 70 | + ], 200); | |
| 71 | + } | |
| 72 | +} | |