PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.19
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.19
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 3.6.66 All 195 releases
fluentform / app / Modules / Form / Settings / FormSettings.php

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

257 lines 9.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\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 $result = apply_filters('fluentform_get_meta_key_settings_response', $result, $this->formId, $metaKey);
74 wp_send_json_success(['result' => $result], 200);
75 }
76
77 public function getGeneralSettingsAjax()
78 {
79 $formId = intval($this->request->get('form_id'));
80 $form = new Form($this->app);
81 $settings = [
82 'generalSettings' => $form->getFormsDefaultSettings($formId),
83 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId),
84 ];
85
86 $settings = apply_filters('fluentform_form_settings_ajax', $settings, $formId);
87
88 wp_send_json_success($settings, 200);
89 }
90
91 public function saveGeneralSettingsAjax()
92 {
93 $formId = intval($this->request->get('form_id'));
94 $form = new Form($this->app);
95 $formSettings = \json_decode($this->request->get('formSettings'), true);
96
97 $sanitizerMap = [
98 'redirectTo' => 'sanitize_text_field',
99 'redirectMessage' => 'fluentform_sanitize_html',
100 'messageToShow' => 'fluentform_sanitize_html',
101 'customPage' => 'sanitize_text_field',
102 'samePageFormBehavior' => 'sanitize_text_field',
103 'customUrl' => 'sanitize_url',
104 'enabled' => 'rest_sanitize_boolean',
105 'numberOfEntries' => 'intval',
106 'period' => 'intval',
107 'limitReachedMsg' => 'sanitize_text_field',
108 'start' => 'sanitize_text_field',
109 'end' => 'sanitize_text_field',
110 'pendingMsg' => 'sanitize_text_field',
111 'expiredMsg' => 'sanitize_text_field',
112 'requireLoginMsg' => 'sanitize_text_field',
113 'message' => 'sanitize_text_field',
114 'labelPlacement' => 'sanitize_text_field',
115 'helpMessagePlacement' => 'sanitize_text_field',
116 'errorMessagePlacement' => 'sanitize_text_field',
117 'asteriskPlacement' => 'sanitize_text_field',
118 'delete_entry_on_submission' => 'sanitize_text_field',
119 'id' => 'intval',
120 'showLabel' => 'rest_sanitize_boolean',
121 'showCount' => 'rest_sanitize_boolean',
122 'status' => 'rest_sanitize_boolean',
123 'type' => 'sanitize_text_field',
124 'field' => 'sanitize_text_field',
125 'operator' => 'sanitize_text_field',
126 'value' => 'sanitize_text_field',
127 'error_message' => 'sanitize_text_field',
128 'validation_type' => 'sanitize_text_field',
129 ];
130 $formSettings = $this->sanitizeData($formSettings, $sanitizerMap);
131
132 $advancedValidationSettings = \json_decode($this->request->get('advancedValidationSettings'), true);
133 $advancedValidationSettings = $this->sanitizeData($advancedValidationSettings, $sanitizerMap);
134
135 Validator::validate(
136 'confirmations',
137 ArrayHelper::get($formSettings, 'confirmation', [])
138 );
139
140 $form->updateMeta($formId, 'formSettings', $formSettings);
141 $form->updateMeta($formId, 'advancedValidationSettings', $advancedValidationSettings);
142
143 $deleteAfterXDaysStatus = ArrayHelper::get($formSettings, 'delete_after_x_days');
144 $deleteDaysCount = ArrayHelper::get($formSettings, 'auto_delete_days');
145 $deleteOnSubmission = ArrayHelper::get($formSettings, 'delete_entry_on_submission');
146
147 if ('yes' != $deleteOnSubmission && $deleteDaysCount && 'yes' == $deleteAfterXDaysStatus) {
148 // We have to set meta values
149 $form->updateMeta($formId, 'auto_delete_days', intval($deleteDaysCount));
150 } else {
151 // we have to delete meta values
152 $form->deleteMeta($formId, 'auto_delete_days');
153 }
154
155 do_action('fluentform_after_save_form_settings', $formId, $this->request->all());
156
157 wp_send_json_success([
158 'message' => __('Settings has been saved.', 'fluentform'),
159 ], 200);
160 }
161
162 /**
163 * Save settings/meta for a form in database
164 */
165 public function store()
166 {
167 $value = $this->request->get('value', '');
168
169 $valueArray = $value ? json_decode($value, true) : [];
170
171 $key = sanitize_text_field($this->request->get('meta_key'));
172
173 if ('formSettings' == $key) {
174 Validator::validate(
175 'confirmations',
176 ArrayHelper::get(
177 $valueArray,
178 'confirmation',
179 []
180 )
181 );
182 } else {
183 Validator::validate($key, $valueArray);
184 }
185 $sanitizerMap = [
186 'name' => 'sanitize_text_field',
187 'field' => 'sanitize_text_field',
188 'email' => 'sanitize_text_field',
189 'operator' => 'sanitize_text_field',
190 'value' => 'sanitize_text_field',
191 'fromName' => 'sanitize_text_field',
192 'fromEmail' => 'sanitize_text_field',
193 'replyTo' => 'sanitize_text_field',
194 'bcc' => 'sanitize_text_field',
195 'subject' => 'sanitize_text_field',
196 'message' => 'wp_kses_post',
197 'status' => 'rest_sanitize_boolean',
198 'enabled' => 'rest_sanitize_boolean',
199 'type' => 'sanitize_text_field',
200 'url' => 'sanitize_url',
201 'webhook' => 'sanitize_url',
202 'textTitle' => 'sanitize_text_field',
203 ];
204
205 $valueArray = $this->sanitizeData($valueArray, $sanitizerMap);
206 $value = json_encode($valueArray);
207
208 $data = [
209 'meta_key' => $key,
210 'value' => $value,
211 'form_id' => $this->formId,
212 ];
213
214 // If the request has an valid id field it's safe to assume
215 // that the user wants to update an existing settings.
216 // So, we'll proceed to do so by finding it first.
217 $id = intval($this->request->get('id'));
218
219 if ($id) {
220 $settings = $this->settingsQuery->find($id);
221 }
222
223 if (isset($settings)) {
224 $this->settingsQuery->where('id', $settings->id)->update($data);
225 $insertId = $settings->id;
226 } else {
227 $insertId = $this->settingsQuery->insert($data);
228 }
229
230 wp_send_json_success([
231 'message' => __('Settings has been saved.', 'fluentform'),
232 'settings' => json_decode($value, true),
233 'id' => $insertId,
234 ], 200);
235 }
236
237 /**
238 * Delete settings/meta from database for a given form
239 */
240 public function remove()
241 {
242 $id = intval($this->request->get('id'));
243
244 $this->settingsQuery->where('id', $id)->delete();
245
246 wp_send_json([], 200);
247 }
248
249 private function sanitizeData($settings, $sanitizerMap)
250 {
251 if (fluentformCanUnfilteredHTML()) {
252 return $settings;
253 }
254 return fluentform_backend_sanitizer($settings, $sanitizerMap);
255 }
256 }
257