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

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

137 lines 5.6 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) $new_lay_fields['fields'];
27 $formData->layout = $new_lay_fields['layout'];
28 $formData->form_name = $formName;
29
30 if (isset($data['formSettings'])) {
31 $formSettings = (array) (!is_string($data['formSettings']) ? $data['formSettings'] : json_decode($data['formSettings']));
32
33 if (isset($formSettings['confirmation'])) {
34 $confirmation = !is_string($formSettings['confirmation']) ? wp_json_encode($formSettings['confirmation']) : $formSettings['confirmation'];
35 $formSettings['confirmation'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $confirmation));
36 }
37
38 if (isset($formSettings['integrations'])) {
39 $integrations = !is_string($formSettings['integrations']) ? wp_json_encode($formSettings['integrations']) : $formSettings['integrations'];
40 $integrations = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $integrations);
41 $formSettings['integrations'] = json_decode(\preg_replace('/(\"formField\"\s*\:\s*\"bf?)(\d+)([- \d]+)/', '${1}' . $newFormId . '$3', $integrations));
42 }
43
44 if (isset($formSettings['mailTem'])) {
45 $mailTem = !is_string($formSettings['mailTem']) ? wp_json_encode($formSettings['mailTem']) : $formSettings['mailTem'];
46 $formSettings['mailTem'] = json_decode(\preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $mailTem));
47 $formData->formSettings = (object)$formSettings;
48 }
49 }
50
51 if (isset($data['workFlows'])) {
52 $workFlows = !is_string($data['workFlows']) ? wp_json_encode($data['workFlows']) : $data['workFlows'];
53 $workFlows = \preg_replace('/(\${bf?)(\d+)([- \d]+\})/', '${1}' . $newFormId . '$3', $workFlows);
54 $workFlows = json_decode(\preg_replace('/(\"bf?)(\d+)([- \d]+\")/', '${1}' . $newFormId . '$3', $workFlows));
55 $formData->workFlows = $workFlows;
56 }
57
58 if (isset($data['reports'])) {
59 $reports = !is_string($data['reports']) ? wp_json_encode($data['reports']) : $data['reports'];
60 $reports = json_decode(\preg_replace("/(\"bf?)(\d+)([- \d]+\")/", '${1}' . $newFormId . '$3', $reports));
61 $formData->reports = $reports;
62 }
63
64 if (isset($data['additional'])) {
65 $formData->additional = $data['additional'];
66 }
67
68 return $formData;
69 }
70
71 public static function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
72 {
73 $newField = (object) [];
74 foreach ((array)$layout->lg as $ind => $itm) {
75 $fld_tmp = $fields->{$layout->lg[$ind]->i};
76 $layout->lg[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->lg[$ind]->i);
77 $layout->md[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->md[$ind]->i);
78 $layout->sm[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->sm[$ind]->i);
79 $newField->{$layout->lg[$ind]->i} = json_decode(str_replace("bf$oldId-", "bf$newId-", wp_json_encode($fld_tmp)));
80 }
81 return ['layout' => $layout, 'fields' => $newField];
82 }
83
84 public static function calcRowHeight($formStyle, $formID)
85 {
86 $rowHeight = 0;
87 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\.\#]*font-size\s*:\s*(\d+)[px|em|rem|!important]*;/', $formStyle, $fontSize);
88 if ($fontSize) {
89 $lineHeight = 1;
90 preg_match('/fld-lbl-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*line-height\s*:\s*([.\d]+)[!important]*;/', $formStyle, $lineHeightMatched);
91 if ($lineHeightMatched) {
92 $lineHeight = $lineHeightMatched[1];
93 }
94 $rowHeight = $fontSize[1] * $lineHeight;
95 }
96
97 preg_match('/fld-wrp-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*padding\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $padding);
98 if ($padding) {
99 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $padding[1])), 'Y');
100 }
101
102 preg_match('/input.fld-' . $formID . '\s*,\s*textarea.fld-' . $formID . '\s*{[\w\-\(\)\!\;\,\s\:\#]*margin\s*:\s*([\d\spx|em|rem|]+)[!important]*;/', $formStyle, $margin);
103 if ($margin) {
104 $rowHeight = $rowHeight + static::cssPropSumByAxis(explode(' ', preg_replace('/px|em|rem|!important/', '', $margin[1])), 'Y');
105 }
106 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);
107 if ($height) {
108 $rowHeight = $rowHeight + $height[1];
109 } else {
110 $rowHeight = $rowHeight + 40;
111 }
112
113 return $rowHeight / 2;
114 }
115
116 public static function cssPropSumByAxis($value, $axis)
117 {
118 if (1 === count($value)) {
119 $value = array_fill(0, 4, $value[0]);
120 }
121
122 if (2 === count($value)) {
123 $value[] = $value[0];
124 $value[] = $value[1];
125 }
126
127 if (3 === count($value)) {
128 $value[] = $value[1];
129 }
130 if ('X' === $axis) {
131 return intval($value[1]) + intval($value[3]);
132 } else {
133 return intval($value[0]) + intval($value[2]);
134 }
135 }
136 }
137