Edit File by line
/home/barbar84/public_h.../wp-inclu...
File: compat.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
[2] Fix | Delete
*
[3] Fix | Delete
* @package PHP
[4] Fix | Delete
* @access private
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
// If gettext isn't available.
[8] Fix | Delete
if ( ! function_exists( '_' ) ) {
[9] Fix | Delete
function _( $string ) {
[10] Fix | Delete
return $string;
[11] Fix | Delete
}
[12] Fix | Delete
}
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
[16] Fix | Delete
*
[17] Fix | Delete
* @ignore
[18] Fix | Delete
* @since 4.2.2
[19] Fix | Delete
* @access private
[20] Fix | Delete
*
[21] Fix | Delete
* @param bool $set - Used for testing only
[22] Fix | Delete
* null : default - get PCRE/u capability
[23] Fix | Delete
* false : Used for testing - return false for future calls to this function
[24] Fix | Delete
* 'reset': Used for testing - restore default behavior of this function
[25] Fix | Delete
*/
[26] Fix | Delete
function _wp_can_use_pcre_u( $set = null ) {
[27] Fix | Delete
static $utf8_pcre = 'reset';
[28] Fix | Delete
[29] Fix | Delete
if ( null !== $set ) {
[30] Fix | Delete
$utf8_pcre = $set;
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
if ( 'reset' === $utf8_pcre ) {
[34] Fix | Delete
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support.
[35] Fix | Delete
$utf8_pcre = @preg_match( '/^./u', 'a' );
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
return $utf8_pcre;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
if ( ! function_exists( 'mb_substr' ) ) :
[42] Fix | Delete
/**
[43] Fix | Delete
* Compat function to mimic mb_substr().
[44] Fix | Delete
*
[45] Fix | Delete
* @ignore
[46] Fix | Delete
* @since 3.2.0
[47] Fix | Delete
*
[48] Fix | Delete
* @see _mb_substr()
[49] Fix | Delete
*
[50] Fix | Delete
* @param string $str The string to extract the substring from.
[51] Fix | Delete
* @param int $start Position to being extraction from in `$str`.
[52] Fix | Delete
* @param int|null $length Optional. Maximum number of characters to extract from `$str`.
[53] Fix | Delete
* Default null.
[54] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[55] Fix | Delete
* @return string Extracted substring.
[56] Fix | Delete
*/
[57] Fix | Delete
function mb_substr( $str, $start, $length = null, $encoding = null ) {
[58] Fix | Delete
return _mb_substr( $str, $start, $length, $encoding );
[59] Fix | Delete
}
[60] Fix | Delete
endif;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Internal compat function to mimic mb_substr().
[64] Fix | Delete
*
[65] Fix | Delete
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
[66] Fix | Delete
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
[67] Fix | Delete
* The behavior of this function for invalid inputs is undefined.
[68] Fix | Delete
*
[69] Fix | Delete
* @ignore
[70] Fix | Delete
* @since 3.2.0
[71] Fix | Delete
*
[72] Fix | Delete
* @param string $str The string to extract the substring from.
[73] Fix | Delete
* @param int $start Position to being extraction from in `$str`.
[74] Fix | Delete
* @param int|null $length Optional. Maximum number of characters to extract from `$str`.
[75] Fix | Delete
* Default null.
[76] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[77] Fix | Delete
* @return string Extracted substring.
[78] Fix | Delete
*/
[79] Fix | Delete
function _mb_substr( $str, $start, $length = null, $encoding = null ) {
[80] Fix | Delete
if ( null === $encoding ) {
[81] Fix | Delete
$encoding = get_option( 'blog_charset' );
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/*
[85] Fix | Delete
* The solution below works only for UTF-8, so in case of a different
[86] Fix | Delete
* charset just use built-in substr().
[87] Fix | Delete
*/
[88] Fix | Delete
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
[89] Fix | Delete
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
if ( _wp_can_use_pcre_u() ) {
[93] Fix | Delete
// Use the regex unicode support to separate the UTF-8 characters into an array.
[94] Fix | Delete
preg_match_all( '/./us', $str, $match );
[95] Fix | Delete
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
[96] Fix | Delete
return implode( '', $chars );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
$regex = '/(
[100] Fix | Delete
[\x00-\x7F] # single-byte sequences 0xxxxxxx
[101] Fix | Delete
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
[102] Fix | Delete
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
[103] Fix | Delete
| [\xE1-\xEC][\x80-\xBF]{2}
[104] Fix | Delete
| \xED[\x80-\x9F][\x80-\xBF]
[105] Fix | Delete
| [\xEE-\xEF][\x80-\xBF]{2}
[106] Fix | Delete
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
[107] Fix | Delete
| [\xF1-\xF3][\x80-\xBF]{3}
[108] Fix | Delete
| \xF4[\x80-\x8F][\x80-\xBF]{2}
[109] Fix | Delete
)/x';
[110] Fix | Delete
[111] Fix | Delete
// Start with 1 element instead of 0 since the first thing we do is pop.
[112] Fix | Delete
$chars = array( '' );
[113] Fix | Delete
do {
[114] Fix | Delete
// We had some string left over from the last round, but we counted it in that last round.
[115] Fix | Delete
array_pop( $chars );
[116] Fix | Delete
[117] Fix | Delete
/*
[118] Fix | Delete
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
[119] Fix | Delete
* the rest of the string).
[120] Fix | Delete
*/
[121] Fix | Delete
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
[122] Fix | Delete
[123] Fix | Delete
$chars = array_merge( $chars, $pieces );
[124] Fix | Delete
[125] Fix | Delete
// If there's anything left over, repeat the loop.
[126] Fix | Delete
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
[127] Fix | Delete
[128] Fix | Delete
return implode( '', array_slice( $chars, $start, $length ) );
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
if ( ! function_exists( 'mb_strlen' ) ) :
[132] Fix | Delete
/**
[133] Fix | Delete
* Compat function to mimic mb_strlen().
[134] Fix | Delete
*
[135] Fix | Delete
* @ignore
[136] Fix | Delete
* @since 4.2.0
[137] Fix | Delete
*
[138] Fix | Delete
* @see _mb_strlen()
[139] Fix | Delete
*
[140] Fix | Delete
* @param string $str The string to retrieve the character length from.
[141] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[142] Fix | Delete
* @return int String length of `$str`.
[143] Fix | Delete
*/
[144] Fix | Delete
function mb_strlen( $str, $encoding = null ) {
[145] Fix | Delete
return _mb_strlen( $str, $encoding );
[146] Fix | Delete
}
[147] Fix | Delete
endif;
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Internal compat function to mimic mb_strlen().
[151] Fix | Delete
*
[152] Fix | Delete
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
[153] Fix | Delete
* For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
[154] Fix | Delete
* sequence. The behavior of this function for invalid inputs is undefined.
[155] Fix | Delete
*
[156] Fix | Delete
* @ignore
[157] Fix | Delete
* @since 4.2.0
[158] Fix | Delete
*
[159] Fix | Delete
* @param string $str The string to retrieve the character length from.
[160] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[161] Fix | Delete
* @return int String length of `$str`.
[162] Fix | Delete
*/
[163] Fix | Delete
function _mb_strlen( $str, $encoding = null ) {
[164] Fix | Delete
if ( null === $encoding ) {
[165] Fix | Delete
$encoding = get_option( 'blog_charset' );
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/*
[169] Fix | Delete
* The solution below works only for UTF-8, so in case of a different charset
[170] Fix | Delete
* just use built-in strlen().
[171] Fix | Delete
*/
[172] Fix | Delete
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
[173] Fix | Delete
return strlen( $str );
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
if ( _wp_can_use_pcre_u() ) {
[177] Fix | Delete
// Use the regex unicode support to separate the UTF-8 characters into an array.
[178] Fix | Delete
preg_match_all( '/./us', $str, $match );
[179] Fix | Delete
return count( $match[0] );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
$regex = '/(?:
[183] Fix | Delete
[\x00-\x7F] # single-byte sequences 0xxxxxxx
[184] Fix | Delete
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
[185] Fix | Delete
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
[186] Fix | Delete
| [\xE1-\xEC][\x80-\xBF]{2}
[187] Fix | Delete
| \xED[\x80-\x9F][\x80-\xBF]
[188] Fix | Delete
| [\xEE-\xEF][\x80-\xBF]{2}
[189] Fix | Delete
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
[190] Fix | Delete
| [\xF1-\xF3][\x80-\xBF]{3}
[191] Fix | Delete
| \xF4[\x80-\x8F][\x80-\xBF]{2}
[192] Fix | Delete
)/x';
[193] Fix | Delete
[194] Fix | Delete
// Start at 1 instead of 0 since the first thing we do is decrement.
[195] Fix | Delete
$count = 1;
[196] Fix | Delete
do {
[197] Fix | Delete
// We had some string left over from the last round, but we counted it in that last round.
[198] Fix | Delete
$count--;
[199] Fix | Delete
[200] Fix | Delete
/*
[201] Fix | Delete
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
[202] Fix | Delete
* the rest of the string).
[203] Fix | Delete
*/
[204] Fix | Delete
$pieces = preg_split( $regex, $str, 1000 );
[205] Fix | Delete
[206] Fix | Delete
// Increment.
[207] Fix | Delete
$count += count( $pieces );
[208] Fix | Delete
[209] Fix | Delete
// If there's anything left over, repeat the loop.
[210] Fix | Delete
} while ( $str = array_pop( $pieces ) );
[211] Fix | Delete
[212] Fix | Delete
// Fencepost: preg_split() always returns one extra item in the array.
[213] Fix | Delete
return --$count;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
if ( ! function_exists( 'hash_hmac' ) ) :
[217] Fix | Delete
/**
[218] Fix | Delete
* Compat function to mimic hash_hmac().
[219] Fix | Delete
*
[220] Fix | Delete
* The Hash extension is bundled with PHP by default since PHP 5.1.2.
[221] Fix | Delete
* However, the extension may be explicitly disabled on select servers.
[222] Fix | Delete
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
[223] Fix | Delete
* longer be disabled.
[224] Fix | Delete
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
[225] Fix | Delete
* and the associated `_hash_hmac()` function can be safely removed.
[226] Fix | Delete
*
[227] Fix | Delete
* @ignore
[228] Fix | Delete
* @since 3.2.0
[229] Fix | Delete
*
[230] Fix | Delete
* @see _hash_hmac()
[231] Fix | Delete
*
[232] Fix | Delete
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
[233] Fix | Delete
* @param string $data Data to be hashed.
[234] Fix | Delete
* @param string $key Secret key to use for generating the hash.
[235] Fix | Delete
* @param bool $raw_output Optional. Whether to output raw binary data (true),
[236] Fix | Delete
* or lowercase hexits (false). Default false.
[237] Fix | Delete
* @return string|false The hash in output determined by `$raw_output`. False if `$algo`
[238] Fix | Delete
* is unknown or invalid.
[239] Fix | Delete
*/
[240] Fix | Delete
function hash_hmac( $algo, $data, $key, $raw_output = false ) {
[241] Fix | Delete
return _hash_hmac( $algo, $data, $key, $raw_output );
[242] Fix | Delete
}
[243] Fix | Delete
endif;
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Internal compat function to mimic hash_hmac().
[247] Fix | Delete
*
[248] Fix | Delete
* @ignore
[249] Fix | Delete
* @since 3.2.0
[250] Fix | Delete
*
[251] Fix | Delete
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
[252] Fix | Delete
* @param string $data Data to be hashed.
[253] Fix | Delete
* @param string $key Secret key to use for generating the hash.
[254] Fix | Delete
* @param bool $raw_output Optional. Whether to output raw binary data (true),
[255] Fix | Delete
* or lowercase hexits (false). Default false.
[256] Fix | Delete
* @return string|false The hash in output determined by `$raw_output`. False if `$algo`
[257] Fix | Delete
* is unknown or invalid.
[258] Fix | Delete
*/
[259] Fix | Delete
function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
[260] Fix | Delete
$packs = array(
[261] Fix | Delete
'md5' => 'H32',
[262] Fix | Delete
'sha1' => 'H40',
[263] Fix | Delete
);
[264] Fix | Delete
[265] Fix | Delete
if ( ! isset( $packs[ $algo ] ) ) {
[266] Fix | Delete
return false;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
$pack = $packs[ $algo ];
[270] Fix | Delete
[271] Fix | Delete
if ( strlen( $key ) > 64 ) {
[272] Fix | Delete
$key = pack( $pack, $algo( $key ) );
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
$key = str_pad( $key, 64, chr( 0 ) );
[276] Fix | Delete
[277] Fix | Delete
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
[278] Fix | Delete
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );
[279] Fix | Delete
[280] Fix | Delete
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
[281] Fix | Delete
[282] Fix | Delete
if ( $raw_output ) {
[283] Fix | Delete
return pack( $pack, $hmac );
[284] Fix | Delete
}
[285] Fix | Delete
return $hmac;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
if ( ! function_exists( 'hash_equals' ) ) :
[289] Fix | Delete
/**
[290] Fix | Delete
* Timing attack safe string comparison
[291] Fix | Delete
*
[292] Fix | Delete
* Compares two strings using the same time whether they're equal or not.
[293] Fix | Delete
*
[294] Fix | Delete
* Note: It can leak the length of a string when arguments of differing length are supplied.
[295] Fix | Delete
*
[296] Fix | Delete
* This function was added in PHP 5.6.
[297] Fix | Delete
* However, the Hash extension may be explicitly disabled on select servers.
[298] Fix | Delete
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
[299] Fix | Delete
* longer be disabled.
[300] Fix | Delete
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
[301] Fix | Delete
* can be safely removed.
[302] Fix | Delete
*
[303] Fix | Delete
* @since 3.9.2
[304] Fix | Delete
*
[305] Fix | Delete
* @param string $a Expected string.
[306] Fix | Delete
* @param string $b Actual, user supplied, string.
[307] Fix | Delete
* @return bool Whether strings are equal.
[308] Fix | Delete
*/
[309] Fix | Delete
function hash_equals( $a, $b ) {
[310] Fix | Delete
$a_length = strlen( $a );
[311] Fix | Delete
if ( strlen( $b ) !== $a_length ) {
[312] Fix | Delete
return false;
[313] Fix | Delete
}
[314] Fix | Delete
$result = 0;
[315] Fix | Delete
[316] Fix | Delete
// Do not attempt to "optimize" this.
[317] Fix | Delete
for ( $i = 0; $i < $a_length; $i++ ) {
[318] Fix | Delete
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
return 0 === $result;
[322] Fix | Delete
}
[323] Fix | Delete
endif;
[324] Fix | Delete
[325] Fix | Delete
// random_int() was introduced in PHP 7.0.
[326] Fix | Delete
if ( ! function_exists( 'random_int' ) ) {
[327] Fix | Delete
require ABSPATH . WPINC . '/random_compat/random.php';
[328] Fix | Delete
}
[329] Fix | Delete
// sodium_crypto_box() was introduced in PHP 7.2.
[330] Fix | Delete
if ( ! function_exists( 'sodium_crypto_box' ) ) {
[331] Fix | Delete
require ABSPATH . WPINC . '/sodium_compat/autoload.php';
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
if ( ! function_exists( 'is_countable' ) ) {
[335] Fix | Delete
/**
[336] Fix | Delete
* Polyfill for is_countable() function added in PHP 7.3.
[337] Fix | Delete
*
[338] Fix | Delete
* Verify that the content of a variable is an array or an object
[339] Fix | Delete
* implementing the Countable interface.
[340] Fix | Delete
*
[341] Fix | Delete
* @since 4.9.6
[342] Fix | Delete
*
[343] Fix | Delete
* @param mixed $var The value to check.
[344] Fix | Delete
* @return bool True if `$var` is countable, false otherwise.
[345] Fix | Delete
*/
[346] Fix | Delete
function is_countable( $var ) {
[347] Fix | Delete
return ( is_array( $var )
[348] Fix | Delete
|| $var instanceof Countable
[349] Fix | Delete
|| $var instanceof SimpleXMLElement
[350] Fix | Delete
|| $var instanceof ResourceBundle
[351] Fix | Delete
);
[352] Fix | Delete
}
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
if ( ! function_exists( 'is_iterable' ) ) {
[356] Fix | Delete
/**
[357] Fix | Delete
* Polyfill for is_iterable() function added in PHP 7.1.
[358] Fix | Delete
*
[359] Fix | Delete
* Verify that the content of a variable is an array or an object
[360] Fix | Delete
* implementing the Traversable interface.
[361] Fix | Delete
*
[362] Fix | Delete
* @since 4.9.6
[363] Fix | Delete
*
[364] Fix | Delete
* @param mixed $var The value to check.
[365] Fix | Delete
* @return bool True if `$var` is iterable, false otherwise.
[366] Fix | Delete
*/
[367] Fix | Delete
function is_iterable( $var ) {
[368] Fix | Delete
return ( is_array( $var ) || $var instanceof Traversable );
[369] Fix | Delete
}
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
if ( ! function_exists( 'str_starts_with' ) ) {
[373] Fix | Delete
/**
[374] Fix | Delete
* Polyfill for `str_starts_with()` function added in PHP 8.0.
[375] Fix | Delete
*
[376] Fix | Delete
* Performs a case-sensitive check indicating if
[377] Fix | Delete
* the haystack begins with needle.
[378] Fix | Delete
*
[379] Fix | Delete
* @since 5.9.0
[380] Fix | Delete
*
[381] Fix | Delete
* @param string $haystack The string to search in.
[382] Fix | Delete
* @param string $needle The substring to search for in the `$haystack`.
[383] Fix | Delete
* @return bool True if `$haystack` starts with `$needle`, otherwise false.
[384] Fix | Delete
*/
[385] Fix | Delete
function str_starts_with( $haystack, $needle ) {
[386] Fix | Delete
if ( '' === $needle ) {
[387] Fix | Delete
return true;
[388] Fix | Delete
}
[389] Fix | Delete
[390] Fix | Delete
return 0 === strpos( $haystack, $needle );
[391] Fix | Delete
}
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
if ( ! function_exists( 'str_ends_with' ) ) {
[395] Fix | Delete
/**
[396] Fix | Delete
* Polyfill for `str_ends_with()` function added in PHP 8.0.
[397] Fix | Delete
*
[398] Fix | Delete
* Performs a case-sensitive check indicating if
[399] Fix | Delete
* the haystack ends with needle.
[400] Fix | Delete
*
[401] Fix | Delete
* @since 5.9.0
[402] Fix | Delete
*
[403] Fix | Delete
* @param string $haystack The string to search in.
[404] Fix | Delete
* @param string $needle The substring to search for in the `$haystack`.
[405] Fix | Delete
* @return bool True if `$haystack` ends with `$needle`, otherwise false.
[406] Fix | Delete
*/
[407] Fix | Delete
function str_ends_with( $haystack, $needle ) {
[408] Fix | Delete
if ( '' === $haystack ) {
[409] Fix | Delete
return '' === $needle;
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
$len = strlen( $needle );
[413] Fix | Delete
[414] Fix | Delete
return substr( $haystack, -$len, $len ) === $needle;
[415] Fix | Delete
}
[416] Fix | Delete
}
[417] Fix | Delete
[418] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function