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