|
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. |
@@ -90,6 +92,11 @@ public function to_minimal_format() { |
90 | 92 | unset( $settings[ $key ] ); |
91 | 93 | } |
92 | 94 |
|
| 95 | + // Strip prefix from field IDs before exporting to JSON (minimal format) |
| 96 | + if ( ! empty( $settings['fields'] ) && is_array( $settings['fields'] ) ) { |
| 97 | + Prefixer::remove( $settings['fields'], $settings['prefix'] ?? '' ); |
| 98 | + } |
| 99 | + |
93 | 100 | ksort( $settings ); |
94 | 101 |
|
95 | 102 | return $settings; |
@@ -255,22 +262,53 @@ public function unparse_modified() { |
255 | 262 | return $this; |
256 | 263 | } |
257 | 264 |
|
258 | | - 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 { |
259 | 274 | // If not meta box, return |
260 | 275 | if ( $this->detect_post_type() !== 'meta-box' ) { |
261 | 276 | return $this; |
262 | 277 | } |
263 | 278 |
|
| 279 | + $prefix = $this->lookup( [ 'prefix', 'settings.prefix' ], '' ); |
| 280 | + |
| 281 | + // If meta box is already parsed, normalize the fields array. |
264 | 282 | if ( isset( $this->meta_box ) && is_array( $this->meta_box ) ) { |
265 | 283 | // Fix: error on earlier versions that saved fields as object |
266 | | - $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 ); |
267 | 293 | $this->settings['meta_box']['fields'] = $fields; |
268 | 294 |
|
269 | 295 | return $this; |
270 | 296 | } |
271 | 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 | + |
272 | 306 | $meta_box = $this->get_settings(); |
273 | 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 | + |
274 | 312 | foreach ( $this->get_unneeded_keys() as $key ) { |
275 | 313 | unset( $meta_box[ $key ] ); |
276 | 314 | } |
@@ -312,7 +350,7 @@ private function unparse_settings_page_tabs(): self { |
312 | 350 |
|
313 | 351 | $tab_items = []; |
314 | 352 | foreach ( $tabs as $key => $value ) { |
315 | | - $id = uniqid( 'tab_' ); |
| 353 | + $id = uniqid( 'tab_' ); |
316 | 354 | $tab_items[ $id ] = compact( 'id', 'key', 'value' ); |
317 | 355 | } |
318 | 356 |
|
|
0 commit comments