Utils
in package
Utility helpers.
Table of Contents
Methods
- arrayModifyByKeyPath() : mixed
- Allows to set an array value by passing a key path like `my.awesome.key`.
- enableOptionAutoload() : mixed
- Add an option to autoloading with default, and additionally a filter like `boolval`.
- getActivePluginsMap() : mixed
- Get the list of active plugins in a map: File => Name. This is needed for the config and the notice for `skip-if-active` attribute in cookie opt-in codes.
- getNonceSalt() : string
- Get the nonce salt of the current WordPress installation. This one can be used to hash data unique to the WordPress instance.
- isJson() : array<string|int, mixed>|false
- Check if passed string is JSON.
- isRest() : mixed
- Checks if the current request is a WP REST API request.
- joinWithAndSeparator() : mixed
- Join an array of strings together with comma and the last one with `and`.
- runDirectFilesystem() : mixed|WP_Error
- Run a command temporarily in `direct` filesystem mode. This is helpful when you want to e.g. `unzip_file` a file to the `wp-content/uploads` folder.
- simpleHash() : mixed
- This hash function is used to generate a simple hash from a given string. This is very simple so it can be used in frontend (e.g. Webpack chunk loading).
- simpleObfuscate() : mixed
- This obfuscate function is used to generate a simple encrypted string from a text and secret. This is very simple so it can be used in frontend (e.g. URL obfuscating). This is not a real encryption as it uses the Vignere Cipher implementation.
- suspenseHook() : mixed
- Allows you to find a hook by criteria and suspend the filter. In general, it returns two closures, the first one allows you to suspend and the second one to continue.
- withoutFilters() : mixed
- Run $callback with the $handler disabled for the $hook action/filter
Methods
arrayModifyByKeyPath()
Allows to set an array value by passing a key path like `my.awesome.key`.
public
static arrayModifyByKeyPath(array<string|int, mixed> &$array, string $keyPath, callable $callback) : mixed
Parameters
- $array : array<string|int, mixed>
- $keyPath : string
- $callback : callable
enableOptionAutoload()
Add an option to autoloading with default, and additionally a filter like `boolval`.
public
static enableOptionAutoload(string $optionName, mixed $default[, callable $filter = null ]) : mixed
Parameters
- $optionName : string
- $default : mixed
- $filter : callable = null
getActivePluginsMap()
Get the list of active plugins in a map: File => Name. This is needed for the config and the notice for `skip-if-active` attribute in cookie opt-in codes.
public
static getActivePluginsMap([bool $includeSlugs = true ][, callable $filter = null ]) : mixed
Parameters
- $includeSlugs : bool = true
- $filter : callable = null
getNonceSalt()
Get the nonce salt of the current WordPress installation. This one can be used to hash data unique to the WordPress instance.
public
static getNonceSalt() : string
Return values
stringisJson()
Check if passed string is JSON.
public
static isJson(string $string[, mixed $default = false ]) : array<string|int, mixed>|false
Parameters
- $string : string
- $default : mixed = false
Tags
Return values
array<string|int, mixed>|falseisRest()
Checks if the current request is a WP REST API request.
public
static isRest() : mixed
Case #1: After WP_REST_Request initialisation Case #2: Support "plain" permalink settings Case #3: It can happen that WP_Rewrite is not yet initialized, so do this (wp-settings.php) Case #4: URL Path begins with wp-json/ (your REST prefix) Also supports WP installations in subfolders
Tags
joinWithAndSeparator()
Join an array of strings together with comma and the last one with `and`.
public
static joinWithAndSeparator(array<string|int, string> $array, string $andSeparator) : mixed
Parameters
- $array : array<string|int, string>
- $andSeparator : string
runDirectFilesystem()
Run a command temporarily in `direct` filesystem mode. This is helpful when you want to e.g. `unzip_file` a file to the `wp-content/uploads` folder.
public
static runDirectFilesystem(callable $callback[, callable $teardown = null ]) : mixed|WP_Error
Attention: Make sure that the folder you want to interact with is writable by the PHP FPM user.
Parameters
- $callback : callable
-
Arguments: WP_Filesystem_Direct $fs
- $teardown : callable = null
-
This is always called, e.g. to remove a temporary archive file
Return values
mixed|WP_Error —The result of the callback or WP_Error
when exception occurs
simpleHash()
This hash function is used to generate a simple hash from a given string. This is very simple so it can be used in frontend (e.g. Webpack chunk loading).
public
static simpleHash(string $s) : mixed
Parameters
- $s : string
simpleObfuscate()
This obfuscate function is used to generate a simple encrypted string from a text and secret. This is very simple so it can be used in frontend (e.g. URL obfuscating). This is not a real encryption as it uses the Vignere Cipher implementation.
public
static simpleObfuscate(string $input, string $key, bool $encipher) : mixed
Parameters
- $input : string
- $key : string
-
The key needs to contain only alphanumeric values, e.g. no spaces
- $encipher : bool
Tags
suspenseHook()
Allows you to find a hook by criteria and suspend the filter. In general, it returns two closures, the first one allows you to suspend and the second one to continue.
public
static suspenseHook(string $hook, callable $criteriaFilter) : mixed
Parameters
- $hook : string
-
The hook name.
- $criteriaFilter : callable
-
Arguments: function($function, $acceptedArgs, $priority)
withoutFilters()
Run $callback with the $handler disabled for the $hook action/filter
public
static withoutFilters(string $hook, callable $callback) : mixed
Parameters
- $hook : string
-
filter name
- $callback : callable
-
function execited while filter disabled
Tags
Return values
mixed —value returned by $callback