: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file is part of Twig.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
namespace WPML\Core\Twig\Extension;
use WPML\Core\Twig\ExpressionParser;
use WPML\Core\Twig\TokenParser\ApplyTokenParser;
use WPML\Core\Twig\TokenParser\BlockTokenParser;
use WPML\Core\Twig\TokenParser\DeprecatedTokenParser;
use WPML\Core\Twig\TokenParser\DoTokenParser;
use WPML\Core\Twig\TokenParser\EmbedTokenParser;
use WPML\Core\Twig\TokenParser\ExtendsTokenParser;
use WPML\Core\Twig\TokenParser\FilterTokenParser;
use WPML\Core\Twig\TokenParser\FlushTokenParser;
use WPML\Core\Twig\TokenParser\ForTokenParser;
use WPML\Core\Twig\TokenParser\FromTokenParser;
use WPML\Core\Twig\TokenParser\IfTokenParser;
use WPML\Core\Twig\TokenParser\ImportTokenParser;
use WPML\Core\Twig\TokenParser\IncludeTokenParser;
use WPML\Core\Twig\TokenParser\MacroTokenParser;
use WPML\Core\Twig\TokenParser\SetTokenParser;
use WPML\Core\Twig\TokenParser\SpacelessTokenParser;
use WPML\Core\Twig\TokenParser\UseTokenParser;
use WPML\Core\Twig\TokenParser\WithTokenParser;
use WPML\Core\Twig\TwigFilter;
use WPML\Core\Twig\TwigFunction;
use WPML\Core\Twig\TwigTest;
class CoreExtension extends \WPML\Core\Twig\Extension\AbstractExtension
protected $dateFormats = ['F j, Y H:i', '%d days'];
protected $numberFormat = [0, '.', ','];
protected $timezone = null;
protected $escapers = [];
* Defines a new escaper to be used via the escape filter.
* @param string $strategy The strategy name that should be used as a strategy in the escape call
* @param callable $callable A valid PHP callable
public function setEscaper($strategy, $callable)
$this->escapers[$strategy] = $callable;
* Gets all defined escapers.
* @return array An array of escapers
public function getEscapers()
* Sets the default format to be used by the date filter.
* @param string $format The default date format string
* @param string $dateIntervalFormat The default date interval format string
public function setDateFormat($format = null, $dateIntervalFormat = null)
$this->dateFormats[0] = $format;
if (null !== $dateIntervalFormat) {
$this->dateFormats[1] = $dateIntervalFormat;
* Gets the default format to be used by the date filter.
* @return array The default date format string and the default date interval format string
public function getDateFormat()
return $this->dateFormats;
* Sets the default timezone to be used by the date filter.
* @param \DateTimeZone|string $timezone The default timezone string or a \DateTimeZone object
public function setTimezone($timezone)
$this->timezone = $timezone instanceof \DateTimeZone ? $timezone : new \DateTimeZone($timezone);
* Gets the default timezone to be used by the date filter.
* @return \DateTimeZone The default timezone currently in use
public function getTimezone()
if (null === $this->timezone) {
$this->timezone = new \DateTimeZone(\date_default_timezone_get());
* Sets the default format to be used by the number_format filter.
* @param int $decimal the number of decimal places to use
* @param string $decimalPoint the character(s) to use for the decimal point
* @param string $thousandSep the character(s) to use for the thousands separator
public function setNumberFormat($decimal, $decimalPoint, $thousandSep)
$this->numberFormat = [$decimal, $decimalPoint, $thousandSep];
* Get the default format used by the number_format filter.
* @return array The arguments for number_format()
public function getNumberFormat()
return $this->numberFormat;
public function getTokenParsers()
return [new \WPML\Core\Twig\TokenParser\ApplyTokenParser(), new \WPML\Core\Twig\TokenParser\ForTokenParser(), new \WPML\Core\Twig\TokenParser\IfTokenParser(), new \WPML\Core\Twig\TokenParser\ExtendsTokenParser(), new \WPML\Core\Twig\TokenParser\IncludeTokenParser(), new \WPML\Core\Twig\TokenParser\BlockTokenParser(), new \WPML\Core\Twig\TokenParser\UseTokenParser(), new \WPML\Core\Twig\TokenParser\FilterTokenParser(), new \WPML\Core\Twig\TokenParser\MacroTokenParser(), new \WPML\Core\Twig\TokenParser\ImportTokenParser(), new \WPML\Core\Twig\TokenParser\FromTokenParser(), new \WPML\Core\Twig\TokenParser\SetTokenParser(), new \WPML\Core\Twig\TokenParser\SpacelessTokenParser(), new \WPML\Core\Twig\TokenParser\FlushTokenParser(), new \WPML\Core\Twig\TokenParser\DoTokenParser(), new \WPML\Core\Twig\TokenParser\EmbedTokenParser(), new \WPML\Core\Twig\TokenParser\WithTokenParser(), new \WPML\Core\Twig\TokenParser\DeprecatedTokenParser()];
public function getFilters()
new \WPML\Core\Twig\TwigFilter('date', '\\WPML\\Core\\twig_date_format_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('date_modify', '\\WPML\\Core\\twig_date_modify_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('format', 'sprintf'),
new \WPML\Core\Twig\TwigFilter('replace', '\\WPML\\Core\\twig_replace_filter'),
new \WPML\Core\Twig\TwigFilter('number_format', '\\WPML\\Core\\twig_number_format_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('abs', 'abs'),
new \WPML\Core\Twig\TwigFilter('round', '\\WPML\\Core\\twig_round'),
new \WPML\Core\Twig\TwigFilter('url_encode', '\\WPML\\Core\\twig_urlencode_filter'),
new \WPML\Core\Twig\TwigFilter('json_encode', '\\WPML\\Core\\twig_jsonencode_filter'),
new \WPML\Core\Twig\TwigFilter('convert_encoding', '\\WPML\\Core\\twig_convert_encoding'),
new \WPML\Core\Twig\TwigFilter('title', '\\WPML\\Core\\twig_title_string_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('capitalize', '\\WPML\\Core\\twig_capitalize_string_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('upper', 'strtoupper'),
new \WPML\Core\Twig\TwigFilter('lower', 'strtolower'),
new \WPML\Core\Twig\TwigFilter('striptags', 'strip_tags'),
new \WPML\Core\Twig\TwigFilter('trim', '\\WPML\\Core\\twig_trim_filter'),
new \WPML\Core\Twig\TwigFilter('nl2br', 'nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]),
new \WPML\Core\Twig\TwigFilter('spaceless', '\\WPML\\Core\\twig_spaceless', ['is_safe' => ['html']]),
new \WPML\Core\Twig\TwigFilter('join', '\\WPML\\Core\\twig_join_filter'),
new \WPML\Core\Twig\TwigFilter('split', '\\WPML\\Core\\twig_split_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('sort', '\\WPML\\Core\\twig_sort_filter'),
new \WPML\Core\Twig\TwigFilter('merge', '\\WPML\\Core\\twig_array_merge'),
new \WPML\Core\Twig\TwigFilter('batch', '\\WPML\\Core\\twig_array_batch'),
new \WPML\Core\Twig\TwigFilter('filter', '\\WPML\\Core\\twig_array_filter'),
new \WPML\Core\Twig\TwigFilter('map', '\\WPML\\Core\\twig_array_map'),
new \WPML\Core\Twig\TwigFilter('reduce', '\\WPML\\Core\\twig_array_reduce'),
new \WPML\Core\Twig\TwigFilter('reverse', '\\WPML\\Core\\twig_reverse_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('length', '\\WPML\\Core\\twig_length_filter', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('slice', '\\WPML\\Core\\twig_slice', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('first', '\\WPML\\Core\\twig_first', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('last', '\\WPML\\Core\\twig_last', ['needs_environment' => \true]),
new \WPML\Core\Twig\TwigFilter('default', '\\WPML\\Core\\_twig_default_filter', ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Filter\\DefaultFilter']),
new \WPML\Core\Twig\TwigFilter('keys', '\\WPML\\Core\\twig_get_array_keys_filter'),
new \WPML\Core\Twig\TwigFilter('escape', '\\WPML\\Core\\twig_escape_filter', ['needs_environment' => \true, 'is_safe_callback' => '\\WPML\\Core\\twig_escape_filter_is_safe']),
new \WPML\Core\Twig\TwigFilter('e', '\\WPML\\Core\\twig_escape_filter', ['needs_environment' => \true, 'is_safe_callback' => '\\WPML\\Core\\twig_escape_filter_is_safe']),
if (\function_exists('mb_get_info')) {
$filters[] = new \WPML\Core\Twig\TwigFilter('upper', '\\WPML\\Core\\twig_upper_filter', ['needs_environment' => \true]);
$filters[] = new \WPML\Core\Twig\TwigFilter('lower', '\\WPML\\Core\\twig_lower_filter', ['needs_environment' => \true]);
public function getFunctions()
return [new \WPML\Core\Twig\TwigFunction('max', 'max'), new \WPML\Core\Twig\TwigFunction('min', 'min'), new \WPML\Core\Twig\TwigFunction('range', 'range'), new \WPML\Core\Twig\TwigFunction('constant', 'twig_constant'), new \WPML\Core\Twig\TwigFunction('cycle', 'twig_cycle'), new \WPML\Core\Twig\TwigFunction('random', 'twig_random', ['needs_environment' => \true]), new \WPML\Core\Twig\TwigFunction('date', 'twig_date_converter', ['needs_environment' => \true]), new \WPML\Core\Twig\TwigFunction('include', 'twig_include', ['needs_environment' => \true, 'needs_context' => \true, 'is_safe' => ['all']]), new \WPML\Core\Twig\TwigFunction('source', 'twig_source', ['needs_environment' => \true, 'is_safe' => ['all']])];
public function getTests()
return [new \WPML\Core\Twig\TwigTest('even', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\EvenTest']), new \WPML\Core\Twig\TwigTest('odd', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\OddTest']), new \WPML\Core\Twig\TwigTest('defined', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\DefinedTest']), new \WPML\Core\Twig\TwigTest('sameas', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\SameasTest', 'deprecated' => '1.21', 'alternative' => 'same as']), new \WPML\Core\Twig\TwigTest('same as', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\SameasTest']), new \WPML\Core\Twig\TwigTest('none', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\NullTest']), new \WPML\Core\Twig\TwigTest('null', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\NullTest']), new \WPML\Core\Twig\TwigTest('divisibleby', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\DivisiblebyTest', 'deprecated' => '1.21', 'alternative' => 'divisible by']), new \WPML\Core\Twig\TwigTest('divisible by', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\DivisiblebyTest']), new \WPML\Core\Twig\TwigTest('constant', null, ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Test\\ConstantTest']), new \WPML\Core\Twig\TwigTest('empty', 'twig_test_empty'), new \WPML\Core\Twig\TwigTest('iterable', 'twig_test_iterable')];
public function getOperators()
return [['not' => ['precedence' => 50, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Unary\\NotUnary'], '-' => ['precedence' => 500, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Unary\\NegUnary'], '+' => ['precedence' => 500, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Unary\\PosUnary']], ['or' => ['precedence' => 10, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\OrBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'and' => ['precedence' => 15, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\AndBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'b-or' => ['precedence' => 16, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\BitwiseOrBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'b-xor' => ['precedence' => 17, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\BitwiseXorBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'b-and' => ['precedence' => 18, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\BitwiseAndBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '==' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\EqualBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '!=' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\NotEqualBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '<' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\LessBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '>' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\GreaterBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '>=' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\GreaterEqualBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '<=' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\LessEqualBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'not in' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\NotInBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'in' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\InBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'matches' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\MatchesBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'starts with' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\StartsWithBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'ends with' => ['precedence' => 20, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\EndsWithBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '..' => ['precedence' => 25, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\RangeBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '+' => ['precedence' => 30, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\AddBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '-' => ['precedence' => 30, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\SubBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '~' => ['precedence' => 40, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\ConcatBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '*' => ['precedence' => 60, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\MulBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '/' => ['precedence' => 60, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\DivBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '//' => ['precedence' => 60, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\FloorDivBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '%' => ['precedence' => 60, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\ModBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'is' => ['precedence' => 100, 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], 'is not' => ['precedence' => 100, 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_LEFT], '**' => ['precedence' => 200, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\Binary\\PowerBinary', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_RIGHT], '??' => ['precedence' => 300, 'class' => 'WPML\\Core\\Twig\\Node\\Expression\\NullCoalesceExpression', 'associativity' => \WPML\Core\Twig\ExpressionParser::OPERATOR_RIGHT]]];
public function getName()
\class_alias('WPML\\Core\\Twig\\Extension\\CoreExtension', 'WPML\\Core\\Twig_Extension_Core');
use WPML\Core\Twig\Environment;
use WPML\Core\Twig\Error\LoaderError;
use WPML\Core\Twig\Error\RuntimeError;
use WPML\Core\Twig\Loader\SourceContextLoaderInterface;
use WPML\Core\Twig\Markup;
use WPML\Core\Twig\Node\Expression\ConstantExpression;
use WPML\Core\Twig\Node\Node;
* @param \ArrayAccess|array $values
* @param int $position The cycle position
* @return string The next value in the cycle
function twig_cycle($values, $position)
if (!\is_array($values) && !$values instanceof \ArrayAccess) {
return $values[$position % \count($values)];
* Returns a random value depending on the supplied parameter type:
* - a random item from a \Traversable or array
* - a random character from a string
* - a random integer between 0 and the integer parameter.
* @param \Traversable|array|int|float|string $values The values to pick a random item from
* @param int|null $max Maximum value used when $values is an int
* @throws RuntimeError when $values is an empty array (does not apply to an empty string which is returned as is)
* @return mixed A random value from the given sequence
function twig_random(\WPML\Core\Twig\Environment $env, $values = null, $max = null)
return null === $max ? \mt_rand() : \mt_rand(0, $max);
if (\is_int($values) || \is_float($values)) {
return \mt_rand($min, $max);
if (\is_string($values)) {
if (null !== ($charset = $env->getCharset())) {
if ('UTF-8' !== $charset) {
$values = \WPML\Core\twig_convert_encoding($values, 'UTF-8', $charset);
// unicode version of str_split()
// split at all positions, but not after the start and not before the end
$values = \preg_split('/(?<!^)(?!$)/u', $values);
if ('UTF-8' !== $charset) {
foreach ($values as $i => $value) {
$values[$i] = \WPML\Core\twig_convert_encoding($value, $charset, 'UTF-8');
return $values[\mt_rand(0, \strlen($values) - 1)];
if (!\WPML\Core\twig_test_iterable($values)) {
$values = \WPML\Core\twig_to_array($values);
if (0 === \count($values)) {
throw new \WPML\Core\Twig\Error\RuntimeError('The random function cannot pick from an empty array.');
return $values[\array_rand($values, 1)];
* Converts a date to the given format.
* {{ post.published_at|date("m/d/Y") }}
* @param \DateTime|\DateTimeInterface|\DateInterval|string $date A date
* @param string|null $format The target format, null to use the default
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
* @return string The formatted date
function twig_date_format_filter(\WPML\Core\Twig\Environment $env, $date, $format = null, $timezone = null)
$formats = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getDateFormat();
$format = $date instanceof \DateInterval ? $formats[1] : $formats[0];
if ($date instanceof \DateInterval) {
return $date->format($format);
return \WPML\Core\twig_date_converter($env, $date, $timezone)->format($format);
* Returns a new date object modified.
* {{ post.published_at|date_modify("-1day")|date("m/d/Y") }}
* @param \DateTime|string $date A date
* @param string $modifier A modifier string
function twig_date_modify_filter(\WPML\Core\Twig\Environment $env, $date, $modifier)
$date = \WPML\Core\twig_date_converter($env, $date, \false);
$resultDate = $date->modify($modifier);
// This is a hack to ensure PHP 5.2 support and support for \DateTimeImmutable
// \DateTime::modify does not return the modified \DateTime object < 5.3.0
// and \DateTimeImmutable does not modify $date.
return null === $resultDate ? $date : $resultDate;
* Converts an input to a \DateTime instance.
* {% if date(user.created_at) < date('+2days') %}
* @param \DateTime|\DateTimeInterface|string|null $date A date
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
function twig_date_converter(\WPML\Core\Twig\Environment $env, $date = null, $timezone = null)
// determine the timezone
if (\false !== $timezone) {
if (null === $timezone) {
$timezone = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone();
} elseif (!$timezone instanceof \DateTimeZone) {
$timezone = new \DateTimeZone($timezone);
if ($date instanceof \DateTimeImmutable) {
return \false !== $timezone ? $date->setTimezone($timezone) : $date;
if ($date instanceof \DateTime || $date instanceof \DateTimeInterface) {
if (\false !== $timezone) {
$date->setTimezone($timezone);
if (null === $date || 'now' === $date) {
return new \DateTime($date, \false !== $timezone ? $timezone : $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone());
$asString = (string) $date;
if (\ctype_digit($asString) || !empty($asString) && '-' === $asString[0] && \ctype_digit(\substr($asString, 1))) {
$date = new \DateTime('@' . $date);
$date = new \DateTime($date, $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone());
if (\false !== $timezone) {
$date->setTimezone($timezone);
* Replaces strings within a string.
* @param string $str String to replace in
* @param array|\Traversable $from Replace values
* @param string|null $to Replace to, deprecated (@see https://secure.php.net/manual/en/function.strtr.php)
function twig_replace_filter($str, $from, $to = null)
if (\is_string($from) && \is_string($to)) {
@\trigger_error('Using "replace" with character by character replacement is deprecated since version 1.22 and will be removed in Twig 2.0', \E_USER_DEPRECATED);
return \strtr($str, $from, $to);
if (!\WPML\Core\twig_test_iterable($from)) {
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".', \is_object($from) ? \get_class($from) : \gettype($from)));
return \strtr($str, \WPML\Core\twig_to_array($from));
* @param int|float $value The value to round
* @param int|float $precision The rounding precision
* @param string $method The method to use for rounding
* @return int|float The rounded number
function twig_round($value, $precision = 0, $method = 'common')
if ('common' == $method) {
return \round($value, $precision);
if ('ceil' != $method && 'floor' != $method) {
throw new \WPML\Core\Twig\Error\RuntimeError('The round filter only supports the "common", "ceil", and "floor" methods.');
return $method($value * \pow(10, $precision)) / \pow(10, $precision);
* All of the formatting options can be left null, in that case the defaults will
* be used. Supplying any of the parameters will override the defaults set in the
* @param mixed $number A float/int/string of the number to format
* @param int $decimal the number of decimal points to display
* @param string $decimalPoint the character(s) to use for the decimal point
* @param string $thousandSep the character(s) to use for the thousands separator
* @return string The formatted number
function twig_number_format_filter(\WPML\Core\Twig\Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null)
$defaults = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getNumberFormat();
if (null === $decimalPoint) {
$decimalPoint = $defaults[1];
if (null === $thousandSep) {
$thousandSep = $defaults[2];
return \number_format((float) $number, $decimal, $decimalPoint, $thousandSep);
* URL encodes (RFC 3986) a string as a path segment or an array as a query string.
* @param string|array $url A URL or an array of query parameters
* @return string The URL encoded value
function twig_urlencode_filter($url)
if (\defined('PHP_QUERY_RFC3986')) {
return \http_build_query($url, '', '&', \PHP_QUERY_RFC3986);
return \http_build_query($url, '', '&');
return \rawurlencode($url);
* JSON encodes a variable.
* @param mixed $value the value to encode
* @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
* @return mixed The JSON encoded value
function twig_jsonencode_filter($value, $options = 0)
if ($value instanceof \WPML\Core\Twig\Markup) {
$value = (string) $value;
} elseif (\is_array($value)) {
\array_walk_recursive($value, '\\WPML\\Core\\_twig_markup2string');
return \json_encode($value, $options);
function _twig_markup2string(&$value)
if ($value instanceof \WPML\Core\Twig\Markup) {
$value = (string) $value;
* Merges an array with another one.
* {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
* {% set items = items|merge({ 'peugeot': 'car' }) %}
* {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
* @param array|\Traversable $arr1 An array
* @param array|\Traversable $arr2 An array
* @return array The merged array
function twig_array_merge($arr1, $arr2)
if (!\WPML\Core\twig_test_iterable($arr1)) {
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('The merge filter only works with arrays or "Traversable", got "%s" as first argument.', \gettype($arr1)));