]`, so a single * `register_setting()` sanitize callback receives the whole option as one array. * * A secret (Schema::SECRETS) is never written into the page: the control and the hidden carry * over both show what Schema::shown() answers for it: Schema::MASK when one is held and '' when not * (for the provider key, a row that says MASK with the key option gone shows ''). An MCP server's header * value gets the same treatment in the carry-over of `toolkits.mcp_servers`. Schema::sanitize() reads * the mask back as "keep what is stored", so a form saved without touching the key keeps it, * unless the same save moves the Base URL to another scheme, host or port * (Schema::providerKeyClearedByMove()), and a form saved with the field emptied clears it. The rule is applied here, at the one * place a value turns into markup, so no caller can print the key by forgetting to mask it. * * Every attribute goes through esc_attr(), every text node through esc_html() or * esc_textarea(): field labels and descriptions are translatable strings, but a stored value * came from a form and may be anything. * * @phpstan-import-type Field from Schema */ final class Fields { /** Multi-line strings get a textarea; every other string is a single line. */ private const TEXTAREAS = ['chat.system_prompt', 'chat.welcome']; public static function name(string $key): string { return Plugin::OPTION . '[' . $key . ']'; } /** `ab-provider-base-url`: what `label_for` points at. */ public static function id(string $key): string { return 'ab-' . str_replace('.', '-', $key); } /** * `$name` is the name the control posts under when it is not the key's own (name()): an entry * of a map field, such as one server's row of `access.mcp`, which posts as * `alpaca_bot_settings[access.mcp][]`. * * @param Field $f */ public static function render(string $key, array $f, mixed $value, ?string $name = null): string { $value = self::display($key, $value); $name = esc_attr($name ?? self::name($key)); $id = esc_attr(self::id($key)); $desc = isset($f['description']) ? '

' . esc_html((string) $f['description']) . '

' : ''; $desc .= self::override($key); switch ($f['type']) { case 'boolean': // The hidden 0 before the box is what an unchecked box posts; without it the // sanitize callback would never hear "off" and the field would keep its default. return sprintf( '%5$s', $name, $id, checked((bool) $value, true, false), esc_html((string) $f['label']), $desc, ); case 'select': $opts = ''; foreach ($f['options'] ?? [] as $v => $label) { $opts .= sprintf('', esc_attr((string) $v), selected((string) (is_scalar($value) ? $value : ''), (string) $v, false), esc_html((string) $label)); } return sprintf('%s', $id, $name, $opts, $desc); case 'integer': case 'number': $bounds = ''; foreach (['min', 'max'] as $bound) { if (isset($f[$bound])) { $bounds .= sprintf(' %s="%s"', $bound, esc_attr((string) $f[$bound])); } } return sprintf( '%s', $id, $name, esc_attr(self::scalar($value)), $f['type'] === 'integer' ? '1' : '0.1', $bounds, $desc, ); case 'array': // An array field has no single control. SettingsPage renders each one itself // (`models.overrides` and `toolkits.mcp_servers` as tables, `toolkits.abilities` // as a list of boxes) or gives it none (`access.mcp`), and never hands one to // this method. return ''; case 'checkbox-list': // One box per option under the same `[]` name, and ahead of them a hidden '' // under that name: a checkbox posts only when checked, so with every box clear // the field would be absent from the POST, Schema::sanitize() would keep what // is stored, and the last tool could never be switched off. The sentinel is the // boolean's hidden 0 for a list; Schema::coerce() drops it as an unknown id. // The stored value is checked against as a list; anything else checks nothing. $chosen = is_array($value) ? $value : []; $boxes = ''; foreach ($f['options'] ?? [] as $v => $label) { $boxes .= sprintf( '
', esc_attr(self::id($key) . '-' . (string) $v), $name, esc_attr((string) $v), checked(in_array((string) $v, array_map('strval', array_filter($chosen, 'is_scalar')), true), true, false), esc_html((string) $label), ); } return sprintf( '
%s%s
%s', $name, esc_html((string) $f['label']), $boxes, $desc, ); default: if (in_array($key, self::TEXTAREAS, true)) { return sprintf('%s', $id, $name, esc_textarea(self::scalar($value)), $desc); } $secret = in_array($key, Schema::SECRETS, true); return sprintf( '%s', $secret ? 'password' : 'text', $id, $name, esc_attr(self::scalar($value)), $secret ? ' autocomplete="new-password"' : '', $desc, ); } } /** * The hidden inputs that post a field the page is not showing, so a save from one tab * carries every other tab's values unchanged. A field a post leaves out is not lost either: * Schema::sanitize() takes the stored value for any key the post does not name, and only a * key with nothing stored falls back to its default. So a map that must not be replaced by * what can be printed of it here (`access.mcp`) is left out rather than carried * (SettingsPage::render()). * * Booleans post as 0/1, the way the visible checkbox does. An array is walked to any depth, * one input per scalar leaf: a map posts `[key][k]` per level (`models.overrides` as * `[key][model][field]`, a server row's approvals as `[key][row][approved][tool]`), and a * scalar in a list posts under `[]`, the way the checked boxes of `toolkits.enabled` would. A * leaf that is not a scalar is dropped rather than printed as "Array", and an empty array * posts nothing, which keeps the stored [] just the same. */ public static function hidden(string $key, mixed $value): string { $value = self::display($key, $value); return is_array($value) ? self::hiddenTree(self::name($key), $value) : self::hiddenInput(self::name($key), self::scalar($value)); } /** * The inputs for `$value` under `$name`, one per scalar leaf (hidden() says how each level is named). * * @param array $value */ private static function hiddenTree(string $name, array $value): string { $out = ''; $list = array_is_list($value); foreach ($value as $k => $v) { if (is_array($v)) { $out .= self::hiddenTree($name . '[' . $k . ']', $v); } elseif (is_scalar($v)) { $out .= self::hiddenInput($name . ($list ? '[]' : '[' . $k . ']'), self::scalar($v)); } } return $out; } /** * The warning under a field whose stored value something outside the settings overrides, or * '' when nothing does. Only `provider.base_url` has one: Provider\Factory::baseUrl() prefers * a non-empty `OLLAMA_API_URL` constant, so on a site that defines it (wp-config.php, * usually) the field still saves and still reads back, and every request goes somewhere * else. Without this the only symptom of pointing the site at a new gateway is that nothing * changes. * * The constant's value is printed because this is the admin screen and the reader is an * administrator who can read wp-config.php anyway; that is also why it stays here and not in * Schema, whose descriptions the REST schema route serves as data. */ private static function override(string $key): string { if ($key !== 'provider.base_url' || !defined('OLLAMA_API_URL') || trim((string) constant('OLLAMA_API_URL')) === '') { return ''; } return '

' . sprintf( /* translators: 1: PHP constant name, 2: the URL the constant is set to */ esc_html__('Overridden: the %1$s constant is set to %2$s, and the provider uses that. This field is saved but ignored until the constant is removed.', 'alpaca-bot'), 'OLLAMA_API_URL', '' . esc_html(trim((string) constant('OLLAMA_API_URL'))) . '', ) . '

'; } /** What the page shows for a value: Schema::shown() for a secret, the MCP server rows with their header values masked (Schema::maskedServers()), the value for anything else. */ private static function display(string $key, mixed $value): mixed { if (in_array($key, Schema::SECRETS, true)) { return Schema::shown($key, $value); } return $key === 'toolkits.mcp_servers' ? Schema::maskedServers($value) : $value; } private static function hiddenInput(string $name, string $value): string { return sprintf('', esc_attr($name), esc_attr($value)); } /** A value as a form posts it: booleans as 0/1, numbers and strings as they print, anything else as ''. */ private static function scalar(mixed $value): string { if (is_bool($value)) { return $value ? '1' : '0'; } return is_scalar($value) ? (string) $value : ''; } }