| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utility Class |
| 4 |
* |
| 5 |
* @copyright Copyright (c) 2010 - 2013, Usability Dynamics, Inc. |
| 6 |
* |
| 7 |
* @author team@UD |
| 8 |
* @namespace UDX |
| 9 |
* @module Utility |
| 10 |
*/ |
| 11 |
namespace UDX { |
| 12 |
|
| 13 |
if( !class_exists( 'UDX\Utility' ) ) { |
| 14 |
|
| 15 |
/** |
| 16 |
* Utility Library. |
| 17 |
* |
| 18 |
* @submodule Utility |
| 19 |
* @version 0.2.2 |
| 20 |
* @class Utility |
| 21 |
*/ |
| 22 |
class Utility { |
| 23 |
|
| 24 |
/** |
| 25 |
* Class version. |
| 26 |
* |
| 27 |
* @static |
| 28 |
* @property $version |
| 29 |
* @type string |
| 30 |
*/ |
| 31 |
public static $version = '0.4.0'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Textdomain String |
| 35 |
* |
| 36 |
* @public |
| 37 |
* @property text_domain |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public static $text_domain = 'lib-utility'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor for initializing class, in static mode as well as dynamic. |
| 44 |
* |
| 45 |
* @todo Should make the transdomain configuraiton. |
| 46 |
* |
| 47 |
* @since 0.1.1 |
| 48 |
* @author potanin@UD |
| 49 |
*/ |
| 50 |
public function __construct() {} |
| 51 |
|
| 52 |
/** |
| 53 |
* Wrapper for wp_parse_args. |
| 54 |
* |
| 55 |
* @author potanin@UD |
| 56 |
* @since 0.3.0 |
| 57 |
* @param $args |
| 58 |
* @param $defaults |
| 59 |
* |
| 60 |
* @return object |
| 61 |
*/ |
| 62 |
static public function parse_args( $args, $defaults ) { |
| 63 |
|
| 64 |
return (object) wp_parse_args( $args, $defaults ); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Detects Variable Type. |
| 70 |
* |
| 71 |
* Distinguishes between object and array based on associative status. |
| 72 |
* |
| 73 |
* @source http://php.net/manual/en/function.gettype.php |
| 74 |
* @since 1.0.4 |
| 75 |
*/ |
| 76 |
static public function get_type( $var ) { |
| 77 |
|
| 78 |
if( is_object( $var ) ) return get_class( $var ); |
| 79 |
if( is_null( $var ) ) return 'null'; |
| 80 |
if( is_string( $var ) ) return 'string'; |
| 81 |
|
| 82 |
if( is_array( $var ) ) { |
| 83 |
|
| 84 |
if( self::is_associative( $var ) ) { |
| 85 |
return 'object'; |
| 86 |
} |
| 87 |
|
| 88 |
return 'array'; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
if( is_int( $var ) ) return 'integer'; |
| 93 |
if( is_bool( $var ) ) return 'boolean'; |
| 94 |
if( is_float( $var ) ) return 'float'; |
| 95 |
if( is_resource( $var ) ) return 'resource'; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Test if Array is Associative |
| 101 |
* |
| 102 |
* @param $arr |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
static public function is_associative( $arr ) { |
| 106 |
|
| 107 |
if( !$arr ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
return array_keys($arr) !== range(0, count($arr) - 1) ? true : false; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Port of jQuery.extend() function. |
| 117 |
* |
| 118 |
* @since 1.0.3 |
| 119 |
*/ |
| 120 |
static public function extend() { |
| 121 |
|
| 122 |
$arrays = func_get_args(); |
| 123 |
$base = array_shift( $arrays ); |
| 124 |
if( !is_array( $base ) ) $base = empty( $base ) ? array() : array( $base ); |
| 125 |
foreach( (array) $arrays as $append ) { |
| 126 |
if( !is_array( $append ) ) $append = array( $append ); |
| 127 |
foreach( (array) $append as $key => $value ) { |
| 128 |
if( !array_key_exists( $key, $base ) and !is_numeric( $key ) ) { |
| 129 |
$base[ $key ] = $append[ $key ]; |
| 130 |
continue; |
| 131 |
} |
| 132 |
if( ( isset( $value ) && @is_array( $value ) ) || ( isset( $base[ $key ] ) && @is_array( $base[ $key ] ) ) ) { |
| 133 |
|
| 134 |
// extend if exists, otherwise create. |
| 135 |
if( isset( $base[ $key ] ) ) { |
| 136 |
$base[ $key ] = self::extend( $base[ $key ], $append[ $key ] ); |
| 137 |
} else { |
| 138 |
$base[ $key ] = $append[ $key ]; |
| 139 |
} |
| 140 |
|
| 141 |
} else if( is_numeric( $key ) ) { |
| 142 |
if( !in_array( $value, $base ) ) $base[ ] = $value; |
| 143 |
} else { |
| 144 |
$base[ $key ] = $value; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $base; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Localization Functionality. |
| 154 |
* |
| 155 |
* Replaces array's l10n data. |
| 156 |
* Helpful for localization of data which is stored in JSON files ( see /schemas ) |
| 157 |
* |
| 158 |
* Usage: |
| 159 |
* |
| 160 |
* add_filter( 'ud::schema::localization', function($locals){ |
| 161 |
* return array_merge( array( 'value_for_translating' => __( 'Blah Blah' ) ), $locals ); |
| 162 |
* }); |
| 163 |
* |
| 164 |
* $result = self::l10n_localize (array( |
| 165 |
* 'key' => 'l10n.value_for_translating' |
| 166 |
* ) ); |
| 167 |
* |
| 168 |
* |
| 169 |
* @param array $data |
| 170 |
* @param array $l10n translated values |
| 171 |
* @return array |
| 172 |
* @author peshkov@UD |
| 173 |
*/ |
| 174 |
static public function l10n_localize( $data, $l10n = array() ) { |
| 175 |
|
| 176 |
if ( !is_array( $data ) && !is_object( $data ) ) { |
| 177 |
return $data; |
| 178 |
} |
| 179 |
|
| 180 |
//** The Localization's list. */ |
| 181 |
$l10n = apply_filters( 'ud::schema::localization', $l10n ); |
| 182 |
|
| 183 |
//** Replace l10n entries */ |
| 184 |
foreach( $data as $k => $v ) { |
| 185 |
if ( is_array( $v ) ) { |
| 186 |
$data[ $k ] = self::l10n_localize( $v, $l10n ); |
| 187 |
} elseif ( is_string( $v ) ) { |
| 188 |
if ( strpos( $v, 'l10n' ) !== false ) { |
| 189 |
preg_match_all( '/l10n\.([^\s]*)/', $v, $matches ); |
| 190 |
if ( !empty( $matches[ 1 ] ) ) { |
| 191 |
foreach ( $matches[ 1 ] as $i => $m ) { |
| 192 |
if ( array_key_exists( $m, $l10n ) ) { |
| 193 |
$data[ $k ] = str_replace( $matches[ 0 ][ $i ], $l10n[ $m ], $data[ $k ] ); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return $data; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|