Commit ed9adb7a3c6 for woocommerce

commit ed9adb7a3c65eedd86231de456562d19f12c9e16
Author: Faisal Ahammad <faisalahammad24@gmail.com>
Date:   Tue Nov 24 22:56:06 2026 +0600

    Replace deprecated utf8_encode() in the tax rate CSV importer (#66047)

    * fix: update deprecated UTF-8 functions for WordPress 6.9 compatibility

    Replace deprecated wp_check_invalid_utf8() with wp_scrub_utf8()
    and utf8_encode() with mb_convert_encoding() to align with
    WordPress 6.9 best practices.

    Changes:
    - wc_check_invalid_utf8() wrapper now uses wp_scrub_utf8()
    - Updated 3 direct wp_check_invalid_utf8() call sites
    - Replaced utf8_encode() in tax importer with mb_convert_encoding()

    Fixes #62019

    * Fix changelog path, mbstring guard, and wc_check_invalid_utf8 docblock

    - Move changelog to correct path (plugins/woocommerce/changelog/)
    - Guard mb_convert_encoding with function_exists() in tax rate CSV importer
    - Update wc_check_invalid_utf8 docblock with @since 11.0.0 annotation
      documenting behavioral change

    Refs #66047

    * Fix PHPStan errors for WordPress 6.9 UTF-8 functions

    - Add phpstan stub declaring wp_scrub_utf8(), introduced in WordPress
      6.9, until the installed wordpress-stubs package ships a 6.9 release.
    - Cast CSV data to string and fall back to wp_scrub_utf8() when
      mb_convert_encoding() is unavailable or fails in the tax rate importer.
    - Mark wc_check_invalid_utf8() as deprecated in favor of wp_scrub_utf8(),
      documenting the U+FFFD behavior change.

    Refs #66047

    * fix(ci): skip changelog push for cross-repository pull requests

    * fix(utf8): handle null and Latin-1 input safely

    Cast null values to string before calling wp_scrub_utf8() so the
    non-mbstring fallback does not throw a TypeError. Order item names
    and wc_check_invalid_utf8() now accept null.

    The tax rate importer no longer relies on wp_scrub_utf8() for the
    ISO-8859-1 fallback, because that call replaced non-ASCII bytes
    instead of converting them. It now uses a small pure PHP Latin-1 to
    UTF-8 converter that works without mbstring.

    Add tests for null input, invalid bytes and the Latin-1 fallback.

    Addresses PR feedback. Refs #66047

    * fix(tax-importer): only replace deprecated utf8_encode

    - Revert wp_check_invalid_utf8() swaps; it is not deprecated
    - Remove hand-written WP 6.9 PHPStan stubs
    - Keep mb_convert_encoding() with Latin-1 fallback in tax importer
    - Rename $match callback param to clear phpcs warning
    - Rewrite changelog to match the change

    Addresses PR feedback.

    Refs #66047

diff --git a/plugins/woocommerce/changelog/fix-62019-update-deprecated-utf8-functions b/plugins/woocommerce/changelog/fix-62019-update-deprecated-utf8-functions
new file mode 100644
index 00000000000..a252f19e23b
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-62019-update-deprecated-utf8-functions
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Convert ISO-8859-1 tax rate CSV data to UTF-8 without the utf8_encode() function, which is deprecated in PHP 8.2.
diff --git a/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php b/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php
index b4d76541d16..02ab69171ee 100644
--- a/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php
+++ b/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php
@@ -125,7 +125,37 @@ class WC_Tax_Rate_Importer extends WP_Importer {
 	 * @return string
 	 */
 	public function format_data_from_csv( $data, $enc ) {
-		return ( 'UTF-8' === $enc ) ? $data : utf8_encode( $data );
+		if ( 'UTF-8' === $enc ) {
+			return $data;
+		}
+
+		$data = (string) $data;
+
+		if ( function_exists( 'mb_convert_encoding' ) ) {
+			$converted = mb_convert_encoding( $data, 'UTF-8', 'ISO-8859-1' );
+			if ( false !== $converted ) {
+				return $converted;
+			}
+		}
+
+		return self::latin1_to_utf8( $data );
+	}
+
+	/**
+	 * Convert ISO-8859-1 data to UTF-8 without the mbstring extension.
+	 *
+	 * @param string $data ISO-8859-1 data.
+	 * @return string
+	 */
+	private static function latin1_to_utf8( string $data ): string {
+		return (string) preg_replace_callback(
+			'/[\x80-\xFF]/',
+			static function ( $matches ) {
+				$byte = ord( $matches[0] );
+				return chr( 0xC0 | ( $byte >> 6 ) ) . chr( 0x80 | ( $byte & 0x3F ) );
+			},
+			$data
+		);
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/includes/admin/importers/class-wc-tax-rate-importer-test.php b/plugins/woocommerce/tests/php/includes/admin/importers/class-wc-tax-rate-importer-test.php
new file mode 100644
index 00000000000..8de5295624b
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/admin/importers/class-wc-tax-rate-importer-test.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Tests for the WC_Tax_Rate_Importer class.
+ *
+ * @package WooCommerce\Tests\Admin\Importers
+ */
+
+declare( strict_types = 1 );
+
+require_once ABSPATH . 'wp-admin/includes/class-wp-importer.php';
+require_once WC_ABSPATH . 'includes/admin/importers/class-wc-tax-rate-importer.php';
+
+/**
+ * Tests for the WC_Tax_Rate_Importer class.
+ */
+class WC_Tax_Rate_Importer_Test extends WC_Unit_Test_Case {
+
+	/**
+	 * The System Under Test.
+	 *
+	 * @var WC_Tax_Rate_Importer
+	 */
+	private $sut;
+
+	/**
+	 * Set up test fixtures.
+	 */
+	public function setUp(): void {
+		parent::setUp();
+		$this->sut = new WC_Tax_Rate_Importer();
+	}
+
+	/**
+	 * @testdox Should return UTF-8 data unchanged and convert ISO-8859-1 data to UTF-8.
+	 */
+	public function test_format_data_from_csv_converts_latin1_to_utf8(): void {
+		$this->assertSame( 'Café', $this->sut->format_data_from_csv( 'Café', 'UTF-8' ), 'UTF-8 data should not change.' );
+		$this->assertSame( 'Café', $this->sut->format_data_from_csv( "Caf\xE9", 'ISO-8859-1' ), 'ISO-8859-1 data should be converted.' );
+	}
+
+	/**
+	 * @testdox Should convert ISO-8859-1 bytes to UTF-8 when mbstring is not available.
+	 */
+	public function test_latin1_fallback_converts_bytes_to_utf8(): void {
+		$method = new ReflectionMethod( WC_Tax_Rate_Importer::class, 'latin1_to_utf8' );
+		$method->setAccessible( true );
+
+		$this->assertSame( "A\x7F\xC2\x80\xC2\xA0\xC3\xA9\xC3\xBF", $method->invoke( null, "A\x7F\x80\xA0\xE9\xFF" ) );
+	}
+}