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
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
- Scaling
- Scales with input
- Instructions
- 7–117
- Plugin surface
- None
- Called by
- 0
Only touches the object cache, via wp_cache_get().
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 202.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$block | 7 | none |
!$block && !empty($block_content) && !$attributes && !method_exists() && !->next_tag() | 56–61 | wp_get_global_settings(), method_exists(), method_exists(), declarations(), ->next_tag() |
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && method_exists() && !->next_tag() | 56–60 | wp_get_global_settings(), method_exists(), ::WP_Theme_JSON_Gutenberg(), declarations(), ->next_tag() |
!$block && !empty($block_content) && !$attributes && !defined('IS_GUTENBERG_PLUGIN') && !->next_tag() | 60–64 | wp_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 === null | 70–75 | wp_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 === null | 70–74 | wp_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–80 | wp_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–79 | wp_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 === null | 74–78 | wp_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 === null | 76–82 | wp_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 === null | 76–81 | wp_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–83 | wp_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–87 | wp_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–86 | wp_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 === null | 80–85 | wp_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–88 | wp_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–87 | wp_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–90 | wp_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–91 | wp_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–95 | wp_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–94 | wp_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 === null | 88–94 | wp_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 === null | 88–93 | wp_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–99 | wp_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–98 | wp_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–98 | wp_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 === null | 92–97 | wp_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 === null | 94–101 | wp_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 === null | 94–100 | wp_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–102 | wp_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–106 | wp_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–105 | wp_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 === null | 98–104 | wp_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–107 | wp_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–106 | wp_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–109 | wp_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–110 | wp_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–114 | wp_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–113 | wp_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–117 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 202 | 7–117 | 38 | |
| 8.5 | 202 | 7–117 | 38 | |
| 8.4 | 202 | 7–117 | 38 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 205 | 7–117 | 38 | |
| 8.2 | 205 | 7–117 | 38 | |
| 8.1 | 205 | 7–117 | 38 | 5 fewer instructions than PHP 7.4 |
| 7.4 | 210 | 7–118 | 38 |
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
- wp_get_global_settings()Gets the settings resulting of merging core, theme, and user data.
- block_core_navigation_get_layout_custom_property_declarations()Returns the custom properties used by the Navigation block for a layout.
- wp_unique_id()Gets unique ID.
- wp_style_engine_get_stylesheet_from_css_rules()Returns compiled CSS from a collection of selectors and declarations.
- WP_Theme_JSON_Gutenberg::get_viewport_media_queries()
- WP_Theme_JSON::get_viewport_media_queries()Returns CSS media queries for responsive viewport style states.
- WP_HTML_Tag_Processor::__construct()Constructor.
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.