PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
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 / Services / Settings / SettingsService.php

SettingsService.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.2, at app/Services/Settings/SettingsService.php

367 lines 13.2 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\Services\Settings;
4
5 use FluentForm\App\Models\Form;
6 use FluentForm\App\Helpers\Helper;
7 use FluentForm\App\Models\FormMeta;
8 use FluentForm\Framework\Support\Arr;
9 use FluentForm\App\Services\FluentConversational\Classes\Form as FluentConversational;
10
11 class SettingsService
12 {
13 public function get($attributes = [])
14 {
15 $metaKey = sanitize_text_field(Arr::get($attributes, 'meta_key'));
16
17 $formId = (int) Arr::get($attributes, 'form_id');
18
19 $result = FormMeta::where(['meta_key' => $metaKey, 'form_id' => $formId])->get();
20
21 foreach ($result as $item) {
22 $value = Helper::isJson($item->value) ? json_decode($item->value, true) : $item->value;
23
24 if ('notifications' == $metaKey) {
25 if (!$value) {
26 $value = ['name' => ''];
27 }
28 }
29
30 if (isset($value['layout']) && !isset($value['layout']['asteriskPlacement'])) {
31 $value['layout']['asteriskPlacement'] = 'asterisk-right';
32 }
33
34 $item->value = $value;
35 }
36
37 $result = apply_filters_deprecated(
38 'fluentform_get_meta_key_settings_response',
39 [
40 $result,
41 $formId,
42 $metaKey
43 ],
44 FLUENTFORM_FRAMEWORK_UPGRADE,
45 'fluentform/get_meta_key_settings_response',
46 'Use fluentform/get_meta_key_settings_response instead of fluentform_get_meta_key_settings_response'
47 );
48
49 return apply_filters('fluentform/get_meta_key_settings_response', $result, $formId, $metaKey);
50 }
51
52 public function general($formId)
53 {
54 $settings = [
55 'generalSettings' => Form::getFormsDefaultSettings($formId),
56 'advancedValidationSettings' => Form::getAdvancedValidationSettings($formId),
57 ];
58
59 $settings = apply_filters_deprecated(
60 'fluentform_form_settings_ajax',
61 [
62 $settings,
63 $formId
64 ],
65 FLUENTFORM_FRAMEWORK_UPGRADE,
66 'fluentform/form_settings_ajax',
67 'Use fluentform/form_settings_ajax instead of fluentform/form_settings_ajax'
68 );
69
70 $settings = apply_filters('fluentform/form_settings_ajax', $settings, $formId);
71
72 return $settings;
73 }
74
75 public function saveGeneral($attributes = [])
76 {
77 $formId = (int) Arr::get($attributes, 'form_id');
78
79 $formSettings = json_decode(Arr::get($attributes, 'formSettings'), true);
80
81 $formSettings = $this->sanitizeData($formSettings);
82
83 $advancedValidationSettings = json_decode(Arr::get($attributes, 'advancedValidationSettings'), true);
84
85 $advancedValidationSettings = $this->sanitizeData($advancedValidationSettings);
86
87 Validator::validate(
88 'confirmations',
89 Arr::get($formSettings, 'confirmation', [])
90 );
91
92 FormMeta::persist($formId, 'formSettings', $formSettings);
93
94 FormMeta::persist($formId, 'advancedValidationSettings', $advancedValidationSettings);
95
96 $deleteAfterXDaysStatus = Arr::get($formSettings, 'delete_after_x_days');
97 $deleteDaysCount = Arr::get($formSettings, 'auto_delete_days');
98 $deleteOnSubmission = Arr::get($formSettings, 'delete_entry_on_submission');
99
100 if ('yes' != $deleteOnSubmission && $deleteDaysCount && 'yes' == $deleteAfterXDaysStatus) {
101 // We have to set meta values
102 FormMeta::persist($formId, 'auto_delete_days', $deleteDaysCount);
103 } else {
104 // we have to delete meta values
105 FormMeta::remove($formId, 'auto_delete_days');
106 }
107
108 $convFormPerStepSave = Arr::get($formSettings, 'conv_form_per_step_save') && Helper::isConversionForm($formId);
109
110 if ($convFormPerStepSave) {
111 FormMeta::persist($formId, 'conv_form_per_step_save', true);
112 } else {
113 FormMeta::remove($formId, 'conv_form_per_step_save');
114 }
115
116 $convFormResumeFromLastStep = $convFormPerStepSave && Arr::get($formSettings, 'conv_form_resume_from_last_step');
117 if ($convFormResumeFromLastStep) {
118 FormMeta::persist($formId, 'conv_form_resume_from_last_step', true);
119 } else {
120 FormMeta::remove($formId, 'conv_form_resume_from_last_step');
121 }
122
123
124
125 do_action('fluentform/after_save_form_settings', $formId, $attributes);
126 }
127
128 private function sanitizeData($settings)
129 {
130 if (fluentformCanUnfilteredHTML()) {
131 return $settings;
132 }
133
134 $sanitizerMap = [
135 'redirectTo' => 'sanitize_text_field',
136 'redirectMessage' => 'fluentform_sanitize_html',
137 'messageToShow' => 'fluentform_sanitize_html',
138 'customPage' => 'sanitize_text_field',
139 'samePageFormBehavior' => 'sanitize_text_field',
140 'customUrl' => 'sanitize_url',
141 'enabled' => 'rest_sanitize_boolean',
142 'numberOfEntries' => 'intval',
143 'period' => 'intval',
144 'limitReachedMsg' => 'sanitize_text_field',
145 'start' => 'sanitize_text_field',
146 'end' => 'sanitize_text_field',
147 'pendingMsg' => 'sanitize_text_field',
148 'expiredMsg' => 'sanitize_text_field',
149 'requireLoginMsg' => 'sanitize_text_field',
150 'labelPlacement' => 'sanitize_text_field',
151 'helpMessagePlacement' => 'sanitize_text_field',
152 'errorMessagePlacement' => 'sanitize_text_field',
153 'asteriskPlacement' => 'sanitize_text_field',
154 'delete_entry_on_submission' => 'sanitize_text_field',
155 'id' => 'intval',
156 'showLabel' => 'rest_sanitize_boolean',
157 'showCount' => 'rest_sanitize_boolean',
158 'status' => 'rest_sanitize_boolean',
159 'type' => 'sanitize_text_field',
160 'field' => 'sanitize_text_field',
161 'operator' => 'sanitize_text_field',
162 'value' => 'sanitize_text_field',
163 'error_message' => 'sanitize_text_field',
164 'validation_type' => 'sanitize_text_field',
165 'name' => 'sanitize_text_field',
166 'email' => 'sanitize_text_field',
167 'fromName' => 'sanitize_text_field',
168 'fromEmail' => 'sanitize_text_field',
169 'replyTo' => 'sanitize_text_field',
170 'bcc' => 'sanitize_text_field',
171 'subject' => 'sanitize_text_field',
172 'message' => 'fluentform_sanitize_html',
173 'url' => 'sanitize_url',
174 'webhook' => 'sanitize_url',
175 'textTitle' => 'sanitize_text_field',
176 'conv_form_per_step_save' => 'rest_sanitize_boolean'
177 ];
178
179 return fluentform_backend_sanitizer($settings, $sanitizerMap);
180 }
181
182 public function store($attributes = [])
183 {
184 $formId = (int) Arr::get($attributes, 'form_id');
185
186 $value = Arr::get($attributes, 'value', '');
187
188 $valueArray = $value ? json_decode($value, true) : [];
189
190 $key = sanitize_text_field(Arr::get($attributes, 'meta_key'));
191
192 if ('formSettings' == $key) {
193 Validator::validate(
194 'confirmations',
195 Arr::get(
196 $valueArray,
197 'confirmation',
198 []
199 )
200 );
201 } else {
202 Validator::validate($key, $valueArray);
203 }
204
205 $valueArray = $this->sanitizeData($valueArray);
206
207 $value = json_encode($valueArray);
208
209 $data = [
210 'meta_key' => $key,
211 'value' => $value,
212 'form_id' => $formId,
213 ];
214
215 // If the request has an valid id field it's safe to assume
216 // that the user wants to update an existing settings.
217 // So, we'll proceed to do so by finding it first.
218 $id = (int) Arr::get($attributes, 'meta_id');
219
220 $settingsQuery = FormMeta::where('form_id', $formId);
221
222 $settings = null;
223 if ($id) {
224 $settings = $settingsQuery->find($id);
225 }
226
227 if (!empty($settings)) {
228 $settingsQuery->where('id', $settings->id)->update($data);
229 $insertId = $settings->id;
230 } else {
231 $insertId = $settingsQuery->insertGetId($data);
232 }
233
234 return [
235 $insertId,
236 $valueArray,
237 ];
238 }
239
240 public function remove($attributes = [])
241 {
242 $formId = intval(Arr::get($attributes, 'form_id'));
243 $id = intval(Arr::get($attributes, 'meta_id'));
244
245 FormMeta::where('form_id', $formId)->where('id', $id)->delete();
246 }
247
248 public function conversationalDesign($formId)
249 {
250 $conversationalForm = new FluentConversational();
251
252 return [
253 'design_settings' => $conversationalForm->getDesignSettings($formId),
254 'meta_settings' => $conversationalForm->getMetaSettings($formId),
255 'has_pro' => defined('FLUENTFORMPRO'),
256 ];
257 }
258
259 public function storeConversationalDesign($attributes, $formId)
260 {
261 $metaKey = "ffc_form_settings";
262 $formId = intval($formId);
263
264 $attributes = fluentFormSanitizer($attributes);
265 $settings = Arr::get($attributes, 'design_settings');
266 FormMeta::persist($formId, $metaKey . '_design', $settings);
267
268 $generatedCss = wp_strip_all_tags(Arr::get($attributes, 'generated_css'));
269 if ($generatedCss) {
270 FormMeta::persist($formId, $metaKey . '_generated_css', $generatedCss);
271 }
272
273 $meta = Arr::get($attributes, 'meta_settings', []);
274 $metaSanitizationMap = [
275 'title' => 'sanitize_text_field',
276 'description' => [$this, 'secureMetaDescription'],
277 'featured_image' => 'esc_url_raw',
278 'share_key' => 'sanitize_text_field',
279 'google_font_href' => 'esc_url_raw',
280 'font_css' => 'wp_kses_post',
281 ];
282 foreach ($metaSanitizationMap as $key => $sanitizer) {
283 if (isset($meta[$key])) {
284 $meta[$key] = call_user_func($sanitizer, $meta[$key]);
285 }
286 }
287
288 if ($meta) {
289 FormMeta::persist($formId, $metaKey . '_meta', $meta);
290 }
291
292 $params = [
293 'fluent-form' => $formId,
294 ];
295 if (isset($meta['share_key']) && !empty($meta['share_key'])) {
296 $params['form'] = $meta['share_key'];
297 }
298
299 $shareUrl = add_query_arg($params, Helper::getFrontendFacingUrl());
300 return [
301 'message' => __('Settings successfully updated','fluentform'),
302 'share_url' => $shareUrl,
303 ];
304 }
305
306 public function getPreset($formId)
307 {
308 $formId = intval($formId);
309 $selectedPreset = Helper::getFormMeta($formId, '_ff_selected_style', 'ffs_default');
310 $selectedPreset = $selectedPreset ?: 'ffs_default';
311
312 // Use Pro FormStyler presets if available
313 if (class_exists('\FluentFormPro\classes\FormStyler')) {
314 $formStyler = new \FluentFormPro\classes\FormStyler();
315 $presets = $formStyler->getPresets();
316 } else {
317 $presets = [
318 'ffs_default' => [
319 'label' => __('Default', 'fluentform'),
320 'style' => '[]',
321 ],
322 'ffs_inherit_theme' => [
323 'label' => __('Inherit Theme Style', 'fluentform'),
324 'style' => '{}',
325 ],
326 ];
327 }
328
329 return [
330 'selected_preset'=> $selectedPreset,
331 'presets' => $presets,
332 ];
333 }
334
335 /**
336 * @throws \Exception
337 */
338 public function savePreset($attributes)
339 {
340 $formId = intval(Arr::get($attributes, 'form_id'));
341 $selectedPreset = Arr::get($attributes, 'selected_preset');
342 if ($selectedPreset && Helper::setFormMeta($formId, '_ff_selected_style', $selectedPreset)) {
343 return [
344 'message' => __('Settings save successfully', 'fluentform'),
345 ];
346 }
347 throw new \Exception(esc_html__('Settings save failed', 'fluentform'));
348 }
349
350 public function secureMetaDescription($description) {
351 $clean = preg_replace(
352 [
353 '/url\s*=/', // Remove URL assignments
354 '/http-equiv\s*=/', // Remove HTTP equiv
355 '/refresh/', // Remove refresh attempts
356 ],
357 '',
358 $description
359 );
360
361 $clean = wp_strip_all_tags($clean);
362 $clean = sanitize_text_field($clean);
363
364 return trim($clean);
365 }
366 }
367