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
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
- Scaling
- Scales with input
- Instructions
- 41–78
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 316.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!current_theme_supports() && !isset($block_metadata) && !$block_metadata && !$options && isset($value) | 41–63 | current_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 === null | 43–53 | current_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–78 | current_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 315 | 41–78 | 68 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 316 | 41–78 | 68 | |
| 8.4 | 316 | 41–78 | 68 | 16 fewer instructions than PHP 8.3 |
| 8.3 | 332 | 41–78 | 68 | |
| 8.2 | 332 | 41–78 | 68 | |
| 8.1 | 332 | 41–78 | 68 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 333 | 41–79 | 68 |
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
- current_theme_supports()Checks a theme's support for a given feature.
- block_has_support()Checks whether the current block type supports the feature requested.
- _wp_array_get()Accesses an array in depth based on a path of keys.
- wp_get_layout_definitions()Returns layout definitions, keyed by layout type.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- WP_Block_Type_Registry::get_instance()::get_registered()
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
- static::get_property_value()
- static::is_safe_css_declaration()
- static::to_ruleset()
Used by · 2
- WP_Theme_JSON::get_root_layout_rules()Outputs the CSS for layout rules on the root.
- WP_Theme_JSON::get_styles_for_block()Gets the CSS rules for a particular block from theme.json.
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.
Signature, return type and hooks compared across 5 parsed releases.
$options added.verified against source$types removed.verified against source$types parameter with $options array; base layout styles controlled via base_layout_styles option.from the docblockAbout 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.