Make WordPress Core


Ignore:
Timestamp:
10/16/2025 11:17:14 PM (12 months ago)
Author:
dmsnell
Message:

Charset: Conditionally polyfill utf8_encode() and utf8_decode().

The utf8_encode() and utf8_decode() functions were deprecated in PHP 8.2.0 and will be removed in PHP 9.0. When that happens, any existing code which calls them will trigger a crash.

This patch introduces polyfills for those functions when they aren’t already present. The polyfill functions maintain backwards compatibility, including a deprecation notice.

Any code calling either of these functions ought to be refactored to avoid using them; there are better options which don’t carry the issues these functions do, and any code calling them is likely calling them inappropriately.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/10011
Discussed in https://core.trac.wordpress.org/ticket/55603
Discussed in https://core.trac.wordpress.org/ticket/63863

See #63863.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r60949 r60950  
    248248}
    249249
     250if ( ! function_exists( 'utf8_encode' ) ) :
     251        if ( extension_loaded( 'mbstring' ) ) :
     252                /**
     253                 * Converts a string from ISO-8859-1 to UTF-8.
     254                 *
     255                 * @deprecated Use {@see \mb_convert_encoding()} instead.
     256                 *
     257                 * @since 6.9.0
     258                 *
     259                 * @param string $iso_8859_1_text Text treated as ISO-8859-1 (latin1) bytes.
     260                 * @return string Text converted into a UTF-8.
     261                 */
     262                function utf8_encode( $iso_8859_1_text ): string {
     263                        _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
     264
     265                        return mb_convert_encoding( $iso_8859_1_text, 'UTF-8', 'ISO-8859-1' );
     266                }
     267
     268        else :
     269                /**
     270                 * @ignore
     271                 * @private
     272                 *
     273                 * @since 6.9.0
     274                 */
     275                function utf8_encode( $iso_8859_1_text ): string {
     276                        _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
     277
     278                        return _wp_utf8_encode_fallback( $iso_8859_1_text );
     279                }
     280
     281        endif;
     282endif;
     283
     284if ( ! function_exists( 'utf8_decode' ) ) :
     285        if ( extension_loaded( 'mbstring' ) ) :
     286                /**
     287                 * Converts a string from UTF-8 to ISO-8859-1.
     288                 *
     289                 * @deprecated Use {@see \mb_convert_encoding()} instead.
     290                 *
     291                 * @since 6.9.0
     292                 *
     293                 * @param string $utf8_text Text treated as UTF-8.
     294                 * @return string Text converted into ISO-8859-1.
     295                 */
     296                function utf8_decode( $utf8_text ): string {
     297                        _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
     298
     299                        return mb_convert_encoding( $utf8_text, 'ISO-8859-1', 'UTF-8' );
     300                }
     301
     302        else :
     303                /**
     304                 * @ignore
     305                 * @private
     306                 *
     307                 * @since 6.9.0
     308                 */
     309                function utf8_decode( $utf8_text ): string {
     310                        _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
     311
     312                        return _wp_utf8_decode_fallback( $utf8_text );
     313                }
     314
     315        endif;
     316endif;
     317
    250318// sodium_crypto_box() was introduced in PHP 7.2.
    251319if ( ! function_exists( 'sodium_crypto_box' ) ) {
Note: See TracChangeset for help on using the changeset viewer.