WordPress.org

Make WordPress Core

Ticket #44531: mb-support-number_format_i18n-old-php.diff

File mb-support-number_format_i18n-old-php.diff, 3.5 KB (added by remcotolsma, 3 years ago)
  • src/wp-includes/class-wp-locale.php

    diff --git src/wp-includes/class-wp-locale.php src/wp-includes/class-wp-locale.php
    index 287914e880..10e4201ad3 100644
    class WP_Locale { 
    198198                /* translators: $thousands_sep argument for https://secure.php.net/number_format, default is , */
    199199                $thousands_sep = __( 'number_format_thousands_sep' );
    200200
    201                 if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
    202                         // Replace space with a non-breaking space to avoid wrapping.
    203                         $thousands_sep = str_replace( ' ', ' ', $thousands_sep );
    204                 } else {
    205                         // PHP < 5.4.0 does not support multiple bytes in thousands separator.
    206                         $thousands_sep = str_replace( array( '&nbsp;', '&#160;' ), ' ', $thousands_sep );
    207                 }
    208 
    209201                $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
    210202
    211203                /* translators: $dec_point argument for https://secure.php.net/number_format, default is . */
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 8ae58299d6..ad911ab930 100644
    function number_format_i18n( $number, $decimals = 0 ) { 
    262262        global $wp_locale;
    263263
    264264        if ( isset( $wp_locale ) ) {
    265                 $formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
     265                $decimal_point = $wp_locale->number_format['decimal_point'];
     266                $thousands_sep = $wp_locale->number_format['thousands_sep'];
     267
     268                if ( version_compare( PHP_VERSION, '5.4', '<' ) && ( 1 !== strlen( $decimal_point ) || 1 !== strlen( $thousands_sep ) ) ) {
     269                        $formatted = strtr( number_format( $number, absint( $decimals ), '.', ',' ), array(
     270                                '.' => $decimal_point,
     271                                ',' => $thousands_sep,
     272                        ) );
     273                } else {
     274                        $formatted = number_format( $number, absint( $decimals ), $decimal_point, $thousands_sep );
     275                }
    266276        } else {
    267277                $formatted = number_format( $number, absint( $decimals ) );
    268278        }
  • tests/phpunit/tests/functions/numberFormatI18n.php

    diff --git tests/phpunit/tests/functions/numberFormatI18n.php tests/phpunit/tests/functions/numberFormatI18n.php
    index 8c514b319c..612a5d70b5 100644
    class Tests_Functions_Number_Format_I18n extends WP_UnitTestCase { 
    4646                $this->assertEquals( '123,457', number_format_i18n( 123456.789, 0 ) );
    4747                $this->assertEquals( '123,456.7890', number_format_i18n( 123456.789, -4 ) );
    4848        }
     49
     50        /**
     51         * Test multiybte format of locale.
     52         *
     53         * Multibyte decimal point and thousands separator in `number_format` are suported since PHP 5.4.
     54         * @link http://php.net/number_format
     55         *
     56         * Therefor this test will fail on PHP < 5.4 with incorrect implemented `number_format_i18n` function.
     57         */
     58        public function test_multibyte_format_of_locale() {
     59                $decimal_point = $GLOBALS['wp_locale']->number_format['decimal_point'];
     60                $thousands_sep = $GLOBALS['wp_locale']->number_format['thousands_sep'];
     61
     62                $GLOBALS['wp_locale']->number_format['decimal_point'] = 'decimal_point';
     63                $GLOBALS['wp_locale']->number_format['thousands_sep'] = 'thousands_sep';
     64
     65                $actual_1 = number_format_i18n( 123456.789, 0 );
     66                $actual_2 = number_format_i18n( 123456.789, 4 );
     67
     68                $GLOBALS['wp_locale']->number_format['decimal_point'] = $decimal_point;
     69                $GLOBALS['wp_locale']->number_format['thousands_sep'] = $thousands_sep;
     70
     71                $this->assertEquals( '123thousands_sep457', $actual_1 );
     72                $this->assertEquals( '123thousands_sep456decimal_point7890', $actual_2 );
     73        }
    4974}