Skip to content
Snippets Groups Projects
Commit 4aec114e authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Implement message in the rules

parent b3ae23e5
No related branches found
No related tags found
No related merge requests found
......@@ -49,29 +49,31 @@ class RulesShippingFrameworkWooCommerce extends RulesShippingFramework {
return apply_filters( 'opentools_shipping_by_rules_replacements', array());
}
public function printWarning($message) {
public function printMessage($message, $type) {
// Keep track of warning messages, so we don't print them twice:
global $printed_warnings;
if (!isset($printed_warnings))
$printed_warnings = array();
if (!in_array($message, $printed_warnings)) {
wc_add_notice( $message, 'error');
$printed_warnings[] = $message;
}
}
/** @tag public-api
* @function debug()
* Print a debug message (untranslated) in the system-specific way.
* @param $message the debug message to be printed
*/
public function debug($message) {
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
global $printed_messages;
if (!isset($printed_messages))
$printed_messages = array();
if ($type == "debug") {
if ( true === WP_DEBUG ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
} elseif (!in_array($message, $printed_messages)) {
switch ($type) {
case 'error':
case 'warning':
wc_add_notice( $message, 'error'); break;
case 'message':
wc_add_notice( $message, 'success'); break;
case 'notice':
default:
wc_add_notice( $message, 'notice'); break;
}
$printed_messages[] = $message;
}
}
......
......@@ -171,13 +171,33 @@ class RulesShippingFramework {
return $this->custom_functions;
}
/** @tag system-specific
* @function printWarning()
* Print a warning in the system-specific way.
* @param $message the warning message to be printed (already properly translated)
/** @tag public-api
* @tag system-specific
* @function message()
* Print a message (to be translated) with given type in the system-specific way.
* @param $message the message to be printed
* @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
protected function printWarning($message) {
echo($message);
public function message($message, $type) {
$args = func_get_args();
// Remove the $type from the args passed to __
unset($args[1]);
$msg = call_user_func_array(array($this, "__"), $args);
$this->printMessage($msg, $type);
}
/** @tag public-api
* @tag system-specific
* @function error()
* Print an error message (to be translated) in the system-specific way.
* @param $message the error message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public function error($message) {
$args = func_get_args();
array_splice($args, 1, 0, 'error'); // insert msg type in second position
call_user_func_array(array($this, "message"), $args);
}
/** @tag public-api
......@@ -189,16 +209,44 @@ class RulesShippingFramework {
*/
public function warning($message) {
$args = func_get_args();
$msg = call_user_func_array(array($this, "__"), $args);
$this->printWarning($msg);
array_splice($args, 1, 0, 'warning'); // insert msg type in second position
call_user_func_array(array($this, "message"), $args);
}
/** @tag public-api
* @tag system-specific
* @function notice()
* Print a message (to be translated) in the system-specific way.
* @param $message the message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public function notice($message) {
$args = func_get_args();
array_splice($args, 1, 0, 'notice'); // insert msg type in second position
call_user_func_array(array($this, "message"), $args);
}
/** @tag public-api
* @tag system-specific
* @function debug()
* Print a debug message (untranslated) in the system-specific way.
* @param $message the debug message to be printed
* Print a debug message in the system-specific way.
* @param $message the message to be printed
* @param $args optional arguments to be inserted into the translated message in sprintf-style
*/
public function debug($message) {
$args = func_get_args();
array_splice($args, 1, 0, 'debug'); // insert msg type in second position
call_user_func_array(array($this, "message"), $args);
}
/** @tag system-specific
* @function printMessage()
* Print a message of given type in the system-specific way.
* @param $message the message to be printed (already properly translated)
* @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
*/
protected function printMessage($message, $type) {
echo($message);
}
/** @tag public-api
......@@ -416,6 +464,12 @@ class RulesShippingFramework {
$this->warning('OTSHIPMENT_RULES_UNKNOWN_TYPE', $r->getType(), $r->rulestring);
break;
}
// Handle messages (error, warning, message/notice, debug:
foreach ($r->messages as $k=>$msgs) {
foreach ($msgs as $msg) {
$this->message($msg, $k);
}
}
}
if (!is_null($result["rule"])) {
$this->match[$id] = $result;
......@@ -593,6 +647,7 @@ class ShippingRule {
var $countries = array();
var $ruleinfo = 0;
var $includes_tax = 0;
var $messages = array('error' => array(), 'warning' => array(), 'notice' => array(), 'debug' => array());
function __construct ($framework, $rule, $countries, $ruleinfo) {
$this->framework = $framework;
......@@ -624,6 +679,11 @@ class ShippingRule {
case 'extrashippingcharge': $this->shipping = $value; $this->ruletype = 'modifiers_add'; break; // modifiers are also stored in the shipping member!
case 'extrashippingmultiplier': $this->shipping = $value; $this->ruletype = 'modifiers_multiply'; break; // modifiers are also stored in the shipping member!
case 'comment': break; // Completely ignore all comments!
case 'error': $this->messages['error'][] = $value; break;
case 'warning': $this->messages['warning'][] = $value; break;
case 'notice':
case 'message': $this->messages['notice'][] = $value; break;
case 'debug': $this->messages['debug'][] = $value; break;
case 'condition': $this->conditions[] = $value; break;
default: $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart);
}
......@@ -646,9 +706,8 @@ class ShippingRule {
$rulepart = trim($rulepart);
if (!isset($rulepart) || $rulepart==='') return;
// Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
$this->handleAssignment ($matches[1], $matches[3], $rulepart);
return;
}
......@@ -1027,9 +1086,25 @@ class ShippingRule {
}
}
protected function calculateShipping ($vals, $products, $cartvals_callback) {
protected function calculateShipping($vals, $products, $cartvals_callback) {
return $this->evaluateTerm($this->shipping, $vals, $products, $cartvals_callback);
}
protected function stringReplaceVariables($str, $vals) {
// Evaluate the rule name as a translatable string with variables inserted:
// Replace all {variable} tags in the name by the variables from $vals
$matches = array();
preg_match_all('/{([A-Za-z0-9_]+)}/', $str, $matches);
foreach ($matches[1] as $m) {
$val = $this->evaluateVariable($m, $vals);
if ($val !== null) {
$str = str_replace("{".$m."}", $val, $str);
}
}
return $str;
}
protected function evaluateRule (&$vals, $products, $cartvals_callback) {
if ($this->evaluated)
......@@ -1053,21 +1128,15 @@ class ShippingRule {
}
// All conditions match
$this->match = True;
foreach ($this->messages as $k=>$msgs) {
foreach ($msgs as $i=>$m) {
$this->messages[$k][$i] = $this->stringReplaceVariables($m, $vals);
}
}
// Calculate the value (i.e. shipping cost or modifier)
$this->value = $this->calculateShipping($vals, $products, $cartvals_callback);
// Evaluate the rule name as a translatable string with variables inserted:
// Replace all {variable} tags in the name by the variables from $vals
$matches = array();
$name = $this->framework->__($this->name);
preg_match_all('/{([A-Za-z0-9_]+)}/', $name, $matches);
foreach ($matches[1] as $m) {
$val = $this->evaluateVariable($m, $vals);
if ($val !== null) {
$name = str_replace("{".$m."}", $val, $name);
}
}
$this->rulename = $name;
$this->rulename = $this->stringReplaceVariables($this->framework->__($this->name), $vals);
}
function matches(&$vals, $products, $cartvals_callback) {
......@@ -1152,7 +1221,7 @@ class ShippingRule_Advanced extends ShippingRule {
// Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
$this->handleAssignment ($matches[1], $matches[3], $rulepart);
return;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment