Documentation

KeyValueMapOption
in package

Adds an option via `add_option` which represents a simple map / array of key value pairs.

It also provides some methods to generate notices with dismiss-functionality (best use-case for a key-value map), and allowing to modify the value via REST API.

Table of Contents

Constants

REST_NAME  = 'key-value-map'

Properties

$core  : mixed
$migrations  : array<string|int, callable>
See `registerMigration`.
$migrationsForKey  : array<string|int, callable>
See `registerMigrationForKey`.
$modifiers  : array<string|int, callable>
See `registerModifier`.
$name  : mixed
$siteWide  : mixed

Methods

__construct()  : mixed
C'tor.
get()  : mixed
Get value by key.
getKeysStartingWith()  : mixed
Get an array with keys starting with a given string.
getMap()  : array<string|int, mixed>
Get the map as array.
getName()  : mixed
Get option name.
getRestEndpointForKey()  : mixed
Generate the REST endpoint to a given key.
isSiteWide()  : mixed
Get if option is site-wide.
noticeDismissOnClickHandler()  : mixed
Generate a `onclick` JavaScript coding which automatically hides the notice, sends a request via REST API and optionally redirects to a given page.
registerMigration()  : mixed
Allows to register a migration for non-existing map. The passed callable is executed, when the map does not yet exist in database. The callable gets called with the newly created map so you can modify and return it.
registerMigrationForKey()  : mixed
Allows to register a migration for non-existing keys. The passed callable is executed, when you try to `$this->get()` a specific key which does not yet exist in the map. The return value of the callable is used for the map value and automatically persisted to database.
registerModifier()  : mixed
Allows to register a modifier for a value of. The passed callable is executed, when you `$this->set()` a new value for a key. The callable is called with the key name as first argument and the value as the second argument. The callable needs to return the new value.
registerRestForKey()  : mixed
Registers a REST route to your existing REST Service which allows updating the value of a given key via `PATCH`, e.g. `wp-json/real-cookie-banner/v1/key-value-map/{name}/{key}` and send a `value` as data.
set()  : mixed
Set value by key.
enableAutoload()  : mixed
Registers the option as autoload in database. This is not supported for site-wide options.
persistMap()  : mixed
Persist map to `wp_options`.

Constants

Properties

$migrations

See `registerMigration`.

private array<string|int, callable> $migrations

$migrationsForKey

See `registerMigrationForKey`.

private array<string|int, callable> $migrationsForKey

$modifiers

See `registerModifier`.

private array<string|int, callable> $modifiers

Methods

__construct()

C'tor.

public __construct(string $name[, Core $core = null ][, bool $siteWide = false ][, bool $autoload = true ]) : mixed
Parameters
$name : string

The option name

$core : Core = null

Pass a Core instance to enable helpers for REST API requests

$siteWide : bool = false
$autoload : bool = true
Tags
codeCoverageIgnore

get()

Get value by key.

public get(string $key[, mixed $default = false ]) : mixed
Parameters
$key : string
$default : mixed = false

getKeysStartingWith()

Get an array with keys starting with a given string.

public getKeysStartingWith(string $prefix) : mixed
Parameters
$prefix : string

getMap()

Get the map as array.

public getMap() : array<string|int, mixed>
Return values
array<string|int, mixed>

getName()

Get option name.

public getName() : mixed
Tags
codeCoverageIgnore

getRestEndpointForKey()

Generate the REST endpoint to a given key.

public getRestEndpointForKey(string $key) : mixed
Parameters
$key : string

isSiteWide()

Get if option is site-wide.

public isSiteWide() : mixed
Tags
codeCoverageIgnore

noticeDismissOnClickHandler()

Generate a `onclick` JavaScript coding which automatically hides the notice, sends a request via REST API and optionally redirects to a given page.

public noticeDismissOnClickHandler(string $key, string $value[, string $redirect = false ]) : mixed
Parameters
$key : string
$value : string
$redirect : string = false

registerMigration()

Allows to register a migration for non-existing map. The passed callable is executed, when the map does not yet exist in database. The callable gets called with the newly created map so you can modify and return it.

public registerMigration(callable $callable) : mixed
Parameters
$callable : callable

registerMigrationForKey()

Allows to register a migration for non-existing keys. The passed callable is executed, when you try to `$this->get()` a specific key which does not yet exist in the map. The return value of the callable is used for the map value and automatically persisted to database.

public registerMigrationForKey(string $key, callable $callable) : mixed
Parameters
$key : string
$callable : callable

registerModifier()

Allows to register a modifier for a value of. The passed callable is executed, when you `$this->set()` a new value for a key. The callable is called with the key name as first argument and the value as the second argument. The callable needs to return the new value.

public registerModifier(callable $callable) : mixed
Parameters
$callable : callable

registerRestForKey()

Registers a REST route to your existing REST Service which allows updating the value of a given key via `PATCH`, e.g. `wp-json/real-cookie-banner/v1/key-value-map/{name}/{key}` and send a `value` as data.

public registerRestForKey(string $capability, string $key, string $args) : mixed
Parameters
$capability : string

Minimum required capability to call this route

$key : string

Pass a regular expression with leading and trailing slashes e.g. /[a-zA-Z0-9_-]+/

$args : string

Argument validation for value (see also https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/#arguments)

set()

Set value by key.

public set(string $key, mixed $value) : mixed
Parameters
$key : string
$value : mixed

Use null to remove the key from the map

enableAutoload()

Registers the option as autoload in database. This is not supported for site-wide options.

protected enableAutoload() : mixed

persistMap()

Persist map to `wp_options`.

protected persistMap(array<string|int, mixed> $map) : mixed
Parameters
$map : array<string|int, mixed>

        
On this page

Search results