| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- <?php
- /*******************************************************************************
- * Copyright (c) 2019, Code Atlantic LLC
- ******************************************************************************/
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- /**
- * Class PUM_Utils_Array
- *
- * Various functions to help manipulating arrays.
- */
- class PUM_Utils_Array {
- /**
- * Filters out null values.
- *
- * @param array $array
- *
- * @return array
- */
- public static function filter_null( $array = [] ) {
- return array_filter( $array, [ __CLASS__, '_filter_null' ] );
- }
- /**
- * @param null $val
- *
- * @return bool
- */
- public static function _filter_null( $val = null ) {
- return isset( $val );
- }
- /**
- * Clean variables using sanitize_text_field.
- *
- * @param $var
- *
- * @return array|string
- */
- public static function sanitize( $var ) {
- if ( is_string( $var ) ) {
- return sanitize_text_field( $var );
- }
- return array_map( [ __CLASS__, 'sanitize' ], (array) $var );
- }
- /**
- * Helper function to move or swap array keys in various ways.
- *
- * PUM_Utils_Array::move_item($arr, 'move me', 'up'); //move it one up
- * PUM_Utils_Array::move_item($arr, 'move me', 'down'); //move it one down
- * PUM_Utils_Array::move_item($arr, 'move me', 'top'); //move it to top
- * PUM_Utils_Array::move_item($arr, 'move me', 'bottom'); //move it to bottom
- *
- * PUM_Utils_Array::move_item($arr, 'move me', -1); //move it one up
- * PUM_Utils_Array::move_item($arr, 'move me', 1); //move it one down
- * PUM_Utils_Array::move_item($arr, 'move me', 2); //move it two down
- *
- * PUM_Utils_Array::move_item($arr, 'move me', 'before', 'b'); //move it before ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', 'up', 'b'); //move it before ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', -1, 'b'); //move it before ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', 'after', 'b'); //move it after ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', 'down', 'b'); //move it after ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', 1, 'b'); //move it after ['b']
- * PUM_Utils_Array::move_item($arr, 'move me', 2, 'b'); //move it two positions after ['b']
- *
- * Special syntax, to swap two elements:
- * PUM_Utils_Array::move_item($arr, 'a', 0, 'd'); //Swap ['a'] with ['d']
- * PUM_Utils_Array::move_item($arr, 'a', 'swap', 'd'); //Swap ['a'] with ['d']
- *
- * @param array $ref_arr
- * @param string $key1
- * @param int|string $move
- * @param string|null $key2
- *
- * @return bool
- */
- public static function move_item( &$ref_arr, $key1, $move, $key2 = null ) {
- $arr = $ref_arr;
- if ( $key2 == null ) {
- $key2 = $key1;
- }
- if ( ! isset( $arr[ $key1 ] ) || ! isset( $arr[ $key2 ] ) ) {
- return false;
- }
- $i = 0;
- foreach ( $arr as &$val ) {
- $val = [
- 'sort' => ( ++ $i * 10 ),
- 'val' => $val,
- ];
- }
- // Add a quick keyword `swap` to make syntax simpler to remember.
- if ( 'swap' === $move ) {
- $move = 0;
- }
- if ( is_numeric( $move ) ) {
- if ( $move == 0 && $key1 == $key2 ) {
- return true;
- } elseif ( $move == 0 ) {
- $tmp = $arr[ $key1 ]['sort'];
- $arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'];
- $arr[ $key2 ]['sort'] = $tmp;
- } else {
- $arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] + ( $move * 10 + ( $key1 == $key2 ? ( $move < 0 ? - 5 : 5 ) : 0 ) );
- }
- } else {
- switch ( $move ) {
- case 'up':
- case 'before':
- $arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] - ( $key1 == $key2 ? 15 : 5 );
- break;
- case 'down':
- case 'after':
- $arr[ $key1 ]['sort'] = $arr[ $key2 ]['sort'] + ( $key1 == $key2 ? 15 : 5 );
- break;
- case 'top':
- $arr[ $key1 ]['sort'] = 5;
- break;
- case 'bottom':
- $arr[ $key1 ]['sort'] = $i * 10 + 5;
- break;
- default:
- return false;
- }
- }
- uasort( $arr, [ __CLASS__, 'sort_by_sort' ] );
- foreach ( $arr as &$val ) {
- $val = $val['val'];
- }
- $ref_arr = $arr;
- return true;
- }
- /**
- * Pluck all array keys beginning with string.
- *
- * @param array $array
- * @param bool|string|array $strings
- *
- * @return array
- */
- public static function pluck_keys_starting_with( $array, $strings = [] ) {
- $to_be_removed = self::remove_keys_starting_with( $array, $strings );
- return array_diff_key( $array, $to_be_removed );
- }
- /**
- * Pluck all array keys ending with string.
- *
- * @param array $array
- * @param bool|string|array $strings
- *
- * @return array
- */
- public static function pluck_keys_ending_with( $array, $strings = [] ) {
- $to_be_removed = self::remove_keys_ending_with( $array, $strings );
- return array_diff_key( $array, $to_be_removed );
- }
- /**
- * Extract only allowed keys from an array.
- *
- * @param array $array Array to be extracted from.
- * @param string[] $allowed_keys List of keys.
- *
- * @return array
- */
- public static function allowed_keys( $array, $allowed_keys = [] ) {
- return array_intersect_key( $array, array_flip( $allowed_keys ) );
- }
- /**
- * This works exactly the same as wp_parse_args, except we remove unused keys for sanitization.
- *
- * @param array $array Array to be parsed.
- * @param array $allowed_args Array of key=>defaultValue pairs for each allowed argument.
- *
- * @return array
- */
- public static function parse_allowed_args( $array, $allowed_args = [] ) {
- $array = wp_parse_args( $array, $allowed_args );
- return self::allowed_keys( $array, array_keys( $allowed_args ) );
- }
- /**
- * Pluck specified array keys.
- *
- * @param array $array
- * @param string[] $keys
- *
- * @return array
- */
- public static function pluck( $array, $keys = [] ) {
- return self::pluck_keys_containing( $array, $keys );
- }
- /**
- * Pluck all array keys containing a string or strings.
- *
- * @param array $array
- * @param string[] $strings
- *
- * @return array
- */
- public static function pluck_keys_containing( $array, $strings = [] ) {
- $to_be_removed = self::remove_keys_containing( $array, $strings );
- return array_diff_key( $array, $to_be_removed );
- }
- /**
- * Remove all array keys beginning with string.
- *
- * @param array $array
- * @param string[] $strings
- *
- * @return array
- */
- public static function remove_keys_starting_with( $array, $strings = [] ) {
- if ( ! $strings ) {
- return $array;
- }
- if ( ! is_array( $strings ) ) {
- $strings = [ $strings ];
- }
- foreach ( $array as $key => $value ) {
- foreach ( $strings as $string ) {
- if ( strpos( $key, $string ) === 0 ) {
- unset( $array[ $key ] );
- }
- }
- }
- return $array;
- }
- /**
- * Remove all array keys ending with string.
- *
- * @param array $array
- * @param bool|string|array $strings
- *
- * @return array
- */
- public static function remove_keys_ending_with( $array, $strings = [] ) {
- if ( ! $strings ) {
- return $array;
- }
- if ( ! is_array( $strings ) ) {
- $strings = [ $strings ];
- }
- foreach ( $array as $key => $value ) {
- foreach ( $strings as $string ) {
- $length = strlen( $string );
- if ( substr( $key, - $length ) === $string ) {
- unset( $array[ $key ] );
- }
- }
- }
- return $array;
- }
- /**
- * Remove all array keys containing string.
- *
- * @param array $array
- * @param bool|string|array $strings
- *
- * @return array
- */
- public static function remove_keys_containing( $array, $strings = [] ) {
- if ( ! $strings ) {
- return $array;
- }
- if ( ! is_array( $strings ) ) {
- $strings = [ $strings ];
- }
- foreach ( $array as $key => $value ) {
- foreach ( $strings as $string ) {
- if ( strpos( $key, $string ) !== false ) {
- unset( $array[ $key ] );
- }
- }
- }
- return $array;
- }
- /**
- * Remove all array keys containing string.
- *
- * @param array $array
- * @param string|array $keys
- *
- * @return array
- */
- public static function remove_keys( $array, $keys = [] ) {
- if ( empty( $keys ) ) {
- return $array;
- }
- if ( is_string( $keys ) ) {
- $keys = [ $keys ];
- }
- foreach ( (array) $keys as $key ) {
- if ( is_string( $key ) && array_key_exists( $key, $array ) ) {
- unset( $array[ $key ] );
- }
- }
- return $array;
- }
- /**
- * Sort nested arrays with various options.
- *
- * @param array $array
- * @param string $type
- * @param bool $reverse
- *
- * @return array
- */
- public static function sort( $array = [], $type = 'key', $reverse = false ) {
- if ( ! is_array( $array ) ) {
- return $array;
- }
- switch ( $type ) {
- case 'key':
- if ( ! $reverse ) {
- ksort( $array );
- } else {
- krsort( $array );
- }
- break;
- case 'natural':
- natsort( $array );
- break;
- case 'priority':
- if ( ! $reverse ) {
- uasort( $array, [ __CLASS__, 'sort_by_priority' ] );
- } else {
- uasort( $array, [ __CLASS__, 'rsort_by_priority' ] );
- }
- break;
- }
- return $array;
- }
- /**
- * @param $a
- * @param $b
- *
- * @return bool
- */
- public static function sort_by_sort( $a, $b ) {
- return $a['sort'] > $b['sort'];
- }
- /**
- * Sort array by priority value
- *
- * @param $a
- * @param $b
- *
- * @return int
- */
- public static function sort_by_priority( $a, $b ) {
- $pri_a = isset( $a['pri'] ) ? $a['pri'] : ( isset( $a['priority'] ) ? $a['priority'] : false );
- $pri_b = isset( $b['pri'] ) ? $b['pri'] : ( isset( $b['priority'] ) ? $b['priority'] : false );
- if ( ! is_numeric( $pri_a ) || ! is_numeric( $pri_b ) || $pri_a === $pri_b ) {
- return 0;
- }
- return ( $pri_a < $pri_b ) ? - 1 : 1;
- }
- /**
- * Sort array in reverse by priority value
- *
- * @param $a
- * @param $b
- *
- * @return int
- */
- public static function rsort_by_priority( $a, $b ) {
- $pri_a = isset( $a['pri'] ) ? $a['pri'] : ( isset( $a['priority'] ) ? $a['priority'] : false );
- $pri_b = isset( $b['pri'] ) ? $b['pri'] : ( isset( $b['priority'] ) ? $b['priority'] : false );
- if ( ! is_numeric( $pri_a ) || ! is_numeric( $pri_b ) || $pri_a === $pri_b ) {
- return 0;
- }
- return ( $pri_a < $pri_b ) ? 1 : - 1;
- }
- /**
- * Replace array key with new key name in same order
- *
- * @param $array
- * @param $old_key
- * @param $new_key
- *
- * @return array
- */
- public static function replace_key( $array, $old_key, $new_key ) {
- $keys = array_keys( $array );
- if ( false === $index = array_search( $old_key, $keys, true ) ) {
- // throw new \Exception( sprintf( 'Key "%s" does not exit', $old_key ) );
- }
- $keys[ $index ] = $new_key;
- return array_combine( $keys, array_values( $array ) );
- }
- /**
- * Converts 'false' & 'true' string values in any array to proper boolean values.
- *
- * @param array|mixed $data
- *
- * @return array|mixed
- */
- public static function fix_json_boolean_values( $data ) {
- if ( is_array( $data ) ) {
- foreach ( (array) $data as $key => $value ) {
- if ( is_string( $value ) && in_array( $value, [ 'true', 'false' ] ) ) {
- $data[ $key ] = json_decode( $value );
- } elseif ( is_array( $value ) ) {
- $data[ $key ] = self::fix_json_boolean_values( $value );
- }
- }
- }
- return $data;
- }
- /**
- * @param $obj
- *
- * @return array
- */
- public static function from_object( $obj ) {
- if ( is_object( $obj ) ) {
- $obj = (array) $obj;
- }
- if ( is_array( $obj ) ) {
- $new = [];
- foreach ( $obj as $key => $val ) {
- $new[ $key ] = self::from_object( $val );
- }
- } else {
- $new = $obj;
- }
- return $new;
- }
- /**
- * @param $array
- *
- * @return array
- */
- public static function safe_json_decode( $array ) {
- if ( ! empty( $array ) && is_string( $array ) ) {
- if ( strpos( $array, '\"' ) >= 0 ) {
- $array = stripslashes( $array );
- }
- $array = json_decode( $array );
- $array = self::from_object( $array );
- $array = self::fix_json_boolean_values( $array );
- }
- return (array) $array;
- }
- /**
- * Ensures proper encoding for strings before json_encode is used.
- *
- * @param array|string $data
- *
- * @return mixed|string
- */
- public static function safe_json_encode( $data = [] ) {
- return wp_json_encode( self::make_safe_for_json_encode( $data ) );
- }
- /**
- * json_encode only accepts valid UTF8 characters, thus we need to properly convert translations and other data to proper utf.
- *
- * This function does that recursively.
- *
- * @param array|string $data
- *
- * @return array|string
- */
- public static function make_safe_for_json_encode( $data = [] ) {
- if ( is_scalar( $data ) ) {
- return html_entity_decode( (string) $data, ENT_QUOTES, 'UTF-8' );
- }
- if ( is_array( $data ) ) {
- foreach ( (array) $data as $key => $value ) {
- if ( is_scalar( $value ) && ! is_bool( $value ) ) {
- $data[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
- } elseif ( is_array( $value ) ) {
- $data[ $key ] = self::make_safe_for_json_encode( $value );
- }
- }
- }
- return $data;
- }
- /**
- * @param $d
- *
- * @return array|string
- */
- public static function utf8_encode_recursive( $d ) {
- if ( is_array( $d ) ) {
- foreach ( $d as $k => $v ) {
- $d[ $k ] = self::utf8_encode_recursive( $v );
- }
- } elseif ( is_string( $d ) ) {
- return utf8_encode( $d );
- }
- return $d;
- }
- /**
- * @param $value
- * @param bool $encode
- *
- * @return string
- */
- public static function maybe_json_attr( $value, $encode = false ) {
- if ( is_object( $value ) || is_array( $value ) ) {
- return $encode ? htmlspecialchars( json_encode( $value ) ) : json_encode( $value );
- }
- return $value;
- }
- /**
- * Remaps array keys.
- *
- * @param array $array an array values.
- * @param array $remap_array an array of $old_key => $new_key values.
- *
- * @return array
- */
- public static function remap_keys( $array, $remap_array = [] ) {
- foreach ( $remap_array as $old_key => $new_key ) {
- $value = isset( $array[ $old_key ] ) ? $array[ $old_key ] : false;
- if ( ! empty( $value ) ) {
- $array[ $new_key ] = $value;
- }
- unset( $array[ $old_key ] );
- }
- return $array;
- }
- }
|