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 / Helpers / Helper.php

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

652 lines 19.5 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 public static function isSlackEnabled()
88 {
89 $globalModules = get_option('fluentform_global_modules_status');
90 return $globalModules && isset($globalModules['slack']) && 'yes' == $globalModules['slack'];
91 }
92
93 public static function getEntryStatuses($form_id = false)
94 {
95 $statuses = apply_filters('fluentform_entry_statuses_core', [
96 'unread' => 'Unread',
97 'read' => 'Read',
98 ], $form_id);
99 $statuses['trashed'] = 'Trashed';
100 return $statuses;
101 }
102
103 public static function getReportableInputs()
104 {
105 return apply_filters('fluentform_reportable_inputs', [
106 'select',
107 'input_radio',
108 'input_checkbox',
109 'ratings',
110 'net_promoter',
111 'select_country',
112 'net_promoter_score',
113 ]);
114 }
115
116 public static function getSubFieldReportableInputs()
117 {
118 return apply_filters('fluentform_subfield_reportable_inputs', [
119 'tabular_grid',
120 ]);
121 }
122
123 public static function getFormMeta($formId, $metaKey, $default = '')
124 {
125 $meta = wpFluent()->table('fluentform_form_meta')
126 ->where('meta_key', $metaKey)
127 ->where('form_id', $formId)
128 ->first();
129
130 if (!$meta || !$meta->value) {
131 return $default;
132 }
133
134 $metaValue = $meta->value;
135 // decode the JSON data
136 $result = json_decode($metaValue, true);
137
138 if (json_last_error() == JSON_ERROR_NONE) {
139 return $result;
140 }
141 return $metaValue;
142 }
143
144 public static function setFormMeta($formId, $metaKey, $value)
145 {
146 $meta = wpFluent()->table('fluentform_form_meta')
147 ->where('meta_key', $metaKey)
148 ->where('form_id', $formId)
149 ->first();
150
151 if (is_array($value) || is_object($value)) {
152 $value = json_encode($value);
153 }
154
155 if (!$meta) {
156 $insetid = wpFluent()->table('fluentform_form_meta')
157 ->insert([
158 'meta_key' => $metaKey,
159 'form_id' => $formId,
160 'value' => $value,
161 ]);
162 return $insetid;
163 } else {
164 wpFluent()->table('fluentform_form_meta')
165 ->where('id', $meta->id)
166 ->update([
167 'value' => $value,
168 ]);
169 }
170
171 return $meta->id;
172 }
173
174 public static function getSubmissionMeta($submissionId, $metaKey, $default = false)
175 {
176 $meta = wpFluent()->table('fluentform_submission_meta')
177 ->where('response_id', $submissionId)
178 ->where('meta_key', $metaKey)
179 ->first();
180
181 if ($meta && $meta->value) {
182 return maybe_unserialize($meta->value);
183 }
184
185 return $default;
186 }
187
188 public static function setSubmissionMeta($submissionId, $metaKey, $value, $formId = false)
189 {
190 $value = maybe_serialize($value);
191
192 // check if submission exist
193 $meta = wpFluent()->table('fluentform_submission_meta')
194 ->where('response_id', $submissionId)
195 ->where('meta_key', $metaKey)
196 ->first();
197
198 if ($meta) {
199 wpFluent()->table('fluentform_submission_meta')
200 ->where('id', $meta->id)
201 ->insert([
202 'value' => $value,
203 'updated_at' => current_time('mysql'),
204 ]);
205 return $meta->id;
206 }
207
208 if (!$formId) {
209 $submission = wpFluent()->table('fluentform_submissions')
210 ->find($submissionId);
211 if ($submission) {
212 $formId = $submission->form_id;
213 }
214 }
215
216 return wpFluent()->table('fluentform_submission_meta')
217 ->insert([
218 'response_id' => $submissionId,
219 'form_id' => $formId,
220 'meta_key' => $metaKey,
221 'value' => $value,
222 'created_at' => current_time('mysql'),
223 'updated_at' => current_time('mysql'),
224 ]);
225 }
226
227 public static function isEntryAutoDeleteEnabled($formId)
228 {
229 $settings = wpFluent()->table('fluentform_form_meta')
230 ->where('form_id', $formId)
231 ->where('meta_key', 'formSettings')
232 ->first();
233
234 if (!$settings) {
235 return false;
236 }
237
238 $formSettings = json_decode($settings->value, true);
239
240 if ($formSettings && ArrayHelper::get($formSettings, 'delete_entry_on_submission') == 'yes') {
241 return true;
242 }
243
244 return false;
245 }
246
247 public static function formExtraCssClass($form)
248 {
249 if (!$form->settings) {
250 $settings = wpFluent()->table('fluentform_form_meta')
251 ->where('form_id', $form->id)
252 ->where('meta_key', 'formSettings')
253 ->first();
254
255 $formSettings = json_decode($settings->value, true);
256 } else {
257 $formSettings = $form->settings;
258 }
259
260 if (!$formSettings) {
261 return '';
262 }
263
264 if ($formSettings && $extraClass = ArrayHelper::get($formSettings, 'form_extra_css_class')) {
265 return esc_attr($extraClass);
266 }
267
268 return '';
269 }
270
271 public static function getNextTabIndex($increment = 1)
272 {
273 if (self::isTabIndexEnabled()) {
274 static::$tabIndex += $increment;
275 return static::$tabIndex;
276 }
277 return '';
278 }
279
280 public static function getFormInstaceClass($formId)
281 {
282 static::$formInstance += 1;
283 return 'ff_form_instance_' . $formId . '_' . static::$formInstance;
284 }
285
286 public static function resetTabIndex()
287 {
288 static::$tabIndex = 0;
289 }
290
291 public static function isFluentAdminPage()
292 {
293 $fluentPages = [
294 'fluent_forms',
295 'fluent_forms_all_entries',
296 'fluent_forms_transfer',
297 'fluent_forms_settings',
298 'fluent_forms_add_ons',
299 'fluent_forms_docs',
300 'fluent_forms_payment_entries',
301 'fluent_forms_smtp',
302 ];
303
304 $status = true;
305
306 $page = wpFluentForm('request')->get('page');
307
308 if (!$page || !in_array($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 ('na' == static::$tabIndexStatus) {
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', [
395 'none' => [
396 'value' => '',
397 'label' => 'None',
398 ],
399 'comma_dot_style' => [
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' => [
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' => [
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' => [
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 ('conversational' == $type) {
516 return self::getConversionUrl($formId);
517 } elseif ('classic' == $type) {
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 ('form' == $paramKey) {
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 = [
557 [
558 'value' => 'default',
559 'label' => __('Fluentforms Default', 'fluentform'),
560 ],
561 [
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 $forms = [];
586
587 if ($ff_list) {
588 $forms[0] = esc_html__('Select a Fluent Forms', 'fluentform');
589 foreach ($ff_list as $form) {
590 $forms[$form->id] = esc_html($form->title) . ' (' . $form->id . ')';
591 }
592 } else {
593 $forms[0] = esc_html__('Create a Form First', 'fluentform');
594 }
595
596 return $forms;
597 }
598
599 public static function replaceBrTag($content, $with = '')
600 {
601 if (is_array($content)) {
602 foreach ($content as $key => $value) {
603 $content[$key] = static::replaceBrTag($value, $with);
604 }
605 } elseif (static::hasBrTag($content)) {
606 $content = str_replace('<br />', $with, $content);
607 }
608
609 return $content;
610 }
611
612 public static function hasBrTag($content)
613 {
614 return is_string($content) && strpos($content, '<br />') !== false;
615 }
616
617 public static function sanitizeForCSV($content)
618 {
619 $formulas = ['=', '-', '+', '@', "\t", "\r"];
620
621 if (Str::startsWith($content, $formulas)) {
622 $content = "'" . $content;
623 }
624
625 return $content;
626 }
627
628 public static function getForm($id)
629 {
630 return wpFluent()->table('fluentform_forms')->where('id', $id)->first();
631 }
632
633 public static function shouldHidePassword($formId)
634 {
635 return apply_filters('fluentform_truncate_password_values', true, $formId) &&
636 (
637 (defined('FLUENTFORM_RENDERING_ENTRIES') && FLUENTFORM_RENDERING_ENTRIES) ||
638 (defined('FLUENTFORM_RENDERING_ENTRY') && FLUENTFORM_RENDERING_ENTRY) ||
639 (defined('FLUENTFORM_EXPORTING_ENTRIES') && FLUENTFORM_EXPORTING_ENTRIES)
640 );
641 }
642
643 public static function getInputNameFromShortCode($value)
644 {
645 preg_match('/{+(.*?)}/', $value, $matches);
646 if ($matches && strpos($matches[1], 'inputs.') !== false) {
647 return substr($matches[1], strlen('inputs.'));
648 }
649 return '';
650 }
651 }
652