From ff7226364325ea09037636f032c2c9a9f15052fb Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 23 Mar 2026 14:55:58 +0000 Subject: [PATCH] Code Modernization: Replace the deprecated `auto_detect_line_endings` setting. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since PHP 8.1, the `auto_detect_line_endings` setting is deprecated: > The `auto_detect_line_endings` ini setting modifies the behavior of `file()` and `fgets()` to support an isolated `\r` (as opposed to `\n` or `\r\n`) as a newline character. These newlines were used by “Classic” Mac OS, a system which has been discontinued in 2001, nearly two decades ago. Interoperability with such systems is no longer relevant. Reference: [https://wiki.php.net/rfc/deprecations_php_8_1#auto_detect_line_endings_ini_setting PHP RFC: Deprecations for PHP 8.1: auto_detect_line_endings ini setting]. > The `auto_detect_line_endings` ini setting has been deprecated. If necessary, handle `\r` line breaks manually instead. Reference: [https://github.com/php/php-src/blob/1cf4fb739f7a4fa8404a4c0958f13d04eae519d4/UPGRADING#L456-L457 PHP 8.1 Upgrade Notes]. This commits adds code to replace the deprecated setting and still handle old-style `\r` line-terminated files in `PO::read_line()` using `strpos()` + `fseek()`. Includes a unit test covering `\r`, `\n`, and `\r\n` line endings. Follow-up to [51633], [51636]. Props akirk, apermo, westonruter, SergeyBiryukov. Fixes #64928. Built from https://develop.svn.wordpress.org/trunk@62093 git-svn-id: http://core.svn.wordpress.org/trunk@61375 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/pomo/po.php | 37 +++++++++++++++++++++++++------------ wp-includes/version.php | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/wp-includes/pomo/po.php b/wp-includes/pomo/po.php index de1bc92064..c43544e142 100644 --- a/wp-includes/pomo/po.php +++ b/wp-includes/pomo/po.php @@ -13,16 +13,6 @@ if ( ! defined( 'PO_MAX_LINE_LEN' ) ) { define( 'PO_MAX_LINE_LEN', 79 ); } -/* - * The `auto_detect_line_endings` setting has been deprecated in PHP 8.1, - * but will continue to work until PHP 9.0. - * For now, we're silencing the deprecation notice as there may still be - * translation files around which haven't been updated in a long time and - * which still use the old MacOS standalone `\r` as a line ending. - * This fix should be revisited when PHP 9.0 is in alpha/beta. - */ -@ini_set( 'auto_detect_line_endings', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - /** * Routines for working with PO files */ @@ -475,8 +465,31 @@ if ( ! class_exists( 'PO', false ) ) : $use_last_line = true; return true; } - $line = $use_last_line ? $last_line : fgets( $f ); - $line = ( "\r\n" === substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line; + + if ( $use_last_line ) { + $line = $last_line; + } else { + $line = fgets( $f ); + if ( false === $line ) { + return $line; + } + + // Handle \r-only terminated lines after the deprecation of auto_detect_line_endings in PHP 8.1. + $r = strpos( $line, "\r" ); + if ( false !== $r ) { + if ( strlen( $line ) === $r + 1 + && "\r\n" === substr( $line, $r ) + ) { + $line = rtrim( $line, "\r\n" ) . "\n"; + } else { + // The lines are terminated by just \r, so we end the line there and rewind. + $rewind = strlen( $line ) - $r - 1; + $line = substr( $line, 0, $r ) . "\n"; + fseek( $f, - $rewind, SEEK_CUR ); + } + } + } + $last_line = $line; $use_last_line = false; return $line; diff --git a/wp-includes/version.php b/wp-includes/version.php index 0374aa68b6..9756b560ce 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '7.0-beta6-62092'; +$wp_version = '7.0-beta6-62093'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.