Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/sitepres.../lib/twig/src/Extensio...
File: CoreExtension.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/*
[2] Fix | Delete
* This file is part of Twig.
[3] Fix | Delete
*
[4] Fix | Delete
* (c) Fabien Potencier
[5] Fix | Delete
*
[6] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[7] Fix | Delete
* file that was distributed with this source code.
[8] Fix | Delete
*/
[9] Fix | Delete
namespace WPML\Core\Twig\Extension;
[10] Fix | Delete
[11] Fix | Delete
use WPML\Core\Twig\ExpressionParser;
[12] Fix | Delete
use WPML\Core\Twig\TokenParser\ApplyTokenParser;
[13] Fix | Delete
use WPML\Core\Twig\TokenParser\BlockTokenParser;
[14] Fix | Delete
use WPML\Core\Twig\TokenParser\DeprecatedTokenParser;
[15] Fix | Delete
use WPML\Core\Twig\TokenParser\DoTokenParser;
[16] Fix | Delete
use WPML\Core\Twig\TokenParser\EmbedTokenParser;
[17] Fix | Delete
use WPML\Core\Twig\TokenParser\ExtendsTokenParser;
[18] Fix | Delete
use WPML\Core\Twig\TokenParser\FilterTokenParser;
[19] Fix | Delete
use WPML\Core\Twig\TokenParser\FlushTokenParser;
[20] Fix | Delete
use WPML\Core\Twig\TokenParser\ForTokenParser;
[21] Fix | Delete
use WPML\Core\Twig\TokenParser\FromTokenParser;
[22] Fix | Delete
use WPML\Core\Twig\TokenParser\IfTokenParser;
[23] Fix | Delete
use WPML\Core\Twig\TokenParser\ImportTokenParser;
[24] Fix | Delete
use WPML\Core\Twig\TokenParser\IncludeTokenParser;
[25] Fix | Delete
use WPML\Core\Twig\TokenParser\MacroTokenParser;
[26] Fix | Delete
use WPML\Core\Twig\TokenParser\SetTokenParser;
[27] Fix | Delete
use WPML\Core\Twig\TokenParser\SpacelessTokenParser;
[28] Fix | Delete
use WPML\Core\Twig\TokenParser\UseTokenParser;
[29] Fix | Delete
use WPML\Core\Twig\TokenParser\WithTokenParser;
[30] Fix | Delete
use WPML\Core\Twig\TwigFilter;
[31] Fix | Delete
use WPML\Core\Twig\TwigFunction;
[32] Fix | Delete
use WPML\Core\Twig\TwigTest;
[33] Fix | Delete
/**
[34] Fix | Delete
* @final
[35] Fix | Delete
*/
[36] Fix | Delete
class CoreExtension extends \WPML\Core\Twig\Extension\AbstractExtension
[37] Fix | Delete
{
[38] Fix | Delete
protected $dateFormats = ['F j, Y H:i', '%d days'];
[39] Fix | Delete
protected $numberFormat = [0, '.', ','];
[40] Fix | Delete
protected $timezone = null;
[41] Fix | Delete
protected $escapers = [];
[42] Fix | Delete
/**
[43] Fix | Delete
* Defines a new escaper to be used via the escape filter.
[44] Fix | Delete
*
[45] Fix | Delete
* @param string $strategy The strategy name that should be used as a strategy in the escape call
[46] Fix | Delete
* @param callable $callable A valid PHP callable
[47] Fix | Delete
*/
[48] Fix | Delete
public function setEscaper($strategy, $callable)
[49] Fix | Delete
{
[50] Fix | Delete
$this->escapers[$strategy] = $callable;
[51] Fix | Delete
}
[52] Fix | Delete
/**
[53] Fix | Delete
* Gets all defined escapers.
[54] Fix | Delete
*
[55] Fix | Delete
* @return array An array of escapers
[56] Fix | Delete
*/
[57] Fix | Delete
public function getEscapers()
[58] Fix | Delete
{
[59] Fix | Delete
return $this->escapers;
[60] Fix | Delete
}
[61] Fix | Delete
/**
[62] Fix | Delete
* Sets the default format to be used by the date filter.
[63] Fix | Delete
*
[64] Fix | Delete
* @param string $format The default date format string
[65] Fix | Delete
* @param string $dateIntervalFormat The default date interval format string
[66] Fix | Delete
*/
[67] Fix | Delete
public function setDateFormat($format = null, $dateIntervalFormat = null)
[68] Fix | Delete
{
[69] Fix | Delete
if (null !== $format) {
[70] Fix | Delete
$this->dateFormats[0] = $format;
[71] Fix | Delete
}
[72] Fix | Delete
if (null !== $dateIntervalFormat) {
[73] Fix | Delete
$this->dateFormats[1] = $dateIntervalFormat;
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
/**
[77] Fix | Delete
* Gets the default format to be used by the date filter.
[78] Fix | Delete
*
[79] Fix | Delete
* @return array The default date format string and the default date interval format string
[80] Fix | Delete
*/
[81] Fix | Delete
public function getDateFormat()
[82] Fix | Delete
{
[83] Fix | Delete
return $this->dateFormats;
[84] Fix | Delete
}
[85] Fix | Delete
/**
[86] Fix | Delete
* Sets the default timezone to be used by the date filter.
[87] Fix | Delete
*
[88] Fix | Delete
* @param \DateTimeZone|string $timezone The default timezone string or a \DateTimeZone object
[89] Fix | Delete
*/
[90] Fix | Delete
public function setTimezone($timezone)
[91] Fix | Delete
{
[92] Fix | Delete
$this->timezone = $timezone instanceof \DateTimeZone ? $timezone : new \DateTimeZone($timezone);
[93] Fix | Delete
}
[94] Fix | Delete
/**
[95] Fix | Delete
* Gets the default timezone to be used by the date filter.
[96] Fix | Delete
*
[97] Fix | Delete
* @return \DateTimeZone The default timezone currently in use
[98] Fix | Delete
*/
[99] Fix | Delete
public function getTimezone()
[100] Fix | Delete
{
[101] Fix | Delete
if (null === $this->timezone) {
[102] Fix | Delete
$this->timezone = new \DateTimeZone(\date_default_timezone_get());
[103] Fix | Delete
}
[104] Fix | Delete
return $this->timezone;
[105] Fix | Delete
}
[106] Fix | Delete
/**
[107] Fix | Delete
* Sets the default format to be used by the number_format filter.
[108] Fix | Delete
*
[109] Fix | Delete
* @param int $decimal the number of decimal places to use
[110] Fix | Delete
* @param string $decimalPoint the character(s) to use for the decimal point
[111] Fix | Delete
* @param string $thousandSep the character(s) to use for the thousands separator
[112] Fix | Delete
*/
[113] Fix | Delete
public function setNumberFormat($decimal, $decimalPoint, $thousandSep)
[114] Fix | Delete
{
[115] Fix | Delete
$this->numberFormat = [$decimal, $decimalPoint, $thousandSep];
[116] Fix | Delete
}
[117] Fix | Delete
/**
[118] Fix | Delete
* Get the default format used by the number_format filter.
[119] Fix | Delete
*
[120] Fix | Delete
* @return array The arguments for number_format()
[121] Fix | Delete
*/
[122] Fix | Delete
public function getNumberFormat()
[123] Fix | Delete
{
[124] Fix | Delete
return $this->numberFormat;
[125] Fix | Delete
}
[126] Fix | Delete
public function getTokenParsers()
[127] Fix | Delete
{
[128] Fix | Delete
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()];
[129] Fix | Delete
}
[130] Fix | Delete
public function getFilters()
[131] Fix | Delete
{
[132] Fix | Delete
$filters = [
[133] Fix | Delete
// formatting filters
[134] Fix | Delete
new \WPML\Core\Twig\TwigFilter('date', '\\WPML\\Core\\twig_date_format_filter', ['needs_environment' => \true]),
[135] Fix | Delete
new \WPML\Core\Twig\TwigFilter('date_modify', '\\WPML\\Core\\twig_date_modify_filter', ['needs_environment' => \true]),
[136] Fix | Delete
new \WPML\Core\Twig\TwigFilter('format', 'sprintf'),
[137] Fix | Delete
new \WPML\Core\Twig\TwigFilter('replace', '\\WPML\\Core\\twig_replace_filter'),
[138] Fix | Delete
new \WPML\Core\Twig\TwigFilter('number_format', '\\WPML\\Core\\twig_number_format_filter', ['needs_environment' => \true]),
[139] Fix | Delete
new \WPML\Core\Twig\TwigFilter('abs', 'abs'),
[140] Fix | Delete
new \WPML\Core\Twig\TwigFilter('round', '\\WPML\\Core\\twig_round'),
[141] Fix | Delete
// encoding
[142] Fix | Delete
new \WPML\Core\Twig\TwigFilter('url_encode', '\\WPML\\Core\\twig_urlencode_filter'),
[143] Fix | Delete
new \WPML\Core\Twig\TwigFilter('json_encode', '\\WPML\\Core\\twig_jsonencode_filter'),
[144] Fix | Delete
new \WPML\Core\Twig\TwigFilter('convert_encoding', '\\WPML\\Core\\twig_convert_encoding'),
[145] Fix | Delete
// string filters
[146] Fix | Delete
new \WPML\Core\Twig\TwigFilter('title', '\\WPML\\Core\\twig_title_string_filter', ['needs_environment' => \true]),
[147] Fix | Delete
new \WPML\Core\Twig\TwigFilter('capitalize', '\\WPML\\Core\\twig_capitalize_string_filter', ['needs_environment' => \true]),
[148] Fix | Delete
new \WPML\Core\Twig\TwigFilter('upper', 'strtoupper'),
[149] Fix | Delete
new \WPML\Core\Twig\TwigFilter('lower', 'strtolower'),
[150] Fix | Delete
new \WPML\Core\Twig\TwigFilter('striptags', 'strip_tags'),
[151] Fix | Delete
new \WPML\Core\Twig\TwigFilter('trim', '\\WPML\\Core\\twig_trim_filter'),
[152] Fix | Delete
new \WPML\Core\Twig\TwigFilter('nl2br', 'nl2br', ['pre_escape' => 'html', 'is_safe' => ['html']]),
[153] Fix | Delete
new \WPML\Core\Twig\TwigFilter('spaceless', '\\WPML\\Core\\twig_spaceless', ['is_safe' => ['html']]),
[154] Fix | Delete
// array helpers
[155] Fix | Delete
new \WPML\Core\Twig\TwigFilter('join', '\\WPML\\Core\\twig_join_filter'),
[156] Fix | Delete
new \WPML\Core\Twig\TwigFilter('split', '\\WPML\\Core\\twig_split_filter', ['needs_environment' => \true]),
[157] Fix | Delete
new \WPML\Core\Twig\TwigFilter('sort', '\\WPML\\Core\\twig_sort_filter'),
[158] Fix | Delete
new \WPML\Core\Twig\TwigFilter('merge', '\\WPML\\Core\\twig_array_merge'),
[159] Fix | Delete
new \WPML\Core\Twig\TwigFilter('batch', '\\WPML\\Core\\twig_array_batch'),
[160] Fix | Delete
new \WPML\Core\Twig\TwigFilter('filter', '\\WPML\\Core\\twig_array_filter'),
[161] Fix | Delete
new \WPML\Core\Twig\TwigFilter('map', '\\WPML\\Core\\twig_array_map'),
[162] Fix | Delete
new \WPML\Core\Twig\TwigFilter('reduce', '\\WPML\\Core\\twig_array_reduce'),
[163] Fix | Delete
// string/array filters
[164] Fix | Delete
new \WPML\Core\Twig\TwigFilter('reverse', '\\WPML\\Core\\twig_reverse_filter', ['needs_environment' => \true]),
[165] Fix | Delete
new \WPML\Core\Twig\TwigFilter('length', '\\WPML\\Core\\twig_length_filter', ['needs_environment' => \true]),
[166] Fix | Delete
new \WPML\Core\Twig\TwigFilter('slice', '\\WPML\\Core\\twig_slice', ['needs_environment' => \true]),
[167] Fix | Delete
new \WPML\Core\Twig\TwigFilter('first', '\\WPML\\Core\\twig_first', ['needs_environment' => \true]),
[168] Fix | Delete
new \WPML\Core\Twig\TwigFilter('last', '\\WPML\\Core\\twig_last', ['needs_environment' => \true]),
[169] Fix | Delete
// iteration and runtime
[170] Fix | Delete
new \WPML\Core\Twig\TwigFilter('default', '\\WPML\\Core\\_twig_default_filter', ['node_class' => 'WPML\\Core\\Twig\\Node\\Expression\\Filter\\DefaultFilter']),
[171] Fix | Delete
new \WPML\Core\Twig\TwigFilter('keys', '\\WPML\\Core\\twig_get_array_keys_filter'),
[172] Fix | Delete
// escaping
[173] Fix | Delete
new \WPML\Core\Twig\TwigFilter('escape', '\\WPML\\Core\\twig_escape_filter', ['needs_environment' => \true, 'is_safe_callback' => '\\WPML\\Core\\twig_escape_filter_is_safe']),
[174] Fix | Delete
new \WPML\Core\Twig\TwigFilter('e', '\\WPML\\Core\\twig_escape_filter', ['needs_environment' => \true, 'is_safe_callback' => '\\WPML\\Core\\twig_escape_filter_is_safe']),
[175] Fix | Delete
];
[176] Fix | Delete
if (\function_exists('mb_get_info')) {
[177] Fix | Delete
$filters[] = new \WPML\Core\Twig\TwigFilter('upper', '\\WPML\\Core\\twig_upper_filter', ['needs_environment' => \true]);
[178] Fix | Delete
$filters[] = new \WPML\Core\Twig\TwigFilter('lower', '\\WPML\\Core\\twig_lower_filter', ['needs_environment' => \true]);
[179] Fix | Delete
}
[180] Fix | Delete
return $filters;
[181] Fix | Delete
}
[182] Fix | Delete
public function getFunctions()
[183] Fix | Delete
{
[184] Fix | Delete
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']])];
[185] Fix | Delete
}
[186] Fix | Delete
public function getTests()
[187] Fix | Delete
{
[188] Fix | Delete
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')];
[189] Fix | Delete
}
[190] Fix | Delete
public function getOperators()
[191] Fix | Delete
{
[192] Fix | Delete
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]]];
[193] Fix | Delete
}
[194] Fix | Delete
public function getName()
[195] Fix | Delete
{
[196] Fix | Delete
return 'core';
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
\class_alias('WPML\\Core\\Twig\\Extension\\CoreExtension', 'WPML\\Core\\Twig_Extension_Core');
[200] Fix | Delete
namespace WPML\Core;
[201] Fix | Delete
[202] Fix | Delete
use WPML\Core\Twig\Environment;
[203] Fix | Delete
use WPML\Core\Twig\Error\LoaderError;
[204] Fix | Delete
use WPML\Core\Twig\Error\RuntimeError;
[205] Fix | Delete
use WPML\Core\Twig\Loader\SourceContextLoaderInterface;
[206] Fix | Delete
use WPML\Core\Twig\Markup;
[207] Fix | Delete
use WPML\Core\Twig\Node\Expression\ConstantExpression;
[208] Fix | Delete
use WPML\Core\Twig\Node\Node;
[209] Fix | Delete
/**
[210] Fix | Delete
* Cycles over a value.
[211] Fix | Delete
*
[212] Fix | Delete
* @param \ArrayAccess|array $values
[213] Fix | Delete
* @param int $position The cycle position
[214] Fix | Delete
*
[215] Fix | Delete
* @return string The next value in the cycle
[216] Fix | Delete
*/
[217] Fix | Delete
function twig_cycle($values, $position)
[218] Fix | Delete
{
[219] Fix | Delete
if (!\is_array($values) && !$values instanceof \ArrayAccess) {
[220] Fix | Delete
return $values;
[221] Fix | Delete
}
[222] Fix | Delete
return $values[$position % \count($values)];
[223] Fix | Delete
}
[224] Fix | Delete
/**
[225] Fix | Delete
* Returns a random value depending on the supplied parameter type:
[226] Fix | Delete
* - a random item from a \Traversable or array
[227] Fix | Delete
* - a random character from a string
[228] Fix | Delete
* - a random integer between 0 and the integer parameter.
[229] Fix | Delete
*
[230] Fix | Delete
* @param \Traversable|array|int|float|string $values The values to pick a random item from
[231] Fix | Delete
* @param int|null $max Maximum value used when $values is an int
[232] Fix | Delete
*
[233] Fix | Delete
* @throws RuntimeError when $values is an empty array (does not apply to an empty string which is returned as is)
[234] Fix | Delete
*
[235] Fix | Delete
* @return mixed A random value from the given sequence
[236] Fix | Delete
*/
[237] Fix | Delete
function twig_random(\WPML\Core\Twig\Environment $env, $values = null, $max = null)
[238] Fix | Delete
{
[239] Fix | Delete
if (null === $values) {
[240] Fix | Delete
return null === $max ? \mt_rand() : \mt_rand(0, $max);
[241] Fix | Delete
}
[242] Fix | Delete
if (\is_int($values) || \is_float($values)) {
[243] Fix | Delete
if (null === $max) {
[244] Fix | Delete
if ($values < 0) {
[245] Fix | Delete
$max = 0;
[246] Fix | Delete
$min = $values;
[247] Fix | Delete
} else {
[248] Fix | Delete
$max = $values;
[249] Fix | Delete
$min = 0;
[250] Fix | Delete
}
[251] Fix | Delete
} else {
[252] Fix | Delete
$min = $values;
[253] Fix | Delete
$max = $max;
[254] Fix | Delete
}
[255] Fix | Delete
return \mt_rand($min, $max);
[256] Fix | Delete
}
[257] Fix | Delete
if (\is_string($values)) {
[258] Fix | Delete
if ('' === $values) {
[259] Fix | Delete
return '';
[260] Fix | Delete
}
[261] Fix | Delete
if (null !== ($charset = $env->getCharset())) {
[262] Fix | Delete
if ('UTF-8' !== $charset) {
[263] Fix | Delete
$values = \WPML\Core\twig_convert_encoding($values, 'UTF-8', $charset);
[264] Fix | Delete
}
[265] Fix | Delete
// unicode version of str_split()
[266] Fix | Delete
// split at all positions, but not after the start and not before the end
[267] Fix | Delete
$values = \preg_split('/(?<!^)(?!$)/u', $values);
[268] Fix | Delete
if ('UTF-8' !== $charset) {
[269] Fix | Delete
foreach ($values as $i => $value) {
[270] Fix | Delete
$values[$i] = \WPML\Core\twig_convert_encoding($value, $charset, 'UTF-8');
[271] Fix | Delete
}
[272] Fix | Delete
}
[273] Fix | Delete
} else {
[274] Fix | Delete
return $values[\mt_rand(0, \strlen($values) - 1)];
[275] Fix | Delete
}
[276] Fix | Delete
}
[277] Fix | Delete
if (!\WPML\Core\twig_test_iterable($values)) {
[278] Fix | Delete
return $values;
[279] Fix | Delete
}
[280] Fix | Delete
$values = \WPML\Core\twig_to_array($values);
[281] Fix | Delete
if (0 === \count($values)) {
[282] Fix | Delete
throw new \WPML\Core\Twig\Error\RuntimeError('The random function cannot pick from an empty array.');
[283] Fix | Delete
}
[284] Fix | Delete
return $values[\array_rand($values, 1)];
[285] Fix | Delete
}
[286] Fix | Delete
/**
[287] Fix | Delete
* Converts a date to the given format.
[288] Fix | Delete
*
[289] Fix | Delete
* {{ post.published_at|date("m/d/Y") }}
[290] Fix | Delete
*
[291] Fix | Delete
* @param \DateTime|\DateTimeInterface|\DateInterval|string $date A date
[292] Fix | Delete
* @param string|null $format The target format, null to use the default
[293] Fix | Delete
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
[294] Fix | Delete
*
[295] Fix | Delete
* @return string The formatted date
[296] Fix | Delete
*/
[297] Fix | Delete
function twig_date_format_filter(\WPML\Core\Twig\Environment $env, $date, $format = null, $timezone = null)
[298] Fix | Delete
{
[299] Fix | Delete
if (null === $format) {
[300] Fix | Delete
$formats = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getDateFormat();
[301] Fix | Delete
$format = $date instanceof \DateInterval ? $formats[1] : $formats[0];
[302] Fix | Delete
}
[303] Fix | Delete
if ($date instanceof \DateInterval) {
[304] Fix | Delete
return $date->format($format);
[305] Fix | Delete
}
[306] Fix | Delete
return \WPML\Core\twig_date_converter($env, $date, $timezone)->format($format);
[307] Fix | Delete
}
[308] Fix | Delete
/**
[309] Fix | Delete
* Returns a new date object modified.
[310] Fix | Delete
*
[311] Fix | Delete
* {{ post.published_at|date_modify("-1day")|date("m/d/Y") }}
[312] Fix | Delete
*
[313] Fix | Delete
* @param \DateTime|string $date A date
[314] Fix | Delete
* @param string $modifier A modifier string
[315] Fix | Delete
*
[316] Fix | Delete
* @return \DateTime
[317] Fix | Delete
*/
[318] Fix | Delete
function twig_date_modify_filter(\WPML\Core\Twig\Environment $env, $date, $modifier)
[319] Fix | Delete
{
[320] Fix | Delete
$date = \WPML\Core\twig_date_converter($env, $date, \false);
[321] Fix | Delete
$resultDate = $date->modify($modifier);
[322] Fix | Delete
// This is a hack to ensure PHP 5.2 support and support for \DateTimeImmutable
[323] Fix | Delete
// \DateTime::modify does not return the modified \DateTime object < 5.3.0
[324] Fix | Delete
// and \DateTimeImmutable does not modify $date.
[325] Fix | Delete
return null === $resultDate ? $date : $resultDate;
[326] Fix | Delete
}
[327] Fix | Delete
/**
[328] Fix | Delete
* Converts an input to a \DateTime instance.
[329] Fix | Delete
*
[330] Fix | Delete
* {% if date(user.created_at) < date('+2days') %}
[331] Fix | Delete
* {# do something #}
[332] Fix | Delete
* {% endif %}
[333] Fix | Delete
*
[334] Fix | Delete
* @param \DateTime|\DateTimeInterface|string|null $date A date
[335] Fix | Delete
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
[336] Fix | Delete
*
[337] Fix | Delete
* @return \DateTime
[338] Fix | Delete
*/
[339] Fix | Delete
function twig_date_converter(\WPML\Core\Twig\Environment $env, $date = null, $timezone = null)
[340] Fix | Delete
{
[341] Fix | Delete
// determine the timezone
[342] Fix | Delete
if (\false !== $timezone) {
[343] Fix | Delete
if (null === $timezone) {
[344] Fix | Delete
$timezone = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone();
[345] Fix | Delete
} elseif (!$timezone instanceof \DateTimeZone) {
[346] Fix | Delete
$timezone = new \DateTimeZone($timezone);
[347] Fix | Delete
}
[348] Fix | Delete
}
[349] Fix | Delete
// immutable dates
[350] Fix | Delete
if ($date instanceof \DateTimeImmutable) {
[351] Fix | Delete
return \false !== $timezone ? $date->setTimezone($timezone) : $date;
[352] Fix | Delete
}
[353] Fix | Delete
if ($date instanceof \DateTime || $date instanceof \DateTimeInterface) {
[354] Fix | Delete
$date = clone $date;
[355] Fix | Delete
if (\false !== $timezone) {
[356] Fix | Delete
$date->setTimezone($timezone);
[357] Fix | Delete
}
[358] Fix | Delete
return $date;
[359] Fix | Delete
}
[360] Fix | Delete
if (null === $date || 'now' === $date) {
[361] Fix | Delete
return new \DateTime($date, \false !== $timezone ? $timezone : $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone());
[362] Fix | Delete
}
[363] Fix | Delete
$asString = (string) $date;
[364] Fix | Delete
if (\ctype_digit($asString) || !empty($asString) && '-' === $asString[0] && \ctype_digit(\substr($asString, 1))) {
[365] Fix | Delete
$date = new \DateTime('@' . $date);
[366] Fix | Delete
} else {
[367] Fix | Delete
$date = new \DateTime($date, $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getTimezone());
[368] Fix | Delete
}
[369] Fix | Delete
if (\false !== $timezone) {
[370] Fix | Delete
$date->setTimezone($timezone);
[371] Fix | Delete
}
[372] Fix | Delete
return $date;
[373] Fix | Delete
}
[374] Fix | Delete
/**
[375] Fix | Delete
* Replaces strings within a string.
[376] Fix | Delete
*
[377] Fix | Delete
* @param string $str String to replace in
[378] Fix | Delete
* @param array|\Traversable $from Replace values
[379] Fix | Delete
* @param string|null $to Replace to, deprecated (@see https://secure.php.net/manual/en/function.strtr.php)
[380] Fix | Delete
*
[381] Fix | Delete
* @return string
[382] Fix | Delete
*/
[383] Fix | Delete
function twig_replace_filter($str, $from, $to = null)
[384] Fix | Delete
{
[385] Fix | Delete
if (\is_string($from) && \is_string($to)) {
[386] Fix | Delete
@\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);
[387] Fix | Delete
return \strtr($str, $from, $to);
[388] Fix | Delete
}
[389] Fix | Delete
if (!\WPML\Core\twig_test_iterable($from)) {
[390] Fix | Delete
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)));
[391] Fix | Delete
}
[392] Fix | Delete
return \strtr($str, \WPML\Core\twig_to_array($from));
[393] Fix | Delete
}
[394] Fix | Delete
/**
[395] Fix | Delete
* Rounds a number.
[396] Fix | Delete
*
[397] Fix | Delete
* @param int|float $value The value to round
[398] Fix | Delete
* @param int|float $precision The rounding precision
[399] Fix | Delete
* @param string $method The method to use for rounding
[400] Fix | Delete
*
[401] Fix | Delete
* @return int|float The rounded number
[402] Fix | Delete
*/
[403] Fix | Delete
function twig_round($value, $precision = 0, $method = 'common')
[404] Fix | Delete
{
[405] Fix | Delete
if ('common' == $method) {
[406] Fix | Delete
return \round($value, $precision);
[407] Fix | Delete
}
[408] Fix | Delete
if ('ceil' != $method && 'floor' != $method) {
[409] Fix | Delete
throw new \WPML\Core\Twig\Error\RuntimeError('The round filter only supports the "common", "ceil", and "floor" methods.');
[410] Fix | Delete
}
[411] Fix | Delete
return $method($value * \pow(10, $precision)) / \pow(10, $precision);
[412] Fix | Delete
}
[413] Fix | Delete
/**
[414] Fix | Delete
* Number format filter.
[415] Fix | Delete
*
[416] Fix | Delete
* All of the formatting options can be left null, in that case the defaults will
[417] Fix | Delete
* be used. Supplying any of the parameters will override the defaults set in the
[418] Fix | Delete
* environment object.
[419] Fix | Delete
*
[420] Fix | Delete
* @param mixed $number A float/int/string of the number to format
[421] Fix | Delete
* @param int $decimal the number of decimal points to display
[422] Fix | Delete
* @param string $decimalPoint the character(s) to use for the decimal point
[423] Fix | Delete
* @param string $thousandSep the character(s) to use for the thousands separator
[424] Fix | Delete
*
[425] Fix | Delete
* @return string The formatted number
[426] Fix | Delete
*/
[427] Fix | Delete
function twig_number_format_filter(\WPML\Core\Twig\Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null)
[428] Fix | Delete
{
[429] Fix | Delete
$defaults = $env->getExtension('WPML\\Core\\Twig\\Extension\\CoreExtension')->getNumberFormat();
[430] Fix | Delete
if (null === $decimal) {
[431] Fix | Delete
$decimal = $defaults[0];
[432] Fix | Delete
}
[433] Fix | Delete
if (null === $decimalPoint) {
[434] Fix | Delete
$decimalPoint = $defaults[1];
[435] Fix | Delete
}
[436] Fix | Delete
if (null === $thousandSep) {
[437] Fix | Delete
$thousandSep = $defaults[2];
[438] Fix | Delete
}
[439] Fix | Delete
return \number_format((float) $number, $decimal, $decimalPoint, $thousandSep);
[440] Fix | Delete
}
[441] Fix | Delete
/**
[442] Fix | Delete
* URL encodes (RFC 3986) a string as a path segment or an array as a query string.
[443] Fix | Delete
*
[444] Fix | Delete
* @param string|array $url A URL or an array of query parameters
[445] Fix | Delete
*
[446] Fix | Delete
* @return string The URL encoded value
[447] Fix | Delete
*/
[448] Fix | Delete
function twig_urlencode_filter($url)
[449] Fix | Delete
{
[450] Fix | Delete
if (\is_array($url)) {
[451] Fix | Delete
if (\defined('PHP_QUERY_RFC3986')) {
[452] Fix | Delete
return \http_build_query($url, '', '&', \PHP_QUERY_RFC3986);
[453] Fix | Delete
}
[454] Fix | Delete
return \http_build_query($url, '', '&');
[455] Fix | Delete
}
[456] Fix | Delete
return \rawurlencode($url);
[457] Fix | Delete
}
[458] Fix | Delete
/**
[459] Fix | Delete
* JSON encodes a variable.
[460] Fix | Delete
*
[461] Fix | Delete
* @param mixed $value the value to encode
[462] Fix | Delete
* @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
[463] Fix | Delete
*
[464] Fix | Delete
* @return mixed The JSON encoded value
[465] Fix | Delete
*/
[466] Fix | Delete
function twig_jsonencode_filter($value, $options = 0)
[467] Fix | Delete
{
[468] Fix | Delete
if ($value instanceof \WPML\Core\Twig\Markup) {
[469] Fix | Delete
$value = (string) $value;
[470] Fix | Delete
} elseif (\is_array($value)) {
[471] Fix | Delete
\array_walk_recursive($value, '\\WPML\\Core\\_twig_markup2string');
[472] Fix | Delete
}
[473] Fix | Delete
return \json_encode($value, $options);
[474] Fix | Delete
}
[475] Fix | Delete
function _twig_markup2string(&$value)
[476] Fix | Delete
{
[477] Fix | Delete
if ($value instanceof \WPML\Core\Twig\Markup) {
[478] Fix | Delete
$value = (string) $value;
[479] Fix | Delete
}
[480] Fix | Delete
}
[481] Fix | Delete
/**
[482] Fix | Delete
* Merges an array with another one.
[483] Fix | Delete
*
[484] Fix | Delete
* {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
[485] Fix | Delete
*
[486] Fix | Delete
* {% set items = items|merge({ 'peugeot': 'car' }) %}
[487] Fix | Delete
*
[488] Fix | Delete
* {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
[489] Fix | Delete
*
[490] Fix | Delete
* @param array|\Traversable $arr1 An array
[491] Fix | Delete
* @param array|\Traversable $arr2 An array
[492] Fix | Delete
*
[493] Fix | Delete
* @return array The merged array
[494] Fix | Delete
*/
[495] Fix | Delete
function twig_array_merge($arr1, $arr2)
[496] Fix | Delete
{
[497] Fix | Delete
if (!\WPML\Core\twig_test_iterable($arr1)) {
[498] Fix | Delete
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('The merge filter only works with arrays or "Traversable", got "%s" as first argument.', \gettype($arr1)));
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function