]`, 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 Schema::MASK when a value is stored and '' when not. Schema::sanitize() reads * the mask back as "keep what is stored", so a form saved without touching the key keeps it, * 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); } /** @param Field $f */ public static function render(string $key, array $f, mixed $value): string { $value = self::display($key, $value); $name = esc_attr(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': // A map has no single control; SettingsPage renders `models.overrides` as a table. 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( '' . 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'))) . '',
) . '