Skip to content

Commit

Permalink
Merge branch 'release/2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Jan 17, 2017
2 parents 0969bec + 286f540 commit 0134df4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 94 deletions.
8 changes: 8 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
---------------------------------------------------------------------
Version 2.4
- Added gform_coupons_is_valid_code to override coupon code alphanumeric checking in Gravity Forms 2.2.2.
- Updated coupons.js to use the gform_product_total filter with a later priority.
- Updated how default label for new fields is set.
- Fixed wrong domain for translating the "Coupon" button.
- Fixed an issue with the formatting of the percentage coupon amount when a decimal comma based currency is in use.

---------------------------------------------------------------------
Version 2.3
- Added Dutch translation (nl_NL). Credit: Maarten Emmerink
Expand Down
57 changes: 28 additions & 29 deletions class-gf-coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,13 @@ public function apply_coupon_code() {
public function init_admin() {

parent::init_admin();
add_action( 'gform_editor_js_set_default_values', array( $this, 'set_defaults' ) );
add_filter( $this->_slug . '_feed_actions', array( $this, 'set_action_links' ), 10, 3 );

// don't duplicate feeds with the form, feed duplication not currently supported.
remove_action( 'gform_post_form_duplicated', array( $this, 'post_form_duplicated' ) );

}

/**
* Sets the fields default label in the form editor.
*/
public function set_defaults() {
?>
case "coupon" :
field.label = <?php echo json_encode( esc_html__( 'Coupon', 'gravityformscoupons' ) ); ?>;
break;
<?php
}

/**
* Add the settings tab with the uninstall button.
*/
Expand Down Expand Up @@ -580,7 +568,8 @@ public function get_column_value_couponAmount( $feed ) {
if ( $feed['meta']['couponAmountType'] == 'flat' ) {
$couponAmount = GFCommon::to_money( $feed['meta']['couponAmount'] );
} else {
$couponAmount = GFCommon::to_number( $feed['meta']['couponAmount'] ) . '%';
$number_format = GFCommon::is_currency_decimal_dot() ? 'decimal_dot' : 'decimal_comma';
$couponAmount = GFCommon::format_number( $feed['meta']['couponAmount'], $number_format ) . '%';
}

return $couponAmount;
Expand Down Expand Up @@ -780,17 +769,16 @@ public function settings_coupon_amount_type( $field, $echo = true ) {
</style>';

$js_script = '<script type="text/javascript">
var currency_config = ' . json_encode( RGCurrency::get_currency( GFCommon::get_currency() ) ) . ';
var form = Array();
jQuery(document).on(\'change\', \'.gf_format_money\', function(){
var cur = new Currency(currency_config)
jQuery(this).val(cur.toMoney(jQuery(this).val()));
});
jQuery(document).on(\'change\', \'.gf_format_percentage\', function(event){
var cur = new Currency(currency_config)
var value = cur.toNumber(jQuery(this).val()) ? cur.toNumber(jQuery(this).val()) + \'%\' : \'\';
jQuery(this).val( value );
});
jQuery(document).on(\'change\', \'.gf_format_money\', function(){
var cur = new Currency(gf_vars.gf_currency_config);
jQuery(this).val(cur.toMoney(jQuery(this).val()));
});
jQuery(document).on(\'change\', \'.gf_format_percentage\', function(event){
var cur = new Currency(gf_vars.gf_currency_config),
cleanNum = cur.toNumber(jQuery(this).val()),
value = cleanNum ? cur.numberFormat(cleanNum, cur.currency["decimals"], cur.currency["decimal_separator"], cur.currency["thousand_separator"]) + \'%\' : \'\';
jQuery(this).val( value );
});
function SetCouponType(elem) {
var type = elem.val();
Expand All @@ -807,15 +795,14 @@ function SetCouponType(elem) {
jQuery(\'#couponAmount\').attr("placeholder",placeholderText);
//format initial coupon amount value when there is one and it is currency
var currency_config = ' . json_encode( RGCurrency::get_currency( GFCommon::get_currency() ) ) . ';
var cur = new Currency(currency_config);
couponAmount = jQuery(\'#couponAmount\').val();
var cur = new Currency(gf_vars.gf_currency_config),
couponAmount = jQuery(\'#couponAmount\').val();
if ( couponAmount ){
if (type == \'flat\'){
couponAmount = cur.toMoney(couponAmount);
}
else{
couponAmount = cur.toNumber(couponAmount) + \'%\';
couponAmount = cur.numberFormat(couponAmount, cur.currency["decimals"], cur.currency["decimal_separator"], cur.currency["thousand_separator"]) + \'%\';
}
jQuery(\'#couponAmount\').val(couponAmount);
}
Expand Down Expand Up @@ -884,7 +871,19 @@ public function validate_coupon_amount( $field ) {
public function check_if_duplicate_coupon_code( $field ) {
$settings = $this->get_posted_settings();

if ( ! ctype_alnum( $settings['couponCode'] ) ) {
$is_alphanumeric = ctype_alnum( $settings['couponCode'] );

/**
* Overrides coupon code validation.
*
* @since 2.3.3
*
* @param bool $is_alphanumeric If the coupon code is alphanumeric.
* @param array $field The Field Object.
*/
$is_valid_coupon = apply_filters( 'gform_coupons_is_valid_code', $is_alphanumeric, $field );

if ( ! $is_valid_coupon ) {
$this->set_field_error( $field, esc_html__( 'Please enter a valid Coupon Code. The Coupon Code can only contain alphanumeric characters.', 'gravityformscoupons' ) );

return;
Expand Down
10 changes: 7 additions & 3 deletions class-gf-field-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GF_Field_Coupon extends GF_Field {
* @return string
*/
public function get_form_editor_field_title() {
return __( 'Coupon', 'gravityformscoupon' );
return __( 'Coupon', 'gravityformscoupons' );
}

/**
Expand Down Expand Up @@ -149,12 +149,14 @@ public function validate( $value, $form ) {
}

/**
* Include the gform_form_editor_can_field_be_added script on the form editor page.
* Include the gform_form_editor_can_field_be_added script on the form editor page and set the default label for new fields.
*
* @return string
*/
public function get_form_editor_inline_script_on_page_render() {
return "
$script = sprintf( "function SetDefaultValues_%s(field) {field.label = '%s';}", $this->type, $this->get_form_editor_field_title() ) . PHP_EOL;

$script .= "
gform.addFilter('gform_form_editor_can_field_be_added', function (canFieldBeAdded, type) {
if (type == 'coupon') {
if (GetFieldsByType(['product']).length <= 0) {
Expand All @@ -170,6 +172,8 @@ public function get_form_editor_inline_script_on_page_render() {
}
return canFieldBeAdded;
});";

return $script;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Gravity Forms Coupons Add-On
Plugin URI: http://www.gravityforms.com
Description: Enables Gravity Forms administrators to create coupon codes that can be applied to products, services or subscriptions when used in conjunction with a payment add-on such as PayPal and Authorize.net
Version: 2.3
Version: 2.4
Author: rocketgenius
Author URI: http://www.rocketgenius.com
Text Domain: gravityformscoupons
Expand All @@ -27,7 +27,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

define( 'GF_COUPONS_VERSION', '2.3' );
define( 'GF_COUPONS_VERSION', '2.4' );

add_action( 'gform_loaded', array( 'GF_Coupons_Bootstrap', 'load' ), 5 );

Expand Down
8 changes: 4 additions & 4 deletions js/coupons.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function PopulateDiscountInfo(price, formId) {
' <a href="javascript:void(0);" > + coupon['code'] + '\' , \'' + formId + '\');">(x)</a>' +
' <span class="gf_coupon_name">' + coupon['name'] + '</span>' +
'</td><td class="gf_coupon_discount_container">' +
' <span class="gf_coupon_discount">-' + currency.toMoney(couponDiscount) + '</span>' +
' <span class="gf_coupon_discount">-' + currency.toMoney(couponDiscount,true) + '</span>' +
'</td></tr>';
}

Expand All @@ -109,8 +109,8 @@ function DisableApplyButton(formId) {
}
}

function gform_product_total(formId, total) {
// ignore forms that don't have a coupon field
gform.addFilter('gform_product_total', function (total, formId) {
// Ignore forms that don't have a coupon field.
if (jQuery('#gf_coupon_code_' + formId).length == 0) {
return total;
}
Expand All @@ -131,7 +131,7 @@ function gform_product_total(formId, total) {
DisableApplyButton(formId);

return new_total;
}
}, 50);

function DeleteCoupon(code, formId) {
// check if coupon code is in the process of being applied
Expand Down
2 changes: 1 addition & 1 deletion js/coupons.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0134df4

Please sign in to comment.