Skip to content

Commit

Permalink
Better filters in save_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jul 6, 2015
1 parent 792536b commit 95b2a44
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 69 deletions.
124 changes: 55 additions & 69 deletions includes/admin/class-wc-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,110 +656,96 @@ public static function save_fields( $options ) {
return false;
}

// Options to update will be stored here
// Options to update will be stored here and saved later
$update_options = array();

// Loop options and get values to save
foreach ( $options as $value ) {
if ( ! isset( $value['id'] ) || ! isset( $value['type'] ) ) {
foreach ( $options as $option ) {
if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
continue;
}

// Get posted value
if ( strstr( $value['id'], '[' ) ) {
parse_str( $value['id'], $option_name_array );

if ( strstr( $option['id'], '[' ) ) {
parse_str( $option['id'], $option_name_array );
$option_name = current( array_keys( $option_name_array ) );
$setting_name = key( $option_name_array[ $option_name ] );

$option_value = isset( $_POST[ $option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $option_name ][ $setting_name ] ) : null;
$raw_value = isset( $_POST[ $option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $option_name ][ $setting_name ] ) : null;
} else {
$option_name = $value['id'];
$option_name = $option['id'];
$setting_name = '';
$option_value = isset( $_POST[ $value['id'] ] ) ? wp_unslash( $_POST[ $value['id'] ] ) : null;
$raw_value = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null;
}

// Format value
switch ( sanitize_title( $value['type'] ) ) {
// Format the value based on option type
switch ( $option['type'] ) {
case 'checkbox' :
$option_value = is_null( $option_value ) ? 'no' : 'yes';
$value = is_null( $raw_value ) ? 'no' : 'yes';
break;
case 'textarea' :
$option_value = wp_kses_post( trim( $option_value ) );
break;
case 'text' :
case 'email':
case 'number':
case 'select' :
case 'color' :
case 'password' :
case 'single_select_page' :
case 'single_select_country' :
case 'radio' :
if ( in_array( $value['id'], array( 'woocommerce_price_thousand_sep', 'woocommerce_price_decimal_sep' ) ) ) {
$option_value = wp_kses_post( $option_value );

} elseif ( 'woocommerce_price_num_decimals' == $value['id'] ) {
$option_value = is_null( $option_value ) ? 2 : absint( $option_value );

} elseif ( 'woocommerce_hold_stock_minutes' == $value['id'] ) {
$option_value = ! empty( $option_value ) ? absint( $option_value ) : ''; // Allow > 0 or set to ''

wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );

if ( '' !== $option_value ) {
wp_schedule_single_event( time() + ( absint( $option_value ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}

} else {
$option_value = wc_clean( $option_value );
}
$value = wp_kses_post( trim( $raw_value ) );
break;
case 'multiselect' :
case 'multi_select_countries' :
$option_value = array_filter( array_map( 'wc_clean', (array) $option_value ) );
$value = array_filter( array_map( 'wc_clean', (array) $raw_value ) );
break;
case 'image_width' :
if ( isset( $option_value['width'] ) ) {
$update_options[ $value['id'] ]['width'] = wc_clean( $option_value['width'] );
$update_options[ $value['id'] ]['height'] = wc_clean( $option_value['height'] );
$update_options[ $value['id'] ]['crop'] = isset( $option_value['crop'] ) ? 1 : 0;
$update_options[ $option['id'] ]['width'] = wc_clean( $value['width'] );
$update_options[ $option['id'] ]['height'] = wc_clean( $value['height'] );
$update_options[ $option['id'] ]['crop'] = isset( $value['crop'] ) ? 1 : 0;
} else {
$update_options[ $value['id'] ]['width'] = $value['default']['width'];
$update_options[ $value['id'] ]['height'] = $value['default']['height'];
$update_options[ $value['id'] ]['crop'] = $value['default']['crop'];
$update_options[ $option['id'] ]['width'] = $option['default']['width'];
$update_options[ $option['id'] ]['height'] = $option['default']['height'];
$update_options[ $option['id'] ]['crop'] = $option['default']['crop'];
}
break;
default :
do_action( 'woocommerce_update_option_' . sanitize_title( $value['type'] ), $value );
$value = wc_clean( $raw_value );
break;
}

if ( ! is_null( $option_value ) ) {
// Check if option is an array
if ( $option_name && $setting_name ) {
// Get old option value
if ( ! isset( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = get_option( $option_name, array() );
}

if ( ! is_array( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = array();
}

$update_options[ $option_name ][ $setting_name ] = $option_value;
/**
* Fire an action when a certain 'type' of field is being saved.
* @deprecated 2.4.0 - doesn't allow manipulation of values!
*/
do_action( 'woocommerce_update_option_' . sanitize_title( $value['type'] ), $value );

/**
* Sanitize the value of an option
* @since 2.4.0
*/
$value = apply_filters( 'woocommerce_admin_settings_sanitize_option', $value, $option, $raw_value );

/**
* Sanitize the value of an option by option name
* @since 2.4.0
*/
$value = apply_filters( "woocommerce_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value );

if ( is_null( $value ) ) {
continue;
}

// Single value
} else {
$update_options[ $option_name ] = $option_value;
// Check if option is an array and handle that differently to single values.
if ( $option_name && $setting_name ) {
// Get old option value
if ( ! isset( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = get_option( $option_name, array() );
}
if ( ! is_array( $update_options[ $option_name ] ) ) {
$update_options[ $option_name ] = array();
}
$update_options[ $option_name ][ $setting_name ] = $option_value;
} else {
$update_options[ $option_name ] = $option_value;
}

// Custom handling
do_action( 'woocommerce_update_option', $value );
// @todo how can we make this useful
do_action( 'woocommerce_update_option', $option );
}

// Now save the options
// Save all options in our array
foreach ( $update_options as $name => $value ) {
update_option( $name, $value );
}
Expand Down
45 changes: 45 additions & 0 deletions includes/wc-formatting-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,48 @@ function wc_trim_string( $string, $chars = 200, $suffix = '...' ) {
function wc_format_content( $raw_string ) {
return apply_filters( 'woocommerce_format_content', do_shortcode( shortcode_unautop( wpautop( $raw_string ) ) ), $raw_string );
}

/**
* Formats curency symbols when saved in settings
* @param string $value
* @param array $option
* @param string $raw_value
* @return string
*/
function wc_format_option_price_separators( $value, $option, $raw_value ) {
return wp_kses_post( $raw_value );
}
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_decimal_sep', 'wc_format_option_price_separators', 10, 3 );
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_thousand_sep', 'wc_format_option_price_separators', 10, 3 );

/**
* Formats decimals when saved in settings
* @param string $value
* @param array $option
* @param string $raw_value
* @return string
*/
function wc_format_option_price_num_decimals( $value, $option, $raw_value ) {
return is_null( $raw_value ) ? 2 : absint( $raw_value );
}
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_price_num_decimals', 'wc_format_option_price_num_decimals', 10, 3 );

/**
* Formats hold stock option and sets cron event up.
* @param string $value
* @param array $option
* @param string $raw_value
* @return string
*/
function wc_format_option_hold_stock_minutes( $value, $option, $raw_value ) {
$value = ! empty( $raw_value ) ? absint( $raw_value ) : ''; // Allow > 0 or set to ''

wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );

if ( '' !== $value ) {
wp_schedule_single_event( time() + ( absint( $value ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}

return $value;
}
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_hold_stock_minutes', 'wc_format_option_hold_stock_minutes', 10, 3 );

0 comments on commit 95b2a44

Please sign in to comment.