Date/Time: When determining whether to decline the month name in `wp_maybe_decline_date()`, take word boundaries into account.

Add more unit tests.

Props Rarst, Clorith, timon33, Xendo, SergeyBiryukov.
Fixes #48606.

git-svn-id: https://develop.svn.wordpress.org/trunk@46862 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-12-09 18:48:50 +00:00
parent 2e175cad63
commit 50f01f47e3
2 changed files with 33 additions and 8 deletions

View File

@ -335,24 +335,29 @@ function wp_maybe_decline_date( $date ) {
$months = $wp_locale->month;
$months_genitive = $wp_locale->month_genitive;
// Match a format like 'j F Y' or 'j. F'
if ( preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
/*
* Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
* and decline the month.
*/
if ( preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date ) ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '# ' . $month . '( |$)#u';
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u';
}
foreach ( $months_genitive as $key => $month ) {
$months_genitive[ $key ] = ' ' . $month . '$1';
$months_genitive[ $key ] = ' ' . $month;
}
$date = preg_replace( $months, $months_genitive, $date );
}
// Match a format like 'F jS' or 'F j' and change it to 'j F'
if ( preg_match( '#^[^\d ]+ \d{1,2}(st|nd|rd|th)? #u', trim( $date ) ) ) {
/*
* Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
* and change it to declined 'j F'.
*/
if ( preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) ) ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '#' . $month . ' (\d{1,2})(st|nd|rd|th)?#u';
$months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u';
}
foreach ( $months_genitive as $key => $month ) {

View File

@ -37,6 +37,8 @@ class Tests_Functions_MaybeDeclineDate extends WP_UnitTestCase {
/**
* @ticket 36790
* @ticket 37411
* @ticket 48606
* @dataProvider data_wp_maybe_decline_date
*/
public function test_wp_maybe_decline_date( $test_locale, $input, $output ) {
@ -71,11 +73,15 @@ class Tests_Functions_MaybeDeclineDate extends WP_UnitTestCase {
array( 'ru_RU', '1 Январь 2016', '1 января 2016' ),
array( 'ru_RU', 'Январь 1st 2016', '1 января 2016' ),
array( 'ru_RU', 'Январь 1 2016', '1 января 2016' ),
array( 'ru_RU', 'Январь 1 16', '1 января 16' ),
array( 'ru_RU', 'Суббота, 19 Январь 2019 10:50', 'Суббота, 19 января 2019 10:50' ),
array( 'pl_PL', '1 Styczeń', '1 stycznia' ),
array( 'hr', '1. Siječanj', '1. siječnja' ),
array( 'ca', '1 de abril', "1 d'abril" ),
array( 'cs_CZ', '1. Červen', '1. června' ),
array( 'cs_CZ', '1. Červenec', '1. července' ),
array( 'it_IT', 'Lundeì 11 Novembre 2019', 'Lundeì 11 Novembre 2019' ),
array( 'el', 'Σάββατο, 19 Ιανουάριος 2019 10:50', 'Σάββατο, 19 Ιανουαρίου 2019 10:50' ),
);
}
@ -116,6 +122,20 @@ class Tests_Functions_MaybeDeclineDate extends WP_UnitTestCase {
);
break;
case 'it_IT':
$months = array(
'month' => array( 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre' ),
'month_genitive' => array( 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre' ),
);
break;
case 'el':
$months = array(
'month' => array( 'Ιανουάριος', 'Φεβρουάριος', 'Μάρτιος', 'Απρίλιος', 'Μάιος', 'Ιούνιος', 'Ιούλιος', 'Αύγουστος', 'Σεπτέμβριος', 'Οκτώβριος', 'Νοέμβριος', 'Δεκέμβριος' ),
'month_genitive' => array( 'Ιανουαρίου', 'Φεβρουαρίου', 'Μαρτίου', 'Απριλίου', 'Μαΐου', 'Ιουνίου', 'Ιουλίου', 'Αυγούστου', 'Σεπτεμβρίου', 'Οκτωβρίου', 'Νοεμβρίου', 'Δεκεμβρίου' ),
);
break;
default:
$months = array(
'month' => array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ),