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' ) ) :
46 * Compat function to mimic mb_substr().
53 * @param string $str The string to extract the substring from.
54 * @param int $start Position to being extraction from in `$str`.
55 * @param int|null $length Optional. Maximum number of characters to extract from `$str`.
57 * @param string|null $encoding Optional. Character encoding to use. Default null.
58 * @return string Extracted substring.
60 function mb_substr( $str, $start, $length = null, $encoding = null ) {
61 return _mb_substr( $str, $start, $length, $encoding );
66 * Internal compat function to mimic mb_substr().
68 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
69 * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
70 * The behavior of this function for invalid inputs is undefined.
75 * @param string $str The string to extract the substring from.
76 * @param int $start Position to being extraction from in `$str`.
77 * @param int|null $length Optional. Maximum number of characters to extract from `$str`.
79 * @param string|null $encoding Optional. Character encoding to use. Default null.
80 * @return string Extracted substring.
82 function _mb_substr( $str, $start, $length = null, $encoding = null ) {
83 if ( null === $encoding ) {
84 $encoding = get_option( 'blog_charset' );
88 * The solution below works only for UTF-8, so in case of a different
89 * charset just use built-in substr().
91 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
92 return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
95 if ( _wp_can_use_pcre_u() ) {
96 // Use the regex unicode support to separate the UTF-8 characters into an array.
97 preg_match_all( '/./us', $str, $match );
98 $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
99 return implode( '', $chars );
103 [\x00-\x7F] # single-byte sequences 0xxxxxxx
104 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
105 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
106 | [\xE1-\xEC][\x80-\xBF]{2}
107 | \xED[\x80-\x9F][\x80-\xBF]
108 | [\xEE-\xEF][\x80-\xBF]{2}
109 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
110 | [\xF1-\xF3][\x80-\xBF]{3}
111 | \xF4[\x80-\x8F][\x80-\xBF]{2}
114 // Start with 1 element instead of 0 since the first thing we do is pop.
115 $chars = array( '' );
117 // We had some string left over from the last round, but we counted it in that last round.
121 * Split by UTF-8 character, limit to 1000 characters (last array element will contain
122 * the rest of the string).
124 $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
126 $chars = array_merge( $chars, $pieces );
128 // If there's anything left over, repeat the loop.
129 } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
131 return join( '', array_slice( $chars, $start, $length ) );
134 if ( ! function_exists( 'mb_strlen' ) ) :
136 * Compat function to mimic mb_strlen().
143 * @param string $str The string to retrieve the character length from.
144 * @param string|null $encoding Optional. Character encoding to use. Default null.
145 * @return int String length of `$str`.
147 function mb_strlen( $str, $encoding = null ) {
148 return _mb_strlen( $str, $encoding );
153 * Internal compat function to mimic mb_strlen().
155 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
156 * For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
157 * sequence. The behavior of this function for invalid inputs is undefined.
162 * @param string $str The string to retrieve the character length from.
163 * @param string|null $encoding Optional. Character encoding to use. Default null.
164 * @return int String length of `$str`.
166 function _mb_strlen( $str, $encoding = null ) {
167 if ( null === $encoding ) {
168 $encoding = get_option( 'blog_charset' );
172 * The solution below works only for UTF-8, so in case of a different charset
173 * just use built-in strlen().
175 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
176 return strlen( $str );
179 if ( _wp_can_use_pcre_u() ) {
180 // Use the regex unicode support to separate the UTF-8 characters into an array.
181 preg_match_all( '/./us', $str, $match );
182 return count( $match[0] );
186 [\x00-\x7F] # single-byte sequences 0xxxxxxx
187 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
188 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
189 | [\xE1-\xEC][\x80-\xBF]{2}
190 | \xED[\x80-\x9F][\x80-\xBF]
191 | [\xEE-\xEF][\x80-\xBF]{2}
192 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
193 | [\xF1-\xF3][\x80-\xBF]{3}
194 | \xF4[\x80-\x8F][\x80-\xBF]{2}
197 // Start at 1 instead of 0 since the first thing we do is decrement.
200 // We had some string left over from the last round, but we counted it in that last round.
204 * Split by UTF-8 character, limit to 1000 characters (last array element will contain
205 * the rest of the string).
207 $pieces = preg_split( $regex, $str, 1000 );
210 $count += count( $pieces );
212 // If there's anything left over, repeat the loop.
213 } while ( $str = array_pop( $pieces ) );
215 // Fencepost: preg_split() always returns one extra item in the array.
219 if ( !function_exists('hash_hmac') ):
221 * Compat function to mimic hash_hmac().
228 * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
229 * @param string $data Data to be hashed.
230 * @param string $key Secret key to use for generating the hash.
231 * @param bool $raw_output Optional. Whether to output raw binary data (true),
232 * or lowercase hexits (false). Default false.
233 * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
234 * is unknown or invalid.
236 function hash_hmac($algo, $data, $key, $raw_output = false) {
237 return _hash_hmac($algo, $data, $key, $raw_output);
242 * Internal compat function to mimic hash_hmac().
247 * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
248 * @param string $data Data to be hashed.
249 * @param string $key Secret key to use for generating the hash.
250 * @param bool $raw_output Optional. Whether to output raw binary data (true),
251 * or lowercase hexits (false). Default false.
252 * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
253 * is unknown or invalid.
255 function _hash_hmac($algo, $data, $key, $raw_output = false) {
256 $packs = array('md5' => 'H32', 'sha1' => 'H40');
258 if ( !isset($packs[$algo]) )
261 $pack = $packs[$algo];
263 if (strlen($key) > 64)
264 $key = pack($pack, $algo($key));
266 $key = str_pad($key, 64, chr(0));
268 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
269 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
271 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
274 return pack( $pack, $hmac );
278 if ( !function_exists('json_encode') ) {
279 function json_encode( $string ) {
282 if ( ! ( $wp_json instanceof Services_JSON ) ) {
283 require_once( ABSPATH . WPINC . '/class-json.php' );
284 $wp_json = new Services_JSON();
287 return $wp_json->encodeUnsafe( $string );
291 if ( !function_exists('json_decode') ) {
293 * @global Services_JSON $wp_json
294 * @param string $string
295 * @param bool $assoc_array
296 * @return object|array
298 function json_decode( $string, $assoc_array = false ) {
301 if ( ! ($wp_json instanceof Services_JSON ) ) {
302 require_once( ABSPATH . WPINC . '/class-json.php' );
303 $wp_json = new Services_JSON();
306 $res = $wp_json->decode( $string );
308 $res = _json_decode_object_helper( $res );
313 * @param object $data
316 function _json_decode_object_helper($data) {
317 if ( is_object($data) )
318 $data = get_object_vars($data);
319 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
323 if ( ! function_exists( 'hash_equals' ) ) :
325 * Timing attack safe string comparison
327 * Compares two strings using the same time whether they're equal or not.
329 * This function was added in PHP 5.6.
331 * Note: It can leak the length of a string when arguments of differing length are supplied.
335 * @param string $a Expected string.
336 * @param string $b Actual, user supplied, string.
337 * @return bool Whether strings are equal.
339 function hash_equals( $a, $b ) {
340 $a_length = strlen( $a );
341 if ( $a_length !== strlen( $b ) ) {
346 // Do not attempt to "optimize" this.
347 for ( $i = 0; $i < $a_length; $i++ ) {
348 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
351 return $result === 0;
355 // JSON_PRETTY_PRINT was introduced in PHP 5.4
356 // Defined here to prevent a notice when using it with wp_json_encode()
357 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
358 define( 'JSON_PRETTY_PRINT', 128 );
361 if ( ! function_exists( 'json_last_error_msg' ) ) :
363 * Retrieves the error string of the last json_encode() or json_decode() call.
367 * @internal This is a compatibility function for PHP <5.5
369 * @return bool|string Returns the error message on success, "No Error" if no error has occurred,
370 * or false on failure.
372 function json_last_error_msg() {
373 // See https://core.trac.wordpress.org/ticket/27799.
374 if ( ! function_exists( 'json_last_error' ) ) {
378 $last_error_code = json_last_error();
380 // Just in case JSON_ERROR_NONE is not defined.
381 $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0;
384 case $last_error_code === $error_code_none:
387 case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code:
388 return 'Maximum stack depth exceeded';
390 case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code:
391 return 'State mismatch (invalid or malformed JSON)';
393 case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code:
394 return 'Control character error, possibly incorrectly encoded';
396 case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code:
397 return 'Syntax error';
399 case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code:
400 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
402 case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code:
403 return 'Recursion detected';
405 case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code:
406 return 'Inf and NaN cannot be JSON encoded';
408 case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code:
409 return 'Type is not supported';
412 return 'An unknown error occurred';
417 if ( ! interface_exists( 'JsonSerializable' ) ) {
418 define( 'WP_JSON_SERIALIZE_COMPATIBLE', true );
420 * JsonSerializable interface.
422 * Compatibility shim for PHP <5.4
424 * @link https://secure.php.net/jsonserializable
428 interface JsonSerializable {
429 public function jsonSerialize();
433 // random_int was introduced in PHP 7.0
434 if ( ! function_exists( 'random_int' ) ) {
435 require ABSPATH . WPINC . '/random_compat/random.php';
438 if ( ! function_exists( 'array_replace_recursive' ) ) :
440 * PHP-agnostic version of {@link array_replace_recursive()}.
442 * The array_replace_recursive() function is a PHP 5.3 function. WordPress
443 * currently supports down to PHP 5.2, so this method is a workaround
446 * Note: array_replace_recursive() supports infinite arguments, but for our use-
447 * case, we only need to support two arguments.
449 * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
453 * @see https://secure.php.net/manual/en/function.array-replace-recursive.php#109390
455 * @param array $base Array with keys needing to be replaced.
456 * @param array $replacements Array with the replaced keys.
460 function array_replace_recursive( $base = array(), $replacements = array() ) {
461 foreach ( array_slice( func_get_args(), 1 ) as $replacements ) {
462 $bref_stack = array( &$base );
463 $head_stack = array( $replacements );
468 $bref = &$bref_stack[ key( $bref_stack ) ];
469 $head = array_pop( $head_stack );
471 unset( $bref_stack[ key( $bref_stack ) ] );
473 foreach ( array_keys( $head ) as $key ) {
474 if ( isset( $key, $bref ) &&
475 isset( $bref[ $key ] ) && is_array( $bref[ $key ] ) &&
476 isset( $head[ $key ] ) && is_array( $head[ $key ] )
478 $bref_stack[] = &$bref[ $key ];
479 $head_stack[] = $head[ $key ];
481 $bref[ $key ] = $head[ $key ];
484 } while ( count( $head_stack ) );
491 // SPL can be disabled on PHP 5.2
492 if ( ! function_exists( 'spl_autoload_register' ) ):
493 $_wp_spl_autoloaders = array();
496 * Autoloader compatibility callback.
500 * @param string $classname Class to attempt autoloading.
502 function __autoload( $classname ) {
503 global $_wp_spl_autoloaders;
504 foreach ( $_wp_spl_autoloaders as $autoloader ) {
505 if ( ! is_callable( $autoloader ) ) {
506 // Avoid the extra warning if the autoloader isn't callable.
510 call_user_func( $autoloader, $classname );
512 // If it has been autoloaded, stop processing.
513 if ( class_exists( $classname, false ) ) {
520 * Registers a function to be autoloaded.
524 * @param callable $autoload_function The function to register.
525 * @param bool $throw Optional. Whether the function should throw an exception
526 * if the function isn't callable. Default true.
527 * @param bool $prepend Whether the function should be prepended to the stack.
530 function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
531 if ( $throw && ! is_callable( $autoload_function ) ) {
532 // String not translated to match PHP core.
533 throw new Exception( 'Function not callable' );
536 global $_wp_spl_autoloaders;
538 // Don't allow multiple registration.
539 if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
544 array_unshift( $_wp_spl_autoloaders, $autoload_function );
546 $_wp_spl_autoloaders[] = $autoload_function;
551 * Unregisters an autoloader function.
555 * @param callable $function The function to unregister.
556 * @return bool True if the function was unregistered, false if it could not be.
558 function spl_autoload_unregister( $function ) {
559 global $_wp_spl_autoloaders;
560 foreach ( $_wp_spl_autoloaders as &$autoloader ) {
561 if ( $autoloader === $function ) {
562 unset( $autoloader );
571 * Retrieves the registered autoloader functions.
575 * @return array List of autoloader functions.
577 function spl_autoload_functions() {
578 return $GLOBALS['_wp_spl_autoloaders'];