close
Skip to content

Prevents an infinite loop when the parent menu item of a menu item is itself.#3566

Closed
mhkuu wants to merge 1 commit intoWordPress:trunkfrom
mhkuu:feature/56926
Closed

Prevents an infinite loop when the parent menu item of a menu item is itself.#3566
mhkuu wants to merge 1 commit intoWordPress:trunkfrom
mhkuu:feature/56926

Conversation

@mhkuu
Copy link
Copy Markdown

@mhkuu mhkuu commented Nov 4, 2022

Co-authored-by: Hugo de Vos hdvos93@gmail.com

Created during Yoast Contributor Day on Friday 4 November.

Prevents an infinite loop when the parent menu item of a menu item is itself. We do not know the root cause of this issue, but with this PR, we add a safeguard to make sure the menu is still being output. This PR also adds a unit test to confirm the output is as expected.

Trac ticket: https://core.trac.wordpress.org/ticket/56926


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

… itself.

Co-authored-by: Hugo de Vos <hdvos93@gmail.com>
Copy link
Copy Markdown
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Added a couple of notes inline.

/**
* @ticket 56926
*/
public function test_if_parent_is_self() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function test_if_parent_is_self() {
public function test_self_parented_menu_items_should_not_cause_infinite_recursion() {

Comment on lines +211 to +212
// Do not calculate the depth when the current item has itself as parent.
while ( $menu_item_parent && (int) $menu_item_parent !== $menu_item_key ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if an item lists it's own grandchild as a parent, does that cause an infinite loop too? (Same question applies for further descendants)

Copy link
Copy Markdown
Contributor

@azaozz azaozz Nov 17, 2022

Choose a reason for hiding this comment

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

if an item lists it's own grandchild as a parent, does that cause an infinite loop too

Yea, probably. Since this seems to be coming from plugins thinking it needs a "sensible maximum depth", i.e. the while() loop can be stopped after x number of times. For number of sub-menus such max number would be something like 50 perhaps (can you imagine what a menu would look like with 50 levels of sub-menus?).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another thing here: (int) $menu_item_parent !== $menu_item_key should probably compare int to int, i.e. typecast both sides. Looking above, there doesn't seem to be a guarantee that $menu_item_key is an int, just like $menu_item_parent.

@peterwilsoncc
Copy link
Copy Markdown
Contributor

peterwilsoncc commented Dec 16, 2022

Discussing this with @azaozz it was decided to go with an alternative approach to reduce the chance of further issues looping through the menu item tree.

I've just committed this in https://core.trac.wordpress.org/changeset/54999 / 1a85350 so will close this pull request off.

Thanks for your work on this patch, although it wasn't included it is appreciated and you received props in the commit for the ticket.

@mhkuu mhkuu deleted the feature/56926 branch December 16, 2022 07:53
@mhkuu
Copy link
Copy Markdown
Author

mhkuu commented Dec 16, 2022

Thanks @peterwilsoncc, looks like a more fail-safe approach indeed! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants