From c7a0983f75eea8f61cd1c1a64b0e1735d0e464e5 Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer <reinhold@kainhofer.com> Date: Sun, 7 Feb 2016 00:15:59 +0100 Subject: [PATCH] Fix custom function definitions and their calls in the plugin --- library/rules_shipping_framework.php | 33 +++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/library/rules_shipping_framework.php b/library/rules_shipping_framework.php index 942edc5..aa0ebb4 100644 --- a/library/rules_shipping_framework.php +++ b/library/rules_shipping_framework.php @@ -167,6 +167,10 @@ class RulesShippingFramework { return array (); } + function getCustomFunctionDefinitions() { + return $this->custom_functions; + } + /** @tag system-specific * @function printWarning() * Print a warning in the system-specific way. @@ -203,22 +207,14 @@ class RulesShippingFramework { */ public function setup() { $custfuncdefs = $this->getCustomFunctions(); - // Loop through the return values of all plugins: - foreach ($custfuncdefs as $custfuncs) { - if (empty($custfuncs)) - continue; - if (!is_array($custfuncs)) { - $this->warning('OTSHIPMENT_RULES_CUSTOMFUNCTIONS_NOARRAY'); - } - // Now loop through all custom function definitions of this plugin - // If a function was registered before, print a warning and use the first definition - foreach ($custfuncs as $fname => $func) { - if (isset($this->custom_functions[$fname])) { - $this->warning('OTSHIPMENT_RULES_CUSTOMFUNCTIONS_ALREADY_DEFINED', $fname); - } else { - $this->debug("Defining custom function $fname"); - $this->custom_functions[strtolower($fname)] = $func; - } + // Now loop through all custom function definitions of this plugin + // If a function was registered before, print a warning and use the first definition + foreach ($custfuncdefs as $fname => $func) { + if (isset($this->custom_functions[$fname]) && $this->custom_functions[$fname]!=$custfuncs[$fname]) { + $this->warning('OTSHIPMENT_RULES_CUSTOMFUNCTIONS_ALREADY_DEFINED', $fname); + } else { + $this->debug("Defining custom function $fname"); + $this->custom_functions[strtolower($fname)] = $func; } } } @@ -831,9 +827,10 @@ class ShippingRule { $func = strtolower($function); // Check if we have a custom function definition and use that if so. // This is done first to allow plugins to override even built-in functions! - if (isset($this->plugin->custom_functions[$func])) { + $customfunctions = $this->framework->getCustomFunctionDefinitions(); + if (isset($customfunctions[$func])) { $this->framework->debug("Evaluating custom function $function, defined by a plugin"); - return call_user_func_array($this->plugin->custom_functions[$func], $args, $this); + return call_user_func($customfunctions[$func], $args, $this); } // Functions with no argument: -- GitLab