From 953e1487643db865285a3ae99f3e9224e9206ab4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 20:59:14 +0000 Subject: [PATCH] I18N: Fix plural forms parsing in `WP_Translation_File`. Ensures the plural expression from the translation file header is correctly parsed. Prevents silent failures in the attempt to create the plural form function. Adds additional tests. Props Chouby. See #59656. Built from https://develop.svn.wordpress.org/trunk@57518 git-svn-id: http://core.svn.wordpress.org/trunk@57019 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../l10n/class-wp-translation-file.php | 25 ++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/wp-includes/l10n/class-wp-translation-file.php b/wp-includes/l10n/class-wp-translation-file.php index 93842bec10..e6dbdb85be 100644 --- a/wp-includes/l10n/class-wp-translation-file.php +++ b/wp-includes/l10n/class-wp-translation-file.php @@ -207,7 +207,7 @@ abstract class WP_Translation_File { } /** - * Returns the plural form for a count. + * Returns the plural form for a given number. * * @since 6.5.0 * @@ -219,9 +219,9 @@ abstract class WP_Translation_File { $this->parse_file(); } - // In case a plural form is specified as a header, but no function included, build one. if ( null === $this->plural_forms && isset( $this->headers['plural-forms'] ) ) { - $this->plural_forms = $this->make_plural_form_function( $this->headers['plural-forms'] ); + $expression = $this->get_plural_expression_from_header( $this->headers['plural-forms'] ); + $this->plural_forms = $this->make_plural_form_function( $expression ); } if ( is_callable( $this->plural_forms ) ) { @@ -231,6 +231,7 @@ abstract class WP_Translation_File { * @var int $result Plural form. */ $result = call_user_func( $this->plural_forms, $number ); + return $result; } @@ -238,6 +239,22 @@ abstract class WP_Translation_File { return ( 1 === $number ? 0 : 1 ); } + /** + * Returns the plural forms expression as a tuple. + * + * @since 6.5.0 + * + * @param string $header Plural-Forms header string. + * @return string Plural forms expression. + */ + protected function get_plural_expression_from_header( $header ) { + if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) { + return trim( $matches[2] ); + } + + return 'n != 1'; + } + /** * Makes a function, which will return the right translation index, according to the * plural forms header. @@ -247,7 +264,7 @@ abstract class WP_Translation_File { * @param string $expression Plural form expression. * @return callable(int $num): int Plural forms function. */ - public function make_plural_form_function( string $expression ): callable { + protected function make_plural_form_function( string $expression ): callable { try { $handler = new Plural_Forms( rtrim( $expression, ';' ) ); return array( $handler, 'get' ); diff --git a/wp-includes/version.php b/wp-includes/version.php index b3a409ecf6..e78f96755c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57517'; +$wp_version = '6.5-alpha-57518'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.