Skip to main content

Helpers

Introduction

wp-kit ships static utility classes under WPKit\Helpers. They cover the gaps between PHP's array functions and what plugin code keeps needing.

Arr

Arr works on arrays with dot notation, so nested config and request payloads read cleanly:

use YourPlugin\Deps\BitApps\WPKit\Helpers\Arr;

Arr::get($payload, 'auth.credentials.token', 'default');
Arr::set($payload, 'auth.credentials.token', $token);
Arr::has($payload, 'auth.token');
Arr::forget($payload, 'auth.secret');

Selecting

Arr::only($array, ['id', 'title']); // keep just these keys
Arr::except($array, ['password']); // drop these keys
Arr::pluck($records, 'title', 'id'); // column, optionally keyed
Arr::where($array, fn ($v) => $v > 10); // filter by callback
Arr::first($array, $callback, $default);
Arr::last($array, $callback, $default);

Reshaping

Arr::flatten($nested); // one level deep by default; pass a depth
Arr::collapse($arrayOfArrays);
Arr::dot($nested); // ['auth.token' => '...']
Arr::divide($array); // [keys, values]
Arr::wrap($value); // scalar becomes [scalar]; array passes through
Arr::prepend($array, $value);
Arr::pull($array, 'key'); // read and remove

Inspecting and ordering

Arr::isAssoc($array);
Arr::accessible($value); // array or ArrayAccess
Arr::exists($array, 'key'); // array_key_exists, ArrayAccess-aware
Arr::sort($array);
Arr::sortRecursive($array);
Arr::shuffle($array, $seed);
Arr::random($array, 3);
Arr::query($array); // http_build_query
Arr::toCssClasses(['btn' => true, 'btn-lg' => $isLarge]);

JSON

JSON wraps json_encode/json_decode with the "maybe" variants that leave already-converted values alone:

use YourPlugin\Deps\BitApps\WPKit\Helpers\JSON;

JSON::encode($data);
JSON::decode($json, true); // associative

JSON::maybeEncode($value); // encodes only if not already a string
JSON::maybeDecode($value); // decodes only if it parses as JSON
JSON::is($string); // is this valid JSON?

maybeDecode() is the useful one when reading a column that may hold either JSON or a plain string.

Slug

use YourPlugin\Deps\BitApps\WPKit\Helpers\Slug;

Slug::generate('My Flow Name'); // my-flow-name

DateTimeHelper

Date handling that respects the site's timezone rather than the server's:

use YourPlugin\Deps\BitApps\WPKit\Helpers\DateTimeHelper;

$helper = new DateTimeHelper();

$helper->getCurrentDateTime();
$helper->getDate($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
$helper->getTime($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
$helper->getFormated($dateString, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ);
$helper->getDay('short', $date); // day name
$helper->getMonth('long', $date); // month name

Static timezone accessors mirror WordPress's own:

DateTimeHelper::wp_timezone_string();
DateTimeHelper::wp_timezone();
note

Converting between a third-party API's format and the site's requires both format and timezone on each side, which is why the signatures are long. Pass null for any side you want left at the WordPress default.

Capabilities

use YourPlugin\Deps\BitApps\WPKit\Utils\Capabilities;

Capabilities::check('manage_options');
Capabilities::filter('your_plugin_manage', 'manage_options');

See Nonces & Capabilities for how these gate routes.