: 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;
use WPML\Core\Twig\Error\Error;
use WPML\Core\Twig\Error\LoaderError;
use WPML\Core\Twig\Error\RuntimeError;
* Default base class for compiled templates.
* This class is an implementation detail of how template compilation currently
* works, which might change. It should never be used directly. Use $twig->load()
* instead, which returns an instance of \Twig\TemplateWrapper.
* @author Fabien Potencier <fabien@symfony.com>
abstract class Template implements \WPML\Core\Twig_TemplateInterface
protected static $cache = [];
public function __construct(\WPML\Core\Twig\Environment $env)
* @internal this method will be removed in 2.0 and is only used internally to provide an upgrade path from 1.x to 2.0
public function __toString()
return $this->getTemplateName();
* Returns the template name.
* @return string The template name
public abstract function getTemplateName();
* Returns debug information about the template.
* @return array Debug information
public function getDebugInfo()
* Returns the template source code.
* @return string The template source code
* @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead
public function getSource()
@\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', \E_USER_DEPRECATED);
* Returns information about the original template source code.
public function getSourceContext()
return new \WPML\Core\Twig\Source('', $this->getTemplateName());
* @deprecated since 1.20 (to be removed in 2.0)
public function getEnvironment()
@\trigger_error('The ' . __METHOD__ . ' method is deprecated since version 1.20 and will be removed in 2.0.', \E_USER_DEPRECATED);
* Returns the parent template.
* This method is for internal use only and should never be called
* @return \Twig_TemplateInterface|TemplateWrapper|false The parent template or false if there is no parent
public function getParent(array $context)
if (null !== $this->parent) {
$parent = $this->doGetParent($context);
if (\false === $parent) {
if ($parent instanceof self || $parent instanceof \WPML\Core\Twig\TemplateWrapper) {
return $this->parents[$parent->getSourceContext()->getName()] = $parent;
if (!isset($this->parents[$parent])) {
$this->parents[$parent] = $this->loadTemplate($parent);
} catch (\WPML\Core\Twig\Error\LoaderError $e) {
$e->setSourceContext(null);
return $this->parents[$parent];
protected function doGetParent(array $context)
public function isTraitable()
* Displays a parent block.
* This method is for internal use only and should never be called
* @param string $name The block name to display from the parent
* @param array $context The context
* @param array $blocks The current set of blocks
public function displayParentBlock($name, array $context, array $blocks = [])
if (isset($this->traits[$name])) {
$this->traits[$name][0]->displayBlock($name, $context, $blocks, \false);
} elseif (\false !== ($parent = $this->getParent($context))) {
$parent->displayBlock($name, $context, $blocks, \false);
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('The template has no parent and no traits defining the "%s" block.', $name), -1, $this->getSourceContext());
* This method is for internal use only and should never be called
* @param string $name The block name to display
* @param array $context The context
* @param array $blocks The current set of blocks
* @param bool $useBlocks Whether to use the current set of blocks
public function displayBlock($name, array $context, array $blocks = [], $useBlocks = \true)
if ($useBlocks && isset($blocks[$name])) {
$template = $blocks[$name][0];
$block = $blocks[$name][1];
} elseif (isset($this->blocks[$name])) {
$template = $this->blocks[$name][0];
$block = $this->blocks[$name][1];
// avoid RCEs when sandbox is enabled
if (null !== $template && !$template instanceof self) {
throw new \LogicException('A block must be a method on a \\Twig\\Template instance.');
if (null !== $template) {
$template->{$block}($context, $blocks);
} catch (\WPML\Core\Twig\Error\Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($template->getSourceContext());
// this is mostly useful for \Twig\Error\LoaderError exceptions
// see \Twig\Error\LoaderError
if (-1 === $e->getTemplateLine()) {
} catch (\Exception $e) {
$e = new \WPML\Core\Twig\Error\RuntimeError(\sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getSourceContext(), $e);
} elseif (\false !== ($parent = $this->getParent($context))) {
$parent->displayBlock($name, $context, \array_merge($this->blocks, $blocks), \false);
@\trigger_error(\sprintf('Silent display of undefined block "%s" in template "%s" is deprecated since version 1.29 and will throw an exception in 2.0. Use the "block(\'%s\') is defined" expression to test for block existence.', $name, $this->getTemplateName(), $name), \E_USER_DEPRECATED);
* Renders a parent block.
* This method is for internal use only and should never be called
* @param string $name The block name to render from the parent
* @param array $context The context
* @param array $blocks The current set of blocks
* @return string The rendered block
public function renderParentBlock($name, array $context, array $blocks = [])
if ($this->env->isDebug()) {
$this->displayParentBlock($name, $context, $blocks);
* This method is for internal use only and should never be called
* @param string $name The block name to render
* @param array $context The context
* @param array $blocks The current set of blocks
* @param bool $useBlocks Whether to use the current set of blocks
* @return string The rendered block
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = \true)
if ($this->env->isDebug()) {
$this->displayBlock($name, $context, $blocks, $useBlocks);
* Returns whether a block exists or not in the current context of the template.
* This method checks blocks defined in the current template
* or defined in "used" traits or defined in parent templates.
* @param string $name The block name
* @param array $context The context
* @param array $blocks The current set of blocks
* @return bool true if the block exists, false otherwise
public function hasBlock($name, array $context = null, array $blocks = [])
@\trigger_error('The ' . __METHOD__ . ' method is internal and should never be called; calling it directly is deprecated since version 1.28 and won\'t be possible anymore in 2.0.', \E_USER_DEPRECATED);
return isset($this->blocks[(string) $name]);
if (isset($blocks[$name])) {
return $blocks[$name][0] instanceof self;
if (isset($this->blocks[$name])) {
if (\false !== ($parent = $this->getParent($context))) {
return $parent->hasBlock($name, $context);
* Returns all block names in the current context of the template.
* This method checks blocks defined in the current template
* or defined in "used" traits or defined in parent templates.
* @param array $context The context
* @param array $blocks The current set of blocks
* @return array An array of block names
public function getBlockNames(array $context = null, array $blocks = [])
@\trigger_error('The ' . __METHOD__ . ' method is internal and should never be called; calling it directly is deprecated since version 1.28 and won\'t be possible anymore in 2.0.', \E_USER_DEPRECATED);
return \array_keys($this->blocks);
$names = \array_merge(\array_keys($blocks), \array_keys($this->blocks));
if (\false !== ($parent = $this->getParent($context))) {
$names = \array_merge($names, $parent->getBlockNames($context));
return \array_unique($names);
* @return Template|TemplateWrapper
protected function loadTemplate($template, $templateName = null, $line = null, $index = null)
if (\is_array($template)) {
return $this->env->resolveTemplate($template);
if ($template instanceof self || $template instanceof \WPML\Core\Twig\TemplateWrapper) {
if ($template === $this->getTemplateName()) {
$class = \get_class($this);
if (\false !== ($pos = \strrpos($class, '___', -1))) {
$class = \substr($class, 0, $pos);
return $this->env->loadClass($class, $template, $index);
return $this->env->loadTemplate($template, $index);
} catch (\WPML\Core\Twig\Error\Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($templateName ? new \WPML\Core\Twig\Source('', $templateName) : $this->getSourceContext());
if ($e->getTemplateLine() > 0) {
$e->setTemplateLine($line);
protected function unwrap()
* This method is for internal use only and should never be called
* @return array An array of blocks
public function getBlocks()
public function display(array $context, array $blocks = [])
$this->displayWithErrorHandling($this->env->mergeGlobals($context), \array_merge($this->blocks, $blocks));
public function render(array $context)
$level = \ob_get_level();
if ($this->env->isDebug()) {
$this->display($context);
} catch (\Exception $e) {
while (\ob_get_level() > $level) {
} catch (\Throwable $e) {
while (\ob_get_level() > $level) {
protected function displayWithErrorHandling(array $context, array $blocks = [])
$this->doDisplay($context, $blocks);
} catch (\WPML\Core\Twig\Error\Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($this->getSourceContext());
// this is mostly useful for \Twig\Error\LoaderError exceptions
// see \Twig\Error\LoaderError
if (-1 === $e->getTemplateLine()) {
} catch (\Exception $e) {
$e = new \WPML\Core\Twig\Error\RuntimeError(\sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $this->getSourceContext(), $e);
* Auto-generated method to display the template with the given context.
* @param array $context An array of parameters to pass to the template
* @param array $blocks An array of blocks to pass to the template
protected abstract function doDisplay(array $context, array $blocks = []);
* Returns a variable from the context.
* This method is for internal use only and should never be called
* This method should not be overridden in a sub-class as this is an
* implementation detail that has been introduced to optimize variable
* access for versions of PHP before 5.4. This is not a way to override
* the way to get a variable value.
* @param array $context The context
* @param string $item The variable to return from the context
* @param bool $ignoreStrictCheck Whether to ignore the strict variable check or not
* @return mixed The content of the context variable
* @throws RuntimeError if the variable does not exist and Twig is running in strict mode
protected final function getContext($context, $item, $ignoreStrictCheck = \false)
if (!\array_key_exists($item, $context)) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('Variable "%s" does not exist.', $item), -1, $this->getSourceContext());
* Returns the attribute value for a given array/object.
* @param mixed $object The object or array from where to get the item
* @param mixed $item The item to get from the array or object
* @param array $arguments An array of arguments to pass if the item is an object method
* @param string $type The type of attribute (@see \Twig\Template constants)
* @param bool $isDefinedTest Whether this is only a defined check
* @param bool $ignoreStrictCheck Whether to ignore the strict attribute check or not
* @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
* @throws RuntimeError if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
protected function getAttribute($object, $item, array $arguments = [], $type = self::ANY_CALL, $isDefinedTest = \false, $ignoreStrictCheck = \false)
if (self::METHOD_CALL !== $type) {
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item;
if ((\is_array($object) || $object instanceof \ArrayObject) && (isset($object[$arrayItem]) || \array_key_exists($arrayItem, (array) $object)) || $object instanceof \ArrayAccess && isset($object[$arrayItem])) {
return $object[$arrayItem];
if (self::ARRAY_CALL === $type || !\is_object($object)) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
if ($object instanceof \ArrayAccess) {
$message = \sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist.', $arrayItem, \get_class($object));
} elseif (\is_object($object)) {
$message = \sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface.', $item, \get_class($object));
} elseif (\is_array($object)) {
$message = \sprintf('Key "%s" does not exist as the array is empty.', $arrayItem);
$message = \sprintf('Key "%s" for array with keys "%s" does not exist.', $arrayItem, \implode(', ', \array_keys($object)));
} elseif (self::ARRAY_CALL === $type) {