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

453 lines 16.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' => 'sanitize_text_field',
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 'form_settings' => Form::getFormsDefaultSettings($formId),
256 'pretty_url' => $this->getPrettyUrlSettings($formId),
257 'has_pro' => defined('FLUENTFORMPRO'),
258 'has_pro_share_page' => $this->hasProSharePage(),
259 ];
260 }
261
262 public function storeConversationalDesign($attributes, $formId)
263 {
264 $metaKey = "ffc_form_settings";
265 $formId = intval($formId);
266
267 $attributes = fluentFormSanitizer($attributes);
268 $settings = Arr::get($attributes, 'design_settings');
269 FormMeta::persist($formId, $metaKey . '_design', $settings);
270
271 $generatedCss = wp_strip_all_tags(Arr::get($attributes, 'generated_css'));
272 if ($generatedCss) {
273 FormMeta::persist($formId, $metaKey . '_generated_css', $generatedCss);
274 }
275
276 $meta = Arr::get($attributes, 'meta_settings', []);
277 $metaSanitizationMap = [
278 'title' => 'sanitize_text_field',
279 'description' => [$this, 'secureMetaDescription'],
280 'featured_image' => 'esc_url_raw',
281 'share_key' => 'sanitize_text_field',
282 'google_font_href' => 'esc_url_raw',
283 'font_css' => 'wp_kses_post',
284 ];
285 foreach ($metaSanitizationMap as $key => $sanitizer) {
286 if (isset($meta[$key])) {
287 $meta[$key] = call_user_func($sanitizer, $meta[$key]);
288 }
289 }
290
291 if ($meta) {
292 FormMeta::persist($formId, $metaKey . '_meta', $meta);
293 }
294
295 $prettyUrl = $this->savePrettyUrlSettings(Arr::get($attributes, 'pretty_url'), $formId);
296
297 $formSettings = Arr::get($attributes, 'form_settings');
298 if (is_array($formSettings) && isset($formSettings['restrictions'])) {
299 $this->saveFormRestrictions($formId, Arr::get($formSettings, 'restrictions', []));
300 }
301
302 $params = [
303 'fluent-form' => $formId,
304 ];
305 if (isset($meta['share_key']) && !empty($meta['share_key'])) {
306 $params['form'] = $meta['share_key'];
307 }
308
309 $shareUrl = add_query_arg($params, Helper::getFrontendFacingUrl());
310 return [
311 'message' => __('Settings successfully updated','fluentform'),
312 'share_url' => $shareUrl,
313 'pretty_url' => $prettyUrl,
314 ];
315 }
316
317 public function saveFormRestrictions($formId, $restrictions)
318 {
319 $formId = intval($formId);
320
321 if (!is_array($restrictions)) {
322 return Form::getFormsDefaultSettings($formId);
323 }
324
325 $existingFormSettings = Form::getFormsDefaultSettings($formId);
326 $existingFormSettings['restrictions'] = $restrictions;
327 $existingFormSettings = $this->sanitizeData($existingFormSettings);
328
329 FormMeta::persist($formId, 'formSettings', $existingFormSettings);
330
331 return $existingFormSettings;
332 }
333
334 public function getPreset($formId)
335 {
336 $formId = intval($formId);
337 $selectedPreset = Helper::getFormMeta($formId, '_ff_selected_style', 'ffs_default');
338 $selectedPreset = $selectedPreset ?: 'ffs_default';
339
340 // Use Pro FormStyler presets if available
341 if (class_exists('\FluentFormPro\classes\FormStyler')) {
342 $formStyler = new \FluentFormPro\classes\FormStyler();
343 $presets = $formStyler->getPresets();
344 } else {
345 $presets = [
346 'ffs_default' => [
347 'label' => __('Default', 'fluentform'),
348 'style' => '[]',
349 ],
350 'ffs_inherit_theme' => [
351 'label' => __('Inherit Theme Style', 'fluentform'),
352 'style' => '{}',
353 ],
354 ];
355 }
356
357 return [
358 'selected_preset'=> $selectedPreset,
359 'presets' => $presets,
360 ];
361 }
362
363 /**
364 * @throws \Exception
365 */
366 public function savePreset($attributes)
367 {
368 $formId = intval(Arr::get($attributes, 'form_id'));
369 $selectedPreset = Arr::get($attributes, 'selected_preset');
370 if ($selectedPreset && Helper::setFormMeta($formId, '_ff_selected_style', $selectedPreset)) {
371 return [
372 'message' => __('Settings save successfully', 'fluentform'),
373 ];
374 }
375 throw new \Exception(esc_html__('Settings save failed', 'fluentform'));
376 }
377
378 public function secureMetaDescription($description) {
379 $clean = preg_replace(
380 [
381 '/url\s*=/', // Remove URL assignments
382 '/http-equiv\s*=/', // Remove HTTP equiv
383 '/refresh/', // Remove refresh attempts
384 ],
385 '',
386 $description
387 );
388
389 $clean = wp_strip_all_tags($clean);
390 $clean = sanitize_text_field($clean);
391
392 return trim($clean);
393 }
394
395 private function hasProSharePage()
396 {
397 return defined('FLUENTFORMPRO') && class_exists('\FluentFormPro\classes\SharePage\SharePage');
398 }
399
400 private function hasProPrettyUrl()
401 {
402 return defined('FLUENTFORMPRO') && class_exists('\FluentFormPro\classes\SharePage\FormPrettyUrlService');
403 }
404
405 private function getPrettyUrlSettings($formId)
406 {
407 if (!$this->hasProPrettyUrl()) {
408 return [
409 'available' => false,
410 'slug' => '',
411 'enabled' => false,
412 'pretty_url' => '',
413 'base_slug' => 'form',
414 ];
415 }
416
417 $service = '\FluentFormPro\classes\SharePage\FormPrettyUrlService';
418 $slug = $service::getSlug($formId);
419 if (!$slug) {
420 $form = Form::find($formId);
421 $slug = $form ? $service::generateSlug($form->title, $formId) : '';
422 }
423
424 return [
425 'available' => true,
426 'slug' => $slug,
427 'enabled' => $service::isEnabled($formId),
428 'pretty_url' => $service::getFormPrettyUrl($formId),
429 'base_slug' => $service::getBaseSlug(),
430 ];
431 }
432
433 private function savePrettyUrlSettings($prettyUrl, $formId)
434 {
435 if (!is_array($prettyUrl) || !$this->hasProPrettyUrl()) {
436 return $this->getPrettyUrlSettings($formId);
437 }
438
439 $service = '\FluentFormPro\classes\SharePage\FormPrettyUrlService';
440 $slug = sanitize_text_field(Arr::get($prettyUrl, 'slug', ''));
441 $enabled = Arr::isTrue($prettyUrl, 'enabled');
442 $savedSlug = $service::saveSlug($formId, $slug, $enabled);
443
444 return [
445 'available' => true,
446 'slug' => $savedSlug,
447 'enabled' => $service::isEnabled($formId),
448 'pretty_url' => $service::getFormPrettyUrl($formId),
449 'base_slug' => $service::getBaseSlug(),
450 ];
451 }
452 }
453