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.
Built from https://develop.svn.wordpress.org/trunk@47078


git-svn-id: http://core.svn.wordpress.org/trunk@46878 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-01-17 01:12:04 +00:00
parent e884aca67b
commit a370d1a9c0
2 changed files with 22 additions and 6 deletions

View File

@ -286,7 +286,7 @@ function wp_date( $format, $timestamp = null, $timezone = null ) {
} }
$date = $datetime->format( $new_format ); $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. * formats (like 'j F Y'), the month name will be replaced with a correct form.
* *
* @since 4.4.0 * @since 4.4.0
* @since 5.4.0 The `$format` parameter was added.
* *
* @global WP_Locale $wp_locale WordPress date and time locale object. * @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. * @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; global $wp_locale;
// i18n functions are not available in SHORTINIT mode // 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) * Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
* and decline the month. * 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 ) { foreach ( $months as $key => $month ) {
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u'; $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) * 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'. * 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 ) { foreach ( $months as $key => $month ) {
$months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u'; $months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u';
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.4-alpha-47077'; $wp_version = '5.4-alpha-47078';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.