WP_Theme_JSON::sanitize_viewport_settings( mixed $viewport_settings ): array

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

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.

Return

array Sanitized viewport breakpoint settings.

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

VersionDescription
7.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.