PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.16.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.16.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 / Util / FormDuplicateHelper.php

FormDuplicateHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.16.2, at includes/Core/Util/FormDuplicateHelper.php

178 lines 7.9 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\Util;
4
5 /**
6 * Helps to relpicate form information for form duplication or export
7 *
8 */
9 final class FormDuplicateHelper
10 {
11 /**
12 * Create replica
13 *
14 * @param Array $data Data to replicate
15 * @param Integer $oldFormId Id of the form which will be replicated
16 * @param Integer|String $newFormId Id of the new form
17 *
18 * @return $formData Replicated data
19 */
20 public static function createReplica($data, $oldFormId, $newFormId)
21 {
22 // $new_lay_fields = static::genarateNewLayoutNField($data['form_content']['layout'], $data['form_content']['fields'], $oldFormId, $newFormId);
23 $formName = $data['form_name'];
24 $formData = (object)[];
25 $formData->form_id = $newFormId;
26 $formData->fields = (object) $data['form_content']['fields'];
27 $formData->layout = $data['form_content']['layout'];
28 $formData->nestedLayouts = $data['form_content']['nestedLayout'];
29 $formData->form_name = $formName;
30 $formData->formInfo = isset($data['form_content']['formInfo']) ? $data['form_content']['formInfo'] : (object)[];
31
32 if (isset($data['formSettings'])) {
33 $formSettings = (array) (!is_string($data['formSettings']) ? $data['formSettings'] : json_decode($data['formSettings']));
34
35 if (isset($formSettings['confirmation'])) {
36 $confirmation = !is_string($formSettings['confirmation']) ? wp_json_encode($formSettings['confirmation']) : $formSettings['confirmation'];
37 $formSettings['confirmation'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $confirmation));
38 }
39
40 if (isset($formSettings['integrations'])) {
41 $integrations = !is_string($formSettings['integrations']) ? wp_json_encode($formSettings['integrations']) : $formSettings['integrations'];
42 $integrations = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $integrations);
43 $formSettings['integrations'] = json_decode(\preg_replace('/(\"formField\"\s*\:\s*\"bf?)(\d+)([- \d]+)/', '${1}' . $newFormId . '$3', $integrations));
44 }
45
46 if (isset($formSettings['mailTem'])) {
47 $mailTem = !is_string($formSettings['mailTem']) ? wp_json_encode($formSettings['mailTem']) : $formSettings['mailTem'];
48 $formSettings['mailTem'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $mailTem));
49 $formData->formSettings = (object)$formSettings;
50 }
51
52 if (isset($formSettings['pdfTem'])) {
53 $pdfTem = !is_string($formSettings['pdfTem']) ? wp_json_encode($formSettings['pdfTem']) : $formSettings['pdfTem'];
54 $formSettings['pdfTem'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $pdfTem));
55 $formData->formSettings = (object)$formSettings;
56 }
57 }
58
59 if (isset($data['breakpointSize'])) {
60 $formData->breakpointSize = $data['breakpointSize'];
61 }
62 if (isset($data['style'])) {
63 // $styleTemp = !is_string($data['style']) ? wp_json_encode($data['style']) : $data['style'];
64 // $formData->style = json_decode(\preg_replace('/(\.?b)(\d+)(-\d+)/', '${1}' . $newFormId . '$3', $styleTemp));
65 $formData->style = $data['style'];
66 }
67 if (isset($data['themeVars'])) {
68 $formData->themeVars = $data['themeVars'];
69 }
70 if (isset($data['staticStyles'])) {
71 $formData->staticStyles = $data['staticStyles'];
72 }
73 if (isset($data['themeColors'])) {
74 $formData->themeColors = $data['themeColors'];
75 }
76 if (isset($data['builderSettings'])) {
77 $formData->builderSettings = $data['builderSettings'];
78 }
79
80 if (isset($data['workFlows'])) {
81 $workFlows = !is_string($data['workFlows']) ? wp_json_encode($data['workFlows']) : $data['workFlows'];
82 $workFlows = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $workFlows);
83 $workFlows = json_decode(\preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}' . $newFormId . '$3', $workFlows));
84 $formData->workFlows = $workFlows;
85 }
86
87 if (isset($data['reports'])) {
88 $reports = !is_string($data['reports']) ? wp_json_encode($data['reports']) : $data['reports'];
89 $reports = json_decode(\preg_replace("/(\"bf?)(\d+)([- \d]+\")/", '${1}' . $newFormId . '$3', $reports));
90 $formData->reports = $reports;
91 }
92
93 if (isset($data['additional'])) {
94 $formData->additional = $data['additional'];
95 }
96
97 $formData->customCode = (object) [];
98 if (isset($data['customCode']->customCss)) {
99 $customCss = !is_string($data['customCode']->customCss) ? wp_json_encode($data['customCode']->customCss) : $data['customCode']->customCss;
100 $customCss = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $customCss);
101 $formData->customCode->customCss = \preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}' . $newFormId . '$3', $customCss);
102 }
103
104 if (isset($data['customCode']->customJs)) {
105 $customJs = !is_string($data['customCode']->customJs) ? wp_json_encode($data['customCode']->customJs) : $data['customCode']->customJs;
106 $customJs = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $customJs);
107 $formData->customCode->customJs = \preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}' . $newFormId . '$3', $customJs);
108 }
109 return $formData;
110 }
111
112 public static function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
113 {
114 $newField = (object) [];
115 foreach ((array)$layout->lg as $ind => $itm) {
116 $fld_tmp = $fields->{$layout->lg[$ind]->i};
117 $layout->lg[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->lg[$ind]->i);
118 $layout->md[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->md[$ind]->i);
119 $layout->sm[$ind]->i = \preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", $layout->sm[$ind]->i);
120 $newField->{$layout->lg[$ind]->i} = json_decode(\preg_replace('/bf?(\d+)(-\d+)/', "b$newId$2", wp_json_encode($fld_tmp)));
121 }
122 return ['layout' => $layout, 'fields' => $newField];
123 }
124
125 public static function calcRowHeight($formStyle, $formID)
126 {
127 $rowHeight = 0;
128 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\.\#]*font-size\s*:\s*(\d+)[px|em|rem|!important]*;/', $formStyle, $fontSize);
129 if ($fontSize) {
130 $lineHeight = 1;
131 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*line-height\s*:\s*([.\d]+)[!important]*;/', $formStyle, $lineHeightMatched);
132 if ($lineHeightMatched) {
133 $lineHeight = $lineHeightMatched[1];
134 }
135 $rowHeight = $fontSize[1] * $lineHeight;
136 }
137
138 preg_match('/fld-wrp-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*padding\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $padding);
139 if ($padding) {
140 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $padding[1])), 'Y');
141 }
142
143 preg_match('/input.fld-' . $formID . '\s*,\s*textarea.fld-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*margin\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $margin);
144 if ($margin) {
145 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $margin[1])), 'Y');
146 }
147 preg_match('/input.fld-' . $formID . '\s*,\s*textarea.fld-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*[^line\-height]height\s*:\s*([\d\s]+)[px|em|rem|!important]*;/', $formStyle, $height);
148 if ($height) {
149 $rowHeight = $rowHeight + $height[1];
150 } else {
151 $rowHeight = $rowHeight + 40;
152 }
153
154 return $rowHeight / 2;
155 }
156
157 public static function cssPropSumByAxis($value, $axis)
158 {
159 if (1 === count($value)) {
160 $value = array_fill(0, 4, $value[0]);
161 }
162
163 if (2 === count($value)) {
164 $value[] = $value[0];
165 $value[] = $value[1];
166 }
167
168 if (3 === count($value)) {
169 $value[] = $value[1];
170 }
171 if ('X' === $axis) {
172 return intval($value[1]) + intval($value[3]);
173 } else {
174 return intval($value[0]) + intval($value[2]);
175 }
176 }
177 }
178