From 0716f96d8dafd2fd65187cf0e7bdca76e8256349 Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Wed, 9 Mar 2016 20:22:36 -0800 Subject: [PATCH] Added `json_decode` and `get_cookie` twig filters --- system/src/Grav/Common/Twig/TwigExtension.php | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index a385bfe17..fb37f934c 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -25,9 +25,9 @@ class TwigExtension extends \Twig_Extension */ public function __construct() { - $this->grav = Grav::instance(); + $this->grav = Grav::instance(); $this->debugger = isset($this->grav['debugger']) ? $this->grav['debugger'] : null; - $this->config = $this->grav['config']; + $this->config = $this->grav['config']; } /** @@ -85,6 +85,7 @@ class TwigExtension extends \Twig_Extension new \Twig_SimpleFilter('ta', [$this, 'translateArray']), new \Twig_SimpleFilter('truncate', ['\Grav\Common\Utils', 'truncate']), new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']), + new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']), ]; } @@ -111,8 +112,8 @@ class TwigExtension extends \Twig_Extension new \Twig_simpleFunction('t', [$this, 'translate']), new \Twig_simpleFunction('ta', [$this, 'translateArray']), new \Twig_SimpleFunction('url', [$this, 'urlFunc']), - - + new \Twig_SimpleFunction('json_decode', [$this, 'jsonDecodeFilter']), + new \Twig_SimpleFunction('get_cookie', [$this, 'getCookie']), ]; } @@ -139,7 +140,7 @@ class TwigExtension extends \Twig_Extension */ public function safeEmailFilter($str) { - $email = ''; + $email = ''; $str_len = strlen($str); for ($i = 0; $i < $str_len; $i++) { $email .= "&#" . ord($str[$i]) . ";"; @@ -374,11 +375,11 @@ class TwigExtension extends \Twig_Extension // is it future date or past date if ($now > $unix_date) { $difference = $now - $unix_date; - $tense = $this->grav['language']->translate('NICETIME.AGO', null, true); + $tense = $this->grav['language']->translate('NICETIME.AGO', null, true); } else { $difference = $unix_date - $now; - $tense = $this->grav['language']->translate('NICETIME.FROM_NOW', null, true); + $tense = $this->grav['language']->translate('NICETIME.FROM_NOW', null, true); } for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) { @@ -411,7 +412,7 @@ class TwigExtension extends \Twig_Extension */ public function absoluteUrlFilter($string) { - $url = $this->grav['uri']->base(); + $url = $this->grav['uri']->base(); $string = preg_replace('/((?:href|src) *= *[\'"](?!(http|ftp)))/i', "$1$url", $string); return $string; @@ -425,7 +426,7 @@ class TwigExtension extends \Twig_Extension */ public function markdownFilter($string) { - $page = $this->grav['page']; + $page = $this->grav['page']; $defaults = $this->config->get('system.pages.markdown'); // Initialize the preferred variant of Parsedown @@ -674,16 +675,16 @@ class TwigExtension extends \Twig_Extension * Workaround for twig associative array initialization * Returns a key => val array * - * @param string $key key of item - * @param string $val value of item - * @param string $current_array optional array to add to + * @param string $key key of item + * @param string $val value of item + * @param string $current_array optional array to add to * * @return array */ public function arrayKeyValueFunc($key, $val, $current_array = null) { if (empty($current_array)) { - return array( $key => $val ); + return array($key => $val); } else { $current_array[$key] = $val; return $current_array; @@ -757,14 +758,40 @@ class TwigExtension extends \Twig_Extension return $string; } + /** + * Decodes string from JSON. + * + * @param string $str + * @param bool $assoc + * @param int $depth + * @param int $options + * @return array + */ + public function jsonDecodeFilter($str, $assoc = false, $depth = 512, $options = 0) + { + return json_decode(html_entity_decode($str), $assoc, $depth, $options); + } + + /** + * Used to retrieve a cookie value + * + * @param string $key The cookie name to retrieve + * + * @return mixed + */ + public function getCookie($key) + { + return filter_input(INPUT_COOKIE, $key, FILTER_SANITIZE_STRING); + } + /** * Twig wrapper for PHP's preg_replace method * - * @param mixed $subject the content to perform the replacement on - * @param mixed $pattern the regex pattern to use for matches - * @param mixed $replace the replacement value either as a string or an array of replacements - * @param int $limit the maximum possible replacements for each pattern in each subject - + * @param mixed $subject the content to perform the replacement on + * @param mixed $pattern the regex pattern to use for matches + * @param mixed $replace the replacement value either as a string or an array of replacements + * @param int $limit the maximum possible replacements for each pattern in each subject + * * @return mixed the resulting content */ public function regexReplace($subject, $pattern, $replace, $limit = -1)