Skip to content

Instantly share code, notes, and snippets.

@noisysocks
Created July 31, 2020 06:11
Show Gist options
  • Save noisysocks/c3624a74073259df34719692d3cde42f to your computer and use it in GitHub Desktop.
Save noisysocks/c3624a74073259df34719692d3cde42f to your computer and use it in GitHub Desktop.
Render navigation block if theme requests it
diff --git a/lib/compat.php b/lib/compat.php
index 5941bbb1f2..c0e7150384 100644
--- a/lib/compat.php
+++ b/lib/compat.php
@@ -640,6 +640,69 @@ function gutenberg_output_html_nav_menu_item( $item_output, $item, $depth, $args
}
add_filter( 'walker_nav_menu_start_el', 'gutenberg_output_html_nav_menu_item', 10, 4 );
+function gutenberg_capture_menu_items( $menu_items, $args ) {
+ $args->menu_items = $menu_items;
+ return $menu_items;
+}
+add_filter( 'wp_nav_menu_objects', 'gutenberg_capture_menu_items', 10, 2 );
+
+function gutenberg_convert_menu_item_to_block( $menu_item, &$menu_items_by_parent_id ) {
+ if ( 'html' === $menu_item->type ) {
+ return parse_blocks( $menu_item->content )[0];
+ }
+
+ $inner_blocks = array();
+
+ if ( isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ) {
+ foreach ( $menu_items_by_parent_id[ $menu_item->ID ] as $child_menu_item ) {
+ $inner_blocks[] = gutenberg_convert_menu_item_to_block(
+ $child_menu_item,
+ $menu_items_by_parent_id
+ );
+ }
+ }
+
+ return array(
+ 'blockName' => 'core/navigation-link',
+ 'attrs' => array(
+ 'label' => $menu_item->title,
+ 'url' => $menu_item->url,
+ ),
+ 'innerBlocks' => $inner_blocks,
+ );
+}
+
+function gutenberg_handle_block_nav_menu( $nav_menu, $args ) {
+ if ( ! current_theme_supports( 'block-nav-menus' ) ) {
+ return $nav_menu;
+ }
+
+ $menu_items_by_parent_id = array();
+ foreach ( $args->menu_items as $menu_item ) {
+ $menu_items_by_parent_id[ intval( $menu_item->menu_item_parent ) ][] = $menu_item;
+ }
+
+ $inner_blocks = array();
+
+ if ( isset( $menu_items_by_parent_id[0] ) ) {
+ foreach ( $menu_items_by_parent_id[0] as $menu_item ) {
+ $inner_blocks[] = gutenberg_convert_menu_item_to_block(
+ $menu_item,
+ $menu_items_by_parent_id
+ );
+ }
+ }
+
+ $navigation_block = array(
+ 'blockName' => 'core/navigation',
+ 'attrs' => array(),
+ 'innerBlocks' => $inner_blocks,
+ );
+
+ return render_block( $navigation_block );
+}
+add_filter( 'wp_nav_menu', 'gutenberg_handle_block_nav_menu', 10, 2 );
+
/**
* Amends the paths to preload when initializing edit post.
*
Index: src/wp-content/themes/twentytwenty/functions.php
===================================================================
--- src/wp-content/themes/twentytwenty/functions.php (revision 48655)
+++ src/wp-content/themes/twentytwenty/functions.php (working copy)
@@ -142,6 +142,7 @@
$loader = new TwentyTwenty_Script_Loader();
add_filter( 'script_loader_tag', array( $loader, 'filter_script_loader_tag' ), 10, 2 );
+ add_theme_support( 'block-nav-menus' );
}
add_action( 'after_setup_theme', 'twentytwenty_theme_support' );
Index: src/wp-content/themes/twentytwenty/header.php
===================================================================
--- src/wp-content/themes/twentytwenty/header.php (revision 48655)
+++ src/wp-content/themes/twentytwenty/header.php (working copy)
@@ -83,25 +83,25 @@
<?php
if ( has_nav_menu( 'primary' ) || ! has_nav_menu( 'expanded' ) ) {
- ?>
- <nav class="primary-menu-wrapper" aria-label="<?php esc_attr_e( 'Horizontal', 'twentytwenty' ); ?>" role="navigation">
+ if ( has_nav_menu( 'primary' ) ) {
- <ul class="primary-menu reset-list-style">
+ wp_nav_menu(
+ array(
+ 'container' => '',
+ 'items_wrap' => '%3$s',
+ 'theme_location' => 'primary',
+ )
+ );
- <?php
- if ( has_nav_menu( 'primary' ) ) {
+ } elseif ( ! has_nav_menu( 'expanded' ) ) {
+ ?>
- wp_nav_menu(
- array(
- 'container' => '',
- 'items_wrap' => '%3$s',
- 'theme_location' => 'primary',
- )
- );
+ <nav class="primary-menu-wrapper" aria-label="<?php esc_attr_e( 'Horizontal', 'twentytwenty' ); ?>" role="navigation">
- } elseif ( ! has_nav_menu( 'expanded' ) ) {
+ <ul class="primary-menu reset-list-style">
+ <?php
wp_list_pages(
array(
'match_menu_classes' => true,
@@ -110,15 +110,14 @@
'walker' => new TwentyTwenty_Walker_Page(),
)
);
+ ?>
- }
- ?>
-
</ul>
</nav><!-- .primary-menu-wrapper -->
- <?php
+ <?php
+ }
}
if ( true === $enable_header_search || has_nav_menu( 'expanded' ) ) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment