Edit File by line
/home/zeestwma/richards.../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 _( $message ) {
[10] Fix | Delete
return $message;
[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
/**
[42] Fix | Delete
* Indicates if a given slug for a character set represents the UTF-8 text encoding.
[43] Fix | Delete
*
[44] Fix | Delete
* A charset is considered to represent UTF-8 if it is a case-insensitive match
[45] Fix | Delete
* of "UTF-8" with or without the hyphen.
[46] Fix | Delete
*
[47] Fix | Delete
* Example:
[48] Fix | Delete
*
[49] Fix | Delete
* true === _is_utf8_charset( 'UTF-8' );
[50] Fix | Delete
* true === _is_utf8_charset( 'utf8' );
[51] Fix | Delete
* false === _is_utf8_charset( 'latin1' );
[52] Fix | Delete
* false === _is_utf8_charset( 'UTF 8' );
[53] Fix | Delete
*
[54] Fix | Delete
* // Only strings match.
[55] Fix | Delete
* false === _is_utf8_charset( [ 'charset' => 'utf-8' ] );
[56] Fix | Delete
*
[57] Fix | Delete
* `is_utf8_charset` should be used outside of this file.
[58] Fix | Delete
*
[59] Fix | Delete
* @ignore
[60] Fix | Delete
* @since 6.6.1
[61] Fix | Delete
*
[62] Fix | Delete
* @param string $charset_slug Slug representing a text character encoding, or "charset".
[63] Fix | Delete
* E.g. "UTF-8", "Windows-1252", "ISO-8859-1", "SJIS".
[64] Fix | Delete
*
[65] Fix | Delete
* @return bool Whether the slug represents the UTF-8 encoding.
[66] Fix | Delete
*/
[67] Fix | Delete
function _is_utf8_charset( $charset_slug ) {
[68] Fix | Delete
if ( ! is_string( $charset_slug ) ) {
[69] Fix | Delete
return false;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
return (
[73] Fix | Delete
0 === strcasecmp( 'UTF-8', $charset_slug ) ||
[74] Fix | Delete
0 === strcasecmp( 'UTF8', $charset_slug )
[75] Fix | Delete
);
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
if ( ! function_exists( 'mb_substr' ) ) :
[79] Fix | Delete
/**
[80] Fix | Delete
* Compat function to mimic mb_substr().
[81] Fix | Delete
*
[82] Fix | Delete
* @ignore
[83] Fix | Delete
* @since 3.2.0
[84] Fix | Delete
*
[85] Fix | Delete
* @see _mb_substr()
[86] Fix | Delete
*
[87] Fix | Delete
* @param string $string The string to extract the substring from.
[88] Fix | Delete
* @param int $start Position to being extraction from in `$string`.
[89] Fix | Delete
* @param int|null $length Optional. Maximum number of characters to extract from `$string`.
[90] Fix | Delete
* Default null.
[91] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[92] Fix | Delete
* @return string Extracted substring.
[93] Fix | Delete
*/
[94] Fix | Delete
function mb_substr( $string, $start, $length = null, $encoding = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
[95] Fix | Delete
return _mb_substr( $string, $start, $length, $encoding );
[96] Fix | Delete
}
[97] Fix | Delete
endif;
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Internal compat function to mimic mb_substr().
[101] Fix | Delete
*
[102] Fix | Delete
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
[103] Fix | Delete
* For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
[104] Fix | Delete
* sequence. The behavior of this function for invalid inputs is undefined.
[105] Fix | Delete
*
[106] Fix | Delete
* @ignore
[107] Fix | Delete
* @since 3.2.0
[108] Fix | Delete
*
[109] Fix | Delete
* @param string $str The string to extract the substring from.
[110] Fix | Delete
* @param int $start Position to being extraction from in `$str`.
[111] Fix | Delete
* @param int|null $length Optional. Maximum number of characters to extract from `$str`.
[112] Fix | Delete
* Default null.
[113] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[114] Fix | Delete
* @return string Extracted substring.
[115] Fix | Delete
*/
[116] Fix | Delete
function _mb_substr( $str, $start, $length = null, $encoding = null ) {
[117] Fix | Delete
if ( null === $str ) {
[118] Fix | Delete
return '';
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
if ( null === $encoding ) {
[122] Fix | Delete
$encoding = get_option( 'blog_charset' );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/*
[126] Fix | Delete
* The solution below works only for UTF-8, so in case of a different
[127] Fix | Delete
* charset just use built-in substr().
[128] Fix | Delete
*/
[129] Fix | Delete
if ( ! _is_utf8_charset( $encoding ) ) {
[130] Fix | Delete
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
if ( _wp_can_use_pcre_u() ) {
[134] Fix | Delete
// Use the regex unicode support to separate the UTF-8 characters into an array.
[135] Fix | Delete
preg_match_all( '/./us', $str, $match );
[136] Fix | Delete
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
[137] Fix | Delete
return implode( '', $chars );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$regex = '/(
[141] Fix | Delete
[\x00-\x7F] # single-byte sequences 0xxxxxxx
[142] Fix | Delete
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
[143] Fix | Delete
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
[144] Fix | Delete
| [\xE1-\xEC][\x80-\xBF]{2}
[145] Fix | Delete
| \xED[\x80-\x9F][\x80-\xBF]
[146] Fix | Delete
| [\xEE-\xEF][\x80-\xBF]{2}
[147] Fix | Delete
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
[148] Fix | Delete
| [\xF1-\xF3][\x80-\xBF]{3}
[149] Fix | Delete
| \xF4[\x80-\x8F][\x80-\xBF]{2}
[150] Fix | Delete
)/x';
[151] Fix | Delete
[152] Fix | Delete
// Start with 1 element instead of 0 since the first thing we do is pop.
[153] Fix | Delete
$chars = array( '' );
[154] Fix | Delete
[155] Fix | Delete
do {
[156] Fix | Delete
// We had some string left over from the last round, but we counted it in that last round.
[157] Fix | Delete
array_pop( $chars );
[158] Fix | Delete
[159] Fix | Delete
/*
[160] Fix | Delete
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
[161] Fix | Delete
* the rest of the string).
[162] Fix | Delete
*/
[163] Fix | Delete
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
[164] Fix | Delete
[165] Fix | Delete
$chars = array_merge( $chars, $pieces );
[166] Fix | Delete
[167] Fix | Delete
// If there's anything left over, repeat the loop.
[168] Fix | Delete
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
[169] Fix | Delete
[170] Fix | Delete
return implode( '', array_slice( $chars, $start, $length ) );
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
if ( ! function_exists( 'mb_strlen' ) ) :
[174] Fix | Delete
/**
[175] Fix | Delete
* Compat function to mimic mb_strlen().
[176] Fix | Delete
*
[177] Fix | Delete
* @ignore
[178] Fix | Delete
* @since 4.2.0
[179] Fix | Delete
*
[180] Fix | Delete
* @see _mb_strlen()
[181] Fix | Delete
*
[182] Fix | Delete
* @param string $string The string to retrieve the character length from.
[183] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[184] Fix | Delete
* @return int String length of `$string`.
[185] Fix | Delete
*/
[186] Fix | Delete
function mb_strlen( $string, $encoding = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
[187] Fix | Delete
return _mb_strlen( $string, $encoding );
[188] Fix | Delete
}
[189] Fix | Delete
endif;
[190] Fix | Delete
[191] Fix | Delete
/**
[192] Fix | Delete
* Internal compat function to mimic mb_strlen().
[193] Fix | Delete
*
[194] Fix | Delete
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
[195] Fix | Delete
* For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
[196] Fix | Delete
* sequence. The behavior of this function for invalid inputs is undefined.
[197] Fix | Delete
*
[198] Fix | Delete
* @ignore
[199] Fix | Delete
* @since 4.2.0
[200] Fix | Delete
*
[201] Fix | Delete
* @param string $str The string to retrieve the character length from.
[202] Fix | Delete
* @param string|null $encoding Optional. Character encoding to use. Default null.
[203] Fix | Delete
* @return int String length of `$str`.
[204] Fix | Delete
*/
[205] Fix | Delete
function _mb_strlen( $str, $encoding = null ) {
[206] Fix | Delete
if ( null === $encoding ) {
[207] Fix | Delete
$encoding = get_option( 'blog_charset' );
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/*
[211] Fix | Delete
* The solution below works only for UTF-8, so in case of a different charset
[212] Fix | Delete
* just use built-in strlen().
[213] Fix | Delete
*/
[214] Fix | Delete
if ( ! _is_utf8_charset( $encoding ) ) {
[215] Fix | Delete
return strlen( $str );
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
if ( _wp_can_use_pcre_u() ) {
[219] Fix | Delete
// Use the regex unicode support to separate the UTF-8 characters into an array.
[220] Fix | Delete
preg_match_all( '/./us', $str, $match );
[221] Fix | Delete
return count( $match[0] );
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
$regex = '/(?:
[225] Fix | Delete
[\x00-\x7F] # single-byte sequences 0xxxxxxx
[226] Fix | Delete
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
[227] Fix | Delete
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
[228] Fix | Delete
| [\xE1-\xEC][\x80-\xBF]{2}
[229] Fix | Delete
| \xED[\x80-\x9F][\x80-\xBF]
[230] Fix | Delete
| [\xEE-\xEF][\x80-\xBF]{2}
[231] Fix | Delete
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
[232] Fix | Delete
| [\xF1-\xF3][\x80-\xBF]{3}
[233] Fix | Delete
| \xF4[\x80-\x8F][\x80-\xBF]{2}
[234] Fix | Delete
)/x';
[235] Fix | Delete
[236] Fix | Delete
// Start at 1 instead of 0 since the first thing we do is decrement.
[237] Fix | Delete
$count = 1;
[238] Fix | Delete
[239] Fix | Delete
do {
[240] Fix | Delete
// We had some string left over from the last round, but we counted it in that last round.
[241] Fix | Delete
--$count;
[242] Fix | Delete
[243] Fix | Delete
/*
[244] Fix | Delete
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
[245] Fix | Delete
* the rest of the string).
[246] Fix | Delete
*/
[247] Fix | Delete
$pieces = preg_split( $regex, $str, 1000 );
[248] Fix | Delete
[249] Fix | Delete
// Increment.
[250] Fix | Delete
$count += count( $pieces );
[251] Fix | Delete
[252] Fix | Delete
// If there's anything left over, repeat the loop.
[253] Fix | Delete
} while ( $str = array_pop( $pieces ) );
[254] Fix | Delete
[255] Fix | Delete
// Fencepost: preg_split() always returns one extra item in the array.
[256] Fix | Delete
return --$count;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
if ( ! function_exists( 'hash_hmac' ) ) :
[260] Fix | Delete
/**
[261] Fix | Delete
* Compat function to mimic hash_hmac().
[262] Fix | Delete
*
[263] Fix | Delete
* The Hash extension is bundled with PHP by default since PHP 5.1.2.
[264] Fix | Delete
* However, the extension may be explicitly disabled on select servers.
[265] Fix | Delete
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
[266] Fix | Delete
* longer be disabled.
[267] Fix | Delete
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
[268] Fix | Delete
* and the associated `_hash_hmac()` function can be safely removed.
[269] Fix | Delete
*
[270] Fix | Delete
* @ignore
[271] Fix | Delete
* @since 3.2.0
[272] Fix | Delete
*
[273] Fix | Delete
* @see _hash_hmac()
[274] Fix | Delete
*
[275] Fix | Delete
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
[276] Fix | Delete
* @param string $data Data to be hashed.
[277] Fix | Delete
* @param string $key Secret key to use for generating the hash.
[278] Fix | Delete
* @param bool $binary Optional. Whether to output raw binary data (true),
[279] Fix | Delete
* or lowercase hexits (false). Default false.
[280] Fix | Delete
* @return string|false The hash in output determined by `$binary`.
[281] Fix | Delete
* False if `$algo` is unknown or invalid.
[282] Fix | Delete
*/
[283] Fix | Delete
function hash_hmac( $algo, $data, $key, $binary = false ) {
[284] Fix | Delete
return _hash_hmac( $algo, $data, $key, $binary );
[285] Fix | Delete
}
[286] Fix | Delete
endif;
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Internal compat function to mimic hash_hmac().
[290] Fix | Delete
*
[291] Fix | Delete
* @ignore
[292] Fix | Delete
* @since 3.2.0
[293] Fix | Delete
*
[294] Fix | Delete
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
[295] Fix | Delete
* @param string $data Data to be hashed.
[296] Fix | Delete
* @param string $key Secret key to use for generating the hash.
[297] Fix | Delete
* @param bool $binary Optional. Whether to output raw binary data (true),
[298] Fix | Delete
* or lowercase hexits (false). Default false.
[299] Fix | Delete
* @return string|false The hash in output determined by `$binary`.
[300] Fix | Delete
* False if `$algo` is unknown or invalid.
[301] Fix | Delete
*/
[302] Fix | Delete
function _hash_hmac( $algo, $data, $key, $binary = false ) {
[303] Fix | Delete
$packs = array(
[304] Fix | Delete
'md5' => 'H32',
[305] Fix | Delete
'sha1' => 'H40',
[306] Fix | Delete
);
[307] Fix | Delete
[308] Fix | Delete
if ( ! isset( $packs[ $algo ] ) ) {
[309] Fix | Delete
return false;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$pack = $packs[ $algo ];
[313] Fix | Delete
[314] Fix | Delete
if ( strlen( $key ) > 64 ) {
[315] Fix | Delete
$key = pack( $pack, $algo( $key ) );
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
$key = str_pad( $key, 64, chr( 0 ) );
[319] Fix | Delete
[320] Fix | Delete
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
[321] Fix | Delete
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );
[322] Fix | Delete
[323] Fix | Delete
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
[324] Fix | Delete
[325] Fix | Delete
if ( $binary ) {
[326] Fix | Delete
return pack( $pack, $hmac );
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
return $hmac;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
if ( ! function_exists( 'hash_equals' ) ) :
[333] Fix | Delete
/**
[334] Fix | Delete
* Timing attack safe string comparison.
[335] Fix | Delete
*
[336] Fix | Delete
* Compares two strings using the same time whether they're equal or not.
[337] Fix | Delete
*
[338] Fix | Delete
* Note: It can leak the length of a string when arguments of differing length are supplied.
[339] Fix | Delete
*
[340] Fix | Delete
* This function was added in PHP 5.6.
[341] Fix | Delete
* However, the Hash extension may be explicitly disabled on select servers.
[342] Fix | Delete
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
[343] Fix | Delete
* longer be disabled.
[344] Fix | Delete
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
[345] Fix | Delete
* can be safely removed.
[346] Fix | Delete
*
[347] Fix | Delete
* @since 3.9.2
[348] Fix | Delete
*
[349] Fix | Delete
* @param string $known_string Expected string.
[350] Fix | Delete
* @param string $user_string Actual, user supplied, string.
[351] Fix | Delete
* @return bool Whether strings are equal.
[352] Fix | Delete
*/
[353] Fix | Delete
function hash_equals( $known_string, $user_string ) {
[354] Fix | Delete
$known_string_length = strlen( $known_string );
[355] Fix | Delete
[356] Fix | Delete
if ( strlen( $user_string ) !== $known_string_length ) {
[357] Fix | Delete
return false;
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
$result = 0;
[361] Fix | Delete
[362] Fix | Delete
// Do not attempt to "optimize" this.
[363] Fix | Delete
for ( $i = 0; $i < $known_string_length; $i++ ) {
[364] Fix | Delete
$result |= ord( $known_string[ $i ] ) ^ ord( $user_string[ $i ] );
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
return 0 === $result;
[368] Fix | Delete
}
[369] Fix | Delete
endif;
[370] Fix | Delete
[371] Fix | Delete
// sodium_crypto_box() was introduced in PHP 7.2.
[372] Fix | Delete
if ( ! function_exists( 'sodium_crypto_box' ) ) {
[373] Fix | Delete
require ABSPATH . WPINC . '/sodium_compat/autoload.php';
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
if ( ! function_exists( 'is_countable' ) ) {
[377] Fix | Delete
/**
[378] Fix | Delete
* Polyfill for is_countable() function added in PHP 7.3.
[379] Fix | Delete
*
[380] Fix | Delete
* Verify that the content of a variable is an array or an object
[381] Fix | Delete
* implementing the Countable interface.
[382] Fix | Delete
*
[383] Fix | Delete
* @since 4.9.6
[384] Fix | Delete
*
[385] Fix | Delete
* @param mixed $value The value to check.
[386] Fix | Delete
* @return bool True if `$value` is countable, false otherwise.
[387] Fix | Delete
*/
[388] Fix | Delete
function is_countable( $value ) {
[389] Fix | Delete
return ( is_array( $value )
[390] Fix | Delete
|| $value instanceof Countable
[391] Fix | Delete
|| $value instanceof SimpleXMLElement
[392] Fix | Delete
|| $value instanceof ResourceBundle
[393] Fix | Delete
);
[394] Fix | Delete
}
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
if ( ! function_exists( 'array_key_first' ) ) {
[398] Fix | Delete
/**
[399] Fix | Delete
* Polyfill for array_key_first() function added in PHP 7.3.
[400] Fix | Delete
*
[401] Fix | Delete
* Get the first key of the given array without affecting
[402] Fix | Delete
* the internal array pointer.
[403] Fix | Delete
*
[404] Fix | Delete
* @since 5.9.0
[405] Fix | Delete
*
[406] Fix | Delete
* @param array $array An array.
[407] Fix | Delete
* @return string|int|null The first key of array if the array
[408] Fix | Delete
* is not empty; `null` otherwise.
[409] Fix | Delete
*/
[410] Fix | Delete
function array_key_first( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
[411] Fix | Delete
foreach ( $array as $key => $value ) {
[412] Fix | Delete
return $key;
[413] Fix | Delete
}
[414] Fix | Delete
}
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
if ( ! function_exists( 'array_key_last' ) ) {
[418] Fix | Delete
/**
[419] Fix | Delete
* Polyfill for `array_key_last()` function added in PHP 7.3.
[420] Fix | Delete
*
[421] Fix | Delete
* Get the last key of the given array without affecting the
[422] Fix | Delete
* internal array pointer.
[423] Fix | Delete
*
[424] Fix | Delete
* @since 5.9.0
[425] Fix | Delete
*
[426] Fix | Delete
* @param array $array An array.
[427] Fix | Delete
* @return string|int|null The last key of array if the array
[428] Fix | Delete
*. is not empty; `null` otherwise.
[429] Fix | Delete
*/
[430] Fix | Delete
function array_key_last( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
[431] Fix | Delete
if ( empty( $array ) ) {
[432] Fix | Delete
return null;
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
end( $array );
[436] Fix | Delete
[437] Fix | Delete
return key( $array );
[438] Fix | Delete
}
[439] Fix | Delete
}
[440] Fix | Delete
[441] Fix | Delete
if ( ! function_exists( 'array_is_list' ) ) {
[442] Fix | Delete
/**
[443] Fix | Delete
* Polyfill for `array_is_list()` function added in PHP 8.1.
[444] Fix | Delete
*
[445] Fix | Delete
* Determines if the given array is a list.
[446] Fix | Delete
*
[447] Fix | Delete
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
[448] Fix | Delete
*
[449] Fix | Delete
* @see https://github.com/symfony/polyfill-php81/tree/main
[450] Fix | Delete
*
[451] Fix | Delete
* @since 6.5.0
[452] Fix | Delete
*
[453] Fix | Delete
* @param array<mixed> $arr The array being evaluated.
[454] Fix | Delete
* @return bool True if array is a list, false otherwise.
[455] Fix | Delete
*/
[456] Fix | Delete
function array_is_list( $arr ) {
[457] Fix | Delete
if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
[458] Fix | Delete
return true;
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
$next_key = -1;
[462] Fix | Delete
[463] Fix | Delete
foreach ( $arr as $k => $v ) {
[464] Fix | Delete
if ( ++$next_key !== $k ) {
[465] Fix | Delete
return false;
[466] Fix | Delete
}
[467] Fix | Delete
}
[468] Fix | Delete
[469] Fix | Delete
return true;
[470] Fix | Delete
}
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
if ( ! function_exists( 'str_contains' ) ) {
[474] Fix | Delete
/**
[475] Fix | Delete
* Polyfill for `str_contains()` function added in PHP 8.0.
[476] Fix | Delete
*
[477] Fix | Delete
* Performs a case-sensitive check indicating if needle is
[478] Fix | Delete
* contained in haystack.
[479] Fix | Delete
*
[480] Fix | Delete
* @since 5.9.0
[481] Fix | Delete
*
[482] Fix | Delete
* @param string $haystack The string to search in.
[483] Fix | Delete
* @param string $needle The substring to search for in the `$haystack`.
[484] Fix | Delete
* @return bool True if `$needle` is in `$haystack`, otherwise false.
[485] Fix | Delete
*/
[486] Fix | Delete
function str_contains( $haystack, $needle ) {
[487] Fix | Delete
if ( '' === $needle ) {
[488] Fix | Delete
return true;
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
return false !== strpos( $haystack, $needle );
[492] Fix | Delete
}
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
if ( ! function_exists( 'str_starts_with' ) ) {
[496] Fix | Delete
/**
[497] Fix | Delete
* Polyfill for `str_starts_with()` function added in PHP 8.0.
[498] Fix | Delete
*
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function