| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentForm\App\Modules\MCP\Tools; |
| 4 | 4 | |
| 5 | 5 | defined('ABSPATH') || exit; |
| 6 | 6 | |
| 7 | +use FluentForm\App\Helpers\Helper; | |
| 7 | 8 | use FluentForm\App\Models\Form; |
| 8 | 9 | use FluentForm\App\Modules\MCP\Support\ErrorCodes; |
| 9 | 10 | use FluentForm\App\Modules\MCP\Support\FormAccess; |
| 10 | 11 | use FluentForm\App\Modules\MCP\Support\FormCreator; |
| @@ -157,9 +158,11 @@ | ||
| 157 | 158 | } |
| 158 | 159 | |
| 159 | 160 | // Replace only the fields; keep the fresh submitButton and any |
| 160 | 161 | // other top-level keys as they stand right now. |
| 161 | - $decoded['fields'] = $newFields; | |
| 162 | + $decoded['fields'] = Helper::isConversionForm($formId) | |
| 163 | + ? self::withStylePrefs($newFields, Arr::get($decoded, 'fields', [])) | |
| 164 | + : $newFields; | |
| 162 | 165 | |
| 163 | 166 | (new FormService())->update([ |
| 164 | 167 | 'form_id' => $formId, |
| 165 | 168 | 'formFields' => wp_json_encode($decoded), |
| @@ -186,8 +189,38 @@ | ||
| 186 | 189 | ); |
| 187 | 190 | }, |
| 188 | 191 | ['form_id' => $formId] |
| 189 | 192 | ); |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * create-form gets style_pref from Converter::convertExistingForm; this path | |
| 197 | + * never reaches it, and the editor reads style_pref.layout unguarded. Kept | |
| 198 | + * fields keep their stored block so an added question resets no layout. | |
| 199 | + */ | |
| 200 | + private static function withStylePrefs($fields, $existing) | |
| 201 | + { | |
| 202 | + $stored = []; | |
| 203 | + foreach ($existing as $field) { | |
| 204 | + $name = Arr::get($field, 'attributes.name'); | |
| 205 | + if ($name && isset($field['style_pref'])) { | |
| 206 | + $stored[$name] = $field['style_pref']; | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 210 | + foreach ($fields as $index => $field) { | |
| 211 | + $name = Arr::get($field, 'attributes.name'); | |
| 212 | + $fields[$index]['style_pref'] = isset($stored[$name]) ? $stored[$name] : [ | |
| 213 | + 'layout' => 'default', | |
| 214 | + 'media' => fluentFormGetRandomPhoto(), | |
| 215 | + 'brightness' => 0, | |
| 216 | + 'alt_text' => '', | |
| 217 | + 'media_x_position' => 50, | |
| 218 | + 'media_y_position' => 50, | |
| 219 | + ]; | |
| 220 | + } | |
| 221 | + | |
| 222 | + return $fields; | |
| 190 | 223 | } |
| 191 | 224 | |
| 192 | 225 | /** |
| 193 | 226 | * Every stored field key (attributes.name) in a fields array, recursing into |