PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4.1
3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / framework / blocks-style-manager / migrator / data-normalizer.php
jetformbuilder / modules / framework / blocks-style-manager / migrator Last commit date
data-normalizer.php 2 months ago post-handler.php 2 months ago ui.php 2 months ago
data-normalizer.php
283 lines
1 <?php
2 namespace Crocoblock\Blocks_Style\Migrator;
3
4 class Data_Normalizer {
5
6 private const EDGES = [ 'top', 'right', 'bottom', 'left' ];
7 private $input_data;
8 private $current_key;
9
10 /**
11 * Convert old data to new format.
12 *
13 * @param array $data The data to convert.
14 * @return array The normalized JSON string.
15 */
16 public function normalize( array $data ): array {
17
18 $normalized = [];
19 $this->input_data = $data;
20
21 $normalized_tablet = [];
22 $normalized_mobile = [];
23
24 foreach ( $data as $key => $item ) {
25 if ( ! isset( $item['value'] ) ) {
26 continue;
27 }
28
29 $this->current_key = $key;
30
31 $value = $item['value'];
32
33 $normalized[ $key ] = $this->get_normalized_value( $value, $key );
34
35 if ( ! empty( $item['tablet'] ) ) {
36 $normalized_tablet[ $key ] = $this->get_normalized_value( $item['tablet'], $key );
37 }
38
39 if ( ! empty( $item['mobile'] ) ) {
40 $normalized_mobile[ $key ] = $this->get_normalized_value( $item['mobile'], $key );
41 }
42 }
43
44 $this->current_key = null;
45 $this->input_data = [];
46
47 if ( ! empty( $normalized_tablet ) ) {
48 $normalized['__tablet'] = $normalized_tablet;
49 }
50
51 if ( ! empty( $normalized_mobile ) ) {
52 $normalized['__mobile'] = $normalized_mobile;
53 }
54
55 return $normalized;
56 }
57
58 /**
59 * Get normalized value based on its type.
60 *
61 * @param mixed $value The value to normalize.
62 * @param string $key The key associated with the value.
63 * @return mixed The normalized value.
64 */
65 private function get_normalized_value( $value, $key ) {
66
67 if ( is_scalar( $value ) ) {
68 return $value;
69 }
70
71 if ( $this->is_typography( $value ) ) {
72 return $this->normalize_typography( $value );
73 } elseif ( $this->is_edges( $value ) ) {
74 return $this->normalize_edges( $value );
75 } elseif ( $this->is_border( $value ) ) {
76 return $this->normalize_border( $value );
77 } elseif ( is_array( $value ) && isset( $value['value'] ) && isset( $value['unit'] ) ) {
78 return $value['value'] . $value['unit'];
79 } elseif ( in_array( $key, [ 'icon_size', 'icon_gap' ], true ) ) {
80 return isset( $value['value'] ) ? (int) $value['value'] : 0;
81 } else {
82 return $value;
83 }
84 }
85
86 /**
87 * Check if the value is a border array.
88 *
89 * @param array $value The value to check.
90 * @return bool True if it is a border array, false otherwise.
91 */
92 private function is_border( array $value ): bool {
93 return isset( $value['width'], $value['color'], $value['style'] );
94 }
95
96 /**
97 * Check if the value is a typography array.
98 *
99 * @param array $value The value to check.
100 * @return bool True if it is a typography array, false otherwise.
101 */
102 private function is_typography( array $value ): bool {
103 return isset( $value['family'], $value['size'], $value['weight'] );
104 }
105
106 /**
107 * Check if the value contains at least two edges.
108 *
109 * @param array $value The value to check.
110 * @return bool True if it contains at least two edges, false otherwise.
111 */
112 private function is_edges( array $value ): bool {
113 return count( array_intersect( array_keys( $value ), self::EDGES ) ) >= 2;
114 }
115
116 /**
117 * Normalize typography values.
118 *
119 * @param array $value The typography value to normalize.
120 * @return array The normalized typography value.
121 */
122 private function normalize_typography( array $value ): array {
123
124 $typography = [];
125
126 if ( isset( $value['size'] ) ) {
127 $typography['size'] = $value['size'] . ( $value['s_unit'] ?? 'px' );
128 }
129
130 if ( isset( $value['lineHeight'] ) ) {
131 $typography['lineHeight'] = $value['lineHeight'] . ( $value['lh_unit'] ?? '' );
132 }
133
134 if ( isset( $value['letterSpacing'] ) ) {
135 $typography['letterSpacing'] = $value['letterSpacing'] . ( $value['ls_unit'] ?? 'px' );
136 }
137
138 foreach ( [ 'family', 'weight', 'style', 'transform', 'decoration' ] as $attr ) {
139 if ( isset( $value[ $attr ] ) && 'inherit' !== $value[ $attr ] ) {
140 $typography[ $attr ] = $value[ $attr ];
141 }
142 }
143
144 return $typography;
145 }
146
147 /**
148 * Normalize edges values.
149 *
150 * @param array $value The edges value to normalize.
151 * @return array The normalized edges value.
152 */
153 private function normalize_edges( array $value ): array {
154 return $value;
155 }
156
157 /**
158 * Normalize border values.
159 *
160 * @param array $value The border value to normalize.
161 * @return array The normalized border value.
162 */
163 private function normalize_border( array $value ): array {
164
165 $result = [ 'border' => [] ];
166 $border = [];
167
168 if ( isset( $value['width'] ) && is_array( $value['width'] ) ) {
169
170 $width = $this->get_uniform_edge_value( $value['width'] );
171
172 if ( null !== $width ) {
173 $border['width'] = $width;
174 } else {
175 foreach ( self::EDGES as $edge ) {
176 if ( isset( $value['width'][ $edge ] ) && ! empty( $value['width'][ $edge ] ) ) {
177 $border[ $edge ]['width'] = $value['width'][ $edge ];
178 } else {
179 $border[ $edge ]['width'] = '0';
180 }
181 }
182 }
183 }
184
185 if ( ! empty( $value['color'] ) && ! empty( $border ) ) {
186 if ( $this->has_any_edges( $border ) ) {
187 foreach ( self::EDGES as $edge ) {
188 $border[ $edge ]['color'] = $value['color'];
189 }
190 } else {
191 $border['color'] = $value['color'];
192 }
193 }
194
195 if ( ! empty( $value['style'] ) && 'none' !== $value['style'] && ! empty( $border ) ) {
196 if ( $this->has_any_edges( $border ) ) {
197 foreach ( self::EDGES as $edge ) {
198 $border[ $edge ]['style'] = $value['style'];
199 }
200 } else {
201 $border['style'] = 'solid'; // Default style if not set.
202 }
203 }
204
205 if ( ! empty( $border ) ) {
206 $result['border'] = $border;
207 }
208
209 if ( isset( $value['radius'] ) && is_array( $value['radius'] ) ) {
210 $radius = $this->get_uniform_edge_value( $value['radius'] );
211 if ( null !== $radius ) {
212 $result['radius'] = $radius;
213 }
214 }
215
216 $keys = array_keys( $this->input_data );
217 $curernt_index = array_search( $this->current_key, $keys, true );
218
219 if ( false !== $curernt_index ) {
220 $next_index = $curernt_index + 1;
221 $next_key = isset( $keys[ $next_index ] ) ? $keys[ $next_index ] : null;
222
223 if ( $next_key && false !== strpos( $next_key, 'radius' ) ) {
224 $next_data = $this->input_data[ $next_key ]['value'] ?? [];
225
226 if ( ! empty( $next_data ) && is_array( $next_data ) ) {
227 $radius = $this->get_uniform_edge_value( $next_data );
228 if ( null !== $radius ) {
229 $result['radius'] = $radius;
230 } else {
231 $radius_map = [
232 'top' => 'topLeft',
233 'right' => 'topRight',
234 'bottom' => 'bottomRight',
235 'left' => 'bottomLeft',
236 ];
237
238 $result['radius'] = [];
239
240 foreach ( self::EDGES as $edge ) {
241
242 $current_edge = $radius_map[ $edge ];
243 if ( isset( $next_data[ $edge ] ) && ! empty( $next_data[ $edge ] ) ) {
244 $result['radius'][ $current_edge ] = $next_data[ $edge ];
245 } else {
246 $result['radius'][ $current_edge ] = '0';
247 }
248 }
249 }
250 }
251 }
252 }
253
254 return $result;
255 }
256
257 /**
258 * Check if the value has any edges.
259 *
260 * @param array $value The value to check.
261 * @return bool True if it has any edges, false otherwise.
262 */
263 private function has_any_edges( array $value ): bool {
264 return count( array_intersect( array_keys( $value ), self::EDGES ) ) > 0;
265 }
266
267 /**
268 * Get a uniform edge value from an array of edges.
269 *
270 * If all edges have the same value, return that value; otherwise, return null.
271 *
272 * @param array $edges An associative array of edges (e.g., ['top' => '10px', 'right' => '10px']).
273 * @return string|null The uniform edge value or null if not uniform.
274 */
275 private function get_uniform_edge_value( array $edges ): ?string {
276 $values = array_values( $edges );
277 $unique = array_unique( $values );
278
279 return count( $unique ) === 1 ? $unique[0] : null;
280 }
281
282 }
283