PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.21
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Settings / Settings.php

Settings.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.21, at app/Modules/Settings/Settings.php

367 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Settings;
4
5 use FluentForm\Framework\Request\Request;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7 use FluentForm\App\Modules\ReCaptcha\ReCaptcha;
8 use FluentForm\App\Modules\HCaptcha\HCaptcha;
9 use FluentForm\App\Modules\Turnstile\Turnstile;
10 use FluentForm\App\Services\Integrations\MailChimp\MailChimp;
11
12 /**
13 * Global Settings
14 *
15 * @package FluentForm\App\Modules\Settings
16 */
17 class Settings
18 {
19 /**
20 * Request Object
21 *
22 * @var \FluentForm\Framework\Request\Request
23 */
24 protected $request;
25
26 /**
27 * Settings constructor.
28 *
29 * @param \FluentForm\Framework\Request\Request $request
30 */
31 public function __construct(Request $request)
32 {
33 $this->request = $request;
34 }
35
36 /**
37 * Get a global settings for an specified key.
38 */
39 public function get()
40 {
41 $values = [];
42 $key = $this->request->get('key');
43
44 if (is_array($key)) {
45 foreach ($key as $key_item) {
46 $values[$key_item] = get_option($key_item);
47 }
48 } else {
49 $values[$key] = get_option($key);
50 }
51 $values = apply_filters('fluentform_get_global_settings_values', $values, $key);
52 wp_send_json_success($values, 200);
53 }
54
55 public function store()
56 {
57 $key = $this->request->get('key');
58 $method = 'store' . ucwords($key);
59
60 $allowedMethods = [
61 'storeReCaptcha',
62 'storeHCaptcha',
63 'storeTurnstile',
64 'storeSaveGlobalLayoutSettings',
65 'storeMailChimpSettings',
66 'storeEmailSummarySettings',
67 ];
68 do_action('fluentform_saving_global_settings_with_key_method', $this->request);
69
70 if (in_array($method, $allowedMethods)) {
71 $this->{$method}();
72 }
73 }
74
75 public function storeReCaptcha()
76 {
77 $data = $this->request->get('reCaptcha');
78
79 if ('clear-settings' == $data) {
80 delete_option('_fluentform_reCaptcha_details');
81
82 update_option('_fluentform_reCaptcha_keys_status', false, 'no');
83
84 wp_send_json_success([
85 'message' => __('Your reCAPTCHA settings are deleted.', 'fluentform'),
86 'status' => false,
87 ], 200);
88 }
89
90 $token = ArrayHelper::get($data, 'token');
91 $secretKey = ArrayHelper::get($data, 'secretKey');
92
93 // If token is not empty meaning user verified their captcha.
94 if ($token) {
95 // Validate the reCaptcha response.
96 $version = ArrayHelper::get($data, 'api_version', 'v2_visible');
97
98 $status = ReCaptcha::validate($token, $secretKey, $version);
99
100 // reCaptcha is valid. So proceed to store.
101 if ($status) {
102 // Prepare captcha data.
103 $captchaData = [
104 'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')),
105 'secretKey' => sanitize_text_field($secretKey),
106 'api_version' => ArrayHelper::get($data, 'api_version'),
107 ];
108
109 // Update the reCaptcha details with siteKey & secretKey.
110 update_option('_fluentform_reCaptcha_details', $captchaData, 'no');
111
112 // Update the reCaptcha validation status.
113 update_option('_fluentform_reCaptcha_keys_status', $status, 'no');
114
115 // Send success response letting the user know that
116 // that the reCaptcha is valid and saved properly.
117 wp_send_json_success([
118 'message' => __('Your reCAPTCHA is valid and saved.', 'fluentform'),
119 'status' => $status,
120 ], 200);
121 } else { // reCaptcha is not valid.
122 $message = __('Sorry, Your reCAPTCHA is not valid. Please try again', 'fluentform');
123 }
124 } else { // The token is empty, so the user didn't verify their captcha.
125 $message = __('Please validate your reCAPTCHA first and then hit save.', 'fluentform');
126
127 // Get the already stored reCaptcha status.
128 $status = get_option('_fluentform_reCaptcha_keys_status');
129
130 if ($status) {
131 $message = __('Your reCAPTCHA details are already valid. So no need to save again.', 'fluentform');
132 }
133 }
134
135 wp_send_json_error([
136 'message' => $message,
137 'status' => $status,
138 ], 400);
139 }
140
141 public function storeHCaptcha()
142 {
143 $data = $this->request->get('hCaptcha');
144
145 if ('clear-settings' == $data) {
146 delete_option('_fluentform_hCaptcha_details');
147
148 update_option('_fluentform_hCaptcha_keys_status', false, 'no');
149
150 wp_send_json_success([
151 'message' => __('Your hCaptcha settings are deleted.', 'fluentform'),
152 'status' => false,
153 ], 200);
154 }
155
156 $token = ArrayHelper::get($data, 'token');
157 $secretKey = ArrayHelper::get($data, 'secretKey');
158
159 // If token is not empty meaning user verified their captcha.
160 if ($token) {
161 // Validate the hCaptcha response.
162 $status = HCaptcha::validate($token, $secretKey);
163
164 // hCaptcha is valid. So proceed to store.
165 if ($status) {
166 // Prepare captcha data.
167 $captchaData = [
168 'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')),
169 'secretKey' => sanitize_text_field($secretKey),
170 ];
171
172 // Update the hCaptcha details with siteKey & secretKey.
173 update_option('_fluentform_hCaptcha_details', $captchaData, 'no');
174
175 // Update the hCaptcha validation status.
176 update_option('_fluentform_hCaptcha_keys_status', $status, 'no');
177
178 // Send success response letting the user know that
179 // that the hCaptcha is valid and saved properly.
180 wp_send_json_success([
181 'message' => __('Your hCaptcha is valid and saved.', 'fluentform'),
182 'status' => $status,
183 ], 200);
184 } else { // hCaptcha is not valid.
185 $message = __('Sorry, Your hCaptcha is not valid, Please try again', 'fluentform');
186 }
187 } else { // The token is empty, so the user didn't verify their captcha.
188 $message = __('Please validate your hCaptcha first and then hit save.', 'fluentform');
189
190 // Get the already stored hCaptcha status.
191 $status = get_option('_fluentform_hCaptcha_keys_status');
192
193 if ($status) {
194 $message = __('Your hCaptcha details are already valid, So no need to save again.', 'fluentform');
195 }
196 }
197
198 wp_send_json_error([
199 'message' => $message,
200 'status' => $status,
201 ], 400);
202 }
203
204 public function storeTurnstile()
205 {
206 $data = $this->request->get('turnstile');
207
208 if ('clear-settings' == $data) {
209 delete_option('_fluentform_turnstile_details');
210
211 update_option('_fluentform_turnstile_keys_status', false, 'no');
212
213 wp_send_json_success([
214 'message' => __('Your Turnstile settings are deleted.', 'fluentform'),
215 'status' => false,
216 ], 200);
217 }
218
219 $token = ArrayHelper::get($data, 'token');
220 $secretKey = sanitize_text_field(ArrayHelper::get($data, 'secretKey'));
221
222 // Prepare captcha data.
223 $captchaData = [
224 'siteKey' => ArrayHelper::get($data, 'siteKey'),
225 'secretKey' => $secretKey,
226 'invisible' => ArrayHelper::get($data, 'invisible', 'no'),
227 'theme' => ArrayHelper::get($data, 'theme', 'auto')
228 ];
229
230 // If token is not empty meaning user verified their captcha.
231 if ($token) {
232 // Validate the turnstile response.
233 $status = Turnstile::validate($token, $secretKey);
234
235 // turnstile is valid. So proceed to store.
236 if ($status) {
237 // Update the turnstile details with siteKey & secretKey.
238 update_option('_fluentform_turnstile_details', $captchaData, 'no');
239
240 // Update the turnstile validation status.
241 update_option('_fluentform_turnstile_keys_status', $status, 'no');
242
243 // Send success response letting the user know that
244 // that the turnstile is valid and saved properly.
245 wp_send_json_success([
246 'message' => __('Your Turnstile Keys are valid.', 'fluentform'),
247 'status' => $status,
248 ], 200);
249 } else {
250 // turnstile is not valid.
251 $message = __('Sorry, Your Turnstile Keys are not valid. Please try again!', 'fluentform');
252 }
253 } else {
254 // The token is empty, so the user didn't verify their captcha.
255 $message = __('Please validate your Turnstile first and then hit save.', 'fluentform');
256
257 // Get the already stored reCaptcha status.
258 $status = get_option('_fluentform_turnstile_keys_status');
259
260 if ($status) {
261 update_option('_fluentform_turnstile_details', $captchaData, 'no');
262 $message = __('Your Turnstile settings is saved.', 'fluentform');
263
264 wp_send_json_success([
265 'message' => $message,
266 'status' => $status,
267 ], 200);
268 }
269 }
270
271 wp_send_json_error([
272 'message' => $message,
273 'status' => $status,
274 ], 400);
275 }
276
277 public function storeSaveGlobalLayoutSettings()
278 {
279 $settings = $this->request->get('value');
280 $settings = json_decode($settings, true);
281 $sanitizedSettings = fluentFormSanitizer($settings);
282
283 if (ArrayHelper::get($settings, 'misc.email_footer_text')) {
284 $sanitizedSettings['misc']['email_footer_text'] = wp_unslash($settings['misc']['email_footer_text']);
285 }
286
287 update_option('_fluentform_global_form_settings', $sanitizedSettings, 'no');
288
289 wp_send_json_success([
290 'message' => __('Global layout settings has been saved'),
291 ], 200);
292 }
293
294 public function storeMailChimpSettings()
295 {
296 $mailChimp = $this->request->get('mailchimp');
297
298 if (!$mailChimp['apiKey']) {
299 $mailChimpSettings = [
300 'apiKey' => '',
301 'status' => false,
302 ];
303 // Update the reCaptcha details with siteKey & secretKey.
304
305 update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no');
306
307 wp_send_json_success([
308 'message' => __('Your settings has been updated', 'fluentform'),
309 'status' => false,
310 ], 200);
311 }
312
313 // Verify API key now
314 try {
315 $MailChimp = new MailChimp($mailChimp['apiKey']);
316 $result = $MailChimp->get('lists');
317 if (!$MailChimp->success()) {
318 throw new \Exception($MailChimp->getLastError());
319 }
320 } catch (\Exception $exception) {
321 wp_send_json_error([
322 'message' => $exception->getMessage(),
323 ], 400);
324 }
325
326 // Mailchimp key is verified now, Proceed now
327
328 $mailChimpSettings = [
329 'apiKey' => sanitize_text_field($mailChimp['apiKey']),
330 'status' => true,
331 ];
332
333 // Update the reCaptcha details with siteKey & secretKey.
334 update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no');
335
336 wp_send_json_success([
337 'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'),
338 'status' => true,
339 ], 200);
340 }
341
342 public function storeEmailSummarySettings()
343 {
344 $defaults = [
345 'status' => 'yes',
346 'send_to_type' => 'admin_email',
347 'custom_recipients' => '',
348 'sending_day' => 'Mon',
349 ];
350 $settings = $this->request->get('value');
351 $settings = json_decode($settings, true);
352
353 $settings = wp_parse_args($settings, $defaults);
354
355 update_option('_fluentform_email_report_summary', $settings);
356
357 $emailReportHookName = 'fluentform_do_email_report_scheduled_tasks';
358 if (!wp_next_scheduled($emailReportHookName)) {
359 wp_schedule_event(time(), 'daily', $emailReportHookName);
360 }
361
362 wp_send_json_success([
363 'message' => __('Email Summary Settings has been updated'),
364 ], 200);
365 }
366 }
367