Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Fix performance regression #58513

Merged
merged 13 commits into from
Feb 2, 2024
47 changes: 30 additions & 17 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
* Helper functions used to render the navigation block.
*/
class WP_Navigation_Block_Renderer {

/**
* Used to determine whether or not a navigation has submenus.
*/
private static $has_submenus = false;

/**
* Used to determine which blocks are wrapped in an <li>.
*
Expand Down Expand Up @@ -57,23 +63,22 @@
/**
* Returns whether or not a navigation has a submenu.
*
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return bool Returns whether or not a navigation has a submenu.
* @param array $inner_blocks An array of inner blocks.
* @return bool Returns whether or not a navigation has a submenu and also sets the member variable.
*/
private static function has_submenus( $inner_blocks ) {
if ( true === static::$has_submenus ) {
return static::$has_submenus;
}

foreach ( $inner_blocks as $inner_block ) {
$inner_block_content = $inner_block->render();
$p = new WP_HTML_Tag_Processor( $inner_block_content );
if ( $p->next_tag(
array(
'name' => 'LI',
'class_name' => 'has-child',
)
) ) {
return true;
if ( 'core/navigation-submenu' === $inner_block->name ) {
static::$has_submenus = true;
return static::$has_submenus;
draganescu marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false;

return static::$has_submenus;
}

/**
Expand All @@ -83,8 +88,8 @@
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return bool Returns whether or not to load the view script.
*/
private static function is_interactive( $attributes, $inner_blocks ) {

Check warning on line 91 in packages/block-library/src/navigation/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused function parameter $inner_blocks.
$has_submenus = static::has_submenus( $inner_blocks );
$has_submenus = static::$has_submenus;
$is_responsive_menu = static::is_responsive( $attributes );
return ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
}
Expand All @@ -107,6 +112,7 @@
*/
private static function get_markup_for_inner_block( $inner_block ) {
$inner_block_content = $inner_block->render();

if ( ! empty( $inner_block_content ) ) {
if ( static::does_block_need_a_list_item_wrapper( $inner_block ) ) {
return '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>';
Expand All @@ -124,9 +130,6 @@
* @return string Returns the html for the inner blocks of the navigation block.
*/
private static function get_inner_blocks_html( $attributes, $inner_blocks ) {
$has_submenus = static::has_submenus( $inner_blocks );
$is_interactive = static::is_interactive( $attributes, $inner_blocks );

$style = static::get_styles( $attributes );
$class = static::get_classes( $attributes );
$container_attributes = get_block_wrapper_attributes(
Expand Down Expand Up @@ -163,6 +166,8 @@
}

// Add directives to the submenu if needed.
$has_submenus = static::$has_submenus;
$is_interactive = static::is_interactive( $attributes, $inner_blocks );
if ( $has_submenus && $is_interactive ) {
$tags = new WP_HTML_Tag_Processor( $inner_blocks_html );
$inner_blocks_html = block_core_navigation_add_directives_to_submenu( $tags, $attributes );
Expand Down Expand Up @@ -632,6 +637,14 @@
unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );

$inner_blocks = static::get_inner_blocks( $attributes, $block );

// This sets the `has_submenus` member variable
// which is used by other functions in the class
// to determine whether or not a navigation has submenus.
// This has to be called before any other code that relies
// on the has_submenus member variable.
static::has_submenus( $inner_blocks );
draganescu marked this conversation as resolved.
Show resolved Hide resolved

// Prevent navigation blocks referencing themselves from rendering.
if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
return '';
Expand All @@ -642,7 +655,7 @@
return sprintf(
'<nav %1$s>%2$s</nav>',
static::get_nav_wrapper_attributes( $attributes, $inner_blocks ),
static::get_wrapper_markup( $attributes, $inner_blocks )
static::get_wrapper_markup( $attributes, $inner_blocks ),

Check failure on line 658 in packages/block-library/src/navigation/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Trailing comma's are not allowed in function calls in PHP 7.2 or earlier
);
}
}
Expand Down