Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Unparsers/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Field extends Base {

private $choice_types = [ 'select', 'radio', 'checkbox_list', 'select_advanced', 'button_group', 'image_select', 'autocomplete' ];

/** Keys from the original imported JSON, captured before any default values are injected. */
private array $original_keys = [];

/**
* This is revert of parse method. While parse method converts to the minimal format,
* this method converts back to the original format.
Expand All @@ -19,6 +22,8 @@ class Field extends Base {
* @return void
*/
public function unparse() {
$this->original_keys = array_keys( $this->settings );
Comment thread
rilwis marked this conversation as resolved.

$this->unparse_default_values()
->unparse_boolean_values()
->unparse_numeric_values()
Expand All @@ -40,6 +45,7 @@ public function unparse() {
if ( method_exists( $this, $func ) ) {
$this->$func();
}
$this->unparse_custom_settings();
}

private function unparse_datalist() {
Expand Down Expand Up @@ -311,4 +317,48 @@ private function get_js_option_value( array $js_options, string $key ) {

return null;
}

/**
* Detect keys in the JSON that are not native MetaBox field settings
* and move them into the custom_settings array format that the Builder
* UI can read and display in the Advanced tab.
*
* Requires meta-box-builder plugin to be active (safe fallback if not).
*/
private function unparse_custom_settings(): self {
// Safe fallback: only run when Builder's FieldKeys helper is available.
if ( ! class_exists( '\\MBB\\Helpers\\FieldKeys' ) ) {
return $this;
}

$all_keys = \MBB\Helpers\FieldKeys::all();

// Move any unrecognized key into custom_settings format.
$custom_settings = $this->settings['custom_settings'] ?? [];

foreach ( $this->original_keys as $key ) {
if ( in_array( $key, $all_keys, true ) || ! isset( $this->settings[ $key ] ) ) {
continue;
}

$value = $this->settings[ $key ];
$values = $this->array_to_dot_notation( [ $key => $value ] );
foreach ( $values as $k => $v ) {
$uid = uniqid();
$custom_settings[ $uid ] = [
'id' => $uid,
'key' => $k,
'value' => $v,
];
}

unset( $this->settings[ $key ] );
}

if ( ! empty( $custom_settings ) ) {
$this->settings['custom_settings'] = $custom_settings;
}

return $this;
}
}
Loading