PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.2
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 / Migration / MigrationHelper.php

MigrationHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.2, at includes/Core/Migration/MigrationHelper.php

288 lines 9.1 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\Migration;
4
5 class MigrationHelper
6 {
7 public static function setNestedProperty(&$rootObj, $properties, $value)
8 {
9 $firstProperty = array_shift($properties);
10 $current = &$rootObj;
11
12 if (is_object($current)) {
13 if (!property_exists($current, $firstProperty)) {
14 $current->$firstProperty = (object)[];
15 }
16 if (count($properties) > 0) {
17 self::setNestedProperty($current->$firstProperty, $properties, $value);
18 } else {
19 $current->$firstProperty = $value;
20 }
21 } elseif (is_array($current)) {
22 if (!array_key_exists($firstProperty, $current)) {
23 $current[$firstProperty] = [];
24 }
25 if (count($properties) > 0) {
26 self::setNestedProperty($current[$firstProperty], $properties, $value);
27 } else {
28 $current[$firstProperty] = $value;
29 }
30 }
31 }
32
33 public static function getNestedPropertyValue($rootObj, $properties)
34 {
35 $firstProperty = array_shift($properties);
36 $current = $rootObj;
37 if (is_object($current) && count($properties) > 0) {
38 return self::getNestedPropertyValue($current->$firstProperty, $properties);
39 } elseif (is_object($current)) {
40 return property_exists($current, $firstProperty) ? $current->$firstProperty : null;
41 } elseif (is_array($current) && count($properties) > 0) {
42 return self::getNestedPropertyValue($current[$firstProperty], $properties);
43 } elseif (is_array($current)) {
44 return array_key_exists($firstProperty, $current) ? $current[$firstProperty] : null;
45 }
46 }
47
48 public static function optionListConvert($optionList)
49 {
50 $newOptionList = [];
51 foreach ($optionList as $option) {
52 $newOpt = (object)[];
53 if (isset($option->label)) {
54 $newOpt->lbl = $option->label;
55 }
56 if (isset($option->value)) {
57 $newOpt->val = $option->value;
58 }
59 if (isset($option->code)) {
60 $newOpt->i = $option->code;
61 }
62 if (isset($option->prefix_img)) {
63 $newOpt->img = str_replace('/v1', '', BITFORMS_ASSET_URI . $option->prefix_img);
64 }
65 $newOptionList[] = $newOpt;
66 }
67 return $newOptionList;
68 }
69
70 public static function convertFields($fieldsArray)
71 {
72 $conversionFormate = new FieldsConversionFormate();
73 $newFieldsArray = [];
74
75 foreach ($fieldsArray as $fieldKey => $field) {
76 // print_r(gettype($field));
77 $convertFormate = $conversionFormate->getFormate($field->typ);
78 $newFieldProperty = [];
79
80 // convert unchange property
81 foreach ($convertFormate['unchangeProperty'] as $property) {
82 if (property_exists($field, $property)) {
83 $newFieldProperty[$property] = $field->{$property};
84 }
85 }
86
87 // add new property
88 foreach ($convertFormate['newProperty'] as $property => $value) {
89 $properties = explode('->', $property);
90 self::setNestedProperty($newFieldProperty, $properties, $value);
91 }
92
93 // modify changed property
94 foreach ($convertFormate['changedProperty'] as $oldProperty => $newPropertyArr) {
95 $properties = explode('->', $oldProperty);
96 $value = self::getNestedPropertyValue($field, $properties);
97 foreach ($newPropertyArr as $newProperty) {
98 if (is_null($value) && isset($newProperty['val'])) {
99 $value = $newProperty['val'];
100 }
101 if (isset($newProperty['isEqualTo']) && $value === $newProperty['isEqualTo']) {
102 $value = $newProperty['val'];
103 } elseif (isset($newProperty['isEqualTo'])) {
104 continue;
105 }
106 if (!is_null($value)) {
107 $properties = explode('->', $newProperty['path']);
108 self::setNestedProperty($newFieldProperty, $properties, $value);
109 }
110 }
111 }
112 $newFieldProperty['fieldName'] .= "-$fieldKey";
113
114 if ('select' === $newFieldProperty['typ']) {
115 $newFieldProperty['optionsList'][0]['List-1'] = self::optionListConvert($field->opt);
116 if (isset($newFieldProperty['val']) && is_array($newFieldProperty['val'])) {
117 $newFieldProperty['val'] = implode(BITFORMS_BF_SEPARATOR, $newFieldProperty['val']);
118 }
119 if (isset($field->opt[0]->prefix_img) && !is_null($field->opt[0]->prefix_img)) {
120 $newFieldProperty['config']['optionIcon'] = true;
121 }
122 }
123 $newFieldsArray[$fieldKey] = $newFieldProperty;
124 }
125
126 return $newFieldsArray;
127 }
128
129 public static function convertLayout($layoutObj)
130 {
131 $newLayoutArray = [];
132
133 foreach ($layoutObj as $breakdownSize => $layoutArr) {
134 // v1 width variation
135 $v1WidthVariation = [
136 'lg' => 6,
137 'md' => 4,
138 'sm' => 2,
139 ];
140 $v2Width = 60;
141 $v2RowHeight = 43;
142
143 $newLayoutArray[$breakdownSize] = [];
144 foreach ($layoutArr as $index => $fieldLayout) {
145 $fieldLayout->w = $fieldLayout->w * ($v2Width / $v1WidthVariation[$breakdownSize]);
146 $fieldLayout->x = $fieldLayout->x * ($v2Width / $v1WidthVariation[$breakdownSize]);
147 $fieldLayout->h = $fieldLayout->h * $v2RowHeight;
148 $fieldLayout->y = $fieldLayout->y * $v2RowHeight;
149 unset($fieldLayout->minH, $fieldLayout->maxH);
150 $newLayoutArray[$breakdownSize][] = $fieldLayout;
151 }
152 }
153 return $newLayoutArray;
154 }
155
156 public static function convertSuccessMessage($successMessageArr)
157 {
158 $newSuccessMessageArr = [];
159
160 // this config is set as snackbar in bottom right corner
161 $defaultMessageConfig = [
162 'msgType' => 'snackbar',
163 'position' => 'bottom-right',
164 'animation'=> 'slide-up',
165 'autoHide' => true,
166 'duration' => 3,
167 'styles' => [
168 'width' => '300px',
169 'padding' => '10px 35px 10px 20px',
170 'background' => '#383838',
171 'color' => '#fff',
172 'borderWidth' => '1px',
173 'borderType' => 'none',
174 'borderColor' => 'var(--bg-5)',
175 'borderRadius'=> 'var(--g-bdr-rad)',
176 'boxShadow' => [
177 [
178 'x' => '0px',
179 'y' => '27px',
180 'blur' => '30px',
181 'spread'=> '',
182 'color' => 'rgb(0 0 0 / 18%)',
183 'inset' => ''
184 ]
185 ],
186 'closeBackground'=> '#666',
187 'closeHover' => 'var(--bg-10)',
188 'closeIconColor' => '#fff',
189 'closeIconHover' => 'var(--bg-50)'
190 ]
191 ];
192 foreach ($successMessageArr as $message) {
193 $message->config = $defaultMessageConfig;
194 $newSuccessMessageArr[] = $message;
195 }
196 return $newSuccessMessageArr;
197 }
198
199 public static function convertWebHooksToIntegrations($formSettingsV1)
200 {
201 $webHooks = $formSettingsV1->confirmation->type->webHooks;
202 $integrations = $formSettingsV1->integrations;
203 foreach ($webHooks as $webHook) {
204 $webHook->type = 'Web Hooks';
205 $webHook->name = $webHook->title;
206 $integrations[] = $webHook;
207 }
208 return $integrations;
209 }
210
211 public static function duplicateV1StyleFiles()
212 {
213 $source = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles';
214 $destination = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1';
215 if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1')) {
216 wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1');
217 }
218 self::recursiveCopyFiles($source, $destination);
219 self::recursiveDeleteFiles($source);
220 }
221
222 public static function getBitformDefaultStyleClass()
223 {
224 return self::arrayToCssString(BitformDefaultStyle::commonStyleClasses());
225 }
226
227 public static function recursiveDeleteFiles($src)
228 {
229 if (is_file($src)) {
230 return wp_delete_file($src);
231 } elseif (is_dir($src)) {
232 $scan = glob(rtrim($src, '/') . '/*');
233 foreach ($scan as $path) {
234 self::recursiveDeleteFiles($path);
235 }
236 }
237 }
238
239 public static function recursiveCopyFiles($src, $dst)
240 {
241 if (is_file($src) && !is_dir($dst)) {
242 return copy($src, $dst);
243 } elseif (is_dir($src)) {
244 $scan = glob(rtrim($src, '/') . '/*');
245 foreach ($scan as $path) {
246 self::recursiveCopyFiles($path, $dst . '/' . basename($path));
247 }
248 }
249 }
250
251 public static function arrayToCssString($array)
252 {
253 $cssString = '';
254
255 foreach ($array as $selector => $properties) {
256 $cssString .= $selector . ' {';
257
258 foreach ($properties as $property => $value) {
259 $cssString .= $property . ': ' . $value . '; ';
260 }
261
262 // Remove the trailing space and add closing brace
263 $cssString = rtrim($cssString, ' ') . '}';
264 }
265
266 return $cssString;
267 }
268
269 public static function formatFormData($formData)
270 {
271 $newFormData = json_decode(wp_json_encode($formData));
272
273 $formContent = json_decode(wp_json_encode($formData->form_content));
274
275 $newFormData->fields = $formContent->fields;
276 $newFormData->layout = $formContent->layout;
277 $newFormData->nestedLayouts = $formContent->nestedLayout;
278 $newFormData->formInfo = $formContent->formInfo;
279
280 if (is_array($formData->reports) && count($formData->reports) > 0) {
281 $newFormData->currentReport = $formData->reports[0];
282 $newFormData->report_id = $formData->reports[0]->id;
283 }
284
285 return $newFormData;
286 }
287 }
288