Skip to content

Commit 6a06f82

Browse files
committed
Refactor code
Signed-off-by: Bugo <[email protected]>
1 parent dec6450 commit 6a06f82

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

Sources/Theme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public static function loadSubTemplate(string|array $sub_template_name, bool|str
536536
}
537537
}
538538

539-
if (! $template_loaded) {
539+
if (!$template_loaded) {
540540
$theme_function = 'template_' . $template_name;
541541

542542
$callable = Utils::getCallable($theme_function);
@@ -548,7 +548,7 @@ public static function loadSubTemplate(string|array $sub_template_name, bool|str
548548
}
549549
}
550550

551-
if (! $template_loaded) {
551+
if (!$template_loaded) {
552552
// Handle errors based on the $fatal parameter.
553553
if ($fatal === false) {
554554
ErrorHandler::fatalLang(

Sources/Utils.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,28 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors
24772477
return $callable;
24782478
}
24792479

2480+
/**
2481+
* Universal template callback invoker following the logic of the "first file".
2482+
*
2483+
* @param string $name Callback name (without prefix)
2484+
* @param string $prefix Function name prefix (e.g. 'template_callback_' or 'template_profile_')
2485+
* @return mixed Result of the callback execution or null if not found
2486+
*/
2487+
public static function callTemplateCallback(string $name, string $prefix = 'template_callback_'): mixed
2488+
{
2489+
if (function_exists($prefix . $name)) {
2490+
$name = $prefix . $name;
2491+
}
2492+
2493+
$callable = Utils::getCallable($name);
2494+
2495+
if ($callable) {
2496+
return call_user_func($callable);
2497+
}
2498+
2499+
return null;
2500+
}
2501+
24802502
/*************************
24812503
* Internal static methods
24822504
*************************/

Themes/default/Admin.template.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,7 @@ function ($v)
872872
// Hang about? Are you pulling my leg - a callback?!
873873
if (is_array($config_var) && $config_var['type'] == 'callback')
874874
{
875-
$callbackName = $config_var['name'];
876-
if (function_exists('template_callback_' . $callbackName)) {
877-
$callbackName = 'template_callback_' . $callbackName;
878-
}
879-
880-
$callable = Utils::getCallable($callbackName);
881-
$callable && call_user_func($callable);
875+
Utils::callTemplateCallback($config_var['name']);
882876

883877
continue;
884878
}

Themes/default/Profile.template.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,10 +1505,8 @@ function template_edit_options()
15051505

15061506
elseif ($field['type'] == 'callback')
15071507
{
1508-
if (isset($field['callback_func']) && function_exists('template_profile_' . $field['callback_func']))
1509-
{
1510-
$callback_func = 'template_profile_' . $field['callback_func'];
1511-
$callback_func();
1508+
if (!empty($field['callback_func'])) {
1509+
Utils::callTemplateCallback($field['callback_func'], 'template_profile_');
15121510
}
15131511
}
15141512
else

Themes/default/Register.template.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,8 @@ function verifyAgree()
198198
{
199199
if ($field['type'] == 'callback')
200200
{
201-
if (isset($field['callback_func']) && function_exists('template_profile_' . $field['callback_func']))
202-
{
203-
$callback_func = 'template_profile_' . $field['callback_func'];
204-
$callback_func();
201+
if (!empty($field['callback_func'])) {
202+
Utils::callTemplateCallback($field['callback_func'], 'template_profile_');
205203
}
206204
}
207205
else

0 commit comments

Comments
 (0)