PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.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 3.6.66 All 195 releases
fluentform / app / Helpers / Helper.php

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

644 lines 19.2 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 public static $tabIndex = 0;
10
11 public static $formInstance = 0;
12
13 public static $loadedForms = [];
14
15 public 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 = static::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 public static function getHtmlElementClass($value1, $value2, $class = 'active', $default = '')
69 {
70 return $value1 === $value2 ? $class : $default;
71 }
72
73 /**
74 * Determines if the given string is a valid json.
75 *
76 * @param $string
77 *
78 * @return bool
79 */
80 public static function isJson($string)
81 {
82 json_decode($string);
83
84 return json_last_error() === JSON_ERROR_NONE;
85 }
86
87
88 public static function isSlackEnabled()
89 {
90 $globalModules = get_option('fluentform_global_modules_status');
91 return $globalModules && isset($globalModules['slack']) && $globalModules['slack'] == 'yes';
92 }
93
94 public static function getEntryStatuses($form_id = false)
95 {
96 $statuses = apply_filters('fluentform_entry_statuses_core', [
97 'unread' => 'Unread',
98 'read' => 'Read'
99 ], $form_id);
100 $statuses['trashed'] = 'Trashed';
101 return $statuses;
102 }
103
104 public static function getReportableInputs()
105 {
106 return apply_filters('fluentform_reportable_inputs', [
107 'select',
108 'input_radio',
109 'input_checkbox',
110 'ratings',
111 'net_promoter',
112 'select_country',
113 'net_promoter_score'
114 ]);
115 }
116
117 public static function getSubFieldReportableInputs()
118 {
119 return apply_filters('fluentform_subfield_reportable_inputs', [
120 'tabular_grid'
121 ]);
122 }
123
124 public static function getFormMeta($formId, $metaKey, $default = '')
125 {
126 $meta = wpFluent()->table('fluentform_form_meta')
127 ->where('meta_key', $metaKey)
128 ->where('form_id', $formId)
129 ->first();
130
131 if (!$meta || !$meta->value) {
132 return $default;
133 }
134
135 $metaValue = $meta->value;
136 // decode the JSON data
137 $result = json_decode($metaValue, true);
138
139 if (json_last_error() == JSON_ERROR_NONE) {
140 return $result;
141 }
142 return $metaValue;
143 }
144
145 public static function setFormMeta($formId, $metaKey, $value)
146 {
147 $meta = wpFluent()->table('fluentform_form_meta')
148 ->where('meta_key', $metaKey)
149 ->where('form_id', $formId)
150 ->first();
151
152 if (is_array($value) || is_object($value)) {
153 $value = json_encode($value);
154 }
155
156 if (!$meta) {
157 $insetid = wpFluent()->table('fluentform_form_meta')
158 ->insert([
159 'meta_key' => $metaKey,
160 'form_id' => $formId,
161 'value' => $value
162 ]);
163 return $insetid;
164 } else {
165 wpFluent()->table('fluentform_form_meta')
166 ->where('id', $meta->id)
167 ->update([
168 'value' => $value
169 ]);
170 }
171
172 return $meta->id;
173 }
174
175 public static function getSubmissionMeta($submissionId, $metaKey, $default = false)
176 {
177 $meta = wpFluent()->table('fluentform_submission_meta')
178 ->where('response_id', $submissionId)
179 ->where('meta_key', $metaKey)
180 ->first();
181
182 if ($meta && $meta->value) {
183 return maybe_unserialize($meta->value);
184 }
185
186 return $default;
187 }
188
189 public static function setSubmissionMeta($submissionId, $metaKey, $value, $formId = false)
190 {
191 $value = maybe_serialize($value);
192
193 // check if submission exist
194 $meta = wpFluent()->table('fluentform_submission_meta')
195 ->where('response_id', $submissionId)
196 ->where('meta_key', $metaKey)
197 ->first();
198
199 if ($meta) {
200 wpFluent()->table('fluentform_submission_meta')
201 ->where('id', $meta->id)
202 ->insert([
203 'value' => $value,
204 'updated_at' => current_time('mysql')
205 ]);
206 return $meta->id;
207 }
208
209 if (!$formId) {
210 $submission = wpFluent()->table('fluentform_submissions')
211 ->find($submissionId);
212 if ($submission) {
213 $formId = $submission->form_id;
214 }
215 }
216
217 return wpFluent()->table('fluentform_submission_meta')
218 ->insert([
219 'response_id' => $submissionId,
220 'form_id' => $formId,
221 'meta_key' => $metaKey,
222 'value' => $value,
223 'created_at' => current_time('mysql'),
224 'updated_at' => current_time('mysql')
225 ]);
226 }
227
228 public static function isEntryAutoDeleteEnabled($formId)
229 {
230 $settings = wpFluent()->table('fluentform_form_meta')
231 ->where('form_id', $formId)
232 ->where('meta_key', 'formSettings')
233 ->first();
234
235 if (!$settings) {
236 return false;
237 }
238
239 $formSettings = json_decode($settings->value, true);
240
241 if ($formSettings && ArrayHelper::get($formSettings, 'delete_entry_on_submission') == 'yes') {
242 return true;
243 }
244
245 return false;
246 }
247
248 public static function formExtraCssClass($form)
249 {
250 if (!$form->settings) {
251 $settings = wpFluent()->table('fluentform_form_meta')
252 ->where('form_id', $form->id)
253 ->where('meta_key', 'formSettings')
254 ->first();
255
256 $formSettings = json_decode($settings->value, true);
257 } else {
258 $formSettings = $form->settings;
259 }
260
261 if (!$formSettings) {
262 return '';
263 }
264
265
266 if ($formSettings && $extraClass = ArrayHelper::get($formSettings, 'form_extra_css_class')) {
267 return esc_attr($extraClass);
268 }
269
270 return '';
271 }
272
273 public static function getNextTabIndex($increment = 1)
274 {
275 if (self::isTabIndexEnabled()) {
276 static::$tabIndex += $increment;
277 return static::$tabIndex;
278 }
279 return '';
280 }
281
282 public static function getFormInstaceClass($formId)
283 {
284 static::$formInstance += 1;
285 return 'ff_form_instance_' . $formId . '_' . static::$formInstance;
286 }
287
288 public static function resetTabIndex()
289 {
290 static::$tabIndex = 0;
291 }
292
293 public static function isFluentAdminPage()
294 {
295 $fluentPages = [
296 'fluent_forms',
297 'fluent_forms_all_entries',
298 'fluent_forms_transfer',
299 'fluent_forms_settings',
300 'fluent_forms_add_ons',
301 'fluent_forms_docs',
302 'fluent_forms_payment_entries',
303 'fluent_forms_smtp'
304 ];
305
306 $status = true;
307
308 if (!isset($_GET['page']) || !in_array($_GET['page'], $fluentPages)) {
309 $status = false;
310 }
311
312 return apply_filters('fluentform_is_admin_page', $status);
313 }
314
315 public static function getShortCodeIds($content, $tag = 'fluentform', $selector = 'id')
316 {
317 if (false === strpos($content, '[')) {
318 return [];
319 }
320
321 preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
322 if (empty($matches)) {
323 return [];
324 }
325
326 $ids = [];
327
328 foreach ($matches as $shortcode) {
329 if (count($shortcode) >= 2 && $tag === $shortcode[2]) {
330 // Replace braces with empty string.
331 $parsedCode = str_replace(['[', ']', '&#91;', '&#93;'], '', $shortcode[0]);
332
333 $result = shortcode_parse_atts($parsedCode);
334
335 if (!empty($result[$selector])) {
336 $ids[$result[$selector]] = $result[$selector];
337 }
338 }
339 }
340
341 return $ids;
342 }
343
344 public static function isTabIndexEnabled()
345 {
346 if (static::$tabIndexStatus == 'na') {
347 $globalSettings = get_option('_fluentform_global_form_settings');
348 static::$tabIndexStatus = ArrayHelper::get($globalSettings, 'misc.tabIndex') == 'yes';
349 }
350 return static::$tabIndexStatus;
351 }
352
353 public static function isMultiStepForm($formId)
354 {
355 $form = wpFluent()->table('fluentform_forms')->find($formId);
356 $fields = json_decode($form->form_fields, true);
357
358 if (ArrayHelper::get($fields, 'stepsWrapper')) {
359 return true;
360 }
361 return false;
362 }
363
364 public static function hasFormElement($formId, $elementName)
365 {
366 $form = wpFluent()->table('fluentform_forms')->find($formId);
367 $fieldsJson = $form->form_fields;
368 return strpos($fieldsJson, '"element":"' . $elementName . '"') != false;
369 }
370
371 public static function isUniqueValidation($validation, $field, $formData, $fields, $form)
372 {
373 if (ArrayHelper::get($field, 'raw.settings.is_unique') == 'yes') {
374 $fieldName = ArrayHelper::get($field, 'name');
375 if ($inputValue = ArrayHelper::get($formData, $fieldName)) {
376 $exist = wpFluent()->table('fluentform_entry_details')
377 ->where('form_id', $form->id)
378 ->where('field_name', $fieldName)
379 ->where('field_value', $inputValue)
380 ->first();
381 if ($exist) {
382 return [
383 'unique' => ArrayHelper::get($field, 'raw.settings.unique_validation_message')
384 ];
385 }
386 }
387 }
388
389 return $validation;
390 }
391
392 public static function getNumericFormatters()
393 {
394 return apply_filters('fluentform_numeric_styles', array(
395 'none' => array(
396 'value' => '',
397 'label' => 'None'
398 ),
399 'comma_dot_style' => array(
400 'value' => 'comma_dot_style',
401 'label' => __('US Style with Decimal (EX: 123,456.00)', 'fluentform'),
402 'settings' => [
403 'decimal' => '.',
404 'separator' => ',',
405 'precision' => 2,
406 'symbol' => ''
407 ]
408 ),
409 'dot_comma_style_zero' => array(
410 'value' => 'dot_comma_style_zero',
411 'label' => __('US Style without Decimal (Ex: 123,456,789)', 'fluentform'),
412 'settings' => [
413 'decimal' => '.',
414 'separator' => ',',
415 'precision' => 0,
416 'symbol' => ''
417 ]
418 ),
419 'dot_comma_style' => array(
420 'value' => 'dot_comma_style',
421 'label' => __('EU Style with Decimal (Ex: 123.456,00)', 'fluentform'),
422 'settings' => [
423 'decimal' => ',',
424 'separator' => '.',
425 'precision' => 2,
426 'symbol' => ''
427 ]
428 ),
429 'comma_dot_style_zero' => array(
430 'value' => 'comma_dot_style_zero',
431 'label' => __('EU Style without Decimal (EX: 123.456.789)', 'fluentform'),
432 'settings' => [
433 'decimal' => ',',
434 'separator' => '.',
435 'precision' => 0,
436 'symbol' => ''
437 ]
438 )
439 ));
440 }
441
442 public static function getNumericValue($input, $formatterName)
443 {
444 $formatters = self::getNumericFormatters();
445 if (empty($formatters[$formatterName]['settings'])) {
446 return $input;
447 }
448 $settings = $formatters[$formatterName]['settings'];
449 $number = floatval(str_replace($settings['decimal'], '.', preg_replace('/[^\d' . preg_quote($settings['decimal']) . ']/', '', $input)));
450
451 return number_format($number, $settings['precision'], '.', '');
452 }
453
454 public static function getNumericFormatted($input, $formatterName)
455 {
456 if (!is_numeric($input)) {
457 return $input;
458 }
459 $formatters = self::getNumericFormatters();
460 if (empty($formatters[$formatterName]['settings'])) {
461 return $input;
462 }
463 $settings = $formatters[$formatterName]['settings'];
464 return number_format($input, $settings['precision'], $settings['decimal'], $settings['separator']);
465 }
466
467 public static function getDuplicateFieldNames($fields)
468 {
469 $fields = json_decode($fields, true);
470 $items = $fields['fields'];
471 $inputNames = self::getFieldNamesStatuses($items);
472 $uniqueNames = array_unique($inputNames);
473
474 if (count($inputNames) == count($uniqueNames)) {
475 return [];
476 }
477
478 return array_diff_assoc($inputNames, $uniqueNames);
479 }
480
481 protected static function getFieldNamesStatuses($fields)
482 {
483 $names = [];
484
485 foreach ($fields as $field) {
486 if (ArrayHelper::get($field, 'element') == 'container') {
487 $columns = ArrayHelper::get($field, 'columns', []);
488 foreach ($columns as $column) {
489 $columnInputs = self::getFieldNamesStatuses(ArrayHelper::get($column, 'fields', []));
490 $names = array_merge($names, $columnInputs);
491 }
492 } else {
493 if ($name = ArrayHelper::get($field, 'attributes.name')) {
494 $names[] = $name;
495 }
496 }
497 }
498
499 return $names;
500 }
501
502 public static function isConversionForm($formId)
503 {
504 static $cache = [];
505 if (isset($cache[$formId])) {
506 return $cache[$formId];
507 }
508
509 $cache[$formId] = self::getFormMeta($formId, 'is_conversion_form') == 'yes';
510 return $cache[$formId];
511 }
512
513 public static function getPreviewUrl($formId, $type = '')
514 {
515 if ($type == 'conversational') {
516 return self::getConversionUrl($formId);
517 } elseif ($type == 'classic') {
518 return site_url('?fluent_forms_pages=1&design_mode=1&preview_id=' . $formId) . '#ff_preview';
519 } else {
520 if (self::isConversionForm($formId)) {
521 return self::getConversionUrl($formId);
522 }
523 }
524
525 return site_url('?fluent_forms_pages=1&design_mode=1&preview_id=' . $formId) . '#ff_preview';
526 }
527
528 public static function getFormAdminPermalink($route, $form)
529 {
530 $baseUrl = admin_url('admin.php?page=fluent_forms');
531 return $baseUrl . '&route=' . $route . '&form_id=' . $form->id;
532 }
533
534 public static function getFormSettingsUrl($form)
535 {
536 $baseUrl = admin_url('admin.php?page=fluent_forms');
537 return $baseUrl . '&form_id=' . $form->id . '&route=settings&sub_route=form_settings#basic_settings';
538 }
539
540 private static function getConversionUrl($formId)
541 {
542 $meta = self::getFormMeta($formId, 'ffc_form_settings_meta', []);
543 $key = ArrayHelper::get($meta, 'share_key', '');
544 $paramKey = apply_filters('fluentform_conversational_url_slug', 'fluent-form');
545 if ($paramKey == 'form') {
546 $paramKey = 'fluent-form';
547 }
548 if ($key) {
549 return site_url('?'.$paramKey.'=' . $formId . '&form=' . $key);
550 }
551 return site_url('?'.$paramKey.'=' . $formId);
552 }
553
554 public static function fileUploadLocations()
555 {
556 $locations = array(
557 array(
558 'value' => 'default',
559 'label' => __('Fluentforms Default', 'fluentform'),
560 ),
561 array(
562 'value' => 'wp_media',
563 'label' => __('Media Library', 'fluentform'),
564 ),
565 );
566
567 return apply_filters('fluentform_file_upload_options', $locations);
568 }
569
570 private function unreadCount($formId)
571 {
572 return wpFluent()->table('fluentform_submissions')
573 ->where('status', 'unread')
574 ->where('form_id', $formId)
575 ->count();
576 }
577
578 public static function getForms()
579 {
580 $ff_list = wpFluent()->table('fluentform_forms')
581 ->select(['id', 'title'])
582 ->orderBy('id', 'DESC')
583 ->get();
584
585
586 $forms = array();
587
588 if ($ff_list) {
589 $forms[0] = esc_html__('Select a Fluent Forms', 'fluentform');
590 foreach ($ff_list as $form) {
591 $forms[$form->id] = esc_html($form->title) .' ('.$form->id.')';
592 }
593 } else {
594 $forms[0] = esc_html__('Create a Form First', 'fluentform');
595 }
596
597 return $forms;
598 }
599
600 public static function replaceBrTag($content, $with = '')
601 {
602 if (is_array($content)) {
603 foreach ($content as $key => $value) {
604 $content[$key] = static::replaceBrTag($value, $with);
605 }
606 } elseif (static::hasBrTag($content)) {
607 $content = str_replace('<br />', $with, $content);
608 }
609
610 return $content;
611 }
612
613 public static function hasBrTag($content)
614 {
615 return is_string($content) && strpos($content, '<br />') !== false;
616 }
617
618 public static function sanitizeForCSV($content)
619 {
620 $formulas = ['=', '-', '+', '@', "\t", "\r"];
621
622 if (Str::startsWith($content, $formulas)) {
623 $content = "'" . $content;
624 }
625
626 return $content;
627 }
628
629 public static function getForm($id)
630 {
631 return wpFluent()->table('fluentform_forms')->where('id', $id)->first();
632 }
633
634 public static function shouldHidePassword($formId)
635 {
636 return apply_filters('fluentform_truncate_password_values', true, $formId) &&
637 (
638 (defined('FLUENTFORM_RENDERING_ENTRIES') && FLUENTFORM_RENDERING_ENTRIES) ||
639 (defined('FLUENTFORM_RENDERING_ENTRY') && FLUENTFORM_RENDERING_ENTRY) ||
640 (defined('FLUENTFORM_EXPORTING_ENTRIES') && FLUENTFORM_EXPORTING_ENTRIES)
641 );
642 }
643 }
644