WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#8150 closed defect (bug) (fixed)

walker class special case

Reported by: hailin Owned by:
Milestone: 2.7 Priority: normal
Severity: normal Version: 2.7
Component: General Keywords: has-patch, tested, reporter-feedback
Focuses: Cc:

Description

In function walk( $elements, $max_depth),
when none of the elements is top level (parent = 0)
we assume the first one must be root of the sub elements.
and get it as $root = $elements[0].

This implicitly assumes that the first index is 0.
In fact, in some cases, it can start with any integer.
It's observed that it can start with 2. When that happens,
walker will not construct any top level elements, end up treating
all as orphans.

The better and safe, way is to user array function:

$first = array_slice( $elements, 0, 1 );
$root = $first[0];

Attachments (1)

8150_walker.diff (523 bytes) - added by hailin 5 years ago.
patch

Download all attachments as: .zip

Change History (6)

hailin5 years ago

patch

comment:1 lloydbudd5 years ago

  • Keywords has-patch tested added
  • Milestone changed from 2.8 to 2.7
  • Version set to 2.7

comment:2 westi5 years ago

  • Keywords reporter-feedback added

The patch looks good.

It would be good to know what the user problem for this issue is though.

What can it affect the display of?

comment:3 hailin5 years ago

The issues is that with calls such as:

wp_list_categories('use_desc_for_title=1&child_of=400052&title_li=')

It will produce array $elements (categories), and there is not a single element whose parent=0 since they are children of 400052.

Then it will be handled by:

/*

  • when none of the elements is top level
  • assume the first one must be root of the sub elements */

if ( empty($top_level_elements) )

....

Because $elements is an associative array, the key may not begin with 0.
I've observed a bug on a particular blog, in which case the first element's key is

  1. That caused the $root= $elements[0] to be NULL since the key 0 does not exist.

As a result, $top_level_elements are empty, and then every element is displayed as orphans. The correct results would be nested structure.

With this fix, they are displayed correctly. It's tested in both positive and negative cases.

comment:4 westi5 years ago

Cool. Thanks for that hailin.

I like tickets to document the customer/end user issue so that I can understand how to test the fixes and if the issue is reported again I can easily refer back and say fixed by this.

comment:5 ryan5 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [9685]) Walker fix for when non elements are top-level. Props hailin. fixes #8150

Note: See TracTickets for help on using tickets.