Documentation

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`.
pullJson()  : mixed
Pull a JSON file from a remote URL and parse it, effectively by streaming it.
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
string

isJson()

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
see
https://stackoverflow.com/a/6041773/5506547
Return values
array<string|int, mixed>|false

isRest()

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
returns

boolean

author

matzeeable

see
https://gist.github.com/matzeeable/dfd82239f48c2fedef25141e48c8dc30

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

pullJson()

Pull a JSON file from a remote URL and parse it, effectively by streaming it.

public static pullJson(string $url[, array<string|int, string> $instantPointers = [] ][, array<string|int, string> $arrayInstantPointers = [] ][, array<string|int, string> $nonInstantPointers = [] ][, bool $useFallback = false ]) : mixed

If streaming is not possible, it falls back to a normal wp_remote_get call which could cause memory issues.

Attention: When using this static method, make sure to install jsonmachine on your project as this is a peer dependency.

Parameters
$url : string
$instantPointers : array<string|int, string> = []

This pathes are used to read scalar values which are not too big so we can directly parse them

$arrayInstantPointers : array<string|int, string> = []

This pathes are used to read array values which are not too big so we can directly parse them

$nonInstantPointers : array<string|int, string> = []

This pathes are used to read non-scalar values which are too big so we parse them via streaming

$useFallback : bool = false

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
see
https://www.programmingalgorithms.com/algorithm/vigenere-cipher/php/

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
see
https://gist.github.com/westonruter/6647252#gistcomment-2668616
Return values
mixed

value returned by $callback


        
On this page

Search results