*/ $amp_sub_menu = $sub_menu->cloneNode( true ); /** @var Element $amp_expand_button */ $amp_expand_button = $expand_button->cloneNode( true ); $sub_menu->setAttribute( Attribute::CLASS_, $sub_menu->getAttribute( Attribute::CLASS_ ) . ' display-on-desktop' ); $expand_button->setAttribute( Attribute::CLASS_, $expand_button->getAttribute( Attribute::CLASS_ ) . ' display-on-desktop' ); $menu_item->appendChild( $amp_expand_button ); $menu_item->appendChild( $amp_sub_menu ); $this->update_twentynineteen_main_nested_menu( $amp_sub_menu ); $amp_sub_menu->setAttribute( Attribute::CLASS_, $amp_sub_menu->getAttribute( Attribute::CLASS_ ) . ' expanded-true display-on-mobile' ); $amp_expand_button->setAttribute( Attribute::CLASS_, $amp_expand_button->getAttribute( Attribute::CLASS_ ) . ' display-on-mobile' ); // Handle buttons. $amp_expand_button->addAmpAction( 'tap', "$sidebar_id.open" ); $back_button = $this->get_first_element( $xpaths['close_button_in_submenu'], $amp_sub_menu ); if ( ! empty( $back_button ) ) { $back_button->addAmpAction( 'tap', "$sidebar_id.close" ); } $amp_nested_menu->appendChild( $amp_sub_menu ); $amp_sidebar->appendChild( $amp_nested_menu ); $menu_item->appendChild( $amp_sidebar ); } } /** * Update the markup of the nested menu to AMP compatible markup. * * @param Element $sub_menu Element of sub-menu from main navigation. * * @return void */ public function update_twentynineteen_main_nested_menu( Element $sub_menu ) { $xpaths = [ 'menu_items' => './li[ contains( @class, "menu-item" ) and contains( @class, "menu-item-has-children" ) ]', 'expand_button' => './button[ @class = "submenu-expand" ]', 'back_button' => './li[ contains( @class, "mobile-parent-nav-menu-item" ) ]//button[ contains( @class, "menu-item-link-return" ) ]', 'submenu' => './ul[ @class = "sub-menu" ]', ]; $menu_items = $this->dom->xpath->query( $xpaths['menu_items'], $sub_menu ); if ( 0 === $menu_items->length ) { return; } /** @var Element $menu_item */ foreach ( $menu_items as $menu_item ) { $nested_sub_menu = $this->get_first_element( $xpaths['submenu'], $menu_item ); if ( empty( $nested_sub_menu ) ) { continue; } $this->update_twentynineteen_main_nested_menu( $nested_sub_menu ); $back_button = $this->get_first_element( $xpaths['back_button'], $nested_sub_menu ); if ( ! empty( $back_button ) ) { $back_button->setAttribute( Attribute::AMP_NESTED_SUBMENU_CLOSE, '' ); } $open_button = $this->get_first_element( $xpaths['expand_button'], $menu_item ); if ( ! empty( $open_button ) ) { $open_button->setAttribute( Attribute::AMP_NESTED_SUBMENU_OPEN, '' ); } $amp_nested_menu_div = AMP_DOM_Utils::create_node( $this->dom, Tag::DIV, [ Attribute::AMP_NESTED_SUBMENU => '', ] ); $nested_sub_menu->setAttribute( Attribute::CLASS_, $nested_sub_menu->getAttribute( Attribute::CLASS_ ) . ' expanded-true' ); $amp_nested_menu_div->appendChild( $nested_sub_menu ); $menu_item->appendChild( $amp_nested_menu_div ); } } }