feat: unify CPT and field group definitions in one JSON file (closes #163)#517
Open
faisalahammad wants to merge 1 commit into
Open
feat: unify CPT and field group definitions in one JSON file (closes #163)#517faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
Allows a single JSON file under the acf-json directory to declare a custom post type together with the field groups attached to it, instead of the previous split between post_type_*.json and group_*.json files. The loader runs on acf/init priority 5, before register_post_types at priority 6, so field groups land in the local store before the CPT registers. Detection is by shape: a JSON file containing both a post_type key and either a field_groups array or a top-level fields array routes through the new path. Legacy single-object files continue to load through their existing per-type readers. Top-level fields without an explicit field_groups entry are auto-wrapped into a single synthetic group whose location rule pins it to the owning CPT. Existing user-supplied location rules are preserved alongside the injected rule. Closes WordPress#163.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an additive reader that lets a single
acf-jsonJSON file declare a custom post type together with the field groups attached to it. Today, declaring a CPT with fields requires authoring two separate files and keeping theirlocation.post_typevalue in sync with the CPT slug. This PR lets you ship one file per conceptual unit.Fixes #163
Changes
includes/local-json.phpBefore:
ACF_Local_JSON::scan_files()discovered every JSON shape by key prefix. Field groups ended up in the store throughACF_Local_JSON::include_fields(), which scansgroup_*.jsonfiles. CPT definitions went throughinclude_post_types()forpost_type_*.jsonfiles. Nothing connected a CPT file to the field groups whoselocationrules targeted it.After: Added
ACF_Local_JSON::is_unified_definition()(a static detector for the unified shape) andACF_Local_JSON::include_unified_definitions()(a newacf_initpriority-5 loader).scan_files()now routes unified files to a separate internal slot so the legacy readers do not try to read them. The loader splits the JSON into a CPT and its field groups, registers the CPT, then registers the field groups with a post-type location rule pinned to the owning CPT injected non-destructively. Top-levelfields(withoutfield_groups) are auto-wrapped into a synthetic group whose key isgroup_<slug>_fields.Why: The order at priority 5 vs the existing
register_post_typesat priority 6 means field groups land in the local store before the CPT registers, so the runtime location matcher sees them.schemas/unified-cpt.schema.json(new)JSON Schema definition for the unified envelope. Pulled in by
SCF_JSON_Schema_Validator::validate_required_schemas().includes/class-scf-json-schema-validator.phpAdded
'unified-cpt'toREQUIRED_SCHEMASso the validator self-check finds the new schema.tests/php/test-unified-definitions.php(new)14 tests covering the detector, scan routing, end-to-end load (with both explicit
field_groupsand auto-wrap), preservation of explicit location rules, coexistence with legacy files, source file recording, and schema validation against both a valid and an invalid fixture.Testing
Test 1: Unified loader registers CPT and field groups
post_type_book.jsonto your active theme'sacf-json/directory containingkey,title,post_type, and afield_groupsarray with one group.Show if Post Type == booklocation rule.Test 2: Auto-wrap top-level fields
post_type_movie.jsonwith only a top-levelfieldsarray (nofield_groups).group_movie_fieldsgroup is registered and attached to themovieCPT.Test 3: Legacy files still load
post_type_album.json(CPT payload only) andgroup_legacy.json(field group payload only) to the same directory.Automated tests:
composer test:phpruns all 2858 tests including the 14 new ones. PHPStan clean againstbin/baseline.neon(regenerated for the new methods). PHPCS reports no new violations on the changed lines (verified viaphpcs-changed).Verification environment
phpcs-changedon changed lines: cleanpost_type_*.jsonandgroup_*.jsonfiles continue to work.Screenshot