Make WordPress Core


Ignore:
Timestamp:
12/09/2019 06:48:50 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: When determining whether to decline the month name in wp_maybe_decline_date(), take word boundaries into account.

Add more unit tests.

Props Rarst, Clorith, timon33, Xendo, SergeyBiryukov.
Fixes #48606.

File:
1 edited

Legend:

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

    r46822 r46862  
    336336        $months_genitive = $wp_locale->month_genitive;
    337337
    338         // Match a format like 'j F Y' or 'j. F'
    339         if ( preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) {
    340 
     338        /*
     339         * Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name)
     340         * and decline the month.
     341         */
     342        if ( preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date ) ) {
    341343            foreach ( $months as $key => $month ) {
    342                 $months[ $key ] = '# ' . $month . '( |$)#u';
     344                $months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u';
    343345            }
    344346
    345347            foreach ( $months_genitive as $key => $month ) {
    346                 $months_genitive[ $key ] = ' ' . $month . '$1';
     348                $months_genitive[ $key ] = ' ' . $month;
    347349            }
    348350
     
    350352        }
    351353
    352         // Match a format like 'F jS' or 'F j' and change it to 'j F'
    353         if ( preg_match( '#^[^\d ]+ \d{1,2}(st|nd|rd|th)? #u', trim( $date ) ) ) {
     354        /*
     355         * Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix)
     356         * and change it to declined 'j F'.
     357         */
     358        if ( preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) ) ) {
    354359            foreach ( $months as $key => $month ) {
    355                 $months[ $key ] = '#' . $month . ' (\d{1,2})(st|nd|rd|th)?#u';
     360                $months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?\b#u';
    356361            }
    357362
Note: See TracChangeset for help on using the changeset viewer.