Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Sources/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,25 +507,34 @@ public static function loadSubTemplates(): void
public static function loadSubTemplate(string|array $sub_template_name, bool|string $fatal = false): void
{
$template_name = \is_array($sub_template_name) ? $sub_template_name[0] : $sub_template_name;
$function_params = \is_array($sub_template_name) ? ($sub_template_name[1] ?? []) : [];

// Add the sub-template to the debug context if debugging is enabled.
if (DebugUtils::isDebugEnabled()) {
DebugUtils::addDebugSource('sub_templates', $template_name);
}

// Determine the template function name and any associated parameters.
if (\is_array($sub_template_name)) {
$theme_function = 'template_' . $sub_template_name[0];
$function_params = $sub_template_name[1] ?? [];
} else {
$theme_function = 'template_' . $sub_template_name;
$function_params = [];
$template_loaded = false;

if (\is_string($template_name) && \str_contains($template_name, '::')) {
$template_name = \preg_replace_callback(
'/(#?)_(above|below)$/i',
fn($m) => \ucfirst($m[2]) . $m[1],
$template_name
);

if ($callable = Utils::getCallable($template_name, true)) {
\call_user_func_array($callable, $function_params);
$template_loaded = true;
}
}

// Attempt to call the sub-template function.
if (\is_callable($theme_function)) {
\call_user_func_array($theme_function, $function_params);
} else {
if (!$template_loaded && ($callable = Utils::getCallable('template_' . $template_name, true))) {
\call_user_func_array($callable, $function_params);
$template_loaded = true;
}

if (!$template_loaded) {
// Handle errors based on the $fatal parameter.
if ($fatal === false) {
ErrorHandler::fatalLang(
Expand Down
22 changes: 22 additions & 0 deletions Sources/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,28 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors
return $callable;
}

/**
* Universal template callback invoker following the logic of the "first file".
*
* @param string $name Callback name (without prefix)
* @param string $prefix Function name prefix (e.g. 'template_callback_' or 'template_profile_')
* @return mixed Result of the callback execution or null if not found
*/
public static function callTemplateCallback(string $name, string $prefix = 'template_callback_'): mixed
{
if (function_exists($prefix . $name)) {
$name = $prefix . $name;
}

$callable = Utils::getCallable($name);

if ($callable) {
return call_user_func($callable);
}

return null;
}

/*************************
* Internal static methods
*************************/
Expand Down
3 changes: 1 addition & 2 deletions Themes/default/Admin.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,7 @@ function ($v)
// Hang about? Are you pulling my leg - a callback?!
if (is_array($config_var) && $config_var['type'] == 'callback')
{
if (function_exists('template_callback_' . $config_var['name']))
call_user_func('template_callback_' . $config_var['name']);
Utils::callTemplateCallback($config_var['name']);

continue;
}
Expand Down
6 changes: 2 additions & 4 deletions Themes/default/Profile.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1505,10 +1505,8 @@ function template_edit_options()

elseif ($field['type'] == 'callback')
{
if (isset($field['callback_func']) && function_exists('template_profile_' . $field['callback_func']))
{
$callback_func = 'template_profile_' . $field['callback_func'];
$callback_func();
if (!empty($field['callback_func'])) {
Utils::callTemplateCallback($field['callback_func'], 'template_profile_');
}
}
else
Expand Down
6 changes: 2 additions & 4 deletions Themes/default/Register.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,8 @@ function verifyAgree()
{
if ($field['type'] == 'callback')
{
if (isset($field['callback_func']) && function_exists('template_profile_' . $field['callback_func']))
{
$callback_func = 'template_profile_' . $field['callback_func'];
$callback_func();
if (!empty($field['callback_func'])) {
Utils::callTemplateCallback($field['callback_func'], 'template_profile_');
}
}
else
Expand Down