| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Migration; |
| 4 | 4 | |
| 5 | -class MigrationHelper { | |
| 6 | - public static function setNestedProperty(&$rootObj, $properties, $value) { | |
| 5 | +class MigrationHelper | |
| 6 | +{ | |
| 7 | + public static function setNestedProperty(&$rootObj, $properties, $value) | |
| 8 | + { | |
| 7 | 9 | $firstProperty = array_shift($properties); |
| 8 | 10 | $current = &$rootObj; |
| 9 | 11 | |
| 10 | 12 | if (is_object($current)) { |
| @@ -27,9 +29,10 @@ | ||
| 27 | 29 | } |
| 28 | 30 | } |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | - public static function getNestedPropertyValue($rootObj, $properties) { | |
| 33 | + public static function getNestedPropertyValue($rootObj, $properties) | |
| 34 | + { | |
| 32 | 35 | $firstProperty = array_shift($properties); |
| 33 | 36 | $current = $rootObj; |
| 34 | 37 | if (is_object($current) && count($properties) > 0) { |
| 35 | 38 | return self::getNestedPropertyValue($current->$firstProperty, $properties); |
| @@ -41,9 +44,10 @@ | ||
| 41 | 44 | return array_key_exists($firstProperty, $current) ? $current[$firstProperty] : null; |
| 42 | 45 | } |
| 43 | 46 | } |
| 44 | 47 | |
| 45 | - public static function optionListConvert($optionList) { | |
| 48 | + public static function optionListConvert($optionList) | |
| 49 | + { | |
| 46 | 50 | $newOptionList = []; |
| 47 | 51 | foreach ($optionList as $option) { |
| 48 | 52 | $newOpt = (object)[]; |
| 49 | 53 | if (isset($option->label)) { |
| @@ -62,9 +66,10 @@ | ||
| 62 | 66 | } |
| 63 | 67 | return $newOptionList; |
| 64 | 68 | } |
| 65 | 69 | |
| 66 | - public static function convertFields($fieldsArray) { | |
| 70 | + public static function convertFields($fieldsArray) | |
| 71 | + { | |
| 67 | 72 | $conversionFormate = new FieldsConversionFormate(); |
| 68 | 73 | $newFieldsArray = []; |
| 69 | 74 | |
| 70 | 75 | foreach ($fieldsArray as $fieldKey => $field) { |
| @@ -69,8 +74,13 @@ | ||
| 69 | 74 | |
| 70 | 75 | foreach ($fieldsArray as $fieldKey => $field) { |
| 71 | 76 | // print_r(gettype($field)); |
| 72 | 77 | $convertFormate = $conversionFormate->getFormate($field->typ); |
| 78 | + // Unknown field type: keep the field as-is instead of fataling the migration | |
| 79 | + if (empty($convertFormate)) { | |
| 80 | + $newFieldsArray[$fieldKey] = $field; | |
| 81 | + continue; | |
| 82 | + } | |
| 73 | 83 | $newFieldProperty = []; |
| 74 | 84 | |
| 75 | 85 | // convert unchange property |
| 76 | 86 | foreach ($convertFormate['unchangeProperty'] as $property) { |
| @@ -120,9 +130,10 @@ | ||
| 120 | 130 | |
| 121 | 131 | return $newFieldsArray; |
| 122 | 132 | } |
| 123 | 133 | |
| 124 | - public static function convertLayout($layoutObj) { | |
| 134 | + public static function convertLayout($layoutObj) | |
| 135 | + { | |
| 125 | 136 | $newLayoutArray = []; |
| 126 | 137 | |
| 127 | 138 | foreach ($layoutObj as $breakdownSize => $layoutArr) { |
| 128 | 139 | // v1 width variation |
| @@ -146,9 +157,10 @@ | ||
| 146 | 157 | } |
| 147 | 158 | return $newLayoutArray; |
| 148 | 159 | } |
| 149 | 160 | |
| 150 | - public static function convertSuccessMessage($successMessageArr) { | |
| 161 | + public static function convertSuccessMessage($successMessageArr) | |
| 162 | + { | |
| 151 | 163 | $newSuccessMessageArr = []; |
| 152 | 164 | |
| 153 | 165 | // this config is set as snackbar in bottom right corner |
| 154 | 166 | $defaultMessageConfig = [ |
| @@ -188,11 +200,40 @@ | ||
| 188 | 200 | } |
| 189 | 201 | return $newSuccessMessageArr; |
| 190 | 202 | } |
| 191 | 203 | |
| 192 | - public static function recursiveDeleteFiles($src) { | |
| 204 | + public static function convertWebHooksToIntegrations($formSettingsV1) | |
| 205 | + { | |
| 206 | + $webHooks = $formSettingsV1->confirmation->type->webHooks; | |
| 207 | + $integrations = $formSettingsV1->integrations; | |
| 208 | + foreach ($webHooks as $webHook) { | |
| 209 | + $webHook->type = 'Web Hooks'; | |
| 210 | + $webHook->name = $webHook->title; | |
| 211 | + $integrations[] = $webHook; | |
| 212 | + } | |
| 213 | + return $integrations; | |
| 214 | + } | |
| 215 | + | |
| 216 | + public static function duplicateV1StyleFiles() | |
| 217 | + { | |
| 218 | + $source = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles'; | |
| 219 | + $destination = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'; | |
| 220 | + if (!file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1')) { | |
| 221 | + wp_mkdir_p(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles-v1'); | |
| 222 | + } | |
| 223 | + self::recursiveCopyFiles($source, $destination); | |
| 224 | + self::recursiveDeleteFiles($source); | |
| 225 | + } | |
| 226 | + | |
| 227 | + public static function getBitformDefaultStyleClass() | |
| 228 | + { | |
| 229 | + return self::arrayToCssString(BitformDefaultStyle::commonStyleClasses()); | |
| 230 | + } | |
| 231 | + | |
| 232 | + public static function recursiveDeleteFiles($src) | |
| 233 | + { | |
| 193 | 234 | if (is_file($src)) { |
| 194 | - return unlink($src); | |
| 235 | + return wp_delete_file($src); | |
| 195 | 236 | } elseif (is_dir($src)) { |
| 196 | 237 | $scan = glob(rtrim($src, '/') . '/*'); |
| 197 | 238 | foreach ($scan as $path) { |
| 198 | 239 | self::recursiveDeleteFiles($path); |
| @@ -199,9 +240,10 @@ | ||
| 199 | 240 | } |
| 200 | 241 | } |
| 201 | 242 | } |
| 202 | 243 | |
| 203 | - public static function recursiveCopyFiles($src, $dst) { | |
| 244 | + public static function recursiveCopyFiles($src, $dst) | |
| 245 | + { | |
| 204 | 246 | if (is_file($src) && !is_dir($dst)) { |
| 205 | 247 | return copy($src, $dst); |
| 206 | 248 | } elseif (is_dir($src)) { |
| 207 | 249 | $scan = glob(rtrim($src, '/') . '/*'); |
| @@ -208,6 +250,43 @@ | ||
| 208 | 250 | foreach ($scan as $path) { |
| 209 | 251 | self::recursiveCopyFiles($path, $dst . '/' . basename($path)); |
| 210 | 252 | } |
| 211 | 253 | } |
| 254 | + } | |
| 255 | + | |
| 256 | + public static function arrayToCssString($array) | |
| 257 | + { | |
| 258 | + $cssString = ''; | |
| 259 | + | |
| 260 | + foreach ($array as $selector => $properties) { | |
| 261 | + $cssString .= $selector . ' {'; | |
| 262 | + | |
| 263 | + foreach ($properties as $property => $value) { | |
| 264 | + $cssString .= $property . ': ' . $value . '; '; | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Remove the trailing space and add closing brace | |
| 268 | + $cssString = rtrim($cssString, ' ') . '}'; | |
| 269 | + } | |
| 270 | + | |
| 271 | + return $cssString; | |
| 272 | + } | |
| 273 | + | |
| 274 | + public static function formatFormData($formData) | |
| 275 | + { | |
| 276 | + $newFormData = json_decode(wp_json_encode($formData)); | |
| 277 | + | |
| 278 | + $formContent = json_decode(wp_json_encode($formData->form_content)); | |
| 279 | + | |
| 280 | + $newFormData->fields = $formContent->fields; | |
| 281 | + $newFormData->layout = $formContent->layout; | |
| 282 | + $newFormData->nestedLayouts = $formContent->nestedLayout; | |
| 283 | + $newFormData->formInfo = $formContent->formInfo; | |
| 284 | + | |
| 285 | + if (is_array($formData->reports) && count($formData->reports) > 0) { | |
| 286 | + $newFormData->currentReport = $formData->reports[0]; | |
| 287 | + $newFormData->report_id = $formData->reports[0]->id; | |
| 288 | + } | |
| 289 | + | |
| 290 | + return $newFormData; | |
| 212 | 291 | } |
| 213 | 292 | } |