From 6c2781eedfbe7ac9ba5767fdaff26cbef9051c3d Mon Sep 17 00:00:00 2001 From: Nestor Soriano Date: Thu, 8 Jun 2023 12:07:36 +0200 Subject: [PATCH 1/2] 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. --- .../includes/export/abstract-wc-csv-exporter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/includes/export/abstract-wc-csv-exporter.php b/plugins/woocommerce/includes/export/abstract-wc-csv-exporter.php index d87576f14f7..733719cba52 100644 --- a/plugins/woocommerce/includes/export/abstract-wc-csv-exporter.php +++ b/plugins/woocommerce/includes/export/abstract-wc-csv-exporter.php @@ -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 ); From 76270ef8cb226dd6a78c49feccdfc1e24fb5167c Mon Sep 17 00:00:00 2001 From: Nestor Soriano Date: Thu, 8 Jun 2023 12:11:51 +0200 Subject: [PATCH 2/2] Add changelog file --- .../changelog/fix-encoding-in-product-exporter-in-php-8.1 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-encoding-in-product-exporter-in-php-8.1 diff --git a/plugins/woocommerce/changelog/fix-encoding-in-product-exporter-in-php-8.1 b/plugins/woocommerce/changelog/fix-encoding-in-product-exporter-in-php-8.1 new file mode 100644 index 00000000000..1993c66352b --- /dev/null +++ b/plugins/woocommerce/changelog/fix-encoding-in-product-exporter-in-php-8.1 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix character encoding detection in CSV exporter for PHP 8.1