Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\PaymentGateways\Schema
AbstractPaymentGatewaySettingsSchema::sanitize_field_value
Sanitize field value based on type.
Method of the class: AbstractPaymentGatewaySettingsSchema{}
No Hooks.
Returns
Mixed. Sanitized value.
Usage
// private - for code of main (parent) class only $result = $this->sanitize_field_value( $type, $value );
- $type(string) (required)
- Field type.
- $value(mixed) (required)
- Field value.
AbstractPaymentGatewaySettingsSchema::sanitize_field_value() AbstractPaymentGatewaySettingsSchema::sanitize field value code WC 10.7.0
private function sanitize_field_value( string $type, $value ) {
switch ( $type ) {
case 'checkbox':
return wc_bool_to_string( $value );
case 'number':
if ( ! is_numeric( $value ) ) {
return '';
}
$int_value = filter_var( $value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE );
return null !== $int_value ? $int_value : floatval( $value );
case 'multiselect':
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
return is_string( $value ) ? array( sanitize_text_field( $value ) ) : array();
case 'textarea':
return sanitize_textarea_field( $value );
case 'email':
return sanitize_email( $value );
case 'password':
// Only trim — no stripslashes() (REST JSON is not magic-quote-escaped),
// no wp_strip_all_tags() or wc_clean() which would corrupt passwords
// containing '<', backslashes, or percent-like sequences.
// Non-scalar values (arrays, objects, null) from malformed requests → empty string.
// Scalars coerced to string to preserve numeric PINs/API keys.
return is_scalar( $value ) ? trim( (string) $value ) : '';
case 'color':
return sanitize_text_field( $value );
case 'text':
case 'safe_text':
case 'select':
case 'radio':
case 'image_width':
default:
return sanitize_text_field( $value );
}
}