Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Account
Controller::sanitize_setting_value
Sanitize setting value based on its type.
Method of the class: Controller{}
No Hooks.
Returns
Mixed. Sanitized value.
Usage
// private - for code of main (parent) class only $result = $this->sanitize_setting_value( $setting_type, $value );
- $setting_type(string) (required)
- Setting type.
- $value(mixed) (required)
- Setting value.
Controller::sanitize_setting_value() Controller::sanitize setting value code WC 10.4.3
private function sanitize_setting_value( $setting_type, $value ) {
// Normalize WooCommerce setting types to REST API schema types.
$type_map = array(
'single_select_page' => 'select',
'single_select_page_with_search' => 'select',
);
$setting_type = $type_map[ $setting_type ] ?? $setting_type;
switch ( $setting_type ) {
case 'text':
return sanitize_text_field( $value );
case 'textarea':
return sanitize_textarea_field( $value );
case 'number':
if ( ! is_numeric( $value ) ) {
return 0;
}
return filter_var( $value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE ) ?? floatval( $value );
case 'checkbox':
// Ensure we have a scalar value for checkbox settings.
if ( is_array( $value ) ) {
$value = ! empty( $value ); // Convert array to boolean based on emptiness.
}
return wc_bool_to_string( $value );
case 'select':
return sanitize_text_field( $value );
case 'multiselect':
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
if ( is_string( $value ) ) {
return array( sanitize_text_field( $value ) );
}
if ( is_scalar( $value ) ) {
return array( sanitize_text_field( (string) $value ) );
}
return array();
default:
// If a type is not explicitly handled, treat it as text.
return sanitize_text_field( $value );
}
}