Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount formatting #734

Merged
merged 28 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
59bf9e0
Add function to get decimal and thousand separator
ravinderk Jul 15, 2016
f2b05fb
Add function to format price by decimal
ravinderk Jul 15, 2016
54505a6
Remove price formatting from goal amount input field
ravinderk Jul 15, 2016
e92981f
Remove price formatting from set donation & custom minimum amount inp…
ravinderk Jul 15, 2016
c0a1996
Add function give_maybe_unformat_amount to remove formatting from exi…
ravinderk Jul 15, 2016
37e17b6
Remove price formatting from multi level donation amount input field
ravinderk Jul 15, 2016
01a6fae
Remove thousand and decimal formatting in give_format_decimal function
ravinderk Jul 15, 2016
90906ca
Add custom sanitization for thousand separator
ravinderk Jul 15, 2016
b39058a
Update amount sanitisation function give_sanitize_amount
ravinderk Jul 15, 2016
21f6ccd
Remove thousand separator after decimal separator from number
ravinderk Jul 15, 2016
cf83a9f
Show decimal formatted amount in amount related input fields
ravinderk Jul 15, 2016
8d0ecc4
Update give_sanitize_amount function
ravinderk Jul 15, 2016
c083400
Add function to format number only by decimal
ravinderk Jul 15, 2016
bb24138
Add function to get default decimal place count
ravinderk Jul 15, 2016
807b21f
Remove any entity before decimal from amount when sanitising
ravinderk Jul 15, 2016
79cae55
Sanitizing amount before save to db and format amount by decimal in a…
ravinderk Jul 15, 2016
880d305
Remove testing code block
ravinderk Jul 15, 2016
2af3107
Sanitize price before formatting with decimal separator
ravinderk Jul 15, 2016
d83e975
Format payment amount by decimal separator
ravinderk Jul 15, 2016
5e98615
Check decimal dot separator in sanitize amount
ravinderk Jul 15, 2016
a663388
Use give_get_price_decimals function instead of give_format_amount_de…
ravinderk Jul 15, 2016
391e52e
Add missing give_sanitize_amount_decimals filter to give_sanitize_amo…
ravinderk Jul 15, 2016
238a9ba
Add amount separator information to give_vars
ravinderk Jul 18, 2016
876e6b6
Add price format quide tooltip text
ravinderk Jul 18, 2016
9737239
Show qtip message to user on money field if user have thousand separa…
ravinderk Jul 18, 2016
9b9b16a
show qtip price format message for give-price-field settign field
ravinderk Jul 18, 2016
f68c57d
Show price format message on keyup event
ravinderk Jul 18, 2016
0770f9b
Fix price string when user focusout from price field setting
ravinderk Jul 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion assets/js/admin/admin-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,70 @@ jQuery.noConflict();
$('.give_user_search_results span').html('');
});

});
/**
* Amount format validation form price field setting
*/
var $give_money_fields = $('input.give-money-field, input.give-price-field');
if( $give_money_fields.length ) {
var thousand_separator = give_vars.thousands_separator.trim(),
decimal_separator = give_vars.decimal_separator.trim(),
thousand_separator_count = '',
price_string = '',

// Thousand separation limit in price depends upon decimal separator symbol.
// If thousand separator is equal to decimal separator then price does not have more then 1 thousand separator otherwise limit is zero.
thousand_separator_limit = ( decimal_separator === thousand_separator ? 1 : 0 );

// Add qtip to all money input fields.
$give_money_fields.each(function() {
$(this).qtip({
style: 'qtip-dark qtip-tipsy',
content: {
text: give_vars.price_format_guide.trim()
},
show: '',
position: {
my: 'bottom center',
at: 'top center'
}
});
});

// Check & show message on keyup event.
$give_money_fields.bind( 'keyup', function(){
// Count thousand separator in price string.
thousand_separator_count = ( $(this).val().match( new RegExp( thousand_separator, 'g' ) ) || [] ).length;

// Show qtip conditionally if thousand separator detected on price string.
if(
( -1 !== $(this).val().indexOf( thousand_separator ) )
&& ( thousand_separator_limit < thousand_separator_count )
) {
$(this).qtip('show');
}else{
$(this).qtip('hide');
}

// Reset thousand separator count.
thousand_separator_count = '';
});

// Format price sting of input field on focuout.
$give_money_fields.on( 'focusout', function(){
price_string = $(this).val();
thousand_separator_count = ( price_string.match( new RegExp( thousand_separator, 'g' ) ) || [] ).length;

// Replace all thousand separator except one.
while ( thousand_separator_limit < thousand_separator_count ) {
price_string = price_string.replace( thousand_separator, '' );
--thousand_separator_count;
}

// Update format price string in input field.
$(this).val( price_string );
});
}

});

})(jQuery);
2 changes: 1 addition & 1 deletion assets/js/admin/admin-scripts.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions includes/admin/class-give-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public function give_settings( $active_tab ) {
'desc' => __( 'The symbol (typically , or .) to separate thousands', 'give' ),
'id' => 'thousands_separator',
'type' => 'text_small',
'sanitization_cb' => 'give_sanitize_thousand_separator',
'default' => ',',
),
array(
Expand Down
50 changes: 27 additions & 23 deletions includes/admin/forms/metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ function give_single_forms_cmb2_metaboxes( array $meta_boxes ) {
'row_classes' => 'give-subfield',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'attributes' => array(
'placeholder' => give_format_amount( '1.00' ),
'value' => give_format_amount( $price ),
'sanitization_cb' => 'give_sanitize_amount',
'attributes' => array(
'placeholder' => give_format_decimal( '1.00' ),
'value' => give_format_decimal( $price ),
'class' => 'cmb-type-text-small give-money-field',
),
),
Expand All @@ -104,13 +105,14 @@ function give_single_forms_cmb2_metaboxes( array $meta_boxes ) {
'type' => 'levels_id',
),
array(
'name' => __( 'Amount', 'give' ),
'id' => $prefix . 'amount',
'type' => 'text_small',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'attributes' => array(
'placeholder' => give_format_amount( '1.00' ),
'name' => __( 'Amount', 'give' ),
'id' => $prefix . 'amount',
'type' => 'text_small',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'sanitization_cb' => 'give_sanitize_amount',
'attributes' => array(
'placeholder' => '1.00',
'class' => 'cmb-type-text-small give-money-field',
),
'before' => 'give_format_admin_multilevel_amount',
Expand Down Expand Up @@ -164,9 +166,10 @@ function give_single_forms_cmb2_metaboxes( array $meta_boxes ) {
'row_classes' => 'give-subfield',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'attributes' => array(
'placeholder' => give_format_amount( '1.00' ),
'value' => give_format_amount( $custom_amount_minimum ),
'sanitization_cb' => 'give_sanitize_amount',
'attributes' => array(
'placeholder' => give_format_decimal('1.00'),
'value' => give_format_decimal( $custom_amount_minimum ),
'class' => 'cmb-type-text-small give-money-field',
),
),
Expand Down Expand Up @@ -194,16 +197,17 @@ function give_single_forms_cmb2_metaboxes( array $meta_boxes ) {
),
),
array(
'name' => __( 'Goal Amount', 'give' ),
'description' => __( 'This is the monetary goal amount you want to reach for this donation form.', 'give' ),
'id' => $prefix . 'set_goal',
'type' => 'text_small',
'row_classes' => 'give-subfield',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'attributes' => array(
'placeholder' => give_format_amount( '0.00' ),
'value' => isset( $goal ) ? esc_attr( give_format_amount( $goal ) ) : '',
'name' => __( 'Goal Amount', 'give' ),
'description' => __( 'This is the monetary goal amount you want to reach for this donation form.', 'give' ),
'id' => $prefix . 'set_goal',
'type' => 'text_small',
'row_classes' => 'give-subfield',
'before_field' => give_get_option( 'currency_position' ) == 'before' ? '<span class="give-money-symbol give-money-symbol-before">' . give_currency_symbol() . '</span>' : '',
'after_field' => give_get_option( 'currency_position' ) == 'after' ? '<span class="give-money-symbol give-money-symbol-after">' . give_currency_symbol() . '</span>' : '',
'sanitization_cb' => 'give_sanitize_amount',
'attributes' => array(
'placeholder' => give_format_decimal( '0.00' ),
'value' => give_format_decimal( $goal ),
'class' => 'cmb-type-text-small give-money-field',
),
),
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/payments/view-order-details.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<div class="give-order-payment give-admin-box-inside">
<p>
<span class="label"><?php _e( 'Total Donation', 'give' ); ?>:</span>&nbsp;
<?php echo give_currency_symbol( $payment->currency ); ?>&nbsp;<input name="give-payment-total" type="text" class="small-text give-price-field" value="<?php echo esc_attr( give_format_amount( give_get_payment_amount( $payment_id ) ) ); ?>"/>
<?php echo give_currency_symbol( $payment->currency ); ?>&nbsp;<input name="give-payment-total" type="text" class="small-text give-price-field" value="<?php echo esc_attr( give_format_decimal( give_get_payment_amount( $payment_id ) ) ); ?>"/>
</p>
</div>

Expand Down
134 changes: 105 additions & 29 deletions includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,89 @@
exit;
}


/**
* Get decimal count
*
* @since 1.6
*
* @return mixed
*/
function give_get_price_decimals() {
return apply_filters( 'give_sanitize_amount_decimals', 2 );
}

/**
* Get thousand separator
*
* @since 1.6
*
* @return mixed
*/
function give_get_price_thousand_separator() {
return give_get_option( 'thousands_separator', ',' );
}

/**
* Get decimal separator
*
* @since 1.6
*
* @return mixed
*/
function give_get_price_decimal_separator() {
return give_get_option( 'decimal_separator', '.' );
}

/**
* Sanitize Amount
*
* @description: Returns a sanitized amount by stripping out thousands separators.
*
* @since 1.0
*
* @param string $amount Price amount to format
* @param float|string $number Expects either a float or a string with a decimal separator only (no thousands)
* @param bool $trim_zeros From end of string
*
*
* @return string $amount Newly sanitized amount
*/
function give_sanitize_amount( $amount ) {
$is_negative = false;
$thousands_sep = give_get_option( 'thousands_separator', ',' );
$decimal_sep = give_get_option( 'decimal_separator', '.' );
function give_sanitize_amount( $number, $trim_zeros = false ) {
$thousand_separator = give_get_price_thousand_separator();

// Sanitize the amount
if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) {
if ( ( $thousands_sep == '.' || $thousands_sep == ' ' ) && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
$amount = str_replace( $thousands_sep, '', $amount );
} elseif ( empty( $thousands_sep ) && false !== ( $found = strpos( $amount, '.' ) ) ) {
$amount = str_replace( '.', '', $amount );
}
$locale = localeconv();
$decimals = array( give_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );

$amount = str_replace( $decimal_sep, '.', $amount );
} elseif ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
$amount = str_replace( $thousands_sep, '', $amount );
}
// Remove locale from string
if ( ! is_float( $number ) ) {
$number = str_replace( $decimals, '.', $number );
}

if ( $amount < 0 ) {
$is_negative = true;
}
// Remove thousand amount formatting if amount has.
// This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
if( false !== strpos( $number, $thousand_separator ) ) {
$number = str_replace( $thousand_separator, '', $number );
}

// Remove non numeric entity before decimal separator.
$number = preg_replace( '/[^0-9\.]/', '', $number );

$decimals = give_get_price_decimals();
$decimals = apply_filters( 'give_sanitize_amount_decimals', $decimals, $number );

$amount = preg_replace( '/[^0-9\.]/', '', $amount );
$decimals = apply_filters( 'give_sanitize_amount_decimals', 2, $amount );
$amount = number_format( (double) $amount, $decimals, '.', '' );
$number = number_format( floatval( $number ), $decimals, '.', '' );

if ( $is_negative ) {
$amount *= - 1;
// Reset negative amount to zero.
if ( 0 > $number ) {
$number = number_format( 0, 2, '.' );
}

return apply_filters( 'give_sanitize_amount', $amount );
// Trim zeros.
if ( $trim_zeros && strstr( $number, '.' ) ) {
$number = rtrim( rtrim( $number, '0' ), '.' );
}

return apply_filters( 'give_sanitize_amount', $number );
}

/**
Expand Down Expand Up @@ -104,13 +145,34 @@ function give_format_amount( $amount, $decimals = true ) {
$amount = 0;
}

$decimals = apply_filters( 'give_format_amount_decimals', $decimals ? 2 : 0, $amount );
$decimals = give_get_price_decimals();

$formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );

return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep );
}

/**
* Returns a nicely formatted amount with custom decimal separator.
*
* @since 1.0
*
* @param string $amount Formatted or sanitized price
*
* @return string $amount Newly formatted amount or Price Not Available
*/
function give_format_decimal( $amount ){
$decimal_separator = give_get_price_decimal_separator();
$formatted_amount = give_sanitize_amount( $amount );

if( false !== strpos( $formatted_amount, '.' ) ) {
$formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount );
}

return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator );
}


/**
* Format Multi-level Amount
*
Expand All @@ -127,8 +189,7 @@ function give_format_admin_multilevel_amount( $field_args, $field ) {
return false;
}

$field->value = give_format_amount( $field->value );

$field->value = give_format_decimal( $field->value );
}

/**
Expand Down Expand Up @@ -270,4 +331,19 @@ function give_currency_decimal_filter( $decimals = 2 ) {
}

add_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' );
add_filter( 'give_format_amount_decimals', 'give_currency_decimal_filter' );
add_filter( 'give_format_amount_decimals', 'give_currency_decimal_filter' );

/**
* Sanitize thousand separator
*
* @since 1.6
*
* @param string $value
* @param array $field_args
* @param object $field
*
* @return mixed
*/
function give_sanitize_thousand_separator( $value, $field_args, $field ){
return $value;
}
Loading