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,
24 // so in case of a different 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( 'mb_strlen' ) ) :
36 function mb_strlen( $str, $encoding = null ) {
37 return _mb_strlen( $str, $encoding );
41 function _mb_strlen( $str, $encoding = null ) {
42 // The solution below works only for UTF-8,
43 // so in case of a different charset just use built-in strlen()
44 $charset = get_option( 'blog_charset' );
45 if ( ! in_array( $charset, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
46 return strlen( $str );
48 // Use the regex unicode support to separate the UTF-8 characters into an array
49 preg_match_all( '/./us', $str, $match );
50 return count( $match[0] );
53 if ( !function_exists('hash_hmac') ):
54 function hash_hmac($algo, $data, $key, $raw_output = false) {
55 return _hash_hmac($algo, $data, $key, $raw_output);
59 function _hash_hmac($algo, $data, $key, $raw_output = false) {
60 $packs = array('md5' => 'H32', 'sha1' => 'H40');
62 if ( !isset($packs[$algo]) )
65 $pack = $packs[$algo];
67 if (strlen($key) > 64)
68 $key = pack($pack, $algo($key));
70 $key = str_pad($key, 64, chr(0));
72 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
73 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
75 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
78 return pack( $pack, $hmac );
82 if ( !function_exists('json_encode') ) {
83 function json_encode( $string ) {
86 if ( ! ( $wp_json instanceof Services_JSON ) ) {
87 require_once( ABSPATH . WPINC . '/class-json.php' );
88 $wp_json = new Services_JSON();
91 return $wp_json->encodeUnsafe( $string );
95 if ( !function_exists('json_decode') ) {
96 function json_decode( $string, $assoc_array = false ) {
99 if ( ! ($wp_json instanceof Services_JSON ) ) {
100 require_once( ABSPATH . WPINC . '/class-json.php' );
101 $wp_json = new Services_JSON();
104 $res = $wp_json->decode( $string );
106 $res = _json_decode_object_helper( $res );
109 function _json_decode_object_helper($data) {
110 if ( is_object($data) )
111 $data = get_object_vars($data);
112 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
116 if ( ! function_exists( 'hash_equals' ) ) :
118 * Compare two strings in constant time.
120 * This function was added in PHP 5.6.
121 * It can leak the length of a string.
125 * @param string $a Expected string.
126 * @param string $b Actual string.
127 * @return bool Whether strings are equal.
129 function hash_equals( $a, $b ) {
130 $a_length = strlen( $a );
131 if ( $a_length !== strlen( $b ) ) {
136 // Do not attempt to "optimize" this.
137 for ( $i = 0; $i < $a_length; $i++ ) {
138 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
141 return $result === 0;
145 // JSON_PRETTY_PRINT was introduced in PHP 5.4
146 // Defined here to prevent a notice when using it with wp_json_encode()
147 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
148 define( 'JSON_PRETTY_PRINT', 128 );