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('_') ) {
17 * Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
23 * @staticvar string $utf8_pcre
25 * @param bool $set - Used for testing only
26 * null : default - get PCRE/u capability
27 * false : Used for testing - return false for future calls to this function
28 * 'reset': Used for testing - restore default behavior of this function
30 function _wp_can_use_pcre_u( $set = null ) {
31 static $utf8_pcre = 'reset';
33 if ( null !== $set ) {
37 if ( 'reset' === $utf8_pcre ) {
38 $utf8_pcre = @preg_match( '/^./u', 'a' );
44 if ( ! function_exists( 'mb_substr' ) ) :
45 function mb_substr( $str, $start, $length = null, $encoding = null ) {
46 return _mb_substr( $str, $start, $length, $encoding );
51 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
52 * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
53 * The behavior of this function for invalid inputs is undefined.
55 function _mb_substr( $str, $start, $length = null, $encoding = null ) {
56 if ( null === $encoding ) {
57 $encoding = get_option( 'blog_charset' );
60 // The solution below works only for UTF-8,
61 // so in case of a different charset just use built-in substr()
62 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
63 return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
66 if ( _wp_can_use_pcre_u() ) {
67 // Use the regex unicode support to separate the UTF-8 characters into an array
68 preg_match_all( '/./us', $str, $match );
69 $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
70 return implode( '', $chars );
74 [\x00-\x7F] # single-byte sequences 0xxxxxxx
75 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
76 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
77 | [\xE1-\xEC][\x80-\xBF]{2}
78 | \xED[\x80-\x9F][\x80-\xBF]
79 | [\xEE-\xEF][\x80-\xBF]{2}
80 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
81 | [\xF1-\xF3][\x80-\xBF]{3}
82 | \xF4[\x80-\x8F][\x80-\xBF]{2}
85 $chars = array( '' ); // Start with 1 element instead of 0 since the first thing we do is pop
87 // We had some string left over from the last round, but we counted it in that last round.
90 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
91 $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
93 $chars = array_merge( $chars, $pieces );
94 } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
96 return join( '', array_slice( $chars, $start, $length ) );
99 if ( ! function_exists( 'mb_strlen' ) ) :
100 function mb_strlen( $str, $encoding = null ) {
101 return _mb_strlen( $str, $encoding );
106 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
107 * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
108 * The behavior of this function for invalid inputs is undefined.
110 function _mb_strlen( $str, $encoding = null ) {
111 if ( null === $encoding ) {
112 $encoding = get_option( 'blog_charset' );
115 // The solution below works only for UTF-8,
116 // so in case of a different charset just use built-in strlen()
117 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
118 return strlen( $str );
121 if ( _wp_can_use_pcre_u() ) {
122 // Use the regex unicode support to separate the UTF-8 characters into an array
123 preg_match_all( '/./us', $str, $match );
124 return count( $match[0] );
128 [\x00-\x7F] # single-byte sequences 0xxxxxxx
129 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
130 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
131 | [\xE1-\xEC][\x80-\xBF]{2}
132 | \xED[\x80-\x9F][\x80-\xBF]
133 | [\xEE-\xEF][\x80-\xBF]{2}
134 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
135 | [\xF1-\xF3][\x80-\xBF]{3}
136 | \xF4[\x80-\x8F][\x80-\xBF]{2}
139 $count = 1; // Start at 1 instead of 0 since the first thing we do is decrement
141 // We had some string left over from the last round, but we counted it in that last round.
144 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
145 $pieces = preg_split( $regex, $str, 1000 );
148 $count += count( $pieces );
149 } while ( $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
151 // Fencepost: preg_split() always returns one extra item in the array
155 if ( !function_exists('hash_hmac') ):
156 function hash_hmac($algo, $data, $key, $raw_output = false) {
157 return _hash_hmac($algo, $data, $key, $raw_output);
161 function _hash_hmac($algo, $data, $key, $raw_output = false) {
162 $packs = array('md5' => 'H32', 'sha1' => 'H40');
164 if ( !isset($packs[$algo]) )
167 $pack = $packs[$algo];
169 if (strlen($key) > 64)
170 $key = pack($pack, $algo($key));
172 $key = str_pad($key, 64, chr(0));
174 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
175 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
177 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
180 return pack( $pack, $hmac );
184 if ( !function_exists('json_encode') ) {
185 function json_encode( $string ) {
188 if ( ! ( $wp_json instanceof Services_JSON ) ) {
189 require_once( ABSPATH . WPINC . '/class-json.php' );
190 $wp_json = new Services_JSON();
193 return $wp_json->encodeUnsafe( $string );
197 if ( !function_exists('json_decode') ) {
199 * @global Services_JSON $wp_json
200 * @param string $string
201 * @param bool $assoc_array
202 * @return object|array
204 function json_decode( $string, $assoc_array = false ) {
207 if ( ! ($wp_json instanceof Services_JSON ) ) {
208 require_once( ABSPATH . WPINC . '/class-json.php' );
209 $wp_json = new Services_JSON();
212 $res = $wp_json->decode( $string );
214 $res = _json_decode_object_helper( $res );
219 * @param object $data
222 function _json_decode_object_helper($data) {
223 if ( is_object($data) )
224 $data = get_object_vars($data);
225 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
229 if ( ! function_exists( 'hash_equals' ) ) :
231 * Compare two strings in constant time.
233 * This function was added in PHP 5.6.
234 * It can leak the length of a string.
238 * @param string $a Expected string.
239 * @param string $b Actual string.
240 * @return bool Whether strings are equal.
242 function hash_equals( $a, $b ) {
243 $a_length = strlen( $a );
244 if ( $a_length !== strlen( $b ) ) {
249 // Do not attempt to "optimize" this.
250 for ( $i = 0; $i < $a_length; $i++ ) {
251 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
254 return $result === 0;
258 // JSON_PRETTY_PRINT was introduced in PHP 5.4
259 // Defined here to prevent a notice when using it with wp_json_encode()
260 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
261 define( 'JSON_PRETTY_PRINT', 128 );
264 if ( ! function_exists( 'json_last_error_msg' ) ) :
266 * Retrieves the error string of the last json_encode() or json_decode() call.
270 * @internal This is a compatibility function for PHP <5.5
272 * @return bool|string Returns the error message on success, "No Error" if no error has occurred,
273 * or false on failure.
275 function json_last_error_msg() {
276 // See https://core.trac.wordpress.org/ticket/27799.
277 if ( ! function_exists( 'json_last_error' ) ) {
281 $last_error_code = json_last_error();
283 // Just in case JSON_ERROR_NONE is not defined.
284 $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0;
287 case $last_error_code === $error_code_none:
290 case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code:
291 return 'Maximum stack depth exceeded';
293 case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code:
294 return 'State mismatch (invalid or malformed JSON)';
296 case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code:
297 return 'Control character error, possibly incorrectly encoded';
299 case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code:
300 return 'Syntax error';
302 case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code:
303 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
305 case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code:
306 return 'Recursion detected';
308 case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code:
309 return 'Inf and NaN cannot be JSON encoded';
311 case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code:
312 return 'Type is not supported';
315 return 'An unknown error occurred';
320 if ( ! interface_exists( 'JsonSerializable' ) ) {
321 define( 'WP_JSON_SERIALIZE_COMPATIBLE', true );
323 * JsonSerializable interface.
325 * Compatibility shim for PHP <5.4
327 * @link http://php.net/jsonserializable
331 interface JsonSerializable {
332 public function jsonSerialize();
336 // random_int was introduced in PHP 7.0
337 if ( ! function_exists( 'random_int' ) ) {
338 require ABSPATH . WPINC . '/random_compat/random.php';