settings-store.php
Settings store
Reads, validates and writes the single option this plugin owns, resolves constants and core locks, applies presets and decides the role exemption. Every other module goes through here, so validation cannot be bypassed by adding a caller.
Tags
Table of Contents
Functions
- jpkcom_harden_trim_option_name() : string
- Name of the option holding all settings.
-
jpkcom_harden_trim_sanitize_value()
: array{valid: bool, value: mixed, dropped: list
} - Validate one value against its schema entry.
-
jpkcom_harden_trim_sanitize_settings()
: array{settings: array
, rejected: list } - Coerce any input into a complete, valid settings array.
- jpkcom_harden_trim_get_stored() : array<string, mixed>
- The stored settings, sanitised and merged over the defaults.
- jpkcom_harden_trim_constant_state() : array{name: string, defined: bool, valid: bool, value: mixed}
- State of the plugin constant for one key (`JPKCOM_HARDEN_TRIM_<KEY>`).
- jpkcom_harden_trim_core_lock() : array{name: string, value: mixed}|null
- The core constant that locks a key, if it was defined before the plugin loaded.
- jpkcom_harden_trim_locked_keys() : array<string, string>
- Every key whose value the settings page cannot change.
- jpkcom_harden_trim_kill_switch_state() : array{defined: bool, valid: bool, active: bool}
- State of the kill switch `JPKCOM_HARDEN_TRIM_DISABLE_ALL`.
- jpkcom_harden_trim_kill_switch() : bool
- Whether the kill switch is on (no feature hooks at all).
- jpkcom_harden_trim_invalid_constants() : array<int, string>
- Names of plugin constants that are defined but hold an invalid value.
- jpkcom_harden_trim_settings_cache() : array<string, mixed>|null
- Holder of the effective-settings cache, one entry per site.
- jpkcom_harden_trim_settings_reset_cache() : void
- Forget the cached effective settings.
- jpkcom_harden_trim_settings() : array<string, mixed>
- The effective settings: valid plugin constant, else stored value, else default.
-
jpkcom_harden_trim_save_settings()
: array{settings: array
, rejected: list } - Sanitise and store a complete settings array.
-
jpkcom_harden_trim_apply_preset()
: array{settings: array
, changed: list } - Apply a preset to a settings array (does not store it).
- jpkcom_harden_trim_user_roles() : array<int, string>
- The roles a user holds for the role-based settings.
- jpkcom_harden_trim_is_exempt() : bool
- Whether a user is exempt from the role-scoped features.
- jpkcom_harden_trim_is_active_value() : bool
- Whether a value means "the feature does something".
- jpkcom_harden_trim_feature_applies() : bool
- Whether a feature acts for the current request.
- jpkcom_harden_trim_activate() : void
- Store the defaults on activation; never overwrites an existing option.
Functions
jpkcom_harden_trim_option_name()
Name of the option holding all settings.
jpkcom_harden_trim_option_name() : string
Stored structure: ['schema' => 1, 'updated' => string, 'settings' => array].
Tags
Return values
string —Option name.
jpkcom_harden_trim_sanitize_value()
Validate one value against its schema entry.
jpkcom_harden_trim_sanitize_value(string $key, mixed $value) : array{valid: bool, value: mixed, dropped: list}
Role lists are valid as soon as the type is right (array or comma
string); entries that are no existing role slug are dropped and listed
in dropped. Never throws.
Parameters
- $key : string
-
Setting key.
- $value : mixed
-
Raw value (form, import, constant, database).
Tags
Return values
array{valid: bool, value: mixed, dropped: listvalue is null when invalid.
jpkcom_harden_trim_sanitize_settings()
Coerce any input into a complete, valid settings array.
jpkcom_harden_trim_sanitize_settings(mixed $input) : array{settings: array, rejected: list}
Missing keys get their default, invalid values fall back to the default,
unknown keys are dropped. Rejected keys and dropped role slugs
(<key>:<slug>) are reported. Never throws.
Parameters
- $input : mixed
-
Value from the database, an import, a form or a filter.
Tags
Return values
array{settings: arrayjpkcom_harden_trim_get_stored()
The stored settings, sanitised and merged over the defaults.
jpkcom_harden_trim_get_stored() : array<string, mixed>
Tags
Return values
array<string, mixed> —Key => stored value.
jpkcom_harden_trim_constant_state()
State of the plugin constant for one key (`JPKCOM_HARDEN_TRIM_<KEY>`).
jpkcom_harden_trim_constant_state(string $key) : array{name: string, defined: bool, valid: bool, value: mixed}
Parameters
- $key : string
-
Setting key.
Tags
Return values
array{name: string, defined: bool, valid: bool, value: mixed} —value is null unless valid.
jpkcom_harden_trim_core_lock()
The core constant that locks a key, if it was defined before the plugin loaded.
jpkcom_harden_trim_core_lock(string $key) : array{name: string, value: mixed}|null
Reads the snapshot taken by the main file, so a constant this plugin defines itself never counts as a lock.
Parameters
- $key : string
-
Setting key.
Tags
Return values
array{name: string, value: mixed}|null —Null when not locked.
jpkcom_harden_trim_locked_keys()
Every key whose value the settings page cannot change.
jpkcom_harden_trim_locked_keys() : array<string, string>
A core constant defined before the plugin loaded names the lock: core honours it and the feature code then does nothing, so it is what is in effect even when a plugin constant for the same key exists. Otherwise a valid plugin constant names the lock.
Tags
Return values
array<string, string> —Key => constant name.
jpkcom_harden_trim_kill_switch_state()
State of the kill switch `JPKCOM_HARDEN_TRIM_DISABLE_ALL`.
jpkcom_harden_trim_kill_switch_state() : array{defined: bool, valid: bool, active: bool}
Parsed like every boolean setting constant, with filter_var( FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ): the strings 'false', 'off', 'no' and '0' do not switch everything off. A value that is no boolean is ignored and named in the invalid-constants notice.
Tags
Return values
array{defined: bool, valid: bool, active: bool}jpkcom_harden_trim_kill_switch()
Whether the kill switch is on (no feature hooks at all).
jpkcom_harden_trim_kill_switch() : bool
Tags
Return values
bool —True when JPKCOM_HARDEN_TRIM_DISABLE_ALL holds a true boolean value.
jpkcom_harden_trim_invalid_constants()
Names of plugin constants that are defined but hold an invalid value.
jpkcom_harden_trim_invalid_constants() : array<int, string>
Such constants are ignored; the settings page names them in a notice. Covers the setting constants and the kill switch.
Tags
Return values
array<int, string> —Constant names.
jpkcom_harden_trim_settings_cache()
Holder of the effective-settings cache, one entry per site.
jpkcom_harden_trim_settings_cache([string $op = 'get' ][, array<string, mixed>|null $value = null ]) : array<string, mixed>|null
Settings are per site (there is no network settings screen). Keyed by
get_current_blog_id(), so after switch_to_blog() every reader gets the
switched site's settings instead of the booting site's. reset forgets
every site.
Parameters
- $op : string = 'get'
-
get,setorreset. - $value : array<string, mixed>|null = null
-
Value for
set.
Tags
Return values
array<string, mixed>|null —The current site's cached settings, null when empty.
jpkcom_harden_trim_settings_reset_cache()
Forget the cached effective settings.
jpkcom_harden_trim_settings_reset_cache() : void
Tags
jpkcom_harden_trim_settings()
The effective settings: valid plugin constant, else stored value, else default.
jpkcom_harden_trim_settings() : array<string, mixed>
The whole array then passes the jpkcom_harden_trim_settings filter and
the sanitiser again. Core locks do not change the value here; the feature
code skips acting when a core lock exists, because the core constant
already wins inside core.
Tags
Return values
array<string, mixed> —Key => effective value.
jpkcom_harden_trim_save_settings()
Sanitise and store a complete settings array.
jpkcom_harden_trim_save_settings(array<string, mixed> $input[, bool $keep_locked = true ]) : array{settings: array, rejected: list}
Keys missing from $input are stored with their default (a form sends
every field; an unchecked role list sends nothing). Locked keys keep their
stored value, whatever the input says, unless $keep_locked is false:
an import stores what the file says for them too, and the
constant still wins at runtime.
Parameters
- $input : array<string, mixed>
-
Raw settings.
- $keep_locked : bool = true
-
Whether locked keys keep their stored value.
Tags
Return values
array{settings: arrayWhat was stored, and what was rejected.
jpkcom_harden_trim_apply_preset()
Apply a preset to a settings array (does not store it).
jpkcom_harden_trim_apply_preset(string $preset, array<string, mixed> $current) : array{settings: array, changed: list}
Every unlocked preset-managed key gets the preset's value or, if the preset does not list it, its default. All other keys stay as they are.
Parameters
- $preset : string
-
harmless, recommended or strict.
- $current : array<string, mixed>
-
Settings to start from.
Tags
Return values
array{settings: arrayNew settings and the keys whose value changed.
jpkcom_harden_trim_user_roles()
The roles a user holds for the role-based settings.
jpkcom_harden_trim_user_roles(WP_User $user) : array<int, string>
On multisite WP_User::roles only lists the current site's roles, so a
super admin without a role on a site has none there. Policy: a super
admin counts as administrator on every site of the network, both for
the exempt roles and for the application-password roles. On a single
site is_super_admin() is not consulted.
Parameters
- $user : WP_User
-
User.
Tags
Return values
array<int, string> —Role slugs.
jpkcom_harden_trim_is_exempt()
Whether a user is exempt from the role-scoped features.
jpkcom_harden_trim_is_exempt([WP_User|null $user = null ]) : bool
With no exempt roles and no filter callback, the current user is never looked up, so the default settings cannot trigger an early user lookup. Never cached: the answer depends on the user and is decided per call.
Parameters
- $user : WP_User|null = null
-
User to check; null means the current user.
Tags
Return values
bool —True when the user holds an exempt role.
jpkcom_harden_trim_is_active_value()
Whether a value means "the feature does something".
jpkcom_harden_trim_is_active_value(string $key, mixed $value) : bool
Parameters
- $key : string
-
Setting key.
- $value : mixed
-
Value to judge.
Tags
Return values
bool —True when the value switches the feature on.
jpkcom_harden_trim_feature_applies()
Whether a feature acts for the current request.
jpkcom_harden_trim_feature_applies(string $key) : bool
Active value, and for role-scoped keys a user who is not exempt. Feature modules call this inside their callbacks, where the user is known.
Parameters
- $key : string
-
Setting key.
Tags
Return values
bool —True when the feature should act.
jpkcom_harden_trim_activate()
Store the defaults on activation; never overwrites an existing option.
jpkcom_harden_trim_activate() : void