|
1 | 1 | <?php |
2 | 2 | namespace MBBParser\Unparsers; |
3 | 3 |
|
| 4 | +use MBBParser\Prefixer; |
| 5 | + |
4 | 6 | /** |
5 | 7 | * This class is the inverse of the parser. |
6 | 8 | * We convert the parsed data back to the format that can be saved to the database. |
@@ -92,35 +94,14 @@ public function to_minimal_format() { |
92 | 94 |
|
93 | 95 | // Strip prefix from field IDs before exporting to JSON (minimal format) |
94 | 96 | if ( ! empty( $settings['fields'] ) && is_array( $settings['fields'] ) ) { |
95 | | - $prefix = $settings['prefix'] ?? ''; |
96 | | - |
97 | | - if ( $prefix ) { |
98 | | - $this->strip_prefix_from_fields( $settings['fields'], $prefix ); |
99 | | - } |
| 97 | + Prefixer::remove( $settings['fields'], $settings['prefix'] ?? '' ); |
100 | 98 | } |
101 | 99 |
|
102 | 100 | ksort( $settings ); |
103 | 101 |
|
104 | 102 | return $settings; |
105 | 103 | } |
106 | 104 |
|
107 | | - /** |
108 | | - * Recursively strip prefix from field IDs (for export) |
109 | | - */ |
110 | | - private function strip_prefix_from_fields( array &$fields, string $prefix ): void { |
111 | | - foreach ( $fields as &$field ) { |
112 | | - // Strip prefix from field ID |
113 | | - if ( isset( $field['id'] ) && str_starts_with( $field['id'], $prefix ) ) { |
114 | | - $field['id'] = substr( $field['id'], strlen( $prefix ) ); |
115 | | - } |
116 | | - |
117 | | - // Recursively strip from sub-fields (for group fields) |
118 | | - if ( ! empty( $field['fields'] ) && is_array( $field['fields'] ) ) { |
119 | | - $this->strip_prefix_from_fields( $field['fields'], $prefix ); |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | - |
124 | 105 | public function unparse_schema() { |
125 | 106 | $schema = $this->lookup( [ '$schema' ], '' ); |
126 | 107 | if ( ! empty( $schema ) ) { |
@@ -281,22 +262,53 @@ public function unparse_modified() { |
281 | 262 | return $this; |
282 | 263 | } |
283 | 264 |
|
284 | | - public function unparse_meta_box() { |
| 265 | + /** |
| 266 | + * Unparse the meta box. |
| 267 | + * |
| 268 | + * For fields: |
| 269 | + * - Always keep it as a numeric array. |
| 270 | + * - Remove prefix from field IDs for the settings, that can be used for export, builder, local JSON. |
| 271 | + * - Add prefix to field IDs for parsed meta box, that's ready for registering. |
| 272 | + */ |
| 273 | + public function unparse_meta_box(): static { |
285 | 274 | // If not meta box, return |
286 | 275 | if ( $this->detect_post_type() !== 'meta-box' ) { |
287 | 276 | return $this; |
288 | 277 | } |
289 | 278 |
|
| 279 | + $prefix = $this->lookup( [ 'prefix', 'settings.prefix' ], '' ); |
| 280 | + |
| 281 | + // If meta box is already parsed, normalize the fields array. |
290 | 282 | if ( isset( $this->meta_box ) && is_array( $this->meta_box ) ) { |
291 | 283 | // Fix: error on earlier versions that saved fields as object |
292 | | - $fields = array_values( $this->meta_box['fields'] ?? [] ); |
| 284 | + $fields = $this->meta_box['fields'] ?? []; |
| 285 | + $fields = array_values( $fields ); |
| 286 | + |
| 287 | + // Remove prefix from field IDs for the settings, that can be used for export, builder, local JSON. |
| 288 | + Prefixer::remove( $fields, $prefix ); |
| 289 | + $this->fields = $fields; |
| 290 | + |
| 291 | + // Add prefix to field IDs for parsed meta box, that's ready for registering. |
| 292 | + Prefixer::add( $fields, $prefix ); |
293 | 293 | $this->settings['meta_box']['fields'] = $fields; |
294 | 294 |
|
295 | 295 | return $this; |
296 | 296 | } |
297 | 297 |
|
| 298 | + // If meta box is not parsed, normalize the fields array. |
| 299 | + $fields = $this->fields ?: []; |
| 300 | + $fields = array_values( $fields ); |
| 301 | + |
| 302 | + // Remove prefix from field IDs for the settings, that can be used for export, builder, local JSON. |
| 303 | + Prefixer::remove( $fields, $prefix ); |
| 304 | + $this->fields = $fields; |
| 305 | + |
298 | 306 | $meta_box = $this->get_settings(); |
299 | 307 |
|
| 308 | + // Add prefix to field IDs for parsed meta box, that's ready for registering. |
| 309 | + Prefixer::add( $fields, $prefix ); |
| 310 | + $meta_box['fields'] = $fields; |
| 311 | + |
300 | 312 | foreach ( $this->get_unneeded_keys() as $key ) { |
301 | 313 | unset( $meta_box[ $key ] ); |
302 | 314 | } |
@@ -475,21 +487,12 @@ public function unparse_fields() { |
475 | 487 | } |
476 | 488 |
|
477 | 489 | public function convert_fields_for_builder( $fields = [] ): array { |
478 | | - // Get prefix to strip from field IDs during export |
479 | | - $prefix = $this->settings['settings']['prefix'] ?? ''; |
480 | | - |
481 | 490 | foreach ( $fields as $id => $field ) { |
482 | 491 | $unparser = new Field( $field ); |
483 | 492 | $unparser->unparse(); |
484 | 493 |
|
485 | 494 | $field = $unparser->get_settings(); |
486 | 495 |
|
487 | | - // Strip prefix from field ID for clean export |
488 | | - if ( $prefix && isset( $field['id'] ) && str_starts_with( $field['id'], $prefix ) ) { |
489 | | - $field['id'] = substr( $field['id'], strlen( $prefix ) ); |
490 | | - } |
491 | | - |
492 | | - // Recursively process sub-fields |
493 | 496 | if ( isset( $field['fields'] ) && is_array( $field['fields'] ) ) { |
494 | 497 | $field['fields'] = $this->convert_fields_for_builder( $field['fields'] ); |
495 | 498 | } |
|
0 commit comments