Date/Time: Pass the date format to `wp_maybe_decline_date()`.

This ensures that the function has enough context to determine the necessity of replacing the month name with the correct form in locales that require it.

Props SergeyBiryukov, Rarst.
Fixes #48934.

git-svn-id: https://develop.svn.wordpress.org/trunk@47078 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-01-17 01:10:57 +00:00
parent 93b2d5ecc0
commit 5bef137806
2 changed files with 21 additions and 5 deletions

View File

@ -286,7 +286,7 @@ function wp_date( $format, $timestamp = null, $timezone = null ) {
}
$date = $datetime->format( $new_format );
$date = wp_maybe_decline_date( $date );
$date = wp_maybe_decline_date( $date, $format );
}
/**
@ -312,13 +312,15 @@ function wp_date( $format, $timestamp = null, $timezone = null ) {
* formats (like 'j F Y'), the month name will be replaced with a correct form.
*
* @since 4.4.0
* @since 5.4.0 The `$format` parameter was added.
*
* @global WP_Locale $wp_locale WordPress date and time locale object.
*
* @param string $date Formatted date string.
* @param string $date Formatted date string.
* @param string $format Optional. Date format to check. Default empty string.
* @return string The date, declined if locale specifies it.
*/
function wp_maybe_decline_date( $date ) {
function wp_maybe_decline_date( $date, $format = '' ) {
global $wp_locale;
// i18n functions are not available in SHORTINIT mode
@ -339,7 +341,14 @@ function wp_maybe_decline_date( $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 ) ) {
if ( $format ) {
$decline = preg_match( '#[dj]\.? F#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u';
}
@ -355,7 +364,14 @@ function wp_maybe_decline_date( $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 ) ) ) {
if ( $format ) {
$decline = preg_match( '#F [dj]#', $format );
} else {
// If the format is not passed, try to guess it from the date string.
$decline = preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) );
}
if ( $decline ) {
foreach ( $months as $key => $month ) {
$months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u';
}