wp/wp-includes/compat.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/compat.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/compat.php	Fri Sep 05 18:40:08 2025 +0200
@@ -8,8 +8,8 @@
 
 // If gettext isn't available.
 if ( ! function_exists( '_' ) ) {
-	function _( $string ) {
-		return $string;
+	function _( $message ) {
+		return $message;
 	}
 }
 
@@ -40,6 +40,43 @@
 	return $utf8_pcre;
 }
 
+/**
+ * Indicates if a given slug for a character set represents the UTF-8 text encoding.
+ *
+ * A charset is considered to represent UTF-8 if it is a case-insensitive match
+ * of "UTF-8" with or without the hyphen.
+ *
+ * Example:
+ *
+ *     true  === _is_utf8_charset( 'UTF-8' );
+ *     true  === _is_utf8_charset( 'utf8' );
+ *     false === _is_utf8_charset( 'latin1' );
+ *     false === _is_utf8_charset( 'UTF 8' );
+ *
+ *     // Only strings match.
+ *     false === _is_utf8_charset( [ 'charset' => 'utf-8' ] );
+ *
+ * `is_utf8_charset` should be used outside of this file.
+ *
+ * @ignore
+ * @since 6.6.1
+ *
+ * @param string $charset_slug Slug representing a text character encoding, or "charset".
+ *                             E.g. "UTF-8", "Windows-1252", "ISO-8859-1", "SJIS".
+ *
+ * @return bool Whether the slug represents the UTF-8 encoding.
+ */
+function _is_utf8_charset( $charset_slug ) {
+	if ( ! is_string( $charset_slug ) ) {
+		return false;
+	}
+
+	return (
+		0 === strcasecmp( 'UTF-8', $charset_slug ) ||
+		0 === strcasecmp( 'UTF8', $charset_slug )
+	);
+}
+
 if ( ! function_exists( 'mb_substr' ) ) :
 	/**
 	 * Compat function to mimic mb_substr().
@@ -49,24 +86,24 @@
 	 *
 	 * @see _mb_substr()
 	 *
-	 * @param string      $str      The string to extract the substring from.
-	 * @param int         $start    Position to being extraction from in `$str`.
-	 * @param int|null    $length   Optional. Maximum number of characters to extract from `$str`.
+	 * @param string      $string   The string to extract the substring from.
+	 * @param int         $start    Position to being extraction from in `$string`.
+	 * @param int|null    $length   Optional. Maximum number of characters to extract from `$string`.
 	 *                              Default null.
 	 * @param string|null $encoding Optional. Character encoding to use. Default null.
 	 * @return string Extracted substring.
 	 */
