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

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