From 98cb5ab97bca03e011e046d9cc92eae58b3e0381 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 3 May 2022 10:14:56 +0300 Subject: [PATCH 1/3] Add a current-menu-ancestor class to menus --- packages/block-library/src/navigation-submenu/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/navigation-submenu/index.php b/packages/block-library/src/navigation-submenu/index.php index 87c7c91481cddb..deae0bb3707204 100644 --- a/packages/block-library/src/navigation-submenu/index.php +++ b/packages/block-library/src/navigation-submenu/index.php @@ -255,6 +255,10 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { $inner_blocks_html .= $inner_block->render(); } + if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) { + $html = str_replace( 'wp-block-navigation-item__content', 'wp-block-navigation-item__content current-menu-ancestor', $html ); + } + $html .= sprintf( '', $inner_blocks_html From 36b14980e9c995f3fdd8479377f7a001c2468d0b Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 30 Nov 2022 14:18:35 +0200 Subject: [PATCH 2/3] Use WP_HTML_Tag_Processor --- packages/block-library/src/navigation-submenu/index.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation-submenu/index.php b/packages/block-library/src/navigation-submenu/index.php index deae0bb3707204..34166764e057c5 100644 --- a/packages/block-library/src/navigation-submenu/index.php +++ b/packages/block-library/src/navigation-submenu/index.php @@ -255,9 +255,11 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { $inner_blocks_html .= $inner_block->render(); } - if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) { - $html = str_replace( 'wp-block-navigation-item__content', 'wp-block-navigation-item__content current-menu-ancestor', $html ); + $tag_processor = new WP_HTML_Tag_Processor( $inner_blocks_html ); + while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) { + $tag_processor->add_class( 'current-menu-ancestor' ); } + $inner_blocks_html = $tag_processor->get_updated_html(); $html .= sprintf( '', From baef2619d79a1fcdd54c1b0d4dfdc64d481a4d76 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 1 Dec 2022 09:28:54 +0200 Subject: [PATCH 3/3] bugfix --- .../block-library/src/navigation-submenu/index.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation-submenu/index.php b/packages/block-library/src/navigation-submenu/index.php index 34166764e057c5..be6046076e76e1 100644 --- a/packages/block-library/src/navigation-submenu/index.php +++ b/packages/block-library/src/navigation-submenu/index.php @@ -255,11 +255,13 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { $inner_blocks_html .= $inner_block->render(); } - $tag_processor = new WP_HTML_Tag_Processor( $inner_blocks_html ); - while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) { - $tag_processor->add_class( 'current-menu-ancestor' ); + if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) { + $tag_processor = new WP_HTML_Tag_Processor( $html ); + while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) { + $tag_processor->add_class( 'current-menu-ancestor' ); + } + $html = $tag_processor->get_updated_html(); } - $inner_blocks_html = $tag_processor->get_updated_html(); $html .= sprintf( '
    %s
',