Plugin Directory

Changeset 1549928


Ignore:
Timestamp:
12/08/2016 10:30:22 PM (9 years ago)
Author:
opentools
Message:

V1.2.6: Implement messages in the rules

Rules can now also contain rule parts Error=..., Warning=..., Message=..., Notice=...
that will be shown to the customer as messages of corresponding typ (red for
error/warning and blue for most other messages). These messages will only be
shown for those rules that actually match the cart.

Location:
shipping-by-rules-for-woocommerce
Files:
4 edited
13 copied

Legend:

Unmodified
Added
Removed
  • shipping-by-rules-for-woocommerce/tags/1.2.6/includes/rules_shipping_framework_woocommerce.php

    r1485122 r1549928  
    5050    }
    5151   
    52     public function printWarning($message) {
     52    public function printMessage($message, $type) {
    5353        // Keep track of warning messages, so we don't print them twice:
    54         global $printed_warnings;
    55         if (!isset($printed_warnings))
    56             $printed_warnings = array();
    57         if (!in_array($message, $printed_warnings)) {
    58             wc_add_notice( $message, 'error');
    59             $printed_warnings[] = $message;
    60         }
    61     }
    62 
    63     /** @tag public-api
    64      *  @function debug()
    65      *    Print a debug message (untranslated) in the system-specific way.
    66      *  @param $message the debug message to be printed
    67      */
    68     public function debug($message) {
    69         if ( true === WP_DEBUG ) {
    70             if ( is_array( $message ) || is_object( $message ) ) {
    71                 error_log( print_r( $message, true ) );
    72             } else {
    73                 error_log( $message );
    74             }
     54        global $printed_messages;
     55        if (!isset($printed_messages))
     56            $printed_messages = array();
     57        if ($type == "debug") {
     58            if ( true === WP_DEBUG ) {
     59                if ( is_array( $message ) || is_object( $message ) ) {
     60                    error_log( print_r( $message, true ) );
     61                } else {
     62                    error_log( $message );
     63                }
     64            }
     65        } elseif (!in_array($message, $printed_messages)) {
     66            switch ($type) {
     67                case 'error':
     68                case 'warning':
     69                    wc_add_notice( $message, 'error'); break;
     70                case 'message':
     71                    wc_add_notice( $message, 'success'); break;
     72                case 'notice':
     73                default:
     74                    wc_add_notice( $message, 'notice'); break;
     75            }
     76            $printed_messages[] = $message;
    7577        }
    7678    }
  • shipping-by-rules-for-woocommerce/tags/1.2.6/library/rules_shipping_framework.php

    r1485122 r1549928  
    172172    }
    173173   
    174     /** @tag system-specific
    175      *  @function printWarning()
    176      *    Print a warning in the system-specific way.
    177      *  @param $message the warning message to be printed (already properly translated)
    178      */
    179     protected function printWarning($message) {
    180         echo($message);
     174    /** @tag public-api
     175     *  @tag system-specific
     176     *  @function message()
     177     *    Print a message (to be translated) with given type in the system-specific way.
     178     *  @param $message the message to be printed
     179     *  @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
     180     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     181     */
     182    public function message($message, $type) {
     183        $args = func_get_args();
     184        // Remove the $type from the args passed to __
     185        unset($args[1]);
     186        $msg = call_user_func_array(array($this, "__"), $args);
     187        $this->printMessage($msg, $type);
     188    }
     189   
     190    /** @tag public-api
     191     *  @tag system-specific
     192     *  @function error()
     193     *    Print an error message (to be translated) in the system-specific way.
     194     *  @param $message the error message to be printed
     195     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     196     */
     197    public function error($message) {
     198        $args = func_get_args();
     199        array_splice($args, 1, 0, 'error'); // insert msg type in second position
     200        call_user_func_array(array($this, "message"), $args);
    181201    }
    182202   
     
    190210    public function warning($message) {
    191211        $args = func_get_args();
    192         $msg = call_user_func_array(array($this, "__"), $args);
    193         $this->printWarning($msg);
     212        array_splice($args, 1, 0, 'warning'); // insert msg type in second position
     213        call_user_func_array(array($this, "message"), $args);
    194214    }
    195215   
    196216    /** @tag public-api
     217     *  @tag system-specific
     218     *  @function notice()
     219     *    Print a message (to be translated) in the system-specific way.
     220     *  @param $message the message to be printed
     221     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     222     */
     223    public function notice($message) {
     224        $args = func_get_args();
     225        array_splice($args, 1, 0, 'notice'); // insert msg type in second position
     226        call_user_func_array(array($this, "message"), $args);
     227    }
     228   
     229    /** @tag public-api
     230     *  @tag system-specific
    197231     *  @function debug()
    198      *    Print a debug message (untranslated) in the system-specific way.
    199      *  @param $message the debug message to be printed
     232     *    Print a debug message in the system-specific way.
     233     *  @param $message the message to be printed
     234     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
    200235     */
    201236    public function debug($message) {
     237        $args = func_get_args();
     238        array_splice($args, 1, 0, 'debug'); // insert msg type in second position
     239        call_user_func_array(array($this, "message"), $args);
     240    }
     241   
     242    /** @tag system-specific
     243     *  @function printMessage()
     244     *    Print a message of given type in the system-specific way.
     245     *  @param $message the message to be printed (already properly translated)
     246     *  @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
     247     */
     248    protected function printMessage($message, $type) {
     249        echo($message);
    202250    }
    203251   
     
    417465                                    break;
    418466                        }
     467                        // Handle messages (error, warning, message/notice, debug:
     468                        foreach ($r->messages as $k=>$msgs) {
     469                            foreach ($msgs as $msg) {
     470                                $this->message($msg, $k);
     471                            }
     472                        }
    419473                    }
    420474                    if (!is_null($result["rule"])) {
     
    594648    var $ruleinfo = 0;
    595649    var $includes_tax = 0;
     650    var $messages = array('error' => array(), 'warning' => array(), 'notice' => array(), 'debug' => array());
    596651   
    597652    function __construct ($framework, $rule, $countries, $ruleinfo) {
     
    625680            case 'extrashippingmultiplier': $this->shipping = $value; $this->ruletype = 'modifiers_multiply'; break; // modifiers are also stored in the shipping member!
    626681            case 'comment':         break; // Completely ignore all comments!
     682            case 'error':           $this->messages['error'][] = $value; break;
     683            case 'warning':         $this->messages['warning'][] = $value; break;
     684            case 'notice':
     685            case 'message':         $this->messages['notice'][] = $value; break;
     686            case 'debug':           $this->messages['debug'][] = $value; break;
    627687            case 'condition':       $this->conditions[] = $value; break;
    628688            default:                $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart);
     
    647707        if (!isset($rulepart) || $rulepart==='') return;
    648708
    649        
    650709        // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
    651         if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
     710        if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
    652711            $this->handleAssignment ($matches[1], $matches[3], $rulepart);
    653712            return;
     
    10281087    }
    10291088
    1030     protected function calculateShipping ($vals, $products, $cartvals_callback) {
     1089    protected function calculateShipping($vals, $products, $cartvals_callback) {
    10311090        return $this->evaluateTerm($this->shipping, $vals, $products, $cartvals_callback);
     1091    }
     1092   
     1093    protected function stringReplaceVariables($str, $vals) {
     1094        // Evaluate the rule name as a translatable string with variables inserted:
     1095        // Replace all {variable} tags in the name by the variables from $vals
     1096        $matches = array();
     1097        preg_match_all('/{([A-Za-z0-9_]+)}/', $str, $matches);
     1098       
     1099        foreach ($matches[1] as $m) {
     1100            $val = $this->evaluateVariable($m, $vals);
     1101            if ($val !== null) {
     1102                $str = str_replace("{".$m."}", $val, $str);
     1103            }
     1104        }
     1105        return $str;
     1106   
    10321107    }
    10331108
     
    10541129        // All conditions match
    10551130        $this->match = True;
     1131        foreach ($this->messages as $k=>$msgs) {
     1132            foreach ($msgs as $i=>$m) {
     1133                $this->messages[$k][$i] = $this->stringReplaceVariables($m, $vals);
     1134            }
     1135        }
    10561136        // Calculate the value (i.e. shipping cost or modifier)
    10571137        $this->value = $this->calculateShipping($vals, $products, $cartvals_callback);
    1058         // Evaluate the rule name as a translatable string with variables inserted:
    1059         // Replace all {variable} tags in the name by the variables from $vals
    1060         $matches = array();
    1061         $name = $this->framework->__($this->name);
    1062         preg_match_all('/{([A-Za-z0-9_]+)}/', $name, $matches);
    10631138       
    1064         foreach ($matches[1] as $m) {
    1065             $val = $this->evaluateVariable($m, $vals);
    1066             if ($val !== null) {
    1067                 $name = str_replace("{".$m."}", $val, $name);
    1068             }
    1069         }
    1070         $this->rulename = $name;
     1139        $this->rulename = $this->stringReplaceVariables($this->framework->__($this->name), $vals);
    10711140    }
    10721141
     
    11531222       
    11541223        // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
    1155         if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
     1224        if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
    11561225            $this->handleAssignment ($matches[1], $matches[3], $rulepart);
    11571226            return;
  • shipping-by-rules-for-woocommerce/tags/1.2.6/readme.txt

    r1485122 r1549928  
    33Tags: WooCommerce, Shipment, Shipping, Rules shipping
    44Requires at least: 4.0
    5 Tested up to: 4.5
    6 Stable tag: 1.2.5
     5Tested up to: 4.7
     6Stable tag: 1.2.6
    77License: GPLv3 or later
    88License URI: http://www.gnu.org/licenses/gpl.html
     
    7070== Changelog ==
    7171
     72= 1.2.6 =
     73* Add message functionality (Error=..., Warning=..., Message=... rule parts)
     74
    7275= 1.2.5 =
    7376* Add variables username, first_name, last_name, email
  • shipping-by-rules-for-woocommerce/tags/1.2.6/woocommerce-shipping-by-rules.php

    r1485122 r1549928  
    44 * Plugin URI: http://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html
    55 * Description: Define Shipping cost by very general and flexible (text-based) rules.
    6  * Version: 1.2.5
     6 * Version: 1.2.6
    77 * Author: Open Tools, Reinhold Kainhofer
    88 * Author URI: http://open-tools.net
     
    1111 * License: GPL2+
    1212 * WC requires at least: 2.2
    13  * WC tested up to: 2.6
     13 * WC tested up to: 2.7
    1414 
    1515
     
    4949     * @var string $version Plugin version number.
    5050     */
    51     public $version = '1.2.5';
     51    public $version = '1.2.6';
    5252
    5353
  • shipping-by-rules-for-woocommerce/trunk/includes/rules_shipping_framework_woocommerce.php

    r1485122 r1549928  
    5050    }
    5151   
    52     public function printWarning($message) {
     52    public function printMessage($message, $type) {
    5353        // Keep track of warning messages, so we don't print them twice:
    54         global $printed_warnings;
    55         if (!isset($printed_warnings))
    56             $printed_warnings = array();
    57         if (!in_array($message, $printed_warnings)) {
    58             wc_add_notice( $message, 'error');
    59             $printed_warnings[] = $message;
    60         }
    61     }
    62 
    63     /** @tag public-api
    64      *  @function debug()
    65      *    Print a debug message (untranslated) in the system-specific way.
    66      *  @param $message the debug message to be printed
    67      */
    68     public function debug($message) {
    69         if ( true === WP_DEBUG ) {
    70             if ( is_array( $message ) || is_object( $message ) ) {
    71                 error_log( print_r( $message, true ) );
    72             } else {
    73                 error_log( $message );
    74             }
     54        global $printed_messages;
     55        if (!isset($printed_messages))
     56            $printed_messages = array();
     57        if ($type == "debug") {
     58            if ( true === WP_DEBUG ) {
     59                if ( is_array( $message ) || is_object( $message ) ) {
     60                    error_log( print_r( $message, true ) );
     61                } else {
     62                    error_log( $message );
     63                }
     64            }
     65        } elseif (!in_array($message, $printed_messages)) {
     66            switch ($type) {
     67                case 'error':
     68                case 'warning':
     69                    wc_add_notice( $message, 'error'); break;
     70                case 'message':
     71                    wc_add_notice( $message, 'success'); break;
     72                case 'notice':
     73                default:
     74                    wc_add_notice( $message, 'notice'); break;
     75            }
     76            $printed_messages[] = $message;
    7577        }
    7678    }
  • shipping-by-rules-for-woocommerce/trunk/library/rules_shipping_framework.php

    r1485122 r1549928  
    172172    }
    173173   
    174     /** @tag system-specific
    175      *  @function printWarning()
    176      *    Print a warning in the system-specific way.
    177      *  @param $message the warning message to be printed (already properly translated)
    178      */
    179     protected function printWarning($message) {
    180         echo($message);
     174    /** @tag public-api
     175     *  @tag system-specific
     176     *  @function message()
     177     *    Print a message (to be translated) with given type in the system-specific way.
     178     *  @param $message the message to be printed
     179     *  @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
     180     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     181     */
     182    public function message($message, $type) {
     183        $args = func_get_args();
     184        // Remove the $type from the args passed to __
     185        unset($args[1]);
     186        $msg = call_user_func_array(array($this, "__"), $args);
     187        $this->printMessage($msg, $type);
     188    }
     189   
     190    /** @tag public-api
     191     *  @tag system-specific
     192     *  @function error()
     193     *    Print an error message (to be translated) in the system-specific way.
     194     *  @param $message the error message to be printed
     195     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     196     */
     197    public function error($message) {
     198        $args = func_get_args();
     199        array_splice($args, 1, 0, 'error'); // insert msg type in second position
     200        call_user_func_array(array($this, "message"), $args);
    181201    }
    182202   
     
    190210    public function warning($message) {
    191211        $args = func_get_args();
    192         $msg = call_user_func_array(array($this, "__"), $args);
    193         $this->printWarning($msg);
     212        array_splice($args, 1, 0, 'warning'); // insert msg type in second position
     213        call_user_func_array(array($this, "message"), $args);
    194214    }
    195215   
    196216    /** @tag public-api
     217     *  @tag system-specific
     218     *  @function notice()
     219     *    Print a message (to be translated) in the system-specific way.
     220     *  @param $message the message to be printed
     221     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
     222     */
     223    public function notice($message) {
     224        $args = func_get_args();
     225        array_splice($args, 1, 0, 'notice'); // insert msg type in second position
     226        call_user_func_array(array($this, "message"), $args);
     227    }
     228   
     229    /** @tag public-api
     230     *  @tag system-specific
    197231     *  @function debug()
    198      *    Print a debug message (untranslated) in the system-specific way.
    199      *  @param $message the debug message to be printed
     232     *    Print a debug message in the system-specific way.
     233     *  @param $message the message to be printed
     234     *  @param $args optional arguments to be inserted into the translated message in sprintf-style
    200235     */
    201236    public function debug($message) {
     237        $args = func_get_args();
     238        array_splice($args, 1, 0, 'debug'); // insert msg type in second position
     239        call_user_func_array(array($this, "message"), $args);
     240    }
     241   
     242    /** @tag system-specific
     243     *  @function printMessage()
     244     *    Print a message of given type in the system-specific way.
     245     *  @param $message the message to be printed (already properly translated)
     246     *  @param $type the type of message (one of "error", "warning", "message"/"notice" or "debug")
     247     */
     248    protected function printMessage($message, $type) {
     249        echo($message);
    202250    }
    203251   
     
    417465                                    break;
    418466                        }
     467                        // Handle messages (error, warning, message/notice, debug:
     468                        foreach ($r->messages as $k=>$msgs) {
     469                            foreach ($msgs as $msg) {
     470                                $this->message($msg, $k);
     471                            }
     472                        }
    419473                    }
    420474                    if (!is_null($result["rule"])) {
     
    594648    var $ruleinfo = 0;
    595649    var $includes_tax = 0;
     650    var $messages = array('error' => array(), 'warning' => array(), 'notice' => array(), 'debug' => array());
    596651   
    597652    function __construct ($framework, $rule, $countries, $ruleinfo) {
     
    625680            case 'extrashippingmultiplier': $this->shipping = $value; $this->ruletype = 'modifiers_multiply'; break; // modifiers are also stored in the shipping member!
    626681            case 'comment':         break; // Completely ignore all comments!
     682            case 'error':           $this->messages['error'][] = $value; break;
     683            case 'warning':         $this->messages['warning'][] = $value; break;
     684            case 'notice':
     685            case 'message':         $this->messages['notice'][] = $value; break;
     686            case 'debug':           $this->messages['debug'][] = $value; break;
    627687            case 'condition':       $this->conditions[] = $value; break;
    628688            default:                $this->framework->warning('OTSHIPMENT_RULES_UNKNOWN_VARIABLE', $var, $rulepart);
     
    647707        if (!isset($rulepart) || $rulepart==='') return;
    648708
    649        
    650709        // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
    651         if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
     710        if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
    652711            $this->handleAssignment ($matches[1], $matches[3], $rulepart);
    653712            return;
     
    10281087    }
    10291088
    1030     protected function calculateShipping ($vals, $products, $cartvals_callback) {
     1089    protected function calculateShipping($vals, $products, $cartvals_callback) {
    10311090        return $this->evaluateTerm($this->shipping, $vals, $products, $cartvals_callback);
     1091    }
     1092   
     1093    protected function stringReplaceVariables($str, $vals) {
     1094        // Evaluate the rule name as a translatable string with variables inserted:
     1095        // Replace all {variable} tags in the name by the variables from $vals
     1096        $matches = array();
     1097        preg_match_all('/{([A-Za-z0-9_]+)}/', $str, $matches);
     1098       
     1099        foreach ($matches[1] as $m) {
     1100            $val = $this->evaluateVariable($m, $vals);
     1101            if ($val !== null) {
     1102                $str = str_replace("{".$m."}", $val, $str);
     1103            }
     1104        }
     1105        return $str;
     1106   
    10321107    }
    10331108
     
    10541129        // All conditions match
    10551130        $this->match = True;
     1131        foreach ($this->messages as $k=>$msgs) {
     1132            foreach ($msgs as $i=>$m) {
     1133                $this->messages[$k][$i] = $this->stringReplaceVariables($m, $vals);
     1134            }
     1135        }
    10561136        // Calculate the value (i.e. shipping cost or modifier)
    10571137        $this->value = $this->calculateShipping($vals, $products, $cartvals_callback);
    1058         // Evaluate the rule name as a translatable string with variables inserted:
    1059         // Replace all {variable} tags in the name by the variables from $vals
    1060         $matches = array();
    1061         $name = $this->framework->__($this->name);
    1062         preg_match_all('/{([A-Za-z0-9_]+)}/', $name, $matches);
    10631138       
    1064         foreach ($matches[1] as $m) {
    1065             $val = $this->evaluateVariable($m, $vals);
    1066             if ($val !== null) {
    1067                 $name = str_replace("{".$m."}", $val, $name);
    1068             }
    1069         }
    1070         $this->rulename = $name;
     1139        $this->rulename = $this->stringReplaceVariables($this->framework->__($this->name), $vals);
    10711140    }
    10721141
     
    11531222       
    11541223        // Special-case the name assignment, where we don't want to interpret the value as an arithmetic expression!
    1155         if (preg_match('/^\s*(name|variable|definition)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
     1224        if (preg_match('/^\s*(name|variable|definition|error|warning|message|notice|debug)\s*=\s*(["\']?)(.*)\2\s*$/i', $rulepart, $matches)) {
    11561225            $this->handleAssignment ($matches[1], $matches[3], $rulepart);
    11571226            return;
  • shipping-by-rules-for-woocommerce/trunk/readme.txt

    r1485122 r1549928  
    33Tags: WooCommerce, Shipment, Shipping, Rules shipping
    44Requires at least: 4.0
    5 Tested up to: 4.5
    6 Stable tag: 1.2.5
     5Tested up to: 4.7
     6Stable tag: 1.2.6
    77License: GPLv3 or later
    88License URI: http://www.gnu.org/licenses/gpl.html
     
    7070== Changelog ==
    7171
     72= 1.2.6 =
     73* Add message functionality (Error=..., Warning=..., Message=... rule parts)
     74
    7275= 1.2.5 =
    7376* Add variables username, first_name, last_name, email
  • shipping-by-rules-for-woocommerce/trunk/woocommerce-shipping-by-rules.php

    r1485122 r1549928  
    44 * Plugin URI: http://open-tools.net/woocommerce/advanced-shipping-by-rules-for-woocommerce.html
    55 * Description: Define Shipping cost by very general and flexible (text-based) rules.
    6  * Version: 1.2.5
     6 * Version: 1.2.6
    77 * Author: Open Tools, Reinhold Kainhofer
    88 * Author URI: http://open-tools.net
     
    1111 * License: GPL2+
    1212 * WC requires at least: 2.2
    13  * WC tested up to: 2.6
     13 * WC tested up to: 2.7
    1414 
    1515
     
    4949     * @var string $version Plugin version number.
    5050     */
    51     public $version = '1.2.5';
     51    public $version = '1.2.6';
    5252
    5353
Note: See TracChangeset for help on using the changeset viewer.