Skip to content

Navigation Link, Navigation Submenu: Fix undefined key warning #69951

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

Merged
merged 3 commits into from
Apr 21, 2025

Conversation

Maikuolan
Copy link
Contributor

@Maikuolan Maikuolan commented Apr 20, 2025

What?

This pull request attempts to fix an undefined key warning.

Why?

At a few WordPress websites I manage, I've been regularly seeing these kinds of errors generated for a while now:

[18-Apr-2025 07:53:08 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[18-Apr-2025 07:53:08 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[18-Apr-2025 21:45:39 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[18-Apr-2025 21:45:39 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[18-Apr-2025 21:49:35 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[18-Apr-2025 21:49:35 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 00:17:29 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 00:17:29 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 00:17:29 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 00:17:29 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 08:46:34 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 08:46:34 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 10:20:02 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 10:20:02 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 10:58:54 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91
[19-Apr-2025 10:58:54 UTC] PHP Warning:  Undefined array key "url" in /home/[REDACTED]/public_html/wp-includes/blocks/navigation-submenu.php on line 91

Seeing as the solution seems simple enough and hasn't yet been implemented, I decided to try submitting a pull request here to address it (which, if accepted, I assume will eventually find its way upstream and into some future WordPress update at some point in the future).

Although I'm not entirely sure of the exact state and circumstances of the implementation at the time when the error occurs (i.e., the elements available to $attributes, as populated at the points where the function is called/invoked), a simple isset() check feels the most simple, direct, and logical solution for the problem. At most other points in the affected function where an element of $attributes is used, its use is similarly preceded by an isset() check; the exception which this pull request addresses is one of the very few exceptions. This pull request brings that exception into line with the various other points of use which are preceded by isset().

How?

Uses isset() to check the existence of the key before attempting to compare it.

Copy link

github-actions bot commented Apr 20, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Maikuolan <maikuolan@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Apr 20, 2025
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Maikuolan! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

AFAIK, there is no way to empty url attribute from the UI, but we can remove the attribute from the block via the code editor, which causes the issue reported.

This PR makes sense to me, but do you mind updating the Navigation Link block as well?

if ( $attributes['url'] === $queried_archive_link ) {

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Navigation Link Affects the Navigation Link Block [Block] Submenu Affects the Submenu Block - for submenus in navigation labels Apr 21, 2025
@Maikuolan
Copy link
Contributor Author

but do you mind updating the Navigation Link block as well?

Done. :-)

@t-hamano t-hamano changed the title Fix undefined key warning. Navigation Link, Navigation Submenu: Fix undefined key warning Apr 21, 2025
@t-hamano
Copy link
Contributor

Thanks for the update!

Sorry, I found the more ideal logic. The current PR executes get_post_type_archive_link() even though $attributes['url'] is not set. Additionally, I think the empty() is better than isset() to skip the check when the URL is empty,

For better performance, I'd suggest the following logic:

if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) {
	$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
	if ( $attributes['url'] === $queried_archive_link ) {
		$is_active = true;
	}
}

@Maikuolan
Copy link
Contributor Author

Good catch. :-) Editing now.

@Maikuolan
Copy link
Contributor Author

Done. :-)

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Thanks for your work!

@t-hamano t-hamano merged commit b749af3 into WordPress:trunk Apr 21, 2025
59 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.8 milestone Apr 21, 2025
@Maikuolan Maikuolan deleted the patch-1 branch April 21, 2025 05:26
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…ress#69951)

* Fix undefined key warning.

* Fix undefined key warning.

* Use better logic.

Co-authored-by: Maikuolan <maikuolan@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Link Affects the Navigation Link Block [Block] Submenu Affects the Submenu Block - for submenus in navigation First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants