*/ private array $fields = array(); /** * Register a field definition. * * @param FieldDefinition $definition * @return void * @throws InvalidArgumentException If field type is already registered */ public function register( FieldDefinition $definition ): void { if ( isset( $this->fields[ $definition->type ] ) ) { // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new InvalidArgumentException( sprintf( 'Field type "%s" is already registered.', $definition->type ) ); // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped } $this->fields[ $definition->type ] = $definition; } /** * Get a field definition by type. * * @param string $type * @return FieldDefinition|null */ public function get( string $type ): ?FieldDefinition { return $this->fields[ $type ] ?? null; } /** * Get all registered field definitions. * * @return array */ public function all(): array { return $this->fields; } /** * Get fields grouped by category. * * @return array> */ public function byCategory(): array { $grouped = array(); foreach ( $this->fields as $field ) { // Skip palette-hidden (internal/system) fields if ( $field->paletteHidden ) { continue; } $category = $field->category; if ( ! isset( $grouped[ $category ] ) ) { $grouped[ $category ] = array(); } $grouped[ $category ][] = $field; } // Library order: categories by their first field, fields by meta.order. foreach ( $grouped as &$list ) { usort( $list, static fn( $a, $b ) => ( $a->meta['order'] ?? 10000 ) <=> ( $b->meta['order'] ?? 10000 ) ); } unset( $list ); uasort( $grouped, static fn( $a, $b ) => ( $a[0]->meta['order'] ?? 10000 ) <=> ( $b[0]->meta['order'] ?? 10000 ) ); return $grouped; } /** * Check if a field type exists. * * @param string $type * @return bool */ public function has( string $type ): bool { return isset( $this->fields[ $type ] ); } /** * Get all field definitions as array representation. * * @return array */ public function toArray(): array { return array_map( fn( $field ) => $field->toArray(), $this->fields ); } }