wppaste
WordPress

WP_Theme_JSON::get_viewport_media_queries( mixed $viewport_settings = null, array $options = array() ): array

Since
7.1.0
Source
wp-includes/class-wp-theme-json.php:720
Returns CSS media queries for responsive viewport style states.

Description

Breakpoint values are read from settings.viewport, sanitized, and normalized before the media query strings are generated. By default, the returned keys are the theme.json style-state names (@mobile, @tablet).
When $options['include_desktop'] is truthy, @desktop is included.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$viewport_settingsmixedoptional
Viewport settings from theme.json.Default: null
$optionsarrayoptional
Options for generating media queries.Default: array()
  • $include_desktopbooldefault: false

    Whether to include the desktop media query.

Return value

array
Responsive media queries.

Performance profile

How much work a call to WP_Theme_JSON::get_viewport_media_queries() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–41

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 46.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::get_viewport_media_queries() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–41::sanitize_viewport_settings()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4614–415
8.54614–415
8.44614–4151 fewer instruction than PHP 8.3
8.34714–425
8.24714–425
8.14714–4251 fewer instruction than PHP 7.4
7.44814–425

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • static::sanitize_viewport_settings()

Used by · 4

Source code

	public static function get_viewport_media_queries( $viewport_settings = null, $options = array() ) {		$breakpoints = static::sanitize_viewport_settings( $viewport_settings ); 		$responsive_media_queries = array(); 		if ( isset( $breakpoints['mobile'] ) ) {			$responsive_media_queries['@mobile'] = "@media (width <= {$breakpoints['mobile']})";		} 		if ( isset( $breakpoints['tablet'] ) ) {			$responsive_media_queries['@tablet'] = isset( $breakpoints['mobile'] )				? sprintf(					'@media (%s < width <= %s)',					$breakpoints['mobile'],					$breakpoints['tablet']				)				: "@media (width <= {$breakpoints['tablet']})";		} 		if ( ! empty( $options['include_desktop'] ) ) {			if ( isset( $breakpoints['tablet'] ) ) {				$desktop_breakpoint = $breakpoints['tablet'];			} else {				$desktop_breakpoint = $breakpoints['mobile'];			} 			$responsive_media_queries['@desktop'] =				"@media (width > {$desktop_breakpoint})";		} 		return $responsive_media_queries;	}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.