Fix character encoding detection in CSV exporter for PHP 8.1

In PHP 8.1 the behavior of mb_detect_encoding has changed from
"return the first suitable encoding from the list" to
"return the most probable encoding from the list". The algorithm used
for "most probable" often fails, though, and detects valid UTF-8
strings as if they were ISO-8859-1. The fix is to use mb_check_encoding
instead.

Additionally, if conversion is needed, mb_convert_encoding is now used
since utf8_encode is deprecated.
This commit is contained in:
Nestor Soriano 2023-06-08 12:07:36 +02:00
parent 5c4746cefc
commit 6c2781eedf
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
1 changed files with 4 additions and 2 deletions

View File

@ -398,8 +398,10 @@ abstract class WC_CSV_Exporter {
$use_mb = function_exists( 'mb_convert_encoding' );
if ( $use_mb ) {
$encoding = mb_detect_encoding( $data, 'UTF-8, ISO-8859-1', true );
$data = 'UTF-8' === $encoding ? $data : utf8_encode( $data );
$is_valid_utf_8 = mb_check_encoding( $data, 'UTF-8' );
if ( ! $is_valid_utf_8 ) {
$data = mb_convert_encoding( $data, 'UTF-8', 'ISO-8859-1' );
}
}
return $this->escape_data( $data );