Make WordPress Core

Ticket #41562: 41562.patch

File 41562.patch, 1.9 KB (added by ayeshrajans, 8 years ago)
  • wp-includes/pomo/translations.php

    diff --git wp-includes/pomo/translations.php wp-includes/pomo/translations.php
    index 6623a9f02a..0537aad0e3 100644
    function nplurals_and_expression_from_header($header) { 
    201201         * @param string $expression
    202202         */
    203203        function make_plural_form_function($nplurals, $expression) {
    204                 $expression = str_replace('n', '$n', $expression);
    205                 $func_body = "
    206                         \$index = (int)($expression);
    207                         return (\$index < $nplurals)? \$index : $nplurals - 1;";
    208                 return create_function('$n', $func_body);
     204                $callback = new TranslationMultipleFormCallback($nplurals, $expression);
     205                return array( $callback, 'callback');
    209206        }
    210207
    211208        /**
    function merge_with(&$other) { 
    356353        }
    357354}
    358355endif;
     356
     357if ( ! class_exists( 'TranslationMultipleFormCallback', false ) ):
     358
     359/**
     360 * Provides a callable holding data, which will return the right translation
     361 * index, according to the plural forms header.
     362 * @see https://core.trac.wordpress.org/ticket/37082.
     363 * Since create_function() was deprecated in PHP 7.2, and to support PHP 5.2,
     364 * which does not has closure support, this class is used to set some data, and
     365 * pass a valid callback that can be called with call_user_func() function,
     366 * or with $callback($arguments) syntax since PHP 5.4 and later. Ideally, this
     367 * should be replaced with a full closure when WordPress requires PHP 5.3
     368 * minimum.
     369 */
     370class TranslationMultipleFormCallback {
     371        private $expression;
     372        private $nplurals;
     373
     374        public function __construct($nplurals, $expression) {
     375                $this->expression = str_replace('n', '$n', $expression);
     376                $this->nplurals = $nplurals;
     377        }
     378
     379        /**
     380         * @param $n
     381         * @return string
     382         */
     383        public function callback($n){
     384                $index = intval(@eval('return (' . $this->expression . ');'));
     385                return $index < $this->nplurals ? $index : $this->nplurals - 1;
     386        }
     387}
     388endif;