-	function mb_substr( $str, $start, $length = null, $encoding = null ) {
-		return _mb_substr( $str, $start, $length, $encoding );
+	function mb_substr( $string, $start, $length = null, $encoding = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
+		return _mb_substr( $string, $start, $length, $encoding );
 	}
 endif;
 
 /**
  * Internal compat function to mimic mb_substr().
  *
- * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
- * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
- * The behavior of this function for invalid inputs is undefined.
+ * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
+ * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
+ * sequence. The behavior of this function for invalid inputs is undefined.
  *
  * @ignore
  * @since 3.2.0
@@ -91,7 +128,7 @@
 	 * The solution below works only for UTF-8, so in case of a different
 	 * charset just use built-in substr().
 	 */
-	if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
+	if ( ! _is_utf8_charset( $encoding ) ) {
 		return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
 	}
 
@@ -116,6 +153,7 @@
 
 	// Start with 1 element instead of 0 since the first thing we do is pop.
 	$chars = array( '' );
+
 	do {
 		// We had some string left over from the last round, but we counted it in that last round.
 		array_pop( $chars );
@@ -143,20 +181,20 @@
 	 *
 	 * @see _mb_strlen()
 	 *
-	 * @param string      $str      The string to retrieve the character length from.
+	 * @param string      $string   The string to retrieve the character length from.
 	 * @param string|null $encoding Optional. Character encoding to use. Default null.
-	 * @return int String length of `$str`.
+	 * @return int String length of `$string`.
 	 */
-	function mb_strlen( $str, $encoding = null ) {
-		return _mb_strlen( $str, $encoding );
+	function mb_strlen( $string, $encoding = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.stringFound
+		return _mb_strlen( $string, $encoding );
 	}
 endif;
 
 /**
  * Internal compat function to mimic mb_strlen().
  *
- * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
- * For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
+ * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
+ * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
  * sequence. The behavior of this function for invalid inputs is undefined.
  *
  * @ignore
@@ -175,7 +213,7 @@
 	 * The solution below works only for UTF-8, so in case of a different charset
 	 * just use built-in strlen().
 	 */
-	if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
+	if ( ! _is_utf8_charset( $encoding ) ) {
 		return strlen( $str );
 	}
 
@@ -199,9 +237,10 @@
 
 	// Start at 1 instead of 0 since the first thing we do is decrement.
 	$count = 1;
+
 	do {
 		// We had some string left over from the last round, but we counted it in that last round.
-		$count--;
+		--$count;
 
 		/*
 		 * Split by UTF-8 character, limit to 1000 characters (last array element will contain
@@ -235,16 +274,16 @@
 	 *
 	 * @see _hash_hmac()
 	 *
-	 * @param string $algo       Hash algorithm. Accepts 'md5' or 'sha1'.
-	 * @param string $data       Data to be hashed.
-	 * @param string $key        Secret key to use for generating the hash.
-	 * @param bool   $raw_output Optional. Whether to output raw binary data (true),
-	 *                           or lowercase hexits (false). Default false.
-	 * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
-	 *                      is unknown or invalid.
+	 * @param string $algo   Hash algorithm. Accepts 'md5' or 'sha1'.
+	 * @param string $data   Data to be hashed.
+	 * @param string $key    Secret key to use for generating the hash.
+	 * @param bool   $binary Optional. Whether to output raw binary data (true),
+	 *                       or lowercase hexits (false). Default false.
+	 * @return string|false The hash in output determined by `$binary`.
+	 *                      False if `$algo` is unknown or invalid.
 	 */
-	function hash_hmac( $algo, $data, $key, $raw_output = false ) {
-		return _hash_hmac( $algo, $data, $key, $raw_output );
+	function hash_hmac( $algo, $data, $key, $binary = false ) {
+		return _hash_hmac( $algo, $data, $key, $binary );
 	}
 endif;
 
@@ -254,15 +293,15 @@
  * @ignore
  * @since 3.2.0
  *
- * @param string $algo       Hash algorithm. Accepts 'md5' or 'sha1'.
- * @param string $data       Data to be hashed.
- * @param string $key        Secret key to use for generating the hash.
- * @param bool   $raw_output Optional. Whether to output raw binary data (true),
- *                           or lowercase hexits (false). Default false.
- * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
- *                      is unknown or invalid.
+ * @param string $algo   Hash algorithm. Accepts 'md5' or 'sha1'.
+ * @param string $data   Data to be hashed.
+ * @param string $key    Secret key to use for generating the hash.
+ * @param bool   $binary Optional. Whether to output raw binary data (true),
+ *                       or lowercase hexits (false). Default false.
+ * @return string|false The hash in output determined by `$binary`.
+ *                      False if `$algo` is unknown or invalid.
  */
-function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
+function _hash_hmac( $algo, $data, $key, $binary = false ) {
 	$packs = array(
 		'md5'  => 'H32',
 		'sha1' => 'H40',
@@ -285,15 +324,16 @@
 
 	$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
 
-	if ( $raw_output ) {
+	if ( $binary ) {
 		return pack( $pack, $hmac );
 	}
+
 	return $hmac;
 }
 
 if ( ! function_exists( 'hash_equals' ) ) :
 	/**
-	 * Timing attack safe string comparison
+	 * Timing attack safe string comparison.
 	 *
 	 * Compares two strings using the same time whether they're equal or not.
 	 *
@@ -308,30 +348,28 @@
 	 *
 	 * @since 3.9.2
 	 *
-	 * @param string $a Expected string.
-	 * @param string $b Actual, user supplied, string.
+	 * @param string $known_string Expected string.
+	 * @param string $user_string  Actual, user supplied, string.
 	 * @return bool Whether strings are equal.
 	 */
-	function hash_equals( $a, $b ) {
-		$a_length = strlen( $a );
-		if ( strlen( $b ) !== $a_length ) {
+	function hash_equals( $known_string, $user_string ) {
+		$known_string_length = strlen( $known_string );
+
+		if ( strlen( $user_string ) !== $known_string_length ) {
 			return false;
 		}
+
 		$result = 0;
 
 		// Do not attempt to "optimize" this.
-		for ( $i = 0; $i < $a_length; $i++ ) {
-			$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
+		for ( $i = 0; $i < $known_string_length; $i++ ) {
+			$result |= ord( $known_string[ $i ] ) ^ ord( $user_string[ $i ] );
 		}
 
 		return 0 === $result;
 	}
 endif;
 
-// random_int() was introduced in PHP 7.0.
-if ( ! function_exists( 'random_int' ) ) {
-	require ABSPATH . WPINC . '/random_compat/random.php';
-}
 // sodium_crypto_box() was introduced in PHP 7.2.
 if ( ! function_exists( 'sodium_crypto_box' ) ) {
 	require ABSPATH . WPINC . '/sodium_compat/autoload.php';
@@ -346,35 +384,18 @@
 	 *
 	 * @since 4.9.6
 	 *
-	 * @param mixed $var The value to check.
-	 * @return bool True if `$var` is countable, false otherwise.
+	 * @param mixed $value The value to check.
+	 * @return bool True if `$value` is countable, false otherwise.
 	 */
-	function is_countable( $var ) {
-		return ( is_array( $var )
-			|| $var instanceof Countable
-			|| $var instanceof SimpleXMLElement
-			|| $var instanceof ResourceBundle
+	function is_countable( $value ) {
+		return ( is_array( $value )
+			|| $value instanceof Countable
+			|| $value instanceof SimpleXMLElement
+			|| $value instanceof ResourceBundle
 		);
 	}
 }
 
-if ( ! function_exists( 'is_iterable' ) ) {
-	/**
-	 * Polyfill for is_iterable() function added in PHP 7.1.
-	 *
-	 * Verify that the content of a variable is an array or an object
-	 * implementing the Traversable interface.
-	 *
-	 * @since 4.9.6
-	 *
-	 * @param mixed $var The value to check.
-	 * @return bool True if `$var` is iterable, false otherwise.
-	 */
-	function is_iterable( $var ) {
-		return ( is_array( $var ) || $var instanceof Traversable );
-	}
-}
-
 if ( ! function_exists( 'array_key_first' ) ) {
 	/**
 	 * Polyfill for array_key_first() function added in PHP 7.3.
@@ -384,12 +405,12 @@
 	 *
 	 * @since 5.9.0
 	 *
-	 * @param array $arr An array.
+	 * @param array $array An array.
 	 * @return string|int|null The first key of array if the array
 	 *                         is not empty; `null` otherwise.
 	 */
-	function array_key_first( array $arr ) {
-		foreach ( $arr as $key => $value ) {
+	function array_key_first( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
+		foreach ( $array as $key => $value ) {
 			return $key;
 		}
 	}
@@ -404,16 +425,50 @@
 	 *
 	 * @since 5.9.0
 	 *
-	 * @param array $arr An array.
+	 * @param array $array An array.
 	 * @return string|int|null The last key of array if the array
 	 *.                        is not empty; `null` otherwise.
 	 */
-	function array_key_last( array $arr ) {
-		if ( empty( $arr ) ) {
+	function array_key_last( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
+		if ( empty( $array ) ) {
 			return null;
 		}
-		end( $arr );
-		return key( $arr );
+
+		end( $array );
+
+		return key( $array );
+	}
+}
+
+if ( ! function_exists( 'array_is_list' ) ) {
+	/**
+	 * Polyfill for `array_is_list()` function added in PHP 8.1.
+	 *
+	 * Determines if the given array is a list.
+	 *
+	 * An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
+	 *
+	 * @see https://github.com/symfony/polyfill-php81/tree/main
+	 *
+	 * @since 6.5.0
+	 *
+	 * @param array<mixed> $arr The array being evaluated.
+	 * @return bool True if array is a list, false otherwise.
+	 */
+	function array_is_list( $arr ) {
+		if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
+			return true;
+		}
+
+		$next_key = -1;
+
+		foreach ( $arr as $k => $v ) {
+			if ( ++$next_key !== $k ) {
+				return false;
+			}
+		}
+
+		return true;
 	}
 }
 
@@ -427,11 +482,15 @@
 	 * @since 5.9.0
 	 *
 	 * @param string $haystack The string to search in.
-	 * @param string $needle   The substring to search for in the haystack.
+	 * @param string $needle   The substring to search for in the `$haystack`.
 	 * @return bool True if `$needle` is in `$haystack`, otherwise false.
 	 */
 	function str_contains( $haystack, $needle ) {
-		return ( '' === $needle || false !== strpos( $haystack, $needle ) );
+		if ( '' === $needle ) {
+			return true;
+		}
+
+		return false !== strpos( $haystack, $needle );
 	}
 }
 
@@ -452,6 +511,7 @@
 		if ( '' === $needle ) {
 			return true;
 		}
+
 		return 0 === strpos( $haystack, $needle );
 	}
 }
@@ -470,20 +530,22 @@
 	 * @return bool True if `$haystack` ends with `$needle`, otherwise false.
 	 */
 	function str_ends_with( $haystack, $needle ) {
-		if ( '' === $haystack && '' !== $needle ) {
-			return false;
+		if ( '' === $haystack ) {
+			return '' === $needle;
 		}
+
 		$len = strlen( $needle );
-		return 0 === substr_compare( $haystack, $needle, -$len, $len );
+
+		return substr( $haystack, -$len, $len ) === $needle;
 	}
 }
 
-// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
-if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
-	define( 'IMAGETYPE_WEBP', 18 );
+// IMAGETYPE_AVIF constant is only defined in PHP 8.x or later.
+if ( ! defined( 'IMAGETYPE_AVIF' ) ) {
+	define( 'IMAGETYPE_AVIF', 19 );
 }
 
-// IMG_WEBP constant is only defined in PHP 7.0.10 or later.
-if ( ! defined( 'IMG_WEBP' ) ) {
-	define( 'IMG_WEBP', IMAGETYPE_WEBP );
+// IMG_AVIF constant is only defined in PHP 8.x or later.
+if ( ! defined( 'IMG_AVIF' ) ) {
+	define( 'IMG_AVIF', IMAGETYPE_AVIF );
 }