PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.17
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.17
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.17, at app/Modules/Settings/Settings.php

360 lines 12.3 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 = ArrayHelper::get($data, 'secretKey');
221
222 // If token is not empty meaning user verified their captcha.
223 if ($token) {
224 // Validate the turnstile response.
225 $status = Turnstile::validate($token, $secretKey);
226
227 // turnstile is valid. So proceed to store.
228 if ($status) {
229 // Prepare captcha data.
230 $captchaData = [
231 'siteKey' => sanitize_text_field(ArrayHelper::get($data, 'siteKey')),
232 'secretKey' => sanitize_text_field($secretKey),
233 'token' => $token,
234 ];
235
236 // Update the turnstile details with siteKey & secretKey.
237 update_option('_fluentform_turnstile_details', $captchaData, 'no');
238
239 // Update the turnstile validation status.
240 update_option('_fluentform_turnstile_keys_status', $status, 'no');
241
242 // Send success response letting the user know that
243 // that the turnstile is valid and saved properly.
244 wp_send_json_success([
245 'message' => __('Your Turnstile is valid and saved.', 'fluentform'),
246 'status' => $status,
247 ], 200);
248 } else {
249 // turnstile is not valid.
250 $message = __('Sorry, Your Turnstile is not valid. Please try again', 'fluentform');
251 }
252 } else {
253 // The token is empty, so the user didn't verify their captcha.
254 $message = __('Please validate your Turnstile first and then hit save.', 'fluentform');
255
256 // Get the already stored reCaptcha status.
257 $status = get_option('_fluentform_turnstile_keys_status');
258
259 if ($status) {
260 $message = __('Your Turnstile details are already valid. So no need to save again.', 'fluentform');
261 }
262 }
263
264 wp_send_json_error([
265 'message' => $message,
266 'status' => $status,
267 ], 400);
268 }
269
270 public function storeSaveGlobalLayoutSettings()
271 {
272 $settings = $this->request->get('value');
273 $settings = json_decode($settings, true);
274 $sanitizedSettings = fluentFormSanitizer($settings);
275
276 if (ArrayHelper::get($settings, 'misc.email_footer_text')) {
277 $sanitizedSettings['misc']['email_footer_text'] = wp_unslash($settings['misc']['email_footer_text']);
278 }
279
280 update_option('_fluentform_global_form_settings', $sanitizedSettings, 'no');
281
282 wp_send_json_success([
283 'message' => __('Global layout settings has been saved'),
284 ], 200);
285 }
286
287 public function storeMailChimpSettings()
288 {
289 $mailChimp = $this->request->get('mailchimp');
290
291 if (!$mailChimp['apiKey']) {
292 $mailChimpSettings = [
293 'apiKey' => '',
294 'status' => false,
295 ];
296 // Update the reCaptcha details with siteKey & secretKey.
297
298 update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no');
299
300 wp_send_json_success([
301 'message' => __('Your settings has been updated', 'fluentform'),
302 'status' => false,
303 ], 200);
304 }
305
306 // Verify API key now
307 try {
308 $MailChimp = new MailChimp($mailChimp['apiKey']);
309 $result = $MailChimp->get('lists');
310 if (!$MailChimp->success()) {
311 throw new \Exception($MailChimp->getLastError());
312 }
313 } catch (\Exception $exception) {
314 wp_send_json_error([
315 'message' => $exception->getMessage(),
316 ], 400);
317 }
318
319 // Mailchimp key is verified now, Proceed now
320
321 $mailChimpSettings = [
322 'apiKey' => sanitize_text_field($mailChimp['apiKey']),
323 'status' => true,
324 ];
325
326 // Update the reCaptcha details with siteKey & secretKey.
327 update_option('_fluentform_mailchimp_details', $mailChimpSettings, 'no');
328
329 wp_send_json_success([
330 'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'),
331 'status' => true,
332 ], 200);
333 }
334
335 public function storeEmailSummarySettings()
336 {
337 $defaults = [
338 'status' => 'yes',
339 'send_to_type' => 'admin_email',
340 'custom_recipients' => '',
341 'sending_day' => 'Mon',
342 ];
343 $settings = $this->request->get('value');
344 $settings = json_decode($settings, true);
345
346 $settings = wp_parse_args($settings, $defaults);
347
348 update_option('_fluentform_email_report_summary', $settings);
349
350 $emailReportHookName = 'fluentform_do_email_report_scheduled_tasks';
351 if (!wp_next_scheduled($emailReportHookName)) {
352 wp_schedule_event(time(), 'daily', $emailReportHookName);
353 }
354
355 wp_send_json_success([
356 'message' => __('Email Summary Settings has been updated'),
357 ], 200);
358 }
359 }
360