diff --git a/Makefile b/Makefile index e3fb79b025f873b5addf02fab9b2845f6ac74886..325d99248f4b51f15b49e07db06096a5fa721e89 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ BASE=shipping-by-rules PLATTFORM=woocommerce VENDOR=opentools -VERSION=1.2.5 +VERSION=1.2.6 DIR = $(shell pwd) SVNDIR=wordpress-plugin-svn diff --git a/Screenshots/Opentools_Woocommerce_ShippingRules_Messages.png b/Screenshots/Opentools_Woocommerce_ShippingRules_Messages.png new file mode 100644 index 0000000000000000000000000000000000000000..c6cbf63a1e82e0a87aa404c2a88cd177008a53ba Binary files /dev/null and b/Screenshots/Opentools_Woocommerce_ShippingRules_Messages.png differ diff --git a/includes/rules_shipping_framework_woocommerce.php b/includes/rules_shipping_framework_woocommerce.php index b400a92a32c6b1d316cc68f73259d7d3aa1829d0..2f4f4d3078f7df90fe91ab0aefa1503590b0982e 100644 --- a/includes/rules_shipping_framework_woocommerce.php +++ b/includes/rules_shipping_framework_woocommerce.php @@ -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; } } diff --git a/library/rules_shipping_framework.php b/library/rules_shipping_framework.php index 0c5123cb64119fb01fd43121a8ecb71013cf63bb..fce5baf695d10bfffbc6326e8c904401f8d60cbe 100644 --- a/library/rules_shipping_framework.php +++ b/library/rules_shipping_framework.php @@ -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; } diff --git a/readme-adv.txt b/readme-adv.txt index abf495bc83d80f4478408329981c9852bce066ee..dd4ad2617e9412d31f410506e506c2b319961fde 100644 --- a/readme-adv.txt +++ b/readme-adv.txt @@ -3,7 +3,7 @@ Contributors: opentools Tags: WooCommerce, Shipment, Shipping Requires at least: 4.0 Tested up to: 4.5 -Stable tag: 1.2.5 +Stable tag: 1.2.6 License: GPLv3 or later License URI: http://www.gnu.org/licenses/gpl.html diff --git a/readme.txt b/readme.txt index 97b12da2c1747903d12f277b1818c0c4f7861454..81fdda99d2b07b25fa8d952e0ff46e5f772e6395 100644 --- a/readme.txt +++ b/readme.txt @@ -2,8 +2,8 @@ Contributors: opentools Tags: WooCommerce, Shipment, Shipping, Rules shipping Requires at least: 4.0 -Tested up to: 4.5 -Stable tag: 1.2.5 +Tested up to: 4.7 +Stable tag: 1.2.6 License: GPLv3 or later License URI: http://www.gnu.org/licenses/gpl.html @@ -69,6 +69,9 @@ Please see our support forum at http://open-tools.net/forum/. It might also be a == Changelog == += 1.2.6 = +* Add message functionality (Error=..., Warning=..., Message=... rule parts) + = 1.2.5 = * Add variables username, first_name, last_name, email * Add list variable userroles (advanced version only) diff --git a/releases/opentools-woocommerce-advanced-shipping-by-rules_v1.2.6.zip b/releases/opentools-woocommerce-advanced-shipping-by-rules_v1.2.6.zip new file mode 100644 index 0000000000000000000000000000000000000000..5b341f22b64204698bfb434664b5cf249e0a0261 Binary files /dev/null and b/releases/opentools-woocommerce-advanced-shipping-by-rules_v1.2.6.zip differ diff --git a/releases/opentools-woocommerce-shipping-by-rules_v1.2.6.zip b/releases/opentools-woocommerce-shipping-by-rules_v1.2.6.zip new file mode 100644 index 0000000000000000000000000000000000000000..7c8167f73381feff2c6f91bc9a175c48810dee35 Binary files /dev/null and b/releases/opentools-woocommerce-shipping-by-rules_v1.2.6.zip differ diff --git a/woocommerce-advanced-shipping-by-rules.php b/woocommerce-advanced-shipping-by-rules.php index 8378fb7cfd99919bf5c504691ca9309851719a05..0c40d67b030ea2a09e1ed6d5d7541d01814e209c 100644 --- a/woocommerce-advanced-shipping-by-rules.php +++ b/woocommerce-advanced-shipping-by-rules.php @@ -3,14 +3,14 @@ * Plugin Name: WooCommerce Advanced Shipping By Rules * Plugin URI: http://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html * Description: Define Shipping cost by very general and flexible (text-based) rules. The advanced version also provides mathematical expressions and functions - * Version: 1.2.5 + * Version: 1.2.6 * Author: Open Tools, Reinhold Kainhofer * Author URI: http://open-tools.net * Text Domain: woocommerce-advanced-shipping-by-rules * Domain Path: woocommerce-shipping-by-rules * License: GPL2+ * WC requires at least: 2.2 - * WC tested up to: 2.6 + * WC tested up to: 2.7 * Copyright (C) 2015 Reinhold Kainhofer @@ -75,7 +75,7 @@ class WooCommerce_Shipping_By_Rules_Advanced { * @since 1.0.0 * @var string $version Plugin version number. */ - public $version = '1.2.5'; + public $version = '1.2.6'; /** diff --git a/woocommerce-shipping-by-rules.php b/woocommerce-shipping-by-rules.php index 56d2b0bbad35bc5f443ae0df02cda7e7c9b02bc4..99818abae8aadcbc0effa4521c86a9f28ee62b9c 100644 --- a/woocommerce-shipping-by-rules.php +++ b/woocommerce-shipping-by-rules.php @@ -3,14 +3,14 @@ * Plugin Name: WooCommerce Shipping By Rules * Plugin URI: http://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html * Description: Define Shipping cost by very general and flexible (text-based) rules. - * Version: 1.2.5 + * Version: 1.2.6 * Author: Open Tools, Reinhold Kainhofer * Author URI: http://open-tools.net * Text Domain: woocommerce-shipping-by-rules * Domain Path: * License: GPL2+ * WC requires at least: 2.2 - * WC tested up to: 2.6 + * WC tested up to: 2.7 * Copyright (C) 2015 Reinhold Kainhofer @@ -48,7 +48,7 @@ class WooCommerce_Shipping_By_Rules { * @since 1.0.0 * @var string $version Plugin version number. */ - public $version = '1.2.5'; + public $version = '1.2.6'; /**