From a370d1a9c0b52346a9cb8fe106f1ab4e1bf8bd74 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 17 Jan 2020 01:12:04 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 26 +++++++++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 888343b6d0..e8c6dc9dc3 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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'; } diff --git a/wp-includes/version.php b/wp-includes/version.php index c711a1d011..a777c9f397 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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.