PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.50
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.50
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 / Helpers / Helper.php

Helper.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.50, at app/Helpers/Helper.php

393 lines 10.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\Helpers;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6
7 class Helper
8 {
9 static $tabIndex = 0;
10
11 static $formInstance = 0;
12
13 static $loadedForms = [];
14
15 static $tabIndexStatus = 'na';
16
17 /**
18 * Sanitize form inputs recursively.
19 *
20 * @param $input
21 *
22 * @return string $input
23 */
24 public static function sanitizer($input, $attribute = null, $fields = [])
25 {
26 if (is_string($input)) {
27 if (ArrayHelper::get($fields, $attribute . '.element') === 'textarea') {
28 $input = sanitize_textarea_field($input);
29 } else {
30 $input = sanitize_text_field($input);
31 }
32 } elseif (is_array($input)) {
33 foreach ($input as $key => &$value) {
34 $attribute = $attribute ? $attribute . '[' . $key . ']' : $key;
35
36 $value = Helper::sanitizer($value, $attribute, $fields);
37
38 $attribute = null;
39 }
40 }
41
42 return $input;
43 }
44
45 public static function makeMenuUrl($page = 'fluent_forms_settings', $component = null)
46 {
47 $baseUrl = admin_url('admin.php?page=' . $page);
48
49 $hash = ArrayHelper::get($component, 'hash', '');
50 if ($hash) {
51 $baseUrl = $baseUrl . '#' . $hash;
52 }
53
54 $query = ArrayHelper::get($component, 'query');
55
56 if ($query) {
57 $paramString = http_build_query($query);
58 if ($hash) {
59 $baseUrl .= '?' . $paramString;
60 } else {
61 $baseUrl .= '&' . $paramString;
62 }
63 }
64
65 return $baseUrl;
66
67 }
68
69 public static function getHtmlElementClass($value1, $value2, $class = 'active', $default = '')
70 {
71 return $value1 === $value2 ? $class : $default;
72 }
73
74 /**
75 * Determines if the given string is a valid json.
76 *
77 * @param $string
78 *
79 * @return bool
80 */
81 public static function isJson($string)
82 {
83 json_decode($string);
84
85 return json_last_error() === JSON_ERROR_NONE;
86 }
87
88
89 public static function isSlackEnabled()
90 {
91 $globalModules = get_option('fluentform_global_modules_status');
92 return $globalModules && isset($globalModules['slack']) && $globalModules['slack'] == 'yes';
93 }
94
95 public static function getEntryStatuses($form_id = false)
96 {
97 $statuses = apply_filters('fluentform_entry_statuses', [
98 'unread' => 'Unread',
99 'read' => 'Read'
100 ], $form_id);
101 $statuses['trashed'] = 'Trashed';
102 return $statuses;
103 }
104
105 public static function getReportableInputs()
106 {
107 return apply_filters('fluentform_reportable_inputs', [
108 'select',
109 'input_radio',
110 'input_checkbox',
111 'ratings',
112 'net_promoter',
113 'select_country',
114 'net_promoter_score'
115 ]);
116 }
117
118 public static function getSubFieldReportableInputs()
119 {
120 return apply_filters('fluentform_subfield_reportable_inputs', [
121 'tabular_grid'
122 ]);
123 }
124
125 public static function getFormMeta($formId, $metaKey, $default = '')
126 {
127 $meta = wpFluent()->table('fluentform_form_meta')
128 ->where('meta_key', $metaKey)
129 ->where('form_id', $formId)
130 ->first();
131
132 if (!$meta || !$meta->value) {
133 return $default;
134 }
135
136 $metaValue = $meta->value;
137 // decode the JSON data
138 $result = json_decode($metaValue, true);
139
140 if (json_last_error() == JSON_ERROR_NONE) {
141 return $result;
142 }
143 return $metaValue;
144 }
145
146 public static function setFormMeta($formId, $metaKey, $value)
147 {
148 $meta = wpFluent()->table('fluentform_form_meta')
149 ->where('meta_key', $metaKey)
150 ->where('form_id', $formId)
151 ->first();
152
153 if (is_array($value) || is_object($value)) {
154 $value = json_encode($value);
155 }
156
157 if (!$meta) {
158 $insetid = wpFluent()->table('fluentform_form_meta')
159 ->insert([
160 'meta_key' => $metaKey,
161 'form_id' => $formId,
162 'value' => $value
163 ]);
164 return $insetid;
165 } else {
166 wpFluent()->table('fluentform_form_meta')
167 ->where('id', $meta->id)
168 ->update([
169 'value' => $value
170 ]);
171 }
172
173 return $meta->id;
174 }
175
176 public static function getSubmissionMeta($submissionId, $metaKey, $default = false)
177 {
178 $meta = wpFluent()->table('fluentform_submission_meta')
179 ->where('response_id', $submissionId)
180 ->where('meta_key', $metaKey)
181 ->first();
182
183 if ($meta && $meta->value) {
184 return maybe_unserialize($meta->value);
185 }
186
187 return $default;
188 }
189
190 public static function setSubmissionMeta($submissionId, $metaKey, $value, $formId = false)
191 {
192 $value = maybe_serialize($value);
193
194 // check if submission exist
195 $meta = wpFluent()->table('fluentform_submission_meta')
196 ->where('response_id', $submissionId)
197 ->where('meta_key', $metaKey)
198 ->first();
199
200 if($meta) {
201 wpFluent()->table('fluentform_submission_meta')
202 ->where('id', $meta->id)
203 ->insert([
204 'value' => $value,
205 'updated_at' => current_time('mysql')
206 ]);
207 return $meta->id;
208 }
209
210 if(!$formId) {
211 $submission = wpFluent()->table('fluentform_submissions')
212 ->find($submissionId);
213 if($submission) {
214 $formId = $submission->form_id;
215 }
216 }
217
218 return wpFluent()->table('fluentform_submission_meta')
219 ->insert([
220 'response_id' => $submissionId,
221 'form_id' => $formId,
222 'meta_key' => $metaKey,
223 'value' => $value,
224 'created_at' => current_time('mysql'),
225 'updated_at' => current_time('mysql')
226 ]);
227
228 }
229
230 public static function isEntryAutoDeleteEnabled($formId)
231 {
232 $settings = wpFluent()->table('fluentform_form_meta')
233 ->where('form_id', $formId)
234 ->where('meta_key', 'formSettings')
235 ->first();
236
237 if (!$settings) {
238 return false;
239 }
240
241 $formSettings = json_decode($settings->value, true);
242
243 if ($formSettings && ArrayHelper::get($formSettings, 'delete_entry_on_submission') == 'yes') {
244 return true;
245 }
246
247 return false;
248 }
249
250 public static function formExtraCssClass($form)
251 {
252 if(!$form->settings) {
253 $settings = wpFluent()->table('fluentform_form_meta')
254 ->where('form_id', $form->id)
255 ->where('meta_key', 'formSettings')
256 ->first();
257
258 $formSettings = json_decode($settings->value, true);
259 } else {
260 $formSettings = $form->settings;
261 }
262
263 if (!$formSettings) {
264 return '';
265 }
266
267
268 if ($formSettings && $extraClass = ArrayHelper::get($formSettings, 'form_extra_css_class')) {
269 return esc_attr($extraClass);
270 }
271
272 return '';
273 }
274
275 public static function getNextTabIndex($increment = 1)
276 {
277 if(self::isTabIndexEnabled()) {
278 static::$tabIndex += $increment;
279 return static::$tabIndex;
280 }
281 return '';
282 }
283
284 public static function getFormInstaceClass($formId)
285 {
286 static::$formInstance += 1;
287 return 'ff_form_instance_'.$formId.'_'.static::$formInstance;
288 }
289
290 public static function resetTabIndex()
291 {
292 static::$tabIndex = 0;
293 }
294
295 public static function isFluentAdminPage()
296 {
297 $fluentPages = [
298 'fluent_forms',
299 'fluent_forms_all_entries',
300 'fluent_forms_transfer',
301 'fluent_forms_settings',
302 'fluent_form_add_ons',
303 'fluent_forms_docs',
304 'fluent_form_payment_entries'
305 ];
306
307 $status = true;
308
309 if (!isset($_GET['page']) || !in_array($_GET['page'], $fluentPages)) {
310 $status = false;
311 }
312
313 return apply_filters('fluentform_is_admin_page', $status);
314 }
315
316 public static function getShortCodeIds($content, $tag = 'fluentform', $selector = 'id') {
317
318 if (false === strpos($content, '[')) {
319 return [];
320 }
321
322 preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
323 if (empty($matches)) {
324 return [];
325 }
326
327 $ids = [];
328
329 foreach ($matches as $shortcode) {
330 if (count($shortcode) >= 2 && $tag === $shortcode[2]) {
331 // Replace braces with empty string.
332 $parsedCode = str_replace(['[', ']', '&#91;', '&#93;'], '', $shortcode[0]);
333
334 $result = shortcode_parse_atts($parsedCode);
335
336 if (!empty($result[$selector])) {
337 $ids[$result[$selector]] = $result[$selector];
338 }
339 }
340 }
341
342 return $ids;
343 }
344
345 public static function isTabIndexEnabled()
346 {
347 if(static::$tabIndexStatus == 'na') {
348 $globalSettings = get_option('_fluentform_global_form_settings');
349 static::$tabIndexStatus = ArrayHelper::get($globalSettings, 'misc.tabIndex') == 'yes';
350 }
351 return static::$tabIndexStatus;
352 }
353
354 public static function isMultiStepForm($formId)
355 {
356 $form = wpFluent()->table('fluentform_forms')->find($formId);
357 $fields = json_decode($form->form_fields, true);
358
359 if(ArrayHelper::get($fields, 'stepsWrapper')) {
360 return true;
361 }
362 return false;
363 }
364
365 public static function hasFormElement($formId, $elementName)
366 {
367 $form = wpFluent()->table('fluentform_forms')->find($formId);
368 $fieldsJson = $form->form_fields;
369 return strpos($fieldsJson, '"element":"'.$elementName.'"') != false;
370 }
371
372 public static function isUniqueValidation($validation, $field, $formData, $fields, $form)
373 {
374 if (ArrayHelper::get($field, 'raw.settings.is_unique') == 'yes') {
375 $fieldName = ArrayHelper::get($field, 'name');
376 if ($inputValue = ArrayHelper::get($formData, $fieldName)) {
377 $exist = wpFluent()->table('fluentform_entry_details')
378 ->where('form_id', $form->id)
379 ->where('field_name', $fieldName)
380 ->where('field_value', $inputValue)
381 ->first();
382 if ($exist) {
383 return [
384 'unique' => ArrayHelper::get($field, 'raw.settings.unique_validation_message')
385 ];
386 }
387 }
388 }
389
390 return $validation;
391 }
392
393 }