PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.62
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.62
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.62, at app/Helpers/Helper.php

469 lines 13.7 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
319 if (false === strpos($content, '[')) {
320 return [];
321 }
322
323 preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
324 if (empty($matches)) {
325 return [];
326 }
327
328 $ids = [];
329
330 foreach ($matches as $shortcode) {
331 if (count($shortcode) >= 2 && $tag === $shortcode[2]) {
332 // Replace braces with empty string.
333 $parsedCode = str_replace(['[', ']', '&#91;', '&#93;'], '', $shortcode[0]);
334
335 $result = shortcode_parse_atts($parsedCode);
336
337 if (!empty($result[$selector])) {
338 $ids[$result[$selector]] = $result[$selector];
339 }
340 }
341 }
342
343 return $ids;
344 }
345
346 public static function isTabIndexEnabled()
347 {
348 if (static::$tabIndexStatus == 'na') {
349 $globalSettings = get_option('_fluentform_global_form_settings');
350 static::$tabIndexStatus = ArrayHelper::get($globalSettings, 'misc.tabIndex') == 'yes';
351 }
352 return static::$tabIndexStatus;
353 }
354
355 public static function isMultiStepForm($formId)
356 {
357 $form = wpFluent()->table('fluentform_forms')->find($formId);
358 $fields = json_decode($form->form_fields, true);
359
360 if (ArrayHelper::get($fields, 'stepsWrapper')) {
361 return true;
362 }
363 return false;
364 }
365
366 public static function hasFormElement($formId, $elementName)
367 {
368 $form = wpFluent()->table('fluentform_forms')->find($formId);
369 $fieldsJson = $form->form_fields;
370 return strpos($fieldsJson, '"element":"' . $elementName . '"') != false;
371 }
372
373 public static function isUniqueValidation($validation, $field, $formData, $fields, $form)
374 {
375 if (ArrayHelper::get($field, 'raw.settings.is_unique') == 'yes') {
376 $fieldName = ArrayHelper::get($field, 'name');
377 if ($inputValue = ArrayHelper::get($formData, $fieldName)) {
378 $exist = wpFluent()->table('fluentform_entry_details')
379 ->where('form_id', $form->id)
380 ->where('field_name', $fieldName)
381 ->where('field_value', $inputValue)
382 ->first();
383 if ($exist) {
384 return [
385 'unique' => ArrayHelper::get($field, 'raw.settings.unique_validation_message')
386 ];
387 }
388 }
389 }
390
391 return $validation;
392 }
393
394 public static function getNumericFormatters()
395 {
396 return apply_filters('fluentform_numeric_styles', array(
397 'none' => array(
398 'value' => '',
399 'label' => 'None'
400 ),
401 'comma_dot_style' => array(
402 'value' => 'comma_dot_style',
403 'label' => __('US Style with Decimal (EX: 123,456.00)', 'fluentform'),
404 'settings' => [
405 'decimal' => '.',
406 'separator' => ',',
407 'precision' => 2,
408 'symbol' => ''
409 ]
410 ),
411 'dot_comma_style_zero' => array(
412 'value' => 'dot_comma_style_zero',
413 'label' => __('US Style without Decimal (Ex: 123,456,789)', 'fluentform'),
414 'settings' => [
415 'decimal' => '.',
416 'separator' => ',',
417 'precision' => 0,
418 'symbol' => ''
419 ]
420 ),
421 'dot_comma_style' => array(
422 'value' => 'dot_comma_style',
423 'label' => __('EU Style with Decimal (Ex: 123.456,00)', 'fluentform'),
424 'settings' => [
425 'decimal' => ',',
426 'separator' => '.',
427 'precision' => 2,
428 'symbol' => ''
429 ]
430 ),
431 'comma_dot_style_zero' => array(
432 'value' => 'comma_dot_style_zero',
433 'label' => __('EU Style without Decimal (EX: 123.456.789)', 'fluentform'),
434 'settings' => [
435 'decimal' => ',',
436 'separator' => '.',
437 'precision' => 0,
438 'symbol' => ''
439 ]
440 )
441 ));
442 }
443
444 public static function getNumericValue($input, $formatterName)
445 {
446 $formatters = self::getNumericFormatters();
447 if(empty($formatters[$formatterName]['settings'])) {
448 return $input;
449 }
450 $settings = $formatters[$formatterName]['settings'];
451 $number = floatval(str_replace($settings['decimal'], '.', preg_replace('/[^\d'.preg_quote($settings['decimal']).']/', '', $input)));
452
453 return number_format($number, $settings['precision'], '.', '');
454 }
455
456 public static function getNumericFormatted($input, $formatterName)
457 {
458 if(!is_numeric($input)) {
459 return $input;
460 }
461 $formatters = self::getNumericFormatters();
462 if(empty($formatters[$formatterName]['settings'])) {
463 return $input;
464 }
465 $settings = $formatters[$formatterName]['settings'];
466 return number_format($input, $settings['precision'], $settings['decimal'], $settings['separator']);
467 }
468
469 }