diff --git a/CHANGELOG.md b/CHANGELOG.md index 661aca80c..555f2a4be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,14 @@ +# v1.42 +## mm/dd/2018 + +1. [](#new) + * Added new `|niceszie` Twig filter for pretty file (auto converts to bytes, kB, MB, GB, etc) + * Added new `regex_filter()` Twig function to values in arrays + # v1.4.1 ## 03/11/2018 -1. [](#bugfix) +1. [](#bugfix) * Fixed session timing out because of session cookie was not being sent # v1.4.0 @@ -40,21 +47,21 @@ * Made `modular` blueprint more flexible * Code optimizations to `Utils` class [#1830](https://github.com/getgrav/grav/pull/1830) * Objects: Add protected function `getElement()` to get serialized value for a single property - * `ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()` + * `ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()` * `Grav\Framework\Cache`: Allow unlimited TTL * Optimizations & refactoring to the test suite [#1779](https://github.com/getgrav/grav/pull/1779) * Slight modification of Whoops error colors * Added new configuration option `system.session.initialize` to delay session initialization if needed by a plugin - * Vendor library updated to latest + * Vendor library updated to latest * Updated vendor libraries to latest versions * Removed constructor from `ObjectInterface` * Make it possible to include debug bar also into non-HTML responses * Updated built-in JQuery to latest 3.3.1 -1. [](#bugfix) +1. [](#bugfix) * Fixed issue with image alt tag always getting empted out unless set in markdown * Fixed issue with remote PHP version determination for Grav updates [#1883](https://github.com/getgrav/grav/issues/1883) * Fixed issue with _illegal scheme offset_ in `Uri::convertUrl()` [page-inject#8](https://github.com/getgrav/grav-plugin-page-inject/issues/8) - * Properly validate YAML blueprint fields so admin can save as proper YAML now [addresses many issues] + * Properly validate YAML blueprint fields so admin can save as proper YAML now [addresses many issues] * Fixed OpenGraph metatags so only Twitter uses `name=`, and all others use `property=` [#1849](https://github.com/getgrav/grav/issues/1849) * Fixed an issue with `evaluate()` and `evaluate_twig()` Twig functions that throws invalid template error * Fixed issue with `|sort_by_key` twig filter if the input was null or not an array diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 9076cde1a..6de7e106b 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -63,8 +63,9 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn new \Twig_SimpleFilter('absolute_url', [$this, 'absoluteUrlFilter']), new \Twig_SimpleFilter('contains', [$this, 'containsFilter']), new \Twig_SimpleFilter('chunk_split', [$this, 'chunkSplitFilter']), - new \Twig_SimpleFilter('nicenumber', [$this, 'niceNumberFunc']), + new \Twig_SimpleFilter('nicefilesize', [$this, 'niceFilesizeFunc']), + new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFunc']), new \Twig_SimpleFilter('defined', [$this, 'definedDefaultFilter']), new \Twig_SimpleFilter('ends_with', [$this, 'endsWithFilter']), new \Twig_SimpleFilter('fieldName', [$this, 'fieldNameFilter']), @@ -76,7 +77,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']), new \Twig_SimpleFilter('base64_encode', [$this, 'base64EncodeFilter']), new \Twig_SimpleFilter('base64_decode', [$this, 'base64DecodeFilter']), - new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFilter']), new \Twig_SimpleFilter('randomize', [$this, 'randomizeFilter']), new \Twig_SimpleFilter('modulus', [$this, 'modulusFilter']), new \Twig_SimpleFilter('rtrim', [$this, 'rtrimFilter']), @@ -129,6 +129,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn new \Twig_simpleFunction('random_string', [$this, 'randomStringFunc']), new \Twig_SimpleFunction('repeat', [$this, 'repeatFunc']), new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']), + new \Twig_SimpleFunction('regex_filter', [$this, 'regexFilter']), new \Twig_SimpleFunction('string', [$this, 'stringFunc']), new \Twig_simpleFunction('t', [$this, 'translate']), new \Twig_simpleFunction('tl', [$this, 'translateLanguage']), @@ -145,6 +146,9 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn new \Twig_SimpleFunction('theme_var', [$this, 'themeVarFunc']), new \Twig_SimpleFunction('header_var', [$this, 'pageHeaderVarFunc']), new \Twig_SimpleFunction('read_file', [$this, 'readFileFunc']), + new \Twig_SimpleFunction('nicenumber', [$this, 'niceNumberFunc']), + new \Twig_SimpleFunction('nicefilesize', [$this, 'niceFilesizeFunc']), + new \Twig_SimpleFunction('nicetime', [$this, 'nicetimeFilter']), ]; } @@ -439,7 +443,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn * * @return boolean */ - public function nicetimeFilter($date, $long_strings = true) + public function nicetimeFunc($date, $long_strings = true) { if (empty($date)) { return $this->grav['language']->translate('NICETIME.NO_DATE_PROVIDED', null, true); @@ -958,6 +962,18 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn return preg_replace($pattern, $replace, $subject, $limit); } + /** + * Twig wrapper for PHP's preg_grep method + * + * @param $array + * @param $regex + * @param int $flags + * @return array + */ + public function regexFilter($array, $regex, $flags = 0) { + return preg_grep($regex, $array, $flags); + } + /** * redirect browser from twig * @@ -1088,6 +1104,43 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn var_dump($var); } + /** + * Returns a nicer more readable filesize based on bytes + * + * @param $bytes + * @return string + */ + public function niceFilesizeFunc($bytes) + { + if ($bytes >= 1073741824) + { + $bytes = number_format($bytes / 1073741824, 2) . ' GB'; + } + elseif ($bytes >= 1048576) + { + $bytes = number_format($bytes / 1048576, 2) . ' MB'; + } + elseif ($bytes >= 1024) + { + $bytes = number_format($bytes / 1024, 1) . ' KB'; + } + elseif ($bytes > 1) + { + $bytes = $bytes . ' bytes'; + } + elseif ($bytes == 1) + { + $bytes = $bytes . ' byte'; + } + else + { + $bytes = '0 bytes'; + } + + return $bytes; + } + + /** * Returns a nicer more readable number *