Make WordPress Core

Ticket #22225: 22225.3.patch

File 22225.3.patch, 6.2 KB (added by realloc, 9 years ago)

Patch and Unit tests with tests for filter

  • src/wp-includes/functions.php

     
    113113                $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
    114114                $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
    115115                $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
     116                $dateordinal_suffix = get_ordinal_suffix( $datefunc( 'j', $i ) );
    116117                $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
    117118                $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) );
    118119                $dateformatstring = ' '.$dateformatstring;
     
    120121                $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
    121122                $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
    122123                $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
     124                $dateformatstring = preg_replace( "/([^\\\])S/", "\\1" . backslashit( $dateordinal_suffix ), $dateformatstring );
    123125                $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
    124126                $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
    125127
  • src/wp-includes/l10n.php

     
    815815}
    816816
    817817/**
     818 * Get the ordinal suffix for a given number
     819 *
     820 * @param int|string $number
     821 * @return string
     822 */
     823function get_ordinal_suffix( $number ) {
     824        $suffix = _x( 'th', 'ordinal suffix' );
     825
     826        $value  = intval( $number );
     827        if ( ! in_array( $value, array( 11, 12, 13 ) ) ) {
     828                switch ( $value % 10 ) {
     829                        case 1:
     830                                $suffix = _x( 'st', 'ordinal suffix' );
     831                                break;
     832                        case 2:
     833                                $suffix = _x( 'nd', 'ordinal suffix' );
     834                                break;
     835                        case 3:
     836                                $suffix = _x( 'rd', 'ordinal suffix' );
     837                                break;
     838                }
     839        }
     840
     841        return apply_filters( 'ordinal_suffix', $suffix, $number );
     842}
     843
     844/**
    818845 * Translates role name.
    819846 *
    820847 * Since the role names are in the database and not in the source there
  • tests/phpunit/tests/functions.php

     
    717717                the_date( 'Y', 'before ', ' after', false );
    718718                $this->assertEquals( '', ob_get_clean() );
    719719        }
     720
     721        /**
     722         * Data provider for test_date_i18n_ordinal_suffix
     723         * @return array
     724         */
     725        public function date_i18n_ordinal_suffix_provider() {
     726                return array(
     727                        array( _x( 'st', 'ordinal suffix' ), 1448928000 ), // 2015-12-01
     728                        array( _x( 'nd', 'ordinal suffix' ), 1449014400 ), // 2015-12-02
     729                        array( _x( 'rd', 'ordinal suffix' ), 1449100800 ), // 2015-12-03
     730                        array( _x( 'th', 'ordinal suffix' ), 1449792000 ), // 2015-12-11
     731                        array( _x( 'th', 'ordinal suffix' ), 1449878400 ), // 2015-12-12
     732                        array( _x( 'th', 'ordinal suffix' ), 1449964800 ), // 2015-12-13
     733                        array( _x( 'st', 'ordinal suffix' ), 1450656000 ), // 2015-12-21
     734                        array( _x( 'nd', 'ordinal suffix' ), 1450742400 ), // 2015-12-22
     735                        array( _x( 'rd', 'ordinal suffix' ), 1450828800 ), // 2015-12-23
     736                        array( _x( 'th', 'ordinal suffix' ), 1450915200 ), // 2015-12-24
     737                        array( _x( 'st', 'ordinal suffix' ), 1451520000 ), // 2015-12-31
     738                );
     739        }
     740
     741        /**
     742         * @ticket 22225
     743         * @dataProvider date_i18n_ordinal_suffix_provider
     744         */
     745        function test_date_i18n_ordinal_suffix( $a, $b ) {
     746                $this->assertEquals( $a, date_i18n( 'S', $b ) );
     747        }
     748
     749        /**
     750         * @ticket 22225
     751         */
     752        function test_date_i18n_ordinal_suffix_filter() {
     753                add_filter( 'ordinal_suffix', array( $this, 'filter_i18n_ordinal_suffix_german' ), 10, 2 );
     754
     755                $this->assertEquals( 'te',  date_i18n( 'S', 1448928000 ) ); // 2015-12-01
     756                $this->assertEquals( 'te',  date_i18n( 'S', 1450483200 ) ); // 2015-12-19
     757                $this->assertEquals( 'ste', date_i18n( 'S', 1450569600 ) ); // 2015-12-20
     758                $this->assertEquals( 'ste', date_i18n( 'S', 1451520000 ) ); // 2015-12-31
     759
     760                remove_filter( 'ordinal_suffix', array( $this, 'filter_i18n_ordinal_suffix_german' ), 10 );
     761
     762                add_filter( 'ordinal_suffix', array( $this, 'filter_i18n_ordinal_suffix_russian' ) );
     763
     764                $this->assertEquals( '', date_i18n( 'S', 1448928000 ) ); // 2015-12-01
     765                $this->assertEquals( '', date_i18n( 'S', 1450569600 ) ); // 2015-12-20
     766
     767                remove_filter( 'ordinal_suffix', array( $this, 'filter_i18n_ordinal_suffix_russian' ), 10 );
     768        }
     769
     770        function filter_i18n_ordinal_suffix_german( $suffix, $number ) {
     771                if ( 20 > intval( $number ) ) {
     772                        return 'te';
     773                }
     774                return 'ste';
     775        }
     776
     777        function filter_i18n_ordinal_suffix_russian() {
     778                return '';
     779        }
     780
    720781}
  • tests/phpunit/tests/l10n.php

     
    2626                $this->assertEquals( 'first-before-bar|second-before-bar', before_last_bar( 'first-before-bar|second-before-bar|after-last-bar' ) );
    2727        }
    2828
     29        /**
     30         * Data provider for test_get_ordinal_suffix
     31         * @return array
     32         */
     33        public function ordinal_suffix_provider() {
     34                return array(
     35                        array( _x( 'th', 'ordinal suffix' ), 0 ),
     36                        array( _x( 'st', 'ordinal suffix' ), 1 ),
     37                        array( _x( 'nd', 'ordinal suffix' ), 2 ),
     38                        array( _x( 'rd', 'ordinal suffix' ), 3 ),
     39                        array( _x( 'th', 'ordinal suffix' ), '4' ),
     40                        array( _x( 'th', 'ordinal suffix' ), '05' ),
     41                        array( _x( 'th', 'ordinal suffix' ), 10 ),
     42                        array( _x( 'th', 'ordinal suffix' ), 11 ),
     43                        array( _x( 'th', 'ordinal suffix' ), 12 ),
     44                        array( _x( 'th', 'ordinal suffix' ), 13 ),
     45                        array( _x( 'th', 'ordinal suffix' ), 19 ),
     46                        array( _x( 'st', 'ordinal suffix' ), 21 ),
     47                        array( _x( 'nd', 'ordinal suffix' ), 22 ),
     48                        array( _x( 'rd', 'ordinal suffix' ), 23 ),
     49                        array( _x( 'th', 'ordinal suffix' ), 24 ),
     50                        array( _x( 'st', 'ordinal suffix' ), 31 ),
     51                );
     52        }
     53
     54        /**
     55         * @ticket 22225
     56         * @dataProvider ordinal_suffix_provider
     57         */
     58        function test_get_ordinal_suffix( $a, $b ) {
     59                $this->assertEquals( $a, get_ordinal_suffix( $b ) );
     60        }
     61
    2962}