wppaste
WordPress

WP_Theme_JSON::get_styles_for_block( array $block_metadata ): string

Since
6.1.0, 6.6.0
Source
wp-includes/class-wp-theme-json.php:3815
Gets the CSS rules for a particular block from theme.json.

Compatibility

WordPress
since 6.6.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.

Return value

string
Styles for the block.

Performance profile

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

Executed per call on PHP 8.5. The body compiles to 674.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

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_styles_for_block() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11preg_quote()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev67311851 fewer instruction than PHP 8.5
8.567411851 fewer instruction than PHP 8.4
8.4675118512 fewer instructions than PHP 8.3
8.36871485
8.26871485
8.1687148513 more instructions than PHP 7.4
7.4674131–16485

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

  • _wp_array_get()Accesses an array in depth based on a path of keys.
  • array_last()Polyfill for `array_last()` function added in PHP 8.5.
  • static::get_feature_declarations_for_node()
  • static::get_viewport_media_queries()
  • static::update_paragraph_text_indent_selector()
  • static::update_button_width_declarations()
  • static::get_block_style_variation_feature_selector()
  • static::compute_style_properties()
  • static::get_block_name_from_metadata_path()
  • WP_Theme_JSON::process_pseudo_selectors()Processes pseudo-selectors for any node (block or variation).
  • WP_Theme_JSON::process_blocks_custom_css()Processes the CSS, to apply nesting.
  • static::to_ruleset()
Show all 16
  • static::process_blocks_custom_css()
  • WP_Theme_JSON::get_layout_styles()Gets the CSS layout rules for a particular block from theme.json layout definitions.
  • static::append_to_selector()
  • static::update_separator_declarations()

Source code

	public function get_styles_for_block( $block_metadata ) {		$node                     = _wp_array_get( $this->theme_json, $block_metadata['path'], array() );		$use_root_padding         = isset( $this->theme_json['settings']['useRootPaddingAwareAlignments'] ) && true === $this->theme_json['settings']['useRootPaddingAwareAlignments'];		$selector                 = $block_metadata['selector'];		$settings                 = $this->theme_json['settings'] ?? array();		$feature_declarations     = static::get_feature_declarations_for_node( $block_metadata, $node );		$is_root_selector         = static::ROOT_BLOCK_SELECTOR === $selector;		$media_query              = $block_metadata['media_query'] ?? null;		$responsive_media_queries = static::get_viewport_media_queries( $settings['viewport'] ?? null ); 		// Update text indent selector for paragraph blocks based on the textIndent setting.		$block_name           = $block_metadata['name'] ?? null;		$feature_declarations = static::update_paragraph_text_indent_selector( $feature_declarations, $settings, $block_name );		$block_elements       = $block_metadata['elements'] ?? array(); 		// Update button width declarations for percentage values to use calc() with block gap.		$feature_declarations = static::update_button_width_declarations( $feature_declarations, $settings ); 		// If there are style variations, generate the declarations for them, including any feature selectors the block may have.		$style_variation_declarations          = array();		$style_variation_custom_css            = array();		$style_variation_responsive_css        = array();		$style_variation_responsive_pseudo_css = array();		$style_variation_layout_metadata       = array();		if ( ! $media_query && ! empty( $block_metadata['variations'] ) ) {			foreach ( $block_metadata['variations'] as $style_variation ) {				$style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() ); 				// Generate any feature/subfeature style declarations for the current style variation.				$variation_declarations = static::get_feature_declarations_for_node( $block_metadata, $style_variation_node ); 				// Update text indent selector for paragraph blocks based on the textIndent setting.				$variation_declarations = static::update_paragraph_text_indent_selector( $variation_declarations, $settings, $block_name ); 				// Update button width declarations for percentage values to use calc() with block gap.				$variation_declarations = static::update_button_width_declarations( $variation_declarations, $settings ); 				// Combine selectors with style variation's selector and add to overall style variation declarations.				foreach ( $variation_declarations as $current_selector => $new_declarations ) {					$combined_selectors = static::get_block_style_variation_feature_selector( $style_variation, $current_selector ); 					// Add the new declarations to the overall results under the modified selector.					$style_variation_declarations[ $combined_selectors ] = $new_declarations;				} 				// Compute declarations for remaining styles not covered by feature level selectors.				$style_variation_declarations[ $style_variation['selector'] ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json ); 				// Process pseudo-selectors for this variation (e.g., :hover, :focus)				if ( isset( $block_metadata['name'] ) ) {					$block_name = $block_metadata['name'];				} elseif ( in_array( 'blocks', $block_metadata['path'], true ) && count( $block_metadata['path'] ) >= 3 ) {					$block_name = static::get_block_name_from_metadata_path( $block_metadata );				} else {					$block_name = null;				}				$variation_pseudo_declarations = $this->process_pseudo_selectors( $style_variation_node, $style_variation['selector'], $settings, $block_name, $block_metadata, $style_variation );				$style_variation_declarations  = array_merge( $style_variation_declarations, $variation_pseudo_declarations ); 				// Store custom CSS for the style variation.				if ( isset( $style_variation_node['css'] ) ) {					$style_variation_custom_css[ $style_variation['selector'] ] = $this->process_blocks_custom_css( $style_variation_node['css'], $style_variation['selector'] );				} 				// Store variation metadata and node for layout styles generation.				// Only store if the variation has blockGap defined.				if ( isset( $style_variation_node['spacing']['blockGap'] ) ) {					// Append block selector to the variation selector for proper targeting.					$variation_metadata_with_selector                                = $style_variation;					$variation_metadata_with_selector['selector']                    = $style_variation['selector'] . $block_metadata['css'];					$style_variation_layout_metadata[ $style_variation['selector'] ] = array(						'metadata' => $variation_metadata_with_selector,						'node'     => $style_variation_node,					);				} 				// Store responsive breakpoint CSS for the style variation.				// This includes both base properties and feature-level selectors.				$variation_responsive_css        = '';				$variation_responsive_pseudo_css = '';

Changelog

Introduced in 6.1.0. Unchanged from 6.7.7 through 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.

6.6.0
Setting a min-height of HTML when root styles have a background gradient or image.
Updated general global styles specificity to 0-1-0.
Fixed custom CSS output in block style variations.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.