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