Sanitizes and normalizes viewport breakpoint settings.
Description
Keeps only supported breakpoint keys, trims valid CSS lengths, and returns the default breakpoints when no valid custom breakpoint is provided. When only one breakpoint is valid, it remains keyed by its configured state and uses a single max-width media query. When tablet is not larger than mobile, it is removed.
Parameters
$viewport_settingsmixedrequired- Viewport settings from theme.json.
Source
private static function sanitize_viewport_settings( $viewport_settings ) {
if ( ! is_array( $viewport_settings ) ) {
return static::DEFAULT_VIEWPORT_BREAKPOINTS;
}
$breakpoints = array();
foreach ( array_keys( static::DEFAULT_VIEWPORT_BREAKPOINTS ) as $breakpoint ) {
$value = $viewport_settings[ $breakpoint ] ?? null;
$px = static::get_viewport_breakpoint_value_in_pixels( $value );
if ( null !== $px ) {
$breakpoints[ $breakpoint ] = array(
'value' => trim( $value ),
'px' => $px,
);
}
}
if ( empty( $breakpoints ) ) {
return static::DEFAULT_VIEWPORT_BREAKPOINTS;
}
if ( 1 === count( $breakpoints ) ) {
$breakpoint = key( $breakpoints );
return array( $breakpoint => $breakpoints[ $breakpoint ]['value'] );
}
$sanitized = array( 'mobile' => $breakpoints['mobile']['value'] );
if ( isset( $breakpoints['tablet'] ) && $breakpoints['mobile']['px'] < $breakpoints['tablet']['px'] ) {
$sanitized['tablet'] = $breakpoints['tablet']['value'];
}
return $sanitized;
}
Changelog
| Version | Description |
|---|---|
| 7.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.