<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[60950] trunk: Charset: Conditionally polyfill utf8_encode() and utf8_decode().</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/60950">60950</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/60950","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dmsnell</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-10-16 23:17:14 +0000 (Thu, 16 Oct 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/63863">#63863</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescompatutf8php">trunk/src/wp-includes/compat-utf8.php</a></li>
<li><a href="#trunksrcwpincludescompatphp">trunk/src/wp-includes/compat.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsformattingdeprecatedUtfEncodeDecodephp">trunk/tests/phpunit/tests/formatting/deprecatedUtfEncodeDecode.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescompatutf8php"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/compat-utf8.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/compat-utf8.php 2025-10-16 20:58:26 UTC (rev 60949)
+++ trunk/src/wp-includes/compat-utf8.php 2025-10-16 23:17:14 UTC (rev 60950)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -337,3 +337,142 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $count;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Converts a string from ISO-8859-1 to UTF-8, maintaining backwards compatibility
+ * with the deprecated function from the PHP standard library.
+ *
+ * @since 6.9.0
+ * @access private
+ *
+ * @see \utf8_encode()
+ *
+ * @param string $iso_8859_1_text Text treated as ISO-8859-1 (latin1) bytes.
+ * @return string Text converted into UTF-8.
+ */
+function _wp_utf8_encode_fallback( $iso_8859_1_text ) {
+ $iso_8859_1_text = (string) $iso_8859_1_text;
+ $at = 0;
+ $was_at = 0;
+ $end = strlen( $iso_8859_1_text );
+ $utf8 = '';
+
+ while ( $at < $end ) {
+ // US-ASCII bytes are identical in ISO-8859-1 and UTF-8. These are 0x00–0x7F.
+ $ascii_byte_count = strspn(
+ $iso_8859_1_text,
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" .
+ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" .
+ " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f",
+ $at
+ );
+
+ if ( $ascii_byte_count > 0 ) {
+ $at += $ascii_byte_count;
+ continue;
+ }
+
+ // All other bytes transform into two-byte UTF-8 sequences.
+ $code_point = ord( $iso_8859_1_text[ $at ] );
+ $byte1 = chr( 0xC0 | ( $code_point >> 6 ) );
+ $byte2 = chr( 0x80 | ( $code_point & 0x3F ) );
+
+ $utf8 .= substr( $iso_8859_1_text, $was_at, $at - $was_at );
+ $utf8 .= "{$byte1}{$byte2}";
+
+ ++$at;
+ $was_at = $at;
+ }
+
+ if ( 0 === $was_at ) {
+ return $iso_8859_1_text;
+ }
+
+ $utf8 .= substr( $iso_8859_1_text, $was_at );
+ return $utf8;
+}
+
+/**
+ * Converts a string from UTF-8 to ISO-8859-1, maintaining backwards compatibility
+ * with the deprecated function from the PHP standard library.
+ *
+ * @since 6.9.0
+ * @access private
+ *
+ * @see \utf8_decode()
+ *
+ * @param string $utf8_text Text treated as UTF-8 bytes.
+ * @return string Text converted into ISO-8859-1.
+ */
+function _wp_utf8_decode_fallback( $utf8_text ) {
+ $utf8_text = (string) $utf8_text;
+ $at = 0;
+ $was_at = 0;
+ $end = strlen( $utf8_text );
+ $iso_8859_1_text = '';
+
+ while ( $at < $end ) {
+ // US-ASCII bytes are identical in ISO-8859-1 and UTF-8. These are 0x00–0x7F.
+ $ascii_byte_count = strspn(
+ $utf8_text,
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" .
+ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" .
+ " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f",
+ $at
+ );
+
+ if ( $ascii_byte_count > 0 ) {
+ $at += $ascii_byte_count;
+ continue;
+ }
+
+ $next_at = $at;
+ $invalid_length = 0;
+ $found = _wp_scan_utf8( $utf8_text, $next_at, $invalid_length, null, 1 );
+ $span_length = $next_at - $at;
+ $next_byte = '?';
+
+ if ( 1 !== $found ) {
+ if ( $invalid_length > 0 ) {
+ $next_byte = '';
+ goto flush_sub_part;
+ }
+
+ break;
+ }
+
+ // All convertible code points are two-bytes long.
+ $byte1 = ord( $utf8_text[ $at ] );
+ if ( 0xC0 !== ( $byte1 & 0xE0 ) ) {
+ goto flush_sub_part;
+ }
+
+ // All convertible code points are not greater than U+FF.
+ $byte2 = ord( $utf8_text[ $at + 1 ] );
+ $code_point = ( ( $byte1 & 0x1F ) << 6 ) | ( ( $byte2 & 0x3F ) );
+ if ( $code_point > 0xFF ) {
+ goto flush_sub_part;
+ }
+
+ $next_byte = chr( $code_point );
+
+ flush_sub_part:
+ $iso_8859_1_text .= substr( $utf8_text, $was_at, $at - $was_at );
+ $iso_8859_1_text .= $next_byte;
+ $at += $span_length;
+ $was_at = $at;
+
+ if ( $invalid_length > 0 ) {
+ $iso_8859_1_text .= '?';
+ $at += $invalid_length;
+ $was_at = $at;
+ }
+ }
+
+ if ( 0 === $was_at ) {
+ return $utf8_text;
+ }
+
+ $iso_8859_1_text .= substr( $utf8_text, $was_at );
+ return $iso_8859_1_text;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludescompatphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/compat.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/compat.php 2025-10-16 20:58:26 UTC (rev 60949)
+++ trunk/src/wp-includes/compat.php 2025-10-16 23:17:14 UTC (rev 60950)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -247,6 +247,74 @@
</span><span class="cx" style="display: block; padding: 0 10px"> : strlen( $str );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! function_exists( 'utf8_encode' ) ) :
+ if ( extension_loaded( 'mbstring' ) ) :
+ /**
+ * Converts a string from ISO-8859-1 to UTF-8.
+ *
+ * @deprecated Use {@see \mb_convert_encoding()} instead.
+ *
+ * @since 6.9.0
+ *
+ * @param string $iso_8859_1_text Text treated as ISO-8859-1 (latin1) bytes.
+ * @return string Text converted into a UTF-8.
+ */
+ function utf8_encode( $iso_8859_1_text ): string {
+ _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
+
+ return mb_convert_encoding( $iso_8859_1_text, 'UTF-8', 'ISO-8859-1' );
+ }
+
+ else :
+ /**
+ * @ignore
+ * @private
+ *
+ * @since 6.9.0
+ */
+ function utf8_encode( $iso_8859_1_text ): string {
+ _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
+
+ return _wp_utf8_encode_fallback( $iso_8859_1_text );
+ }
+
+ endif;
+endif;
+
+if ( ! function_exists( 'utf8_decode' ) ) :
+ if ( extension_loaded( 'mbstring' ) ) :
+ /**
+ * Converts a string from UTF-8 to ISO-8859-1.
+ *
+ * @deprecated Use {@see \mb_convert_encoding()} instead.
+ *
+ * @since 6.9.0
+ *
+ * @param string $utf8_text Text treated as UTF-8.
+ * @return string Text converted into ISO-8859-1.
+ */
+ function utf8_decode( $utf8_text ): string {
+ _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
+
+ return mb_convert_encoding( $utf8_text, 'ISO-8859-1', 'UTF-8' );
+ }
+
+ else :
+ /**
+ * @ignore
+ * @private
+ *
+ * @since 6.9.0
+ */
+ function utf8_decode( $utf8_text ): string {
+ _deprecated_function( __FUNCTION__, '6.9.0', 'mb_convert_encoding' );
+
+ return _wp_utf8_decode_fallback( $utf8_text );
+ }
+
+ endif;
+endif;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // sodium_crypto_box() was introduced in PHP 7.2.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! function_exists( 'sodium_crypto_box' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/sodium_compat/autoload.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsformattingdeprecatedUtfEncodeDecodephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/formatting/deprecatedUtfEncodeDecode.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/formatting/deprecatedUtfEncodeDecode.php (rev 0)
+++ trunk/tests/phpunit/tests/formatting/deprecatedUtfEncodeDecode.php 2025-10-16 23:17:14 UTC (rev 60950)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,114 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group formatting
+ */
+class Tests_DeprecatedUtf8EncodeDecodeTest extends WP_UnitTestCase {
+ /**
+ * Ensures that the fallback for {@see \utf8_encode()} maps the ISO-8859-1 characters properly.
+ *
+ * @ticket 63863.
+ */
+ public function test_utf8_encode_characters() {
+ for ( $i = 0; $i <= 0xFF; $i++ ) {
+ $c = chr( $i );
+ $hex_i = strtoupper( str_pad( dechex( $i ), 2, '0', STR_PAD_LEFT ) );
+
+ $this->assertSame(
+ bin2hex( mb_convert_encoding( $c, 'UTF-8', 'ISO-8859-1' ) ),
+ bin2hex( _wp_utf8_encode_fallback( $c ) ),
+ "Failed to convert U+{$hex_i} properly."
+ );
+ }
+ }
+
+ /**
+ * Ensures that the fallback for {@see \utf8_encode()} properly
+ * matches the legacy behavior for a given set of test cases.
+ *
+ * @ticket 63863.
+ *
+ * @dataProvider data_utf8_strings
+ */
+ public function test_utf8_encode_cases( $input ) {
+ $this->assertSame(
+ mb_convert_encoding( $input, 'UTF-8', 'ISO-8859-1' ),
+ _wp_utf8_encode_fallback( $input ),
+ 'Failed to properly convert.'
+ );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[].
+ */
+ public static function data_utf8_strings() {
+ return array(
+ 'Basic valid string' => array( 'Dan eats cinnamon toast.' ),
+ 'Valid with Emoji' => array( 'The best Emoji is 🅰.' ),
+ 'Truncated bytes' => array( substr( 'England has 🏴', 0, -1 ) ),
+ 'Minimal subpart' => array( "One \xC0, two \xE2\x80, three \xF0\x95\x85." ),
+ );
+ }
+
+ /**
+ * Ensures that the fallback for {@see \utf8_decode()} maps the UTF-8 characters properly.
+ *
+ * @ticket 63863.
+ */
+ public function test_utf8_decode_characters() {
+ for ( $i = 0; $i <= 0x10FFFF; $i++ ) {
+ $hex_i = strtoupper( str_pad( dechex( $i ), 2, '0', STR_PAD_LEFT ) );
+
+ if ( $i < 0xD800 || $i > 0xE000 ) {
+ $c = mb_chr( $i );
+ } else {
+ /*
+ * Since the UTF-16 surrogate halves are not valid Unicode characters,
+ * these have to be manually constructed as invalid UTF-8.
+ */
+ $byte1 = 0xE0 | ( $i >> 12 );
+ $byte2 = 0x80 | ( ( $i >> 6 ) & 0x3F );
+ $byte3 = 0x80 | ( $i & 0x3F );
+
+ $c = "{$byte1}{$byte2}{$byte3}";
+ }
+
+ $this->assertSame(
+ bin2hex( mb_convert_encoding( $c, 'ISO-8859-1', 'UTF-8' ) ),
+ bin2hex( _wp_utf8_decode_fallback( $c ) ),
+ "Failed to convert U+{$hex_i} properly."
+ );
+ }
+ }
+
+ /**
+ * Ensures that the fallback for {@see \utf8_encode()} properly
+ * matches the legacy behavior for a given set of test cases.
+ *
+ * @ticket 63863.
+ *
+ * @dataProvider data_iso_8859_1_strings
+ */
+ public function test_utf8_decode_cases( $input ) {
+ $this->assertSame(
+ mb_convert_encoding( $input, 'ISO-8859-1', 'UTF-8' ),
+ _wp_utf8_decode_fallback( $input ),
+ 'Failed to properly convert.'
+ );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[].
+ */
+ public static function data_iso_8859_1_strings() {
+ return array(
+ 'Basic valid string' => array( 'Dan eats cinnamon toast' ),
+ 'Latin1 supplement' => array( 'Pi\xF1a is another name for Pineapple.' ),
+ 'Bytes as invalid UTF-8' => array( 'The \x95 is invalid UTF-8.' ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/formatting/deprecatedUtfEncodeDecode.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>