PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Onboarding / Routes / SettingsRoute.php

SettingsRoute.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Onboarding/Routes/SettingsRoute.php

143 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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