*/ abstract public function settings(): array; /** * Sanitize a value that is saved on the options page. Only used for types with an option. * * @param mixed $value * @param array $field The stored field. * * @return mixed */ public function sanitizeValue($value, array $field) { return $value; } /** * Whether the type loads scripts or styles on the admin screens (see enqueue()). */ public function usesAssets(): bool { return false; } /** * Load scripts and styles; called on admin_enqueue_scripts for each stored field of this type when * usesAssets() is true. * * @param array $field The stored field. */ public function enqueue(array $field): void { } /** * Print the field on the options page (for layout fields: the whole section content). * * @param array $field The stored field. * @param mixed $value The current value of the option (null for layout fields). */ abstract public function render(array $field, $value): void; /** * Whether a value of this type can be shown through the shortcode and the block bindings: the type has an * option and offers the `frontend` setting (Setting::frontend()). A field must also have that setting * switched on. * * @since 1.0.0 */ final public function supportsFrontend(): bool { return $this->hasOption() && in_array('frontend', array_column($this->settings(), 'key'), true); } /** * The value as HTML for the website (shortcode, and the content of a bound block). It must be escaped here. * * @since 1.0.0 * * @param mixed $value The value of the option (the default value of the field when the option is missing). * @param array $field The stored field. * @param array $args Options of the shortcode, all strings (format, size, show, text, link, on, off, ...). */ public function displayValue($value, array $field, array $args): string { return is_scalar($value) ? esc_html((string) $value) : ''; } /** * The value for one attribute of a block that is bound to the field (Block Bindings). `content`, `text` and * `caption` get HTML (escaped, like displayValue()); the other attributes (url, alt, title, id, ...) get plain text, * which WordPress escapes as an attribute. Null: this type has nothing for that attribute and the block * keeps its own. * * @since 1.0.0 * * @param mixed $value As for displayValue(). * @param array $field The stored field. * @param string $attribute The name of the block attribute. */ public function bindingValue($value, array $field, string $attribute): ?string { switch ($attribute) { case 'content': case 'text': case 'caption': return $this->displayValue($value, $field, []); case 'url': case 'id': return null; } return is_scalar($value) ? (string) $value : null; } /** * The head every stored field starts with, plus the settings of this type. * * @return array */ final public function schema(): array { return array_merge([ Setting::make('id', 'hidden', 'ID', 'field_id'), Setting::fixed('type', static::type()), Setting::make('field_key', 'hidden', 'Key', 'number'), Setting::make('page_name', 'hidden', 'Page', 'text', '', ['from' => 'page_slug']), Setting::make('field_toggle', 'hidden', 'Open', 'toggle'), ], $this->settings()); } /** * Turn the field as the editor posted it into the field as it is stored. */ public function sanitizeSettings(array $raw, Validation $validate): array { $stored = []; foreach ($this->schema() as $setting) { $key = $setting['key']; if (array_key_exists('value', $setting)) { $stored[$key] = $setting['value']; } elseif (isset($setting['derive'])) { $stored[$key] = $setting['derive']($stored, $raw); } else { $value = array_key_exists($setting['from'] ?? $key, $raw) ? $raw[$setting['from'] ?? $key] : ($setting['missing'] ?? null); $stored[$key] = Sanitize::run($setting['sanitize'], $validate, $value, $raw); } } return $stored; } /** * The text of a stored setting as it was always printed (slashes removed). */ protected function text(array $field, string $key): string { return isset($field[$key]) ? stripslashes($field[$key]) : ''; } /** * The description as HTML; print it through wp_kses_post(). */ protected function descriptionHtml($description, bool $italic = false): string { return $description ? sprintf( '%s

', $italic ? ' class="description"' : '', stripslashes($description) ) : ''; } }