on the supplied string or array.
*
* Parameters:
* $subject - The input string.
* $pattern - The regular expression to match.
* $replacement - The replacement string.
* $limit - The maximum number of replacements.
*/
function twig_filter_preg_replace(
$subject,
$pattern,
$replacement,
$limit = -1
): ?string {
return preg_replace($pattern, $replacement, $subject, $limit);
}
/**
* Function: twig_filter_contains
* Does the haystack variable contain the needle variable?
*
* Parameters:
* $haystack - The variable to search within.
* $needle - The variable to search for.
*/
function twig_filter_contains(
$haystack,
$needle
): int|bool {
if (is_array($haystack))
return in_array($needle, $haystack);
if (is_string($haystack))
return substr_count($haystack, $needle);
if (is_object($haystack))
return in_array($needle, get_object_vars($haystack));
return false;
}
/**
* Function: twig_filter_inspect
* Exports a variable for inspection.
*
* Parameters:
* $variable - The variable to inspect.
*/
function twig_filter_inspect($variable): string {
return ''.
fix(var_export($variable, true)).
'
';
}
/**
* Function: twig_filter_checked
* Returns a HTML @checked@ attribute if the test evalutaes to true.
*
* Parameters:
* $test - The variable to test.
*/
function twig_filter_checked($test): string {
return ($test) ? " checked" : "" ;
}
/**
* Function: twig_filter_selected
* Returns a HTML @selected@ attribute if the
* test matches any of the supplied arguments.
*
* Parameters:
* $test - The variable to test.
*/
function twig_filter_selected($test): string {
$try = func_get_args();
array_shift($try);
foreach ($try as $value) {
if (
(is_array($value) and in_array($test, $value)) or
($test == $value)
)
return " selected";
}
return "";
}
/**
* Function: twig_filter_disabled
* Returns a HTML @disabled@ attribute if the
* test matches any of the supplied arguments.
*
* Parameters:
* $test - The variable to test.
*/
function twig_filter_disabled($test): string {
$try = func_get_args();
array_shift($try);
foreach ($try as $value) {
if (
(is_array($value) and in_array($test, $value)) or
($test == $value)
)
return " disabled";
}
return "";
}
/**
* Function: twig_filter_download
* Returns a download link for a file located in the uploads directory.
*
* Parameters:
* $filename - The uploaded filename.
*/
function twig_filter_download($filename): string {
$filepath = Config::current()->chyrp_url.
"/includes/download.php?file=".
urlencode($filename);
return fix($filepath, true);
}
/**
* Function: twig_filter_thumbnail
* Returns a thumbnail tag for an uploaded image file,
* optionally with sizes/srcset attributes and enclosing tag.
*
* Parameters:
* $filename - The uploaded filename.
* $alt_text - The alternative text for the image.
* $url - A URL, @true@ to link to the uploaded file, @false@ to disable.
* $args - An array of additional arguments to be appended as GET parameters.
* $sizes - A string, @true@ to use "100vw", @false@ to disable sizes/srcset.
* $lazy - Specify lazy-loading for this image?
*/
function twig_filter_thumbnail(
$filename,
$alt_text = "",
$url = null,
$args = array(),
$sizes = null,
$lazy = true
): string {
$filepath = Config::current()->chyrp_url.
"/includes/thumbnail.php?file=".
urlencode($filename);
$args_filtered = array();
foreach ($args as $arg) {
if (strpos($arg, "max_width=") === 0)
continue;
if (strpos($arg, "max_height=") === 0)
continue;
$args_filtered[] = $arg;
}
$src_args = implode("&", $args);
$set_args = implode("&", $args_filtered);
if (!empty($src_args))
$src_args = "&".$src_args;
if (!empty($set_args))
$set_args = "&".$set_args;
# Source set for responsive images.
$srcset = array(
$filepath."&max_width=2160".$set_args." 2160w",
$filepath."&max_width=1440".$set_args." 1440w",
$filepath."&max_width=1080".$set_args." 1080w",
$filepath."&max_width=720".$set_args." 720w",
$filepath."&max_width=360".$set_args." 360w",
$filepath."&max_width=180".$set_args." 180w"
);
$img = '';
# Enclose in tag? Provide @true@ or a candidate URL.
if (isset($url) and $url !== false) {
$url = (is_url($url)) ?
fix($url, true) :
uploaded($filename) ;
$img = ''.$img.'';
}
return $img;
}