Skip to content

Commit

Permalink
Fix admin fixed commission setting validation (#2437)
Browse files Browse the repository at this point in the history
* Fix admin fixed commission setting validation
Restrict admin commission percentage from 0 to 100

* Fix admin fixed commission setting validation
Restrict admin commission percentage from 0 to 100
  • Loading branch information
Aunshon authored Nov 15, 2024
1 parent ac8cfe4 commit 0235525
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
27 changes: 15 additions & 12 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct() {
add_action( 'dokan_before_saving_settings', [ $this, 'set_withdraw_limit_value_validation' ], 10, 2 );
add_filter( 'dokan_admin_localize_script', [ $this, 'add_admin_settings_nonce' ] );
add_action( 'wp_ajax_dokan_refresh_admin_settings_field_options', [ $this, 'refresh_admin_settings_field_options' ] );
add_filter( 'dokan_get_settings_values', [ $this, 'format_price_values' ], 12, 2 );
add_filter( 'dokan_save_settings_value', [ $this, 'validate_fixed_price_values' ], 12, 2 );
add_filter( 'dokan_get_settings_values', [ $this, 'set_withdraw_limit_gateways' ], 20, 2 );
add_filter( 'dokan_settings_general_site_options', [ $this, 'add_dokan_data_clear_setting' ], 310 );
}
Expand Down Expand Up @@ -61,21 +61,24 @@ public function set_withdraw_limit_gateways( $option_value, $option_name ) {
}

/**
* Format price values for price settings
* Validate price values for saving fixed price settings.
*
* @since 1.0.0
* @since DOKAN_SINCE
*
* @param $option_name
* @param $option_values
* @param string $option_name
* @param array $option_values
*
* @return void
* @return array
*/
public function format_price_values( $option_values, $option_name ) {
if ( 'dokan_selling' === $option_name ) {
if ( isset( $option_values['commission_type'] ) && 'flat' === $option_values['commission_type'] ) {
$option_values['admin_percentage'] = isset( $option_values['admin_percentage'] ) ? wc_format_localized_price( $option_values['admin_percentage'] ) : 0;
} else {
$option_values['admin_percentage'] = isset( $option_values['admin_percentage'] ) ? wc_format_localized_decimal( $option_values['admin_percentage'] ) : 0;
public function validate_fixed_price_values( $option_values, $option_name ) {
$clickable_types = [ 'flat', 'fixed' ];

if ( 'dokan_selling' === $option_name && isset( $option_values['commission_type'] ) && in_array( $option_values['commission_type'], $clickable_types, true ) ) {
$admin_percentage = (float) $option_values['admin_percentage'];
$saved_admin_percentage = dokan_get_option( 'admin_percentage', 'dokan_selling', '' );

if ( $admin_percentage < 0 || $admin_percentage > 100 ) {
$option_values['admin_percentage'] = $saved_admin_percentage;
}
}

Expand Down
9 changes: 7 additions & 2 deletions includes/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function enqueue_scripts() {
wp_enqueue_style( 'dokan-setup', DOKAN_PLUGIN_ASSEST . '/css/setup.css', [ 'wc-setup' ], DOKAN_PLUGIN_VERSION );

wp_enqueue_script(
'dokan-vue-bootstrap',
'dokan-vue-bootstrap',
DOKAN_PLUGIN_ASSEST . '/js/vue-bootstrap.js',
[ 'dokan-vue-vendor', 'dokan-i18n-jed', 'wp-hooks' ],
DOKAN_PLUGIN_VERSION,
Expand Down Expand Up @@ -554,9 +554,14 @@ public function dokan_setup_selling_save() {
public function dokan_setup_commission_save() {
check_admin_referer( 'dokan-setup' );

$dokan_commission_percentage = isset( $_POST['dokan_commission_percentage'] ) ? (float) wc_format_decimal( sanitize_text_field( wp_unslash( $_POST['dokan_commission_percentage'] ) ) ) : 0;
if ( $dokan_commission_percentage < 0 || $dokan_commission_percentage > 100 ) {
$dokan_commission_percentage = 0;
}

$options = get_option( 'dokan_selling', [] );
$options['commission_type'] = isset( $_POST['dokan_commission_type'] ) ? sanitize_text_field( wp_unslash( $_POST['dokan_commission_type'] ) ) : 'fixed';
$options['admin_percentage'] = isset( $_POST['dokan_commission_percentage'] ) ? wc_format_decimal( sanitize_text_field( wp_unslash( $_POST['dokan_commission_percentage'] ) ) ) : 0;
$options['admin_percentage'] = $dokan_commission_percentage;
$options['additional_fee'] = isset( $_POST['dokan_commission_flat'] ) ? sanitize_text_field( wp_unslash( $_POST['dokan_commission_flat'] ) ) : 0;
$options['commission_category_based_values'] = isset( $_POST['dokan_commission_category_based'] ) ? wc_clean( json_decode( sanitize_text_field( wp_unslash( $_POST['dokan_commission_category_based'] ) ), true ) ) : [];

Expand Down
26 changes: 21 additions & 5 deletions src/admin/components/CombineInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,34 @@ import Debounce from "debounce";
watch: {
value: {
handler(newVal, oldVal) {
let newPercentage = this.validatePercentage( newVal.percentage );
let oldPercentage = this.validatePercentage( oldVal.percentage );
console.log(newPercentage, oldPercentage);
if ( ! newPercentage || '' === newPercentage || Number( newPercentage ) < 0 || Number( newPercentage ) > 100 ) {
newPercentage = oldPercentage;
}
this.fixed = this.formatPositiveValue( newVal.fixed );
this.percentage = this.formatPositiveValue( newVal.percentage );
this.percentage = this.formatPositiveValue( newPercentage );
},
deep: true
}
},
methods: {
validatePercentage( percentage ) {
if ( Number( percentage ) < 0 || Number( percentage ) > 100 ) {
percentage = '';
}
return percentage;
},
onInput: Debounce( function() {
let self = this,
data = {
fixed: self.fixed,
percentage: self.percentage
let self = this;
let data = {
fixed: self.fixed ? accounting.unformat(self.fixed, dokan.currency.decimal) : '',
percentage: self.percentage ? accounting.unformat(self.percentage, dokan.currency.decimal): ''
};
this.$emit('change', data);
Expand Down

0 comments on commit 0235525

Please sign in to comment.