wppaste
WordPress

WP_Theme_JSON::get_root_layout_rules( string $selector, array $block_metadata, array $options = array() ): string

Since
6.1.0, 6.6.0, 7.0.0
Source
wp-includes/class-wp-theme-json.php:4203
Outputs the CSS for layout rules on the root.

Compatibility

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

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$selectorstring
The root node selector.
$block_metadataarray
The metadata for the root block.
$optionsarrayoptional
An array of options for now used for internal purposes only.Default: array()

Return value

string
The additional root rules CSS.

Performance profile

How much work a call to WP_Theme_JSON::get_root_layout_rules() 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
34–102

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
!isset($value)34–47->get_layout_styles()
always53–66::get_property_value(), ->get_layout_styles()
always61–83::is_safe_css_declaration(), ::is_safe_css_declaration(), ->get_layout_styles()
isset($value)80–102::is_safe_css_declaration(), ::is_safe_css_declaration(), ::get_property_value(), ->get_layout_styles()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10434–10211
8.510434–10211
8.410434–10211
8.310434–10211
8.210434–10211
8.110434–102112 fewer instructions than PHP 7.4
7.410634–10411

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 · 3

  • static::is_safe_css_declaration()
  • static::get_property_value()
  • WP_Theme_JSON::get_layout_styles()Gets the CSS layout rules for a particular block from theme.json layout definitions.

Used by · 1

Source code

	public function get_root_layout_rules( $selector, $block_metadata, $options = array() ) {		$css              = '';		$settings         = $this->theme_json['settings'] ?? array();		$use_root_padding = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments']; 		/*		 * If there are content and wide widths in theme.json, output them		 * as custom properties on the body element so all blocks can use them.		 */		if ( isset( $settings['layout']['contentSize'] ) || isset( $settings['layout']['wideSize'] ) ) {			$content_size = $settings['layout']['contentSize'] ?? $settings['layout']['wideSize'];			$content_size = static::is_safe_css_declaration( 'max-width', $content_size ) ? $content_size : 'initial';			$wide_size    = $settings['layout']['wideSize'] ?? $settings['layout']['contentSize'];			$wide_size    = static::is_safe_css_declaration( 'max-width', $wide_size ) ? $wide_size : 'initial';			$css         .= static::ROOT_CSS_PROPERTIES_SELECTOR . ' { --wp--style--global--content-size: ' . $content_size . ';';			$css         .= '--wp--style--global--wide-size: ' . $wide_size . '; }';		} 		/*		 * Reset default browser margin on the body element.		 * This is set on the body selector **before** generating the ruleset		 * from the `theme.json`. This is to ensure that if the `theme.json` declares		 * `margin` in its `spacing` declaration for the `body` element then these		 * user-generated values take precedence in the CSS cascade.		 * @link https://github.com/WordPress/gutenberg/issues/36147.		 */		$css .= ':where(body) { margin: 0; }'; 		if ( $use_root_padding ) {			// Top and bottom padding are applied to the outer block container.			$css .= '.wp-site-blocks { padding-top: var(--wp--style--root--padding-top); padding-bottom: var(--wp--style--root--padding-bottom); }';			// Right and left padding are applied to the first container with `.has-global-padding` class.			$css .= '.has-global-padding { padding-right: var(--wp--style--root--padding-right); padding-left: var(--wp--style--root--padding-left); }';			// Alignfull children of the container with left and right padding have negative margins so they can still be full width.			$css .= '.has-global-padding > .alignfull { margin-right: calc(var(--wp--style--root--padding-right) * -1); margin-left: calc(var(--wp--style--root--padding-left) * -1); }';			// Nested children of the container with left and right padding that are not full aligned do not get padding, unless they are direct children of an alignfull flow container.			$css .= '.has-global-padding :where(:not(.alignfull.is-layout-flow) > .has-global-padding:not(.wp-block-block, .alignfull)) { padding-right: 0; padding-left: 0; }';			// Alignfull direct children of the containers that are targeted by the rule above do not need negative margins.			$css .= '.has-global-padding :where(:not(.alignfull.is-layout-flow) > .has-global-padding:not(.wp-block-block, .alignfull)) > .alignfull { margin-left: 0; margin-right: 0; }';		} 		// Skip outputting alignment styles when base_layout_styles is enabled.		// These styles target .wp-site-blocks which is only used by block themes.		if ( empty( $options['base_layout_styles'] ) ) {			$css .= '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }';			$css .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; }';			$css .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';		} 		// Block gap styles will be output unless explicitly set to `null`.		if ( isset( $this->theme_json['settings']['spacing']['blockGap'] ) ) {			$block_gap_value = static::get_property_value( $this->theme_json, array( 'styles', 'spacing', 'blockGap' ) );			$css            .= ":where(.wp-site-blocks) > * { margin-block-start: $block_gap_value; margin-block-end: 0; }";			$css            .= ':where(.wp-site-blocks) > :first-child { margin-block-start: 0; }';			$css            .= ':where(.wp-site-blocks) > :last-child { margin-block-end: 0; }'; 			// For backwards compatibility, ensure the legacy block gap CSS variable is still available.			$css .= static::ROOT_CSS_PROPERTIES_SELECTOR . " { --wp--style--block-gap: $block_gap_value; }";		}		$css .= $this->get_layout_styles( $block_metadata, $options ); 		return $css;	}

Changelog

Introduced in 6.1.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Parameter $options added.verified against source
7.0.0
Added $options parameter to control alignment styles output for classic themes.from the docblock
6.6.0
Use ROOT_CSS_PROPERTIES_SELECTOR for CSS custom properties and improved consistency of root padding rules.
Updated specificity of body margin reset and first/last child selectors.from the docblock
6.1.0
Introduced.from the docblock

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.