Skip to content

Commit

Permalink
Migrate to Smarty 5, add support for modern image formats (webp, avif…
Browse files Browse the repository at this point in the history
…, ...)
  • Loading branch information
iherwig committed Jun 14, 2024
1 parent 5bf90a2 commit f7904dc
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 138 deletions.
7 changes: 3 additions & 4 deletions src/wcmf/application/controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
*/
namespace wcmf\application\controller;

use wcmf\lib\config\ConfigurationException;
use wcmf\lib\io\FileUtil;
use wcmf\lib\presentation\Controller;
use wcmf\lib\util\URIUtil;

if (!class_exists('\elFinder')) {
throw new ConfigurationException(
'wcmf\application\controller\MediaController requires '.
'elFinder. If you are using composer, add studio-42/elfinder '.
throw new \wcmf\lib\config\ConfigurationException(
'\wcmf\application\controller\MediaController requires '.
'\elFinder. If you are using composer, add studio-42/elfinder '.
'as dependency to your project');
}

Expand Down
9 changes: 4 additions & 5 deletions src/wcmf/application/views/plugins/block.assetic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* See the LICENSE file distributed with this work for
* additional information.
*/
use wcmf\lib\config\ConfigurationException;
use wcmf\lib\core\ObjectFactory;
use wcmf\lib\io\FileUtil;
use wcmf\lib\util\StringUtil;
Expand All @@ -24,8 +23,8 @@
use Assetic\Filter\ScssphpFilter;
use Minifier\MinFilter;

if (!class_exists('Assetic\Asset\AssetCollection')) {
throw new ConfigurationException(
if (!class_exists('\Assetic\Asset\AssetCollection')) {
throw new \wcmf\lib\config\ConfigurationException(
'smarty_block_assetic requires '.
'Assetic. If you are using composer, add kriswallsmith/assetic '.
'as dependency to your project');
Expand Down Expand Up @@ -55,11 +54,11 @@
* - scssImportPaths: Array of paths to add to the import paths of the scss compiler (relative to WCMF_BASE)
* - debug: Boolean, if true the content will be returned as is
* @param string $content
* @param Smarty_Internal_Template $template Smarty_Internal_Template
* @param \Smarty\Template $template \Smarty\Template
* @param bool $repeat
* @return string
*/
function smarty_block_assetic($params, $content, Smarty_Internal_Template $template, &$repeat) {
function smarty_block_assetic($params, $content, \Smarty\Template $template, &$repeat) {
if (!$repeat) {
if (isset($content)) {
$debug = $params['debug'];
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/block.if_authorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
* - action: The action to process
* - alternative_content: The content to display if not authorized
* @param $content
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @param $repeat
* @return String
*/
function smarty_block_if_authorized($params, $content, \Smarty_Internal_Template $template, &$repeat) {
function smarty_block_if_authorized($params, $content, \Smarty\Template $template, &$repeat) {
if(!$repeat) {
$permissionManager = ObjectFactory::getInstance('permissionManager');
if ($permissionManager->authorize($params['resource'], $params['context'], $params['action'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/function.configvalue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* @param $params Array with keys:
* - key: The key in the configuration section
* - section: The name of the configuration section
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_configvalue(array $params, Smarty_Internal_Template $template) {
function smarty_function_configvalue(array $params, \Smarty\Template $template) {
$config = ObjectFactory::getInstance('configuration');
echo $config->getValue($params['key'], $params['section']);
}
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/function.daterange.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
* - formattype: Format type used in formats parameter (optional, 'strftime'|'date'|'auto', default: 'auto')
* - displaytime: Also take time into account (optional, default: false)
* @note The automatic decision for a format type is based on the existance of a % char in the formats parameter.
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_daterange(array $params, Smarty_Internal_Template $template) {
function smarty_function_daterange(array $params, \Smarty\Template $template) {
$hasFrom = strlen($params['from']) > 0;
$hasTo = strlen($params['to']) > 0;
if (!$hasFrom && !$hasTo) {
Expand Down
11 changes: 6 additions & 5 deletions src/wcmf/application/views/plugins/function.image.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,26 @@
* - widths: Comma separated, sorted list of width values to be used in the srcset attribute
* - type: Indicates how width values should be used (optional, default: w)
* - w: Values will be used as pixels, e.g. widths="1600,960" results in srcset="... 1600w, ... 960w"
* - x: Values will be used as pixel ration, e.g. widths="1600,960" results in srcset="... 2x, ... 1x"
* - x: Values will be used as pixel ratio, e.g. widths="1600,960" results in srcset="... 2x, ... 1x"
* - sizes: Media queries to define image size in relation of the viewport (optional)
* - formats: Associative array of with format names ('jpeg', 'webp', 'png', 'gif', 'avif', 'jpeg2000') as keys and quality values as values (optional)
* - useDataAttributes: Boolean indicating whether to replace src, srcset, sizes by data-src, data-srcset, data-sizes (optional, default: __false__)
* - alt: Alternative text (optional)
* - class: Image class (optional)
* - title: Image title (optional
* - data: Associative array of key/value pairs to be used as data attributes
* - width: Width in pixels to output for the width attribute, the height attribute will be calculated according to the aspect ration (optional)
* - default: The default file, if src does not exist (optional)
* - generate: Boolean indicating whether to generate the images or not (optional, default: __false__)
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_image($params, Smarty_Internal_Template $template) {
function smarty_function_image($params, \Smarty\Template $template) {
$file = $params['src'];
$default = isset($params['default']) ? $params['default'] : '';
$widths = array_map('trim', isset($params['widths']) ? explode(',', $params['widths']) : []);
$type = isset($params['type']) ? $params['type'] : 'w';
$sizes = isset($params['sizes']) ? $params['sizes'] : '';
$formats = isset($params['formats']) && is_array($params['formats']) ? $params['formats'] : [];
$useDataAttributes = isset($params['useDataAttributes']) ? $params['useDataAttributes'] : false;
$generate = isset($params['generate']) ? $params['generate'] : false;
$alt = isset($params['alt']) ? $params['alt'] : '';
Expand All @@ -106,7 +107,7 @@ function smarty_function_image($params, Smarty_Internal_Template $template) {
$data = isset($params['data']) && is_array($params['data']) ? $params['data'] : [];
$width = isset($params['width']) ? $params['width'] : null;

return ImageUtil::getImageTag($file, $widths, $type, $sizes,
return ImageUtil::getImageTag($file, $widths, $type, $sizes, $formats,
$useDataAttributes, $alt, $class, $title, $data, $width, $default, $generate);
}
?>
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/function.localize.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* - node: The Node instance to extract the value from
* - attribute: The name of the attribute (will be appended with "_$lang")
* - lang: Language of the attribute
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_localize(array $params, Smarty_Internal_Template $template) {
function smarty_function_localize(array $params, \Smarty\Template $template) {
$language = $params['lang'];
$node = $params['node'];
$attribute = $params['attribute'];
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/function.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
* - name: The name of the module that must exist as a key in the configuration section named 'ContentModule'
* + additional module specific parameters
* NOTE: All variables from the including template are passed to the module template.
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_module($params, Smarty_Internal_Template $template) {
function smarty_function_module($params, \Smarty\Template $template) {
$requiredInterface = 'wcmf\lib\presentation\ContentModule';
$config = ObjectFactory::getInstance('configuration');
$modules = $config->getSection('ContentModule');
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/function.translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
* - text: The text to translate
* - r0, r1, ...: Values for text variables (%0%, %1%, ...)
* - lang: The language to translate to
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_function_translate($params, Smarty_Internal_Template $template) {
function smarty_function_translate($params, \Smarty\Template $template) {
$variables = [];
foreach (array_keys($params) as $key) {
if (preg_match("/^r[0-9]+$/", $key)) {
Expand Down
10 changes: 7 additions & 3 deletions src/wcmf/application/views/plugins/modifier.image_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
*/

/**
* Output the cache location of the given image and width
* Output the cache location of the given image and width, type and quality
*
* Example:
* @code
* <img src={$image|image_cache:800}">
* <img src={$image|image_cache:800:'webp'}">
* <img src={$image|image_cache:800:'webp':85}">
* @endcode
*
* @param $image The path to the image
* @param $width
* @param $type
* @param $quality
* @return String
*/
use wcmf\lib\io\ImageUtil;

function smarty_modifier_image_cache($image, $width) {
return ImageUtil::getCacheLocation($image, $width);
function smarty_modifier_image_cache($image, $width, $type=null, $quality=null) {
return ImageUtil::getCacheLocation($image, $width, $type, $quality);
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* Remove closing slashes from void tags according to html5.
*
* @param $output
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_outputfilter_html5_void_tags($output, Smarty_Internal_Template $template) {
function smarty_outputfilter_html5_void_tags($output, \Smarty\Template $template) {
// remove slashes from image tags
$output = preg_replace('/<img([^>]+) ?\/\>/i', "<img $1>", $output);
// replace brs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* @see https://stackoverflow.com/questions/12592363/looking-for-a-php-only-email-address-obfuscator-function#12592364
*
* @param $output
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_outputfilter_obfuscate_email($output, Smarty_Internal_Template $template) {
function smarty_outputfilter_obfuscate_email($output, \Smarty\Template $template) {
$encodePercentage = 50;

// encoding functions
Expand Down
4 changes: 2 additions & 2 deletions src/wcmf/application/views/plugins/prefilter.removeprids.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* Remove protected region ids (used by wCMFGenerator).
*
* @param $output
* @param $template Smarty_Internal_Template
* @param $template \Smarty\Template
* @return String
*/
function smarty_prefilter_removeprids($output, Smarty_Internal_Template $template) {
function smarty_prefilter_removeprids($output, \Smarty\Template $template) {
// remove protected regions
$output = preg_replace("/<!-- PROTECTED REGION .*? -->/U", "", $output);

Expand Down
Loading

0 comments on commit f7904dc

Please sign in to comment.