Make WordPress Core

Changeset 47078


Ignore:
Timestamp:
01/17/2020 01:10:57 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r47060 r47078  
    287287
    288288        $date = $datetime->format( $new_format );
    289         $date = wp_maybe_decline_date( $date );
     289        $date = wp_maybe_decline_date( $date, $format );
    290290    }
    291291
     
    313313 *
    314314 * @since 4.4.0
     315 * @since 5.4.0 The `$format` parameter was added.
    315316 *
    316317 * @global WP_Locale $wp_locale WordPress date and time locale object.
    317318 *
    318  * @param string $date Formatted date string.
     319 * @param string $date   Formatted date string.
     320 * @param string $format Optional. Date format to check. Default empty string.
    319321 * @return string The date, declined if locale specifies it.
    320322 */
    321 function wp_maybe_decline_date( $date ) {
     323function wp_maybe_decline_date( $date, $format = '' ) {
    322324    global $wp_locale;
    323325
     
    340342         * and decline the month.
    341343         */
    342         if ( preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date ) ) {
     344        if ( $format ) {
     345            $decline = preg_match( '#[dj]\.? F#', $format );
     346        } else {
     347            // If the format is not passed, try to guess it from the date string.
     348            $decline = preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date );
     349        }
     350
     351        if ( $decline ) {
    343352            foreach ( $months as $key => $month ) {
    344353                $months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u';
     
    356365         * and change it to declined 'j F'.
    357366         */
    358         if ( preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) ) ) {
     367        if ( $format ) {
     368            $decline = preg_match( '#F [dj]#', $format );
     369        } else {
     370            // If the format is not passed, try to guess it from the date string.
     371            $decline = preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) );
     372        }
     373
     374        if ( $decline ) {
    359375            foreach ( $months as $key => $month ) {
    360376                $months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u';
Note: See TracChangeset for help on using the changeset viewer.