From e5f1a70a8783183e8997be4d35f677fae7ed2d4c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 9 Dec 2019 19:02:04 +0000 Subject: [PATCH] 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. Merges [46862] to the 5.3 branch. Fixes #48606. Built from https://develop.svn.wordpress.org/branches/5.3@46863 git-svn-id: http://core.svn.wordpress.org/branches/5.3@46663 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 21 +++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 5fea050bdc..b80e85fe1f 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 12821ac9fd..5b853b154f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3.1-alpha-46861'; +$wp_version = '5.3.1-alpha-46863'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.