wppaste
WordPress

WP_Theme_JSON::get_layout_styles( array $block_metadata, array $options = array() ): string

Since
6.1.0, 6.3.0, 6.5.1, 6.5.3, 6.6.0, 7.0.0
Source
wp-includes/class-wp-theme-json.php:2219
Gets the CSS layout rules for a particular block from theme.json layout definitions.

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

$block_metadataarray
Metadata about the block to get styles for.
$optionsarrayoptional
An array of options for now used for internal purposes only.Default: array()

Return value

string
Layout styles for the block.

Performance profile

How much work a call to WP_Theme_JSON::get_layout_styles() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
41–78

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Theme_JSON::get_layout_styles()

What one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
!current_theme_supports() && !isset($block_metadata) && !$block_metadata && !$options && isset($value)41–63current_theme_supports(), wp_get_layout_definitions(), ::get_property_value()
!current_theme_supports() && !isset($block_metadata) && !$block_metadata && !$options && $selector !== false && empty($block_type) && !is_array($block_gap_value) && $block_gap_value === null43–53current_theme_supports(), wp_get_layout_definitions()
!current_theme_supports() && !isset($block_metadata) && !$block_metadata && !$options && isset($value) && is_array($block_gap_value) && isset($block_gap_value)61–78current_theme_supports(), wp_get_layout_definitions(), ::get_property_value(), ::get_property_value(), ::get_property_value()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev31541–78681 fewer instruction than PHP 8.5
8.531641–7868
8.431641–786816 fewer instructions than PHP 8.3
8.333241–7868
8.233241–7868
8.133241–78681 fewer instruction than PHP 7.4
7.433341–7968

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

Used by · 2

Source code

	protected function get_layout_styles( $block_metadata, $options = array() ) {		$block_rules = '';		$block_type  = null; 		// Skip outputting layout styles if explicitly disabled.		if ( current_theme_supports( 'disable-layout-styles' ) ) {			return $block_rules;		} 		if ( isset( $block_metadata['name'] ) ) {			$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_metadata['name'] );			if ( ! block_has_support( $block_type, 'layout', false ) && ! block_has_support( $block_type, '__experimentalLayout', false ) ) {				return $block_rules;			}		} 		$selector                 = $block_metadata['selector'] ?? '';		$has_block_gap_support    = isset( $this->theme_json['settings']['spacing']['blockGap'] );		$has_fallback_gap_support = ! $has_block_gap_support; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback gap styles support.		$node                     = $options['node'] ?? _wp_array_get( $this->theme_json, $block_metadata['path'], array() );		$layout_definitions       = wp_get_layout_definitions();		$layout_selector_pattern  = '/^[a-zA-Z0-9\-\.\,\ *+>:\(\)]*$/'; // Allow alphanumeric classnames, spaces, wildcard, sibling, child combinator and pseudo class selectors. 		/*		 * Gap styles will only be output if the theme has block gap support, or supports a fallback gap.		 * Default layout gap styles will be skipped for themes that do not explicitly opt-in to blockGap with a `true` or `false` value.		 */		if ( $has_block_gap_support || $has_fallback_gap_support ) {			$block_gap_value = null;			// Use a fallback gap value if block gap support is not available.			if ( ! $has_block_gap_support ) {				$block_gap_value = static::ROOT_BLOCK_SELECTOR === $selector ? '0.5em' : null;				if ( ! empty( $block_type ) ) {					$block_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? null;				}			} else {				$block_gap_value = static::get_property_value( $node, array( 'spacing', 'blockGap' ) );			} 			// Support split row / column values and concatenate to a shorthand value.			if ( is_array( $block_gap_value ) ) {				if ( isset( $block_gap_value['top'] ) && isset( $block_gap_value['left'] ) ) {					$gap_row         = static::get_property_value( $node, array( 'spacing', 'blockGap', 'top' ) );					$gap_column      = static::get_property_value( $node, array( 'spacing', 'blockGap', 'left' ) );					$block_gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;				} else {					// Skip outputting gap value if not all sides are provided.					$block_gap_value = null;				}			} 			// If the block should have custom gap, add the gap styles.			if ( null !== $block_gap_value && false !== $block_gap_value && '' !== $block_gap_value ) {				foreach ( $layout_definitions as $layout_definition_key => $layout_definition ) {					// Allow outputting fallback gap styles for flex and grid layout types when block gap support isn't available.					if ( ! $has_block_gap_support && 'flex' !== $layout_definition_key && 'grid' !== $layout_definition_key ) {						continue;					} 					$class_name    = $layout_definition['className'] ?? false;					$spacing_rules = $layout_definition['spacingStyles'] ?? array(); 					if (						! empty( $class_name ) &&						! empty( $spacing_rules )					) {						foreach ( $spacing_rules as $spacing_rule ) {							$declarations = array();							if (								isset( $spacing_rule['selector'] ) &&								preg_match( $layout_selector_pattern, $spacing_rule['selector'] ) &&								! empty( $spacing_rule['rules'] )							) {								// Iterate over each of the styling rules and substitute non-string values such as `null` with the real `blockGap` value.								foreach ( $spacing_rule['rules'] as $css_property => $css_value ) {									$current_css_value = is_string( $css_value ) ? $css_value : $block_gap_value;									if ( static::is_safe_css_declaration( $css_property, $current_css_value ) ) {										$declarations[] = array(											'name'  => $css_property,											'value' => $current_css_value,

Changelog

Introduced in 6.1.0. 2 changes 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.4
Parameter $types removed.verified against source
7.0.0
Replaced $types parameter with $options array; base layout styles controlled via base_layout_styles option.from the docblock
6.6.0
Updated layout style specificity to be compatible with overall 0-1-0 specificity in global styles.from the docblock
6.5.3
Add types parameter to check if only base layout styles are needed.from the docblock
6.5.1
Only output rules referencing content and wide sizes when values exist.from the docblock
6.3.0
Reduced specificity for layout margin rules.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.