Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
return '';
}

// Resolve URL binding if present
$url = $attributes['url'] ?? '';
if ( isset( $attributes['metadata']['bindings']['url']['source'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could throw warnings if $attributes['metadata']['bindings']['url'] was not defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the nested isset() check is safe in PHP. Unlike direct array access, isset() doesn't generate warnings when checking nested keys - it simply returns false if any key in the chain doesn't exist.

https://www.php.net/manual/en/function.isset.php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! TIL

$binding = $attributes['metadata']['bindings']['url'];
$source = get_block_bindings_source( $binding['source'] );
if ( $source ) {
$source_args = $binding['args'] ?? array();
$resolved_url = $source->get_value( $source_args, $block, 'url' );
if ( $resolved_url ) {
$url = $resolved_url;
}
}
}

$font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context );
$classes = array_merge(
$font_sizes['css_classes']
Expand All @@ -213,9 +227,9 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );

if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) {
if ( is_post_type_archive() && ! empty( $url ) ) {
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
if ( $attributes['url'] === $queried_archive_link ) {
if ( $url === $queried_archive_link ) {
$is_active = true;
}
}
Expand All @@ -231,8 +245,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
'<a class="wp-block-navigation-item__content" ';

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['url'] ) ) {
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $attributes['url'] ) ) . '"';
if ( ! empty( $url ) ) {
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $url ) ) . '"';
}

if ( $is_active ) {
Expand Down
20 changes: 17 additions & 3 deletions packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,30 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
return '';
}

// Resolve URL binding if present
$url = $attributes['url'] ?? '';
if ( isset( $attributes['metadata']['bindings']['url']['source'] ) ) {
$binding = $attributes['metadata']['bindings']['url'];
$source = get_block_bindings_source( $binding['source'] );
if ( $source ) {
$source_args = $binding['args'] ?? array();
$resolved_url = $source->get_value( $source_args, $block, 'url' );
if ( $resolved_url ) {
$url = $resolved_url;
}
}
}

$font_sizes = block_core_navigation_submenu_build_css_font_sizes( $block->context );
$style_attribute = $font_sizes['inline_styles'];

$has_submenu = count( $block->inner_blocks ) > 0;
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );

if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) {
if ( is_post_type_archive() && ! empty( $url ) ) {
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
if ( $attributes['url'] === $queried_archive_link ) {
if ( $url === $queried_archive_link ) {
$is_active = true;
}
}
Expand Down Expand Up @@ -142,7 +156,7 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
// If Submenus open on hover, we render an anchor tag with attributes.
// If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
if ( ! $open_on_click ) {
$item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
$item_url = $url;
// Start appending HTML attributes to anchor tag.
$html .= '<a class="wp-block-navigation-item__content"';

Expand Down
Loading