wppaste
WordPress

WP_Theme_JSON::get_block_nodes( array $theme_json, array $selectors = array(), array $options = array() ): array

Since
6.1.0, 6.3.0, 6.6.0, 6.7.0, 7.1.0
Source
wp-includes/class-wp-theme-json.php:3519
An internal method to get the block nodes from a theme.json file.

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 every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$theme_jsonarray
The theme.json converted to an array.
$selectorsarrayoptional
Optional list of selectors per block.Default: array()
$optionsarrayoptional
An array of options for now used for internal purposes only (may change without notice).Default: array()
  • $include_block_style_variationsbooldefault: false

    Include nodes for block style variations.
  • $include_node_paths_onlybooldefault: false

    Return only block nodes node paths.

Return value

array
The block nodes in theme.json.

Performance profile

How much work a call to WP_Theme_JSON::get_block_nodes() 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
26–27

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

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

WhenInstructionsCalls it makes
isset($value) && !$options26–27::get_viewport_media_queries()

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-dev52326–27791 fewer instruction than PHP 8.5
8.552426–2779
8.452426–2779
8.352426–2779
8.252426–2779
8.152426–2779
7.452426–2779

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::get_viewport_media_queries()
  • static::get_blocks_metadata()
  • static::append_to_selector()

Source code

	private static function get_block_nodes( $theme_json, $selectors = array(), $options = array() ) {		$nodes = array(); 		if ( ! isset( $theme_json['styles']['blocks'] ) ) {			return $nodes;		} 		$include_variations       = $options['include_block_style_variations'] ?? false;		$include_node_paths_only  = $options['include_node_paths_only'] ?? false;		$responsive_media_queries = static::get_viewport_media_queries( $theme_json['settings']['viewport'] ?? null ); 		// If only node paths are to be returned, skip selector assignment.		if ( ! $include_node_paths_only ) {			$selectors = empty( $selectors ) ? static::get_blocks_metadata() : $selectors;		} 		foreach ( $theme_json['styles']['blocks'] as $name => $node ) {			$node_path = array( 'styles', 'blocks', $name );			if ( $include_node_paths_only ) {				$variation_paths = array();				if ( $include_variations && isset( $node['variations'] ) ) {					foreach ( $node['variations'] as $variation => $variation_node ) {						$variation_paths[] = array(							'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),						);					}				}				$node = array(					'path' => $node_path,				);				if ( ! empty( $variation_paths ) ) {					$node['variations'] = $variation_paths;				}				$nodes[] = $node;			} else {				$selector = null;				if ( isset( $selectors[ $name ]['selector'] ) ) {					$selector = $selectors[ $name ]['selector'];				} 				$duotone_selector = null;				if ( isset( $selectors[ $name ]['duotone'] ) ) {					$duotone_selector = $selectors[ $name ]['duotone'];				} 				$feature_selectors = null;				if ( isset( $selectors[ $name ]['selectors'] ) ) {					$feature_selectors = $selectors[ $name ]['selectors'];				} 				$variation_selectors = array(); 				if ( $include_variations && isset( $node['variations'] ) ) {					foreach ( $node['variations'] as $variation => $node ) {						$variation_selectors[] = array(							'name'     => $variation,							'path'     => array( 'styles', 'blocks', $name, 'variations', $variation ),							'selector' => $selectors[ $name ]['styleVariations'][ $variation ],						);					}				} 				$nodes[] = array(					'name'       => $name,					'path'       => $node_path,					'selector'   => $selector,					'selectors'  => $feature_selectors,					'elements'   => $selectors[ $name ]['elements'] ?? array(),					'duotone'    => $duotone_selector,					'variations' => $variation_selectors,					'css'        => $selector,				); 				// Responsive block nodes: emit one node per breakpoint that has styles.				// These are rendered immediately after the base block node so that				// the cascade order is: .block{} → @media{.block{}}				foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {					if ( isset( $theme_json['styles']['blocks'][ $name ][ $breakpoint ] ) ) {						$nodes[] = array(							'name'        => $name,

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.

7.1.0
Added responsive block nodes for breakpoint-based styles.from the docblock
6.7.0
Added $include_node_paths_only option.from the docblock
6.6.0
Added optional selectors and options for generating block nodes.from the docblock
6.3.0
Refactored and stabilized selectors API.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.