PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.3
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / MetaBox / MetaBoxHelper.php

MetaBoxHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.0.3, at includes/Core/Integration/MetaBox/MetaBoxHelper.php

127 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\MetaBox;
4
5 use BitCode\BitForm\Core\Util\WpFileHandler;
6
7 class MetaBoxHelper
8 {
9 public static function metaBoxFieldMapping($metaBoxMapField, $fieldValues, $metaBoxFieldInfo, $postId, $action)
10 {
11 $selectedTypes = ['select', 'checkbox_list', 'select_advanced', 'user', 'post'];
12
13 $metaBoxMappedData = [];
14
15 foreach ($metaBoxMapField as $fieldPair) {
16 $metaBoxFieldKeys = explode('.', $fieldPair->metaboxField);
17
18 $fieldName = $fieldPair->formField;
19
20 if ('custom' === $fieldName && isset($fieldPair->customValue)) {
21 $fieldValue = $fieldPair->customValue;
22 } elseif (isset($fieldValues[$fieldName])) {
23 $fieldValue = $fieldValues[$fieldName];
24 }
25
26 if (count($metaBoxFieldKeys) > 1) {
27 $metaBoxFieldId = $metaBoxFieldKeys[0];
28 $subFieldId = $metaBoxFieldKeys[1];
29 } else {
30 $metaBoxFieldId = $metaBoxFieldKeys[0];
31 }
32
33 $metaFieldInfo = $metaBoxFieldInfo[$metaBoxFieldId];
34
35 if ('group' === $metaFieldInfo['type'] && isset($subFieldId)) {
36 $metaBoxMappedData = self::setGroupSubFields($fieldName, $metaFieldInfo, $fieldValues, $subFieldId, $metaBoxMappedData);
37 } elseif (in_array($metaFieldInfo['type'], $selectedTypes)) {
38 $values = !is_array($fieldValue) ? explode(',', $fieldValue) : $fieldValue;
39
40 if (true === $metaFieldInfo['multiple'] && is_array($values)) {
41 self::setMultipleTypeFields($values, $postId, $metaBoxFieldId, $action);
42 } else {
43 $metaBoxMappedData[$metaBoxFieldId] = $fieldValue;
44 }
45 } else {
46 $metaBoxMappedData[$metaBoxFieldId] = $fieldValue;
47 }
48 }
49
50 return $metaBoxMappedData;
51 }
52
53 public static function fileUploadMapping($metaboxMapField, $fieldValues, $metaboxFields, $formId, $entryID, $postId)
54 {
55 $fileUploadHandle = new WpFileHandler($formId);
56 foreach ($metaboxMapField as $fieldPair) {
57 if (!property_exists($fieldPair, 'formField')) {
58 continue;
59 }
60
61 if (isset($fieldValues[$fieldPair->formField], $metaboxFields[$fieldPair->metaboxFileUpload])) {
62 $metaInfo = $metaboxFields[$fieldPair->metaboxFileUpload];
63
64 if (false === $metaInfo['multiple']) {
65 $attachMentId = $fileUploadHandle->singleFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId);
66
67 if (!empty($attachMentId)) {
68 add_post_meta($postId, $metaInfo['id'], $attachMentId);
69 }
70 } elseif (true === $metaInfo['multiple']) {
71 $attachMentIds = $fileUploadHandle->multiFileMoveWpMedia($entryID, $fieldValues[$fieldPair->formField], $postId);
72 if (!empty($attachMentIds) && is_array($attachMentIds)) {
73 foreach ($attachMentIds as $attachmentId) {
74 add_post_meta($postId, $metaInfo['id'], $attachmentId);
75 }
76 }
77 }
78 }
79 }
80 }
81
82 private static function setGroupSubFields($fieldName, $metaFieldInfo, $fieldValues, $subFieldId, $metaBoxMappedData)
83 {
84 $formFieldKeys = explode('.', $fieldName);
85
86 if (2 !== count($formFieldKeys)) {
87 return $metaBoxMappedData;
88 }
89
90 $formParentFieldKey = $formFieldKeys[0];
91 $formChildFieldKey = $formFieldKeys[1];
92
93 $metaBoxParentFieldKey = $metaFieldInfo['id'];
94
95 $formRepeaterValues = $fieldValues[$formParentFieldKey];
96
97 if (!isset($metaBoxMappedData[$metaBoxParentFieldKey])) {
98 $metaBoxMappedData[$metaBoxParentFieldKey] = [];
99 }
100
101 foreach ($formRepeaterValues as $repeaterIndex => $values) {
102 $fieldValue = $values[$formChildFieldKey];
103
104 if (!isset($metaBoxMappedData[$metaBoxParentFieldKey][$repeaterIndex])) {
105 $metaBoxMappedData[$metaBoxParentFieldKey][$repeaterIndex] = [];
106 }
107
108 $metaBoxMappedData[$metaBoxParentFieldKey][$repeaterIndex][$subFieldId] = $fieldValue;
109 }
110
111 return $metaBoxMappedData;
112 }
113
114 private static function setMultipleTypeFields($values, $postId, $metaBoxFieldId, $action)
115 {
116 $getMetaValues = get_post_meta($postId, $metaBoxFieldId, false);
117 if ('update' === $action && count($getMetaValues) > 0) {
118 foreach ($getMetaValues as $value) {
119 delete_post_meta($postId, $metaBoxFieldId);
120 }
121 }
122 foreach ($values as $value) {
123 add_post_meta($postId, $metaBoxFieldId, $value);
124 }
125 }
126 }
127