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

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

604 lines 17.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_core', [
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_forms_add_ons',
303 'fluent_forms_docs',
304 'fluent_forms_payment_entries',
305 'fluent_forms_smtp'
306 ];
307
308 $status = true;
309
310 if (!isset($_GET['page']) || !in_array($_GET['page'], $fluentPages)) {
311 $status = false;
312 }
313
314 return apply_filters('fluentform_is_admin_page', $status);
315 }
316
317 public static function getShortCodeIds($content, $tag = 'fluentform', $selector = 'id')
318 {
319
320 if (false === strpos($content, '[')) {
321 return [];
322 }
323
324 preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER);
325 if (empty($matches)) {
326 return [];
327 }
328
329 $ids = [];
330
331 foreach ($matches as $shortcode) {
332 if (count($shortcode) >= 2 && $tag === $shortcode[2]) {
333 // Replace braces with empty string.
334 $parsedCode = str_replace(['[', ']', '&#91;', '&#93;'], '', $shortcode[0]);
335
336 $result = shortcode_parse_atts($parsedCode);
337
338 if (!empty($result[$selector])) {
339 $ids[$result[$selector]] = $result[$selector];
340 }
341 }
342 }
343
344 return $ids;
345 }
346
347 public static function isTabIndexEnabled()
348 {
349 if (static::$tabIndexStatus == 'na') {
350 $globalSettings = get_option('_fluentform_global_form_settings');
351 static::$tabIndexStatus = ArrayHelper::get($globalSettings, 'misc.tabIndex') == 'yes';
352 }
353 return static::$tabIndexStatus;
354 }
355
356 public static function isMultiStepForm($formId)
357 {
358 $form = wpFluent()->table('fluentform_forms')->find($formId);
359 $fields = json_decode($form->form_fields, true);
360
361 if (ArrayHelper::get($fields, 'stepsWrapper')) {
362 return true;
363 }
364 return false;
365 }
366
367 public static function hasFormElement($formId, $elementName)
368 {
369 $form = wpFluent()->table('fluentform_forms')->find($formId);
370 $fieldsJson = $form->form_fields;
371 return strpos($fieldsJson, '"element":"' . $elementName . '"') != false;
372 }
373
374 public static function isUniqueValidation($validation, $field, $formData, $fields, $form)
375 {
376 if (ArrayHelper::get($field, 'raw.settings.is_unique') == 'yes') {
377 $fieldName = ArrayHelper::get($field, 'name');
378 if ($inputValue = ArrayHelper::get($formData, $fieldName)) {
379 $exist = wpFluent()->table('fluentform_entry_details')
380 ->where('form_id', $form->id)
381 ->where('field_name', $fieldName)
382 ->where('field_value', $inputValue)
383 ->first();
384 if ($exist) {
385 return [
386 'unique' => ArrayHelper::get($field, 'raw.settings.unique_validation_message')
387 ];
388 }
389 }
390 }
391
392 return $validation;
393 }
394
395 public static function getNumericFormatters()
396 {
397 return apply_filters('fluentform_numeric_styles', array(
398 'none' => array(
399 'value' => '',
400 'label' => 'None'
401 ),
402 'comma_dot_style' => array(
403 'value' => 'comma_dot_style',
404 'label' => __('US Style with Decimal (EX: 123,456.00)', 'fluentform'),
405 'settings' => [
406 'decimal' => '.',
407 'separator' => ',',
408 'precision' => 2,
409 'symbol' => ''
410 ]
411 ),
412 'dot_comma_style_zero' => array(
413 'value' => 'dot_comma_style_zero',
414 'label' => __('US Style without Decimal (Ex: 123,456,789)', 'fluentform'),
415 'settings' => [
416 'decimal' => '.',
417 'separator' => ',',
418 'precision' => 0,
419 'symbol' => ''
420 ]
421 ),
422 'dot_comma_style' => array(
423 'value' => 'dot_comma_style',
424 'label' => __('EU Style with Decimal (Ex: 123.456,00)', 'fluentform'),
425 'settings' => [
426 'decimal' => ',',
427 'separator' => '.',
428 'precision' => 2,
429 'symbol' => ''
430 ]
431 ),
432 'comma_dot_style_zero' => array(
433 'value' => 'comma_dot_style_zero',
434 'label' => __('EU Style without Decimal (EX: 123.456.789)', 'fluentform'),
435 'settings' => [
436 'decimal' => ',',
437 'separator' => '.',
438 'precision' => 0,
439 'symbol' => ''
440 ]
441 )
442 ));
443 }
444
445 public static function getNumericValue($input, $formatterName)
446 {
447 $formatters = self::getNumericFormatters();
448 if (empty($formatters[$formatterName]['settings'])) {
449 return $input;
450 }
451 $settings = $formatters[$formatterName]['settings'];
452 $number = floatval(str_replace($settings['decimal'], '.', preg_replace('/[^\d' . preg_quote($settings['decimal']) . ']/', '', $input)));
453
454 return number_format($number, $settings['precision'], '.', '');
455 }
456
457 public static function getNumericFormatted($input, $formatterName)
458 {
459 if (!is_numeric($input)) {
460 return $input;
461 }
462 $formatters = self::getNumericFormatters();
463 if (empty($formatters[$formatterName]['settings'])) {
464 return $input;
465 }
466 $settings = $formatters[$formatterName]['settings'];
467 return number_format($input, $settings['precision'], $settings['decimal'], $settings['separator']);
468 }
469
470 public static function getDuplicateFieldNames($fields)
471 {
472 $fields = json_decode($fields, true);
473 $items = $fields['fields'];
474 $inputNames = self::getFieldNamesStatuses($items);
475 $uniqueNames = array_unique($inputNames);
476
477 if (count($inputNames) == count($uniqueNames)) {
478 return [];
479 }
480
481 return array_diff_assoc($inputNames, $uniqueNames);
482 }
483
484 protected static function getFieldNamesStatuses($fields)
485 {
486 $names = [];
487
488 foreach ($fields as $field) {
489 if (ArrayHelper::get($field, 'element') == 'container') {
490 $columns = ArrayHelper::get($field, 'columns', []);
491 foreach ($columns as $column) {
492 $columnInputs = self::getFieldNamesStatuses(ArrayHelper::get($column, 'fields', []));
493 $names = array_merge($names, $columnInputs);
494 }
495 } else {
496 if ($name = ArrayHelper::get($field, 'attributes.name')) {
497 $names[] = $name;
498 }
499 }
500 }
501
502 return $names;
503 }
504
505 public static function isConversionForm($formId)
506 {
507 static $cache = [];
508 if (isset($cache[$formId])) {
509 return $cache[$formId];
510 }
511
512 $cache[$formId] = self::getFormMeta($formId, 'is_conversion_form') == 'yes';
513 return $cache[$formId];
514 }
515
516 public static function getPreviewUrl($formId, $type = '')
517 {
518 if ($type == 'conversational') {
519 return self::getConversionUrl($formId);
520 } else if ($type == 'classic') {
521 return site_url('?fluent_forms_pages=1&design_mode=1&preview_id=' . $formId) . '#ff_preview';
522 } else {
523 if (self::isConversionForm($formId)) {
524 return self::getConversionUrl($formId);
525 }
526 }
527
528 return site_url('?fluent_forms_pages=1&design_mode=1&preview_id=' . $formId) . '#ff_preview';
529 }
530
531 public static function getFormAdminPermalink($route, $form)
532 {
533 $baseUrl = admin_url('admin.php?page=fluent_forms');
534 return $baseUrl . '&route=' . $route . '&form_id=' . $form->id;
535 }
536
537 public static function getFormSettingsUrl($form)
538 {
539 $baseUrl = admin_url('admin.php?page=fluent_forms');
540 return $baseUrl . '&form_id=' . $form->id . '&route=settings&sub_route=form_settings#basic_settings';
541 }
542
543 private static function getConversionUrl($formId)
544 {
545 $meta = self::getFormMeta($formId, 'ffc_form_settings_meta', []);
546 $key = ArrayHelper::get($meta, 'share_key', '');
547 $paramKey = apply_filters('fluentform_conversational_url_slug', 'fluent-form');
548 if($paramKey == 'form') {
549 $paramKey = 'fluent-form';
550 }
551 if ($key) {
552 return site_url('?'.$paramKey.'=' . $formId . '&form=' . $key);
553 }
554 return site_url('?'.$paramKey.'=' . $formId);
555 }
556
557 public static function fileUploadLocations()
558 {
559 $locations = array(
560 array(
561 'value' => 'default',
562 'label' => __('Fluentforms Default', 'fluentform'),
563 ),
564 array(
565 'value' => 'wp_media',
566 'label' => __('Media Library', 'fluentform'),
567 ),
568 );
569
570 return apply_filters('fluentform_file_upload_options', $locations);
571
572 }
573
574 private function unreadCount($formId)
575 {
576 return wpFluent()->table('fluentform_submissions')
577 ->where('status', 'unread')
578 ->where('form_id', $formId)
579 ->count();
580 }
581
582 public static function getForms()
583 {
584 $ff_list = wpFluent()->table('fluentform_forms')
585 ->select(['id', 'title'])
586 ->orderBy('id', 'DESC')
587 ->get();
588
589
590 $forms = array();
591
592 if ($ff_list) {
593 $forms[0] = esc_html__('Select a Fluent Forms', 'fluentform');
594 foreach ($ff_list as $form) {
595 $forms[$form->id] = $form->title .' ('.$form->id.')';
596 }
597 } else {
598 $forms[0] = esc_html__('Create a Form First', 'fluentform');
599 }
600
601 return $forms;
602 }
603 }
604