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 / Form / Settings / FormSettings.php

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

191 lines 5.9 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 * @var \FluentForm\Framework\Request\Request
15 */
16 private $request;
17
18 private $app;
19
20 /**
21 * @var int form ID.
22 */
23 private $formId;
24
25 /**
26 * The settings (fluentform_form_meta) query builder.
27 *
28 * @var \WpFluent\QueryBuilder\QueryBuilderHandler
29 */
30 private $settingsQuery;
31
32 /**
33 * Construct the object
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 * @return void
49 */
50 public function index()
51 {
52 $metaKey = sanitize_text_field($this->request->get('meta_key'));
53
54 // We'll always try to get a collection for a given meta key.
55 // Acknowledging that a certain meta key can have multiple
56 // results. The developer using the api knows beforehand
57 // that whether the expected result contains multiple
58 // or one value. The developer will access that way.
59 $query = $this->settingsQuery->where('meta_key', $metaKey);
60
61 $result = $query->get();
62
63 foreach ($result as $item) {
64 $item->value = json_decode($item->value, true);
65 if($metaKey == 'notifications') {
66 if(!$item->value) {
67 $item->value = ['name' => ''];
68 }
69 }
70 if (isset($item->value['layout']) && !isset($item->value['layout']['asteriskPlacement'])) {
71 $item->value['layout']['asteriskPlacement'] = 'asterisk-right';
72 }
73 }
74 $result = apply_filters('fluentform_get_meta_key_settings_response', $result, $this->formId, $metaKey);
75 wp_send_json_success(['result' => $result], 200);
76 }
77
78
79 public function getGeneralSettingsAjax()
80 {
81 $formId = intval($this->request->get('form_id'));
82 $form = new Form($this->app);
83 $settings = [
84 'generalSettings' => $form->getFormsDefaultSettings($formId),
85 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId)
86 ];
87
88 $settings = apply_filters('fluentform_form_settings_ajax', $settings, $formId);
89
90 wp_send_json_success($settings, 200);
91 }
92
93 public function saveGeneralSettingsAjax()
94 {
95 $formId = intval($this->request->get('form_id'));
96 $form = new Form($this->app);
97 $formSettings = \json_decode($this->request->get('formSettings'), true);
98 $advancedValidationSettings = \json_decode($this->request->get('advancedValidationSettings'), true);
99
100 Validator::validate(
101 'confirmations',
102 ArrayHelper::get($formSettings, 'confirmation', [])
103 );
104
105 $form->updateMeta($formId, 'formSettings', $formSettings);
106 $form->updateMeta($formId, 'advancedValidationSettings', $advancedValidationSettings);
107
108 $deleteAfterXDaysStatus = ArrayHelper::get($formSettings, 'delete_after_x_days');
109 $deleteDaysCount = ArrayHelper::get($formSettings, 'auto_delete_days');
110 $deleteOnSubmission = ArrayHelper::get($formSettings, 'delete_entry_on_submission');
111
112 if($deleteOnSubmission != 'yes' && $deleteDaysCount && $deleteAfterXDaysStatus == 'yes') {
113 // We have to set meta values
114 $form->updateMeta($formId, 'auto_delete_days', intval($deleteDaysCount));
115 } else {
116 // we have to delete meta values
117 $form->deleteMeta($formId, 'auto_delete_days');
118 }
119
120 do_action('fluentform_after_save_form_settings', $formId, $this->request->all());
121
122 wp_send_json_success([
123 'message' => __('Settings has been saved.', 'fluentform')
124 ], 200);
125
126 }
127
128 /**
129 * Save settings/meta for a form in database
130 */
131 public function store()
132 {
133 $value = $this->request->get('value', '');
134
135 $valueArray = $value ? json_decode($value, true) : [];
136
137 $key = sanitize_text_field($this->request->get('meta_key'));
138
139 if ($key == 'formSettings') {
140 Validator::validate(
141 'confirmations', ArrayHelper::get(
142 $valueArray, 'confirmation', []
143 )
144 );
145 } else {
146 Validator::validate($key, $valueArray);
147 }
148
149 $data = [
150 'meta_key' => $key,
151 'value' => $value,
152 'form_id' => $this->formId
153 ];
154
155 // If the request has an valid id field it's safe to assume
156 // that the user wants to update an existing settings.
157 // So, we'll proceed to do so by finding it first.
158 $id = intval($this->request->get('id'));
159
160 if ($id) {
161 $settings = $this->settingsQuery->find($id);
162 }
163
164 if (isset($settings)) {
165 $this->settingsQuery->where('id', $settings->id)->update($data);
166 $insertId = $settings->id;
167 } else {
168 $insertId = $this->settingsQuery->insert($data);
169 }
170
171 wp_send_json_success([
172 'message' => __('Settings has been saved.', 'fluentform'),
173 'settings' => json_decode($value, true),
174 'id' => $insertId
175 ], 200);
176 }
177
178 /**
179 * Delete settings/meta from database for a given form
180 * @return void
181 */
182 public function remove()
183 {
184 $id = intval($this->request->get('id'));
185
186 $this->settingsQuery->where('id', $id)->delete();
187
188 wp_send_json([], 200);
189 }
190 }
191