Skip to content

Commit

Permalink
Fix PHP notice shown when rendering a navigation link block (#28999)
Browse files Browse the repository at this point in the history
* Fix undefined index issue in navigation link rendering

* Add test for plain link block rendering
  • Loading branch information
talldan authored Feb 15, 2021
1 parent 47bb513 commit 7cac6bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ function block_core_navigation_link_render_submenu_icon() {
function render_block_core_navigation_link( $attributes, $content, $block ) {
// Don't render the block's subtree if it is a draft.
if (
isset( $attributes['id'] ) && is_numeric( $attributes['id'] ) &&
isset( $attributes['id'] ) &&
is_numeric( $attributes['id'] ) &&
isset( $attributes['type'] ) &&
( 'post' === $attributes['type'] || 'page' === $attributes['type'] )
) {
$post = get_post( $attributes['id'] );
Expand Down
20 changes: 20 additions & 0 deletions phpunit/class-block-library-navigation-link-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,24 @@ function test_returns_link_for_category() {
) !== false
);
}

function test_returns_link_for_plain_link() {
$parsed_blocks = parse_blocks(
'<!-- wp:navigation-link {"label":"My Website","url":"https://example.com"} /-->'
);
$this->assertEquals( 1, count( $parsed_blocks ) );

$navigation_link_block = new WP_Block( $parsed_blocks[0], array() );
$this->assertEquals(
true,
strpos(
gutenberg_render_block_core_navigation_link(
$navigation_link_block->attributes,
array(),
$navigation_link_block
),
'My Website'
) !== false
);
}
}

0 comments on commit 7cac6bf

Please sign in to comment.