3 * WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
9 // If gettext isn't available
10 if ( !function_exists('_') ) {
16 if ( !function_exists('mb_substr') ):
17 function mb_substr( $str, $start, $length=null, $encoding=null ) {
18 return _mb_substr($str, $start, $length, $encoding);
22 function _mb_substr( $str, $start, $length=null, $encoding=null ) {
23 // the solution below, works only for utf-8, so in case of a different
24 // charset, just use built-in substr
25 $charset = get_option( 'blog_charset' );
26 if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) {
27 return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length);
29 // use the regex unicode support to separate the UTF-8 characters into an array
30 preg_match_all( '/./us', $str, $match );
31 $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
32 return implode( '', $chars );
35 if ( !function_exists('hash_hmac') ):
36 function hash_hmac($algo, $data, $key, $raw_output = false) {
37 return _hash_hmac($algo, $data, $key, $raw_output);
41 function _hash_hmac($algo, $data, $key, $raw_output = false) {
42 $packs = array('md5' => 'H32', 'sha1' => 'H40');
44 if ( !isset($packs[$algo]) )
47 $pack = $packs[$algo];
49 if (strlen($key) > 64)
50 $key = pack($pack, $algo($key));
52 $key = str_pad($key, 64, chr(0));
54 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
55 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
57 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
60 return pack( $pack, $hmac );
64 if ( !function_exists('json_encode') ) {
65 function json_encode( $string ) {
68 if ( !is_a($wp_json, 'Services_JSON') ) {
69 require_once( ABSPATH . WPINC . '/class-json.php' );
70 $wp_json = new Services_JSON();
73 return $wp_json->encodeUnsafe( $string );
77 if ( !function_exists('json_decode') ) {
78 function json_decode( $string, $assoc_array = false ) {
81 if ( !is_a($wp_json, 'Services_JSON') ) {
82 require_once( ABSPATH . WPINC . '/class-json.php' );
83 $wp_json = new Services_JSON();
86 $res = $wp_json->decode( $string );
88 $res = _json_decode_object_helper( $res );
91 function _json_decode_object_helper($data) {
92 if ( is_object($data) )
93 $data = get_object_vars($data);
94 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;