Skip to content

fix: preserve custom field attributes during JSON import#18

Closed
huongcodecb1992 wants to merge 7 commits into
masterfrom
fix/field-custom-settings-import
Closed

fix: preserve custom field attributes during JSON import#18
huongcodecb1992 wants to merge 7 commits into
masterfrom
fix/field-custom-settings-import

Conversation

@huongcodecb1992

Copy link
Copy Markdown
Contributor

No description provided.

@huongcodecb1992
huongcodecb1992 requested a review from rilwis March 10, 2026 18:26
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/Unparsers/Field.php
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/Unparsers/Field.php Outdated
'columns', 'field_name', '_state', 'input_attributes',
'multiple', 'fields', 'tab', 'validation',
'custom_settings', '_callback', 'options',
];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Có cơ chế nào để kiểm tra được danh sách các key này là chính xác? Ko bị thừa, thiếu?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List này được xây dựng bằng cách đọc code và trả lời câu hỏi: key nào trong $settings mà không có unparse_* method nào đọc qua $this->key?

Những key đó sẽ không bao giờ được auto-track → phải khai báo tường minh.

Ví dụ như:

fields, tab, validation, multiple, callback, columns... — không có method nào isset($this->fields) hay $this->tab → không được track → cần list
type, std, options — được access trong unparse_choice
* → đã auto-track → thừa trong list nhưng không hại

Trong codebase: src/Parsers/Field.php — Parser là chiều ngược lại của Unparser, nó đọc và xử lý tất cả native keys. Nếu key nào Parser có handler nhưng Unparser chưa có → key đó chưa được auto-track → cần vào STRUCTURAL_KEYS. Hiện e đã lấy hết các key từ parser sang.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E đã check phần code xử lý 1 số field trong phần Extensions chưa?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E check rồi. a thử kiểm tra xem có còn thiếu field nào nữa không ạ

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VD key desc, input_desc chẳng hạn thì thế nào e?

Comment thread src/Unparsers/Field.php
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/SettingsTrait.php Outdated
Comment thread src/Unparsers/Field.php
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/Unparsers/Field.php Outdated
Comment thread src/SettingsTrait.php Outdated
@wpmetabox wpmetabox deleted a comment from opencode-agent Bot Apr 1, 2026
@wpmetabox wpmetabox deleted a comment from opencode-agent Bot Apr 1, 2026
@wpmetabox wpmetabox deleted a comment from opencode-agent Bot Apr 1, 2026
Comment thread src/Unparsers/Field.php Outdated
@opencode-agent

opencode-agent Bot commented Apr 1, 2026

Copy link
Copy Markdown

Based on my review of the PR, here's my analysis:

Code Review

Issues Found

1. Missing Keys in STRUCTURAL_KEYS (Medium Priority)

Location: src/Unparsers/Field.php:21-27

The Parser (src/Parsers/Field.php) uses several keys not present in STRUCTURAL_KEYS:

  • desc, label_description - Set in unparse_default_values()
  • datalist_choices - Set in unparse_datalist() at line 71
  • inline - Used in parse_object_field() (line 128)
  • flatten, timestamp, force_delete, raw, alpha_channel - Used in parser but not unparser
  • collapsible, select_all_none, field_type - Used in field type parsers

Without these keys, they'll be incorrectly moved to custom_settings.

2. No Test Coverage (High Priority)

No tests exist for this functionality. This makes verification and regression detection impossible.

3. Unused Variable (Low Priority)

Location: src/Unparsers/Field.php:239

$custom = $this->custom_settings ?? [];

This variable is declared but only used for null coalescing check. It appears leftover from earlier implementation.

4. Performance - O(n) Array Merge on Each Access (Low Priority)

Location: src/Unparsers/Field.php:238

$known = array_merge( $this->get_accessed_keys(), self::STRUCTURAL_KEYS );

This runs on every unparse call, but since both arrays are small, it's acceptable.

Positive Observations

  1. Elegant auto-tracking - Using __get, __set, __isset magic methods to track accessed keys is a clean approach
  2. Correct ordering - Field-type handler runs before unparse_custom_settings() ensures proper tracking
  3. Type safety - Proper PHPDoc and array type declarations added in SettingsTrait.php

Recommendations

  1. Add missing keys to STRUCTURAL_KEYS:
private const STRUCTURAL_KEYS = [
    // existing keys...
    'desc', 'label_description', 'datalist_choices', 
    'inline', 'collapsible', 'select_all_none', 'field_type',
];
  1. Add integration tests to verify custom settings are preserved across parse/unparse cycles

  2. Remove or clarify the unused $custom variable at line 239

  3. Document why limit and limit_type are explicitly tracked despite being unset in unparse_text_limiter()

New%20session%20-%202026-04-01T15%3A16%3A06.229Z
opencode session  |  github run

@wpmetabox wpmetabox deleted a comment from opencode-agent Bot Apr 1, 2026
@rilwis rilwis closed this Apr 2, 2026
@rilwis
rilwis deleted the fix/field-custom-settings-import branch April 2, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants