PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
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 / Form / Settings / FormSettings.php

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

294 lines 10.8 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\Form\Settings;
4
5 use FluentForm\App\Modules\Acl\Acl;
6 use FluentForm\App\Modules\Form\Form;
7 use FluentForm\Framework\Helpers\ArrayHelper;
8 use FluentForm\Framework\Foundation\Application;
9 use FluentForm\App\Modules\Form\Settings\Validator\Validator;
10
11 class FormSettings
12 {
13 /**
14 * Request object
15 *
16 * @var \FluentForm\Framework\Request\Request
17 */
18 private $request;
19
20 private $app;
21
22 private $formId;
23
24 /**
25 * The settings (fluentform_form_meta) query builder.
26 *
27 * @var \WpFluent\QueryBuilder\QueryBuilderHandler
28 */
29 private $settingsQuery;
30
31 /**
32 * Construct the object
33 *
34 * @throws \Exception
35 */
36 public function __construct(Application $application)
37 {
38 $this->app = $application;
39
40 $this->request = $application->request;
41 $this->formId = intval($this->request->get('form_id'));
42
43 $this->settingsQuery = wpFluent()->table('fluentform_form_meta')->where('form_id', $this->formId);
44 }
45
46 /**
47 * Get settings for a particular form by id
48 */
49 public function index()
50 {
51 $metaKey = sanitize_text_field($this->request->get('meta_key'));
52
53 // We'll always try to get a collection for a given meta key.
54 // Acknowledging that a certain meta key can have multiple
55 // results. The developer using the api knows beforehand
56 // that whether the expected result contains multiple
57 // or one value. The developer will access that way.
58 $query = $this->settingsQuery->where('meta_key', $metaKey);
59
60 $result = $query->get();
61
62 foreach ($result as $item) {
63 $item->value = json_decode($item->value, true);
64 if ('notifications' == $metaKey) {
65 if (!$item->value) {
66 $item->value = ['name' => ''];
67 }
68 }
69 if (isset($item->value['layout']) && !isset($item->value['layout']['asteriskPlacement'])) {
70 $item->value['layout']['asteriskPlacement'] = 'asterisk-right';
71 }
72 }
73
74 $result = apply_filters_deprecated(
75 'fluentform_get_meta_key_settings_response',
76 [
77 $result,
78 $this->formId,
79 $metaKey
80 ],
81 FLUENTFORM_FRAMEWORK_UPGRADE,
82 'fluentform/get_meta_key_settings_response',
83 'Use fluentform/get_meta_key_settings_response instead of fluentform_get_meta_key_settings_response.'
84 );
85
86 $result = $this->app->applyFilters('fluentform/get_meta_key_settings_response', $result, $this->formId, $metaKey);
87 wp_send_json_success(['result' => $result], 200);
88 }
89
90 public function getGeneralSettingsAjax()
91 {
92 $formId = intval($this->request->get('form_id'));
93 $form = new Form($this->app);
94 $settings = [
95 'generalSettings' => $form->getFormsDefaultSettings($formId),
96 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId),
97 ];
98
99 $settings = apply_filters_deprecated(
100 'fluentform_form_settings_ajax',
101 [
102 $settings,
103 $formId
104 ],
105 FLUENTFORM_FRAMEWORK_UPGRADE,
106 'fluentform/form_settings_ajax',
107 'Use fluentform/form_settings_ajax instead of fluentform_form_settings_ajax.'
108 );
109
110 $settings = $this->app->applyFilters('fluentform/form_settings_ajax', $settings, $formId);
111
112 wp_send_json_success($settings, 200);
113 }
114
115 public function saveGeneralSettingsAjax()
116 {
117 $formId = intval($this->request->get('form_id'));
118 $form = new Form($this->app);
119 $formSettings = \json_decode($this->request->get('formSettings'), true);
120
121 $sanitizerMap = [
122 'redirectTo' => 'sanitize_text_field',
123 'redirectMessage' => 'fluentform_sanitize_html',
124 'messageToShow' => 'fluentform_sanitize_html',
125 'customPage' => 'sanitize_text_field',
126 'samePageFormBehavior' => 'sanitize_text_field',
127 'customUrl' => 'sanitize_url',
128 'enabled' => 'rest_sanitize_boolean',
129 'numberOfEntries' => 'intval',
130 'period' => 'intval',
131 'limitReachedMsg' => 'sanitize_text_field',
132 'start' => 'sanitize_text_field',
133 'end' => 'sanitize_text_field',
134 'pendingMsg' => 'sanitize_text_field',
135 'expiredMsg' => 'sanitize_text_field',
136 'requireLoginMsg' => 'sanitize_text_field',
137 'message' => 'sanitize_text_field',
138 'labelPlacement' => 'sanitize_text_field',
139 'helpMessagePlacement' => 'sanitize_text_field',
140 'errorMessagePlacement' => 'sanitize_text_field',
141 'asteriskPlacement' => 'sanitize_text_field',
142 'delete_entry_on_submission' => 'sanitize_text_field',
143 'id' => 'intval',
144 'showLabel' => 'rest_sanitize_boolean',
145 'showCount' => 'rest_sanitize_boolean',
146 'status' => 'rest_sanitize_boolean',
147 'type' => 'sanitize_text_field',
148 'field' => 'sanitize_text_field',
149 'operator' => 'sanitize_text_field',
150 'value' => 'sanitize_text_field',
151 'error_message' => 'sanitize_text_field',
152 'validation_type' => 'sanitize_text_field',
153 'conv_form_per_step_save' => 'rest_sanitize_boolean',
154 'conv_form_resume_from_last_step' => 'rest_sanitize_boolean',
155 ];
156 $formSettings = $this->sanitizeData($formSettings, $sanitizerMap);
157
158 $advancedValidationSettings = \json_decode($this->request->get('advancedValidationSettings'), true);
159 $advancedValidationSettings = $this->sanitizeData($advancedValidationSettings, $sanitizerMap);
160
161 Validator::validate(
162 'confirmations',
163 ArrayHelper::get($formSettings, 'confirmation', [])
164 );
165
166 $form->updateMeta($formId, 'formSettings', $formSettings);
167 $form->updateMeta($formId, 'advancedValidationSettings', $advancedValidationSettings);
168
169 $deleteAfterXDaysStatus = ArrayHelper::get($formSettings, 'delete_after_x_days');
170 $deleteDaysCount = ArrayHelper::get($formSettings, 'auto_delete_days');
171 $deleteOnSubmission = ArrayHelper::get($formSettings, 'delete_entry_on_submission');
172
173 if ('yes' != $deleteOnSubmission && $deleteDaysCount && 'yes' == $deleteAfterXDaysStatus) {
174 // We have to set meta values
175 $form->updateMeta($formId, 'auto_delete_days', intval($deleteDaysCount));
176 } else {
177 // we have to delete meta values
178 $form->deleteMeta($formId, 'auto_delete_days');
179 }
180
181 do_action_deprecated(
182 'fluentform_after_save_form_settings',
183 [
184 $formId,
185 $this->request->all()
186 ],
187 FLUENTFORM_FRAMEWORK_UPGRADE,
188 'fluentform/after_save_form_settings',
189 'Use fluentform/after_save_form_settings instead of fluentform_after_save_form_settings.'
190 );
191
192 $this->app->doAction('fluentform/after_save_form_settings', $formId, $this->request->all());
193
194 wp_send_json_success([
195 'message' => __('Settings has been saved.', 'fluentform'),
196 ], 200);
197 }
198
199 /**
200 * Save settings/meta for a form in database
201 */
202 public function store()
203 {
204 $value = $this->request->get('value', '');
205
206 $valueArray = $value ? json_decode($value, true) : [];
207
208 $key = sanitize_text_field($this->request->get('meta_key'));
209
210 if ('formSettings' == $key) {
211 Validator::validate(
212 'confirmations',
213 ArrayHelper::get(
214 $valueArray,
215 'confirmation',
216 []
217 )
218 );
219 } else {
220 Validator::validate($key, $valueArray);
221 }
222 $sanitizerMap = [
223 'name' => 'sanitize_text_field',
224 'field' => 'sanitize_text_field',
225 'email' => 'sanitize_text_field',
226 'operator' => 'sanitize_text_field',
227 'value' => 'sanitize_text_field',
228 'fromName' => 'sanitize_text_field',
229 'fromEmail' => 'sanitize_text_field',
230 'replyTo' => 'sanitize_text_field',
231 'bcc' => 'sanitize_text_field',
232 'subject' => 'sanitize_text_field',
233 'message' => 'wp_kses_post',
234 'status' => 'rest_sanitize_boolean',
235 'enabled' => 'rest_sanitize_boolean',
236 'type' => 'sanitize_text_field',
237 'url' => 'sanitize_url',
238 'webhook' => 'sanitize_url',
239 'textTitle' => 'sanitize_text_field',
240 ];
241
242 $valueArray = $this->sanitizeData($valueArray, $sanitizerMap);
243 $value = json_encode($valueArray);
244
245 $data = [
246 'meta_key' => $key,
247 'value' => $value,
248 'form_id' => $this->formId,
249 ];
250
251 // If the request has an valid id field it's safe to assume
252 // that the user wants to update an existing settings.
253 // So, we'll proceed to do so by finding it first.
254 $id = intval($this->request->get('id'));
255
256 if ($id) {
257 $settings = $this->settingsQuery->find($id);
258 }
259
260 if (isset($settings)) {
261 $this->settingsQuery->where('id', $settings->id)->update($data);
262 $insertId = $settings->id;
263 } else {
264 $insertId = $this->settingsQuery->insertGetId($data);
265 }
266
267 wp_send_json_success([
268 'message' => __('Settings has been saved.', 'fluentform'),
269 'settings' => json_decode($value, true),
270 'id' => $insertId,
271 ], 200);
272 }
273
274 /**
275 * Delete settings/meta from database for a given form
276 */
277 public function remove()
278 {
279 $id = intval($this->request->get('id'));
280
281 $this->settingsQuery->where('id', $id)->delete();
282
283 wp_send_json([], 200);
284 }
285
286 private function sanitizeData($settings, $sanitizerMap)
287 {
288 if (fluentformCanUnfilteredHTML()) {
289 return $settings;
290 }
291 return fluentform_backend_sanitizer($settings, $sanitizerMap);
292 }
293 }
294