wppaste
WordPress

block_core_navigation_add_support_classes_to_container( string $block_content, array $block ): string

Since
7.1.0
Source
wp-includes/blocks/navigation.php:1622
Adds Navigation block support classes to inner list containers.

Description

State block support adds the generated wp-states-* class to the outer block wrapper. The Navigation block renders its menu items inside an inner wp-block-navigation__container list, so the same state class is also needed there for state styles to apply directly to the menu list.

Navigation also uses layout classes on its outer wrapper to define custom properties consumed by its inner containers. Viewport layout styles cannot change those classes, so equivalent custom properties and a scoping class are generated for each configured viewport layout.

Currently this is required as a workaround because of how difficult it is for nav child blocks to inherit styles through the complex responsive nav block html. The bug in https://github.com/WordPress/gutenberg/issues/62690 also prevents inheritance.

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 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$block_contentstring
The block content.
$blockarray
The full block, including name and attributes.

Return value

string
The updated block content.

Performance profile

How much work a call to block_core_navigation_add_support_classes_to_container() 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
Moderate

Only touches the object cache, via wp_cache_get().

Scaling
Scales with input

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

Instructions
7–117

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

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 it touches

  • cacheobject cachewp_cache_get()one call below block_core_navigation_add_support_classes_to_container()

Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 40 distinct outcomes

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

WhenInstructionsCalls it makes
!$block7none
!$block && !empty($block_content) && !$attributes && !method_exists() && !->next_tag()56–61wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && !->next_tag()56–60wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && !->next_tag()60–64wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && !is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null70–75wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && !is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null70–74wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute()
!$block && !empty($block_content) && !$attributes && !method_exists() && !is_string($class_attribute) && empty($styles)73–80wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && !is_string($class_attribute) && empty($styles)73–79wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && !is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null74–78wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null76–82wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null76–81wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match()
!$block && !empty($block_content) && !$attributes && !is_string($class_attribute) && empty($styles)77–83wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->get_updated_html()
28 further outcomes, up to 117 instructions
!$block && !empty($block_content) && !$attributes && !method_exists() && is_string($class_attribute) && empty($styles)79–87wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && is_string($class_attribute) && empty($styles)79–86wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && is_string($class_attribute) && empty($styles) && $state_class === null && $layout_class === null80–85wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && !is_string($class_attribute) && empty($styles)81–88wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && !is_string($class_attribute) && empty($styles)81–87wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && is_string($class_attribute) && empty($styles)83–90wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && !is_string($class_attribute) && empty($styles)85–91wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && is_string($class_attribute) && empty($styles)87–95wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && is_string($class_attribute) && empty($styles)87–94wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && !is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null88–94wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && !is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null88–93wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !method_exists() && !is_string($class_attribute) && !empty($styles)91–99wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && !is_string($class_attribute) && !empty($styles)91–98wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && is_string($class_attribute) && empty($styles)91–98wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && !is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null92–97wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null94–101wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null94–100wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !is_string($class_attribute) && !empty($styles)95–102wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !method_exists() && is_string($class_attribute) && !empty($styles)97–106wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && is_string($class_attribute) && !empty($styles)97–105wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && is_string($class_attribute) && !empty($styles) && $state_class === null && $layout_class === null98–104wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && !is_string($class_attribute) && !empty($styles)99–107wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && !is_string($class_attribute) && !empty($styles)99–106wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && is_string($class_attribute) && !empty($styles)101–109wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && !is_string($class_attribute) && !empty($styles)103–110wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !method_exists() && ->next_tag() && is_string($class_attribute) && !empty($styles)105–114wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && ->next_tag() && is_string($class_attribute) && !empty($styles)105–113wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()
!$block && !empty($block_content) && !$attributes && ->next_tag() && is_string($class_attribute) && !empty($styles)109–117wp_get_global_settings(), method_exists(), method_exists(), ::WP_Theme_JSON(), declarations(), ->next_tag(), ->get_attribute(), preg_match(), wp_unique_id(), ->add_class(), wp_style_engine_get_stylesheet_from_css_rules(), ->next_tag(), ->has_class(), ->has_class(), ->get_updated_html()

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-dev2027–11738
8.52027–11738
8.42027–117383 fewer instructions than PHP 8.3
8.32057–11738
8.22057–11738
8.12057–117385 fewer instructions than PHP 7.4
7.42107–11838

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

Source code

function block_core_navigation_add_support_classes_to_container( $block_content, $block ) {	if ( 'core/navigation' !== ( $block['blockName'] ?? null ) || empty( $block_content ) ) {		return $block_content;	} 	$attributes = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array();	$style      = is_array( $attributes['style'] ?? null ) ? $attributes['style'] : array();	if (		defined( 'IS_GUTENBERG_PLUGIN' ) &&		IS_GUTENBERG_PLUGIN &&		function_exists( 'gutenberg_resolve_style_state_aliases' )	) {		$style = gutenberg_resolve_style_state_aliases( $style, 'core/navigation' );	} 	$global_settings          = wp_get_global_settings();	$viewport_settings        = $global_settings['viewport'] ?? null;	$responsive_media_queries = array();	if ( method_exists( 'WP_Theme_JSON_Gutenberg', 'get_viewport_media_queries' ) ) {		$responsive_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries( $viewport_settings );	} elseif ( method_exists( 'WP_Theme_JSON', 'get_viewport_media_queries' ) ) {		$responsive_media_queries = WP_Theme_JSON::get_viewport_media_queries( $viewport_settings );	} 	$styles      = array();	$base_layout = is_array( $attributes['layout'] ?? null ) ? $attributes['layout'] : array();	foreach ( $responsive_media_queries as $breakpoint => $media_query ) {		$viewport_style  = is_array( $style[ $breakpoint ] ?? null ) ? $style[ $breakpoint ] : array();		$viewport_layout = is_array( $viewport_style['layout'] ?? null ) ? $viewport_style['layout'] : array();		if ( empty( $viewport_layout ) ) {			continue;		} 		$styles[] = array(			'declarations' => block_core_navigation_get_layout_custom_property_declarations(				array_replace( $base_layout, $viewport_layout )			),			'rules_group'  => $media_query,		);	} 	$processor = new WP_HTML_Tag_Processor( $block_content );	if ( ! $processor->next_tag() ) {		return $block_content;	} 	$class_attribute = $processor->get_attribute( 'class' );	$state_class     = null;	if ( is_string( $class_attribute ) && preg_match( '/\bwp-states-[a-f0-9]{8}\b/', $class_attribute, $matches ) ) {		$state_class = $matches[0];	} 	$layout_class = null;	if ( ! empty( $styles ) ) {		$layout_class = wp_unique_id( 'wp-block-navigation-' );		// The inner selector includes both Navigation classes so it overrides the		// default layout custom properties set by `.wp-block-navigation.items-*`.		$selector = ".wp-block-navigation.{$layout_class},.wp-block-navigation.wp-block-navigation__container.{$layout_class}";		foreach ( $styles as &$style_rule ) {			$style_rule['selector'] = $selector;		}		unset( $style_rule ); 		$processor->add_class( $layout_class );		wp_style_engine_get_stylesheet_from_css_rules(			$styles,			array( 'context' => 'block-supports' )		);	} 	if ( null === $state_class && null === $layout_class ) {		return $block_content;	} 	while ( $processor->next_tag() ) {		// Custom overlay content can include nested Navigation blocks.		// Avoid applying the outer Navigation classes to an inner nav block.		if ( $processor->has_class( 'wp-block-navigation' ) && ! $processor->has_class( 'wp-block-navigation__container' ) ) {			break;		}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/navigation.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.