Skip to main content

FieldRegistry

\Benecaster\Fields\FieldRegistry

Class Free

Manages CPT registration, field group CRUD, and field definition CRUD for the custom fields system. Maintains the in-memory registry of which CPTs participate in the field system, and owns persistence for field groups and field definitions. Benecaster auto-registers benecaster_episode at boot. Add-ons call benecaster_register_field_cpt() (global wrapper) from a benecaster_boot callback. Registered as a singleton in the container.

Methods

Method Visibility Since Description
register_cpt( string $cpt_slug, string $label ): void Public — Registers a CPT slug so it appears in the Field Group editor's Attached CPT dropdown. Add-on code should use benecaster_register_field_cpt() rather than calling this directly.
get_registered_cpts(): array Public — Returns all registered CPT slugs and their human-readable labels as an associative array keyed by slug. Returns array.
get_groups_for_cpt( string $cpt_slug ): array Public — Returns all field groups attached to a CPT, ordered by display_order then id. Each group includes a field_count computed at query time.
get_group( int $id ): ?array Public — Returns a single field group by its database ID, or null if not found.
find_group_by_name( string $name ): ?array Public — Looks up a field group by name (case-insensitive on most installs). Returns null if not found. Used by benecaster_get_fields() to resolve a group name to an ID.
get_fields_for_group( int $group_id ): array Public — Returns all field definitions for a group, ordered by display_order then id. The options key contains the decoded options array for select and checkbox types, or null for other types.
get_field( int $id ): ?array Public — Returns a single field definition by its database ID, or null if not found.
create_group( string $name, string $cpt_slug, int $display_order = 0 ): int Public — Creates a new field group and returns its new database ID. Returns 0 on failure. Called by FieldGroupsController.
update_group( int $id, ?string $name, ?int $display_order ): bool Public — Updates a group's name and/or display order. Pass null for fields that should not change. Returns true on success.
delete_group( int $id ): void Public — Deletes a field group and cascades to all its field definitions and stored values. Deletion order: field values then field definitions then group row.
create_field( int $group_id, string $field_label, string $field_key, string $field_type, int $display_order = 0, ?string $options_json = null, bool $required = false ): int Public — Creates a new field definition within a group. $options_json is a JSON-encoded string for select and checkbox types; pass null for all other types. Returns the new field ID.
update_field( int $id, array $data ): bool Public — Updates a field definition. Only keys present in $data are written. Accepted keys: field_label, field_key, field_type, display_order, options_json, required.
delete_field( int $id ): void Public — Deletes a field definition and all stored values for that field.