-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Navigation] Makes URL dynamic for pages and posts #21050
Changes from all commits
7223aee
6610e9f
57c9cf1
0a79c60
a0db3c8
a53a729
c1cc9cb
de76e90
6487870
7c796b7
b58ebb6
6bfa53a
072dfde
67fa3af
6373217
4362429
a23b549
bdcaa77
9fded8a
8cbb49e
f474b7f
6207832
8a42dc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,12 @@ function NavigationLinkEdit( { | |
mergeBlocks, | ||
onReplace, | ||
} ) { | ||
const { label, opensInNewTab, url, nofollow, description } = attributes; | ||
const { label, opensInNewTab, url, id, nofollow, description } = attributes; | ||
const link = { | ||
url, | ||
id, | ||
opensInNewTab, | ||
title: label, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, these two aren't really the same thing. If I edit the label it doesn't mean that my page title is changed. Not sure what the alternative is, since we don't actually know the page title. |
||
}; | ||
const [ isLinkOpen, setIsLinkOpen ] = useState( false ); | ||
const itemLabelPlaceholder = __( 'Add link…' ); | ||
|
@@ -258,10 +260,10 @@ function NavigationLinkEdit( { | |
title: newTitle = '', | ||
url: newURL = '', | ||
opensInNewTab: newOpensInNewTab, | ||
id, | ||
id: newId, | ||
} = {} ) => | ||
setAttributes( { | ||
url: encodeURI( newURL ), | ||
url: newId ? undefined : newURL, | ||
label: ( () => { | ||
const normalizedTitle = newTitle.replace( | ||
/http(s?):\/\//gi, | ||
|
@@ -285,7 +287,7 @@ function NavigationLinkEdit( { | |
return escape( normalizedURL ); | ||
} )(), | ||
opensInNewTab: newOpensInNewTab, | ||
id, | ||
id: newId ? Number( newId ) : undefined, | ||
} ) | ||
} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -94,6 +94,22 @@ function block_core_navigation_link_render_submenu_icon() { | |||
return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" transform="rotate(90)"><path d="M8 5v14l11-7z"/><path d="M0 0h24v24H0z" fill="none"/></svg>'; | ||||
} | ||||
|
||||
/** | ||||
* Returns the link for the one nav iteem or false | ||||
* | ||||
* @param array $attrs The Navigation Link block attributes. | ||||
* | ||||
* @return string|bool Returns the determined link or false. | ||||
*/ | ||||
function block_core_navigation_link_get_url( $attrs ) { | ||||
if ( isset( $attrs['id'] ) ) { | ||||
return esc_url( get_permalink( $attrs['id'] ) ); | ||||
} elseif ( isset( $attrs['url'] ) ) { | ||||
return esc_url( $attrs['url'] ); | ||||
} | ||||
return false; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
} | ||||
|
||||
/** | ||||
* Renders the `core/navigation-link` block. | ||||
* | ||||
|
@@ -128,16 +144,23 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { | |||
$css_classes .= ' ' . $class_name; | ||||
}; | ||||
|
||||
$item_url = block_core_navigation_link_get_url( $attributes ); | ||||
$item_tag = $item_url ? 'a' : 'span'; | ||||
|
||||
$html = '<li class="' . esc_attr( $css_classes . ( $has_submenu ? ' has-child' : '' ) ) . | ||||
( $is_active ? ' current-menu-item' : '' ) . '"' . $style_attribute . '>' . | ||||
'<a class="wp-block-navigation-link__content"'; | ||||
'<' . $item_tag . ' class="wp-block-navigation-link__content"'; | ||||
|
||||
// Start appending HTML attributes to anchor tag. | ||||
if ( isset( $attributes['url'] ) ) { | ||||
$html .= ' href="' . esc_url( $attributes['url'] ) . '"'; | ||||
if ( $item_url ) { | ||||
$html .= ' href="' . esc_url( $item_url ) . '"'; | ||||
} | ||||
|
||||
if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { | ||||
if ( | ||||
$item_url && | ||||
isset( $attributes['opensInNewTab'] ) && | ||||
true === $attributes['opensInNewTab'] | ||||
) { | ||||
$html .= ' target="_blank" '; | ||||
} | ||||
// End appending HTML attributes to anchor tag. | ||||
|
@@ -171,10 +194,10 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { | |||
|
||||
$html .= '</span>'; | ||||
|
||||
$html .= '</a>'; | ||||
$html .= '</' . $item_tag . '>'; | ||||
// End anchor tag content. | ||||
|
||||
if ( $block->context['showSubmenuIcon'] && $has_submenu ) { | ||||
if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems unrelated. Is it needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was a suggestion from @adamziel to make sure we don't get PHP Notices sometimes. |
||||
// The submenu icon can be hidden by a CSS rule on the Navigation Block. | ||||
$html .= '<span class="wp-block-navigation-link__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>'; | ||||
} | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part probably needs some design influence, would be good to address as a follow-up.