Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Last active April 9, 2019 06:48
Show Gist options
  • Save monkeymonk/f9120a0788e64cfe32c37cba112f2d15 to your computer and use it in GitHub Desktop.
Save monkeymonk/f9120a0788e64cfe32c37cba112f2d15 to your computer and use it in GitHub Desktop.
Wordpress Useful Helpers #wordpress
<?php
if (!function_exists('add_actions')) {
function add_actions(array $tags, $function, $priority = 10, $accepted_args = 1)
{
foreach ($tags as $tag) {
add_action($tag, $function, $priority, $accepted_args);
}
}
}
if (!function_exists('add_filters')) {
function add_filters(array $tags, $function, $priority = 10, $accepted_args = 1)
{
foreach ($tags as $tag) {
add_filter($tag, $function, $priority, $accepted_args);
}
}
}
if (!function_exists('array_only')) {
/**
* Get a subset of the items from the given array.
*
* @param array $array
* @param array|string $keys
* @return array
*/
function array_only($array, $keys)
{
return array_intersect_key($array, array_flip((array) $keys));
}
}
if (!function_exists('array_get')) {
/**
* Get an item from an array using "dot" notation.
*
* @param array $array
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function array_get($array, $key, $default = null)
{
if (is_null($key)) {
return $array;
}
if (isset($array[$key])) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
return value($default);
}
$array = $array[$segment];
}
return $array;
}
}
if (!function_exists('array_set')) {
/**
* Set an array item to a given value using "dot" notation.
*
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param string $key
* @param mixed $value
*
* @return array
*/
function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
while (count($keys) > 1) {
$key = array_shift($keys);
// If the key doesn't exist at this depth, we will just create an empty array
// to hold the next value, allowing us to create the arrays to hold final
// values at the correct depth. Then we'll keep digging into the array.
if (!isset($array[$key]) || !is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$array[array_shift($keys)] = $value;
return $array;
}
}
if (! function_exists('array_where')) {
/**
* Filter the array using the given callback.
*
* @param array $array
* @param callable $callback
* @return array
*/
function array_where($array, callable $callback)
{
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
}
}
if (!function_exists('array_keys_exists')) {
function array_keys_exists(array $keys, array $search)
{
$count = 0;
foreach ($keys as $key) {
if (array_key_exists($key, $search)) {
$count++;
}
}
return count($keys) == $count;
}
}
if (!function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
*
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
if (!function_exists('str_contains')) {
/**
* Determine if a given string contains a given substring.
*
* @param string $haystack
* @param string|array $needles
*
* @return bool
*/
function str_contains($haystack, $needles)
{
foreach ((array)$needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment