Skip to content

Commit

Permalink
Navigation: Fix performance regression (#58513)
Browse files Browse the repository at this point in the history
* Navigation: Fix performance regression

* update docblock

* use a different method for determining if we have submenus

* linting

* remove unused param

* remove the extra call to has_submenus

* update docblock

* Update packages/block-library/src/navigation/index.php

Co-authored-by: Andrei Draganescu <me@andreidraganescu.info>

* remove unnecessary changes

* remove early return

* remove unnecessary changes

---------

Co-authored-by: Andrei Draganescu <me@andreidraganescu.info>
  • Loading branch information
scruffian and draganescu committed Feb 2, 2024
1 parent 4076bb1 commit 6cd0cba
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 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 @@ -58,22 +64,37 @@ private static function is_responsive( $attributes ) {
* 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.
* @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 this is a page list then work out if any of the pages have children.
if ( 'core/page-list' === $inner_block->name ) {
$all_pages = get_pages(
array(
'sort_column' => 'menu_order,post_title',
'order' => 'asc',
)
);
foreach ( (array) $all_pages as $page ) {
if ( $page->post_parent ) {
static::$has_submenus = true;
break;
}
}
}
// If this is a navigation submenu then we know we have submenus.
if ( 'core/navigation-submenu' === $inner_block->name ) {
static::$has_submenus = true;
break;
}
}
return false;

return static::$has_submenus;
}

/**
Expand Down

0 comments on commit 6cd0cba

Please sign in to comment.