PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Controllers / Settings / UpdateSettingController.php

UpdateSettingController.php in The Innovative Form Builder – IvyForms 1.0, at backend/src/Controllers/Settings/UpdateSettingController.php

75 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 WP_REST_Request;
17 use WP_REST_Response;
18
19 /**
20 * Class UpdateSettingController
21 *
22 * @package IvyForms\Controllers\Settings
23 */
24 class UpdateSettingController extends Controller
25 {
26 private SettingsService $settingsService;
27
28 public function __construct(
29 SettingsService $settingsService
30 ) {
31 $this->settingsService = $settingsService;
32 }
33
34 /**
35 * @param WP_REST_Request $data
36 *
37 * @return WP_REST_Response
38 *
39 * @throws InvalidArgumentException
40 * @throws ForbiddenException
41 */
42 public function handle(WP_REST_Request $data): WP_REST_Response
43 {
44 // Verify the nonce
45 Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));
46
47 // Check if settings parameters are provided
48 if (empty($data->get_params())) {
49 throw new InvalidArgumentException(
50 BackendStrings::getCommonStrings()['invalid_request_data']
51 );
52 }
53
54 $settingsCategory = Sanitizer::sanitizeText($data->get_params()['settingsCategory']);
55 $settingsOption = Sanitizer::sanitizeText($data->get_params()['settingsOption']);
56 $settingsValue = Sanitizer::sanitizeSettingsFields($data->get_params()['settingsValue']);
57
58 if (empty($settingsCategory) || empty($settingsOption)) {
59 throw new InvalidArgumentException(
60 BackendStrings::getSettingsStrings()['settings_category_or_option_missing']
61 );
62 }
63
64 // Update the setting
65 $this->settingsService->setSetting($settingsCategory, $settingsOption, $settingsValue);
66
67 return new WP_REST_Response([
68 'message' => BackendStrings::getSettingsStrings()['settings_option_updated'],
69 'data' => [
70 'value' => $this->settingsService->getSetting($settingsCategory, $settingsOption),
71 ]
72 ], 200);
73 }
74 }
75