| 356 | |
| 357 | if ( ! 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 | */ |
| 370 | class 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 | } |
| 388 | endif; |