From e283457e45958736cbad8d05ff529849052b10bb Mon Sep 17 00:00:00 2001
From: Sergey Biryukov <sergeybiryukov@git.wordpress.org>
Date: Thu, 20 Jun 2019 13:46:20 +0000
Subject: [PATCH] Date/Time: Add more supported formats to
 `wp_maybe_decline_date()`.

Props SergeyBiryukov, Rarst.
Fixes #37411.

git-svn-id: https://develop.svn.wordpress.org/trunk@45555 602fd350-edb4-49c9-b593-d223f7449a82
---
 src/wp-includes/functions.php                 | 19 +++++++++++++++++--
 .../tests/functions/maybeDeclineDate.php      |  3 +++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
index 5dfbf58697..ba7ad34036 100644
--- a/src/wp-includes/functions.php
+++ b/src/wp-includes/functions.php
@@ -219,10 +219,12 @@ function wp_maybe_decline_date( $date ) {
 	 * translate this to 'on'. Do not translate into your own language.
 	 */
 	if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) {
+
+		$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 ) ) {
-			$months          = $wp_locale->month;
-			$months_genitive = $wp_locale->month_genitive;
 
 			foreach ( $months as $key => $month ) {
 				$months[ $key ] = '# ' . $month . '( |$)#u';
@@ -234,6 +236,19 @@ function wp_maybe_decline_date( $date ) {
 
 			$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 ) ) ) {
+			foreach ( $months as $key => $month ) {
+				$months[ $key ] = '#' . $month . ' (\d{1,2})(st|nd|rd|th)?#u';
+			}
+
+			foreach ( $months_genitive as $key => $month ) {
+				$months_genitive[ $key ] = '$1 ' . $month;
+			}
+
+			$date = preg_replace( $months, $months_genitive, $date );
+		}
 	}
 
 	// Used for locale-specific rules
diff --git a/tests/phpunit/tests/functions/maybeDeclineDate.php b/tests/phpunit/tests/functions/maybeDeclineDate.php
index a3ec61a44a..d888a2d53c 100644
--- a/tests/phpunit/tests/functions/maybeDeclineDate.php
+++ b/tests/phpunit/tests/functions/maybeDeclineDate.php
@@ -3,6 +3,7 @@
 /**
  * @group functions.php
  * @group i18n
+ * @group datetime
  */
 class Tests_Functions_MaybeDeclineDate extends WP_UnitTestCase {
 
@@ -68,6 +69,8 @@ class Tests_Functions_MaybeDeclineDate extends WP_UnitTestCase {
 		return array(
 			array( 'ru_RU', '21 Июнь', '21 июня' ),
 			array( 'ru_RU', '1 Январь 2016', '1 января 2016' ),
+			array( 'ru_RU', 'Январь 1st 2016', '1 января 2016' ),
+			array( 'ru_RU', 'Январь 1 2016', '1 января 2016' ),
 			array( 'pl_PL', '1 Styczeń', '1 stycznia' ),
 			array( 'hr', '1. Siječanj', '1. siječnja' ),
 			array( 'ca', '1 de abril', "1 d'abril" ),