Automattic\WooCommerce\Internal\PushNotifications\Services
NotificationPreferencesService::sanitize_value
Apply per-key sanitization to a single preference's sub-options.
Unknown sub-keys are dropped; missing sub-keys fall back to their default. Today only enabled is recognized; future preference types extend this method (or its dispatch) to validate their additional sub-fields.
Method of the class: NotificationPreferencesService{}
No Hooks.
Returns
Array
Usage
// protected - for code of main (parent) or child class $result = $this->sanitize_value( $key, $value, $default_shape ): array;
- $key(string) (required)
- Preference key (e.g.
store_order). - $value(array) (required)
- Submitted sub-options for the key.
- $default_shape(array) (required)
- .
NotificationPreferencesService::sanitize_value() NotificationPreferencesService::sanitize value code WC 10.9.1
protected function sanitize_value( string $key, array $value, array $default_shape ): array {
unset( $key );
$sanitized = array();
foreach ( $default_shape as $sub_key => $sub_default ) {
if ( 'enabled' === $sub_key ) {
$sanitized[ $sub_key ] = array_key_exists( $sub_key, $value )
? (bool) $value[ $sub_key ]
: (bool) $sub_default;
continue;
}
if ( 'min_amount' === $sub_key ) {
if ( ! array_key_exists( $sub_key, $value ) || null === $value[ $sub_key ] ) {
$sanitized[ $sub_key ] = null;
continue;
}
$amount = (float) $value[ $sub_key ];
$sanitized[ $sub_key ] = $amount > 0 ? $amount : null;
continue;
}
if ( 'max_rating' === $sub_key ) {
if ( ! array_key_exists( $sub_key, $value ) || null === $value[ $sub_key ] ) {
$sanitized[ $sub_key ] = null;
continue;
}
$rating = (int) $value[ $sub_key ];
$sanitized[ $sub_key ] = ( $rating >= 1 && $rating <= 5 ) ? $rating : null;
continue;
}
if ( in_array( $sub_key, array( 'low_stock', 'out_of_stock', 'on_backorder' ), true ) ) {
$sanitized[ $sub_key ] = array_key_exists( $sub_key, $value )
? (bool) $value[ $sub_key ]
: (bool) $sub_default;
continue;
}
}//end foreach
return $sanitized;
}