Skip to content

Commit

Permalink
Add filter to replace local nav bar mobile menu icon
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Oct 17, 2023
1 parent 2f37a46 commit 0edbac8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions mu-plugins/blocks/local-navigation-bar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

add_action( 'init', __NAMESPACE__ . '\init' );
add_filter( 'render_block_data', __NAMESPACE__ . '\update_block_attributes' );
add_filter( 'render_block', __NAMESPACE__ . '\customize_navigation_block_icon', 10, 2 );

/**
* Registers the block using the metadata loaded from the `block.json` file.
Expand Down Expand Up @@ -91,3 +92,51 @@ function update_block_attributes( $block ) {

return $block;
}

/**
* Replace a nested navigation block mobile button icon with a caret icon.
* Only applies if it has the 3 bar icon set, as this has an svg with <path> to update.
*
* @param string $block_content The block content.
* @param array $block The parsed block data.
*
* @return string
*/
function customize_navigation_block_icon( $block_content, $block ) {
return $block_content;

if ( ! empty( $block['blockName'] ) && 'wporg/local-navigation-bar' === $block['blockName'] ) {
$tag_processor = new \WP_HTML_Tag_Processor( $block_content );

if (
$tag_processor->next_tag( array(
'tag_name' => 'nav',
'class_name' => 'wp-block-navigation'
)
) ) {
if (
$tag_processor->next_tag( array(
'tag_name' => 'button',
'class_name' => 'wp-block-navigation__responsive-container-open'
) ) &&
$tag_processor->next_tag( 'path' )
) {
$tag_processor->set_attribute( 'd', 'M17.5 11.6L12 16l-5.5-4.4.9-1.2L12 14l4.5-3.6 1 1.2z' );
}

if (
$tag_processor->next_tag( array(
'tag_name' => 'button',
'class_name' => 'wp-block-navigation__responsive-container-close'
) ) &&
$tag_processor->next_tag( 'path' )
) {
$tag_processor->set_attribute( 'd', 'M6.5 12.4L12 8l5.5 4.4-.9 1.2L12 10l-4.5 3.6-1-1.2z' );
}

return $tag_processor->get_updated_html();
}
}

return $block_content;
}

0 comments on commit 0edbac8

Please sign in to comment.