Skip to content

General: Replace core's remaining utf8_encode() calls. - #12920

Open
itzmekhokan wants to merge 3 commits into
WordPress:trunkfrom
itzmekhokan:65828/replace-core-utf8-encode-calls
Open

General: Replace core's remaining utf8_encode() calls.#12920
itzmekhokan wants to merge 3 commits into
WordPress:trunkfrom
itzmekhokan:65828/replace-core-utf8-encode-calls

Conversation

@itzmekhokan

@itzmekhokan itzmekhokan commented Aug 6, 2026

Copy link
Copy Markdown

wxr_cdata() and wp_read_image_metadata() hold the last three calls to utf8_encode() in core, which PHP deprecated in 8.2 and removes in 9.0, and which core polyfilled with its own _deprecated_function() notice in [60950] — so these call sites emit a deprecation notice from core's own code on every affected export and image upload.

This points them at a new private _wp_iso_8859_1_to_utf8() in wp-includes/utf8.php, following the mbstring/fallback split that file already uses.

There is no behaviour change: mb_convert_encoding( $text, 'UTF-8', 'ISO-8859-1' ) is byte-identical to utf8_encode() across all 256 byte values, the no-mbstring branch reuses the existing _wp_utf8_encode_fallback(), and the wp_is_valid_utf8() guards are unchanged.

The tests pin that preserved output, including the keywords from the image attached to #35316.

Verified: 36/36 in the two touched test classes, no failures in the image, unicode, formatting, export and admin groups, and PHPCS reports no new errors or warnings.

Trac ticket: https://core.trac.wordpress.org/ticket/65828

Use of AI Tools

AI assistance: Yes

Tool(s): Claude

Model(s): Opus 5

Used for: initial exploration, tests, and ticket and PR details. All changes are reviewed and validated by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…b_utf8()`.

`wxr_cdata()` and `wp_read_image_metadata()` hold the last three calls to
`utf8_encode()` in core. That function was deprecated in PHP 8.2, is removed in
PHP 9.0, and core itself polyfilled it with a deprecation notice in [60950], so
these sites emit a deprecation notice on every affected export and image upload.

All three used the `if ( ! wp_is_valid_utf8( $text ) ) { utf8_encode( $text ); }`
pattern, which assumes any text failing UTF-8 validation is ISO-8859-1 and
re-encodes the raw bytes on that assumption. That guess is wrong for every other
single-byte encoding and silently produces mojibake. Replacing the invalid spans
with the Unicode replacement character neutralizes the corruption without
inventing an encoding, using the UTF-8 pipeline core added in 6.9.

Adds regression tests covering the WXR export and IPTC metadata that carry
invalid UTF-8.

See #65828, #55603.

@dmsnell dmsnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without commenting on the change itself I have left a few code-level notes.

Comment thread tests/phpunit/tests/admin/exportWp.php Outdated
* Ensures the WXR export neutralizes invalid UTF-8 instead of reinterpreting it as ISO-8859-1.
*
* `wxr_cdata()` previously called the deprecated `utf8_encode()`, which assumed any
* string that failed UTF-8 validation was ISO-8859-1 and re-encoded the raw bytes on

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see note below, but noting in a comment how code used to work can be interesting, but focuses on code that no longer exists.

Comment thread tests/phpunit/tests/admin/exportWp.php Outdated
'Never-valid byte' => array( "a\xC0b", "a\u{FFFD}b" ),
'Truncated sequence' => array( "a\xE2\x9Cb", "a\u{FFFD}b" ),
'Overlong sequence' => array( "a\xC1\xBFb", "a\u{FFFD}\u{FFFD}b" ),
'Surrogate half' => array( "a\xED\xA0\x80b", "a\u{FFFD}\u{FFFD}\u{FFFD}b" ),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are asserting invalid UTF-8 as we tend to think about it, but the noted behavioral change is about alternate input encodings, none of which are demonstrated in these tests.

something that can be clearer than leaving a comment about previous bugs is to leave tests demonstrating the current behaviors. i.e. drop the comment “previously” and add test cases demonstrating the behavior in the presence of alternative inputs.

		'Non-UTF-8-compatible input' => array(
			mb_convert_encoding( 'wyróżnij', 'ISO-8859-2', 'UTF-8' ),
			"wyr\u{FFFD}nij"
		)

}

foreach ( $meta['keywords'] as $key => $keyword ) {
if ( ! wp_is_valid_utf8( $keyword ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there’s no value in calling wp_is_valid_utf8() before calling wp_scrub_utf8() as the latter does the former internally.

- `wp_scrub_utf8()` validates internally, so the preceding `wp_is_valid_utf8()`
  checks were redundant. The `$meta[ $key ]` truthiness check stays, as `iso`
  defaults to int `0`.
- Test docblocks describe current behaviour rather than narrating the code that
  was removed.
- Adds data-provider cases for text in ISO-8859-1, ISO-8859-2, Windows-1251 and
  Windows-1252, which exercise the actual behavioural change; the previous cases
  only covered malformed UTF-8.

See #65828.
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props khokansardar, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Per comment:5 and comment:6, replacing the invalid bytes with U+FFFD regresses
sites whose text really is ISO-8859-1, which is likely most non-UTF-8 content.
The image in #35316 is the concrete case: its Slovak keywords are readable today
and become a run of replacement characters under `wp_scrub_utf8()`.

Removing the deprecated call does not require changing the output. Adds a private
`_wp_iso_8859_1_to_utf8()` alongside the other UTF-8 helpers, following the
mbstring/fallback split already used in that file, and points the three call
sites at it. `mb_convert_encoding( $text, 'UTF-8', 'ISO-8859-1' )` is the
replacement core's own polyfill docblock prescribes and is byte-identical to
`utf8_encode()` across all 256 byte values; the no-mbstring branch reuses
`_wp_utf8_encode_fallback()`, which is already tested for that equivalence in
`Tests_DeprecatedUtf8EncodeDecodeTest`. The `wp_is_valid_utf8()` guards are
restored, since `mb_convert_encoding()` does not validate first.

The tests now pin the preserved behaviour rather than the scrubbed output,
including the #35316 keywords and a case showing that one stray byte sends the
valid portions of a string through the conversion too.

See #65828, #35316, #55603.
@itzmekhokan itzmekhokan changed the title General: Replace core's remaining utf8_encode() calls with wp_scrub_utf8() General: Replace core's remaining utf8_encode() calls. Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants