Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 7 years ago

#23506 closed defect (bug) (fixed)

get_terms() assumes its taxonomies argument is a numeric array with a 0 key

Reported by: nbachiyski's profile nbachiyski Owned by: wonderboymusic's profile wonderboymusic
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.6
Component: Taxonomy Keywords: has-patch commit
Focuses: Cc:

Description

In a few places in get_terms() we are using $taxonomies[0] after we've established that we have more than one taxonomy. The array isn't always 0-based, though.

For example, get_taxonomies() returns arrays with the taxonomy name as a key, which breaks this pattern.

In the attached patch, the taxonomies are run through array_values(), which gives us a 0-based array of the taxonomies.

Attachments (3)

get-terms-0-based-taxonomies.diff (543 bytes) - added by nbachiyski 11 years ago.
23506.diff (2.1 KB) - added by webord 11 years ago.
Using reset to get the first Item of the array
23506.2.diff (2.3 KB) - added by wonderboymusic 11 years ago.

Download all attachments as: .zip

Change History (14)

#2 @Viper007Bond
11 years ago

  • Summary changed from get_terms() assumes its taxonomies argument is 0-based to get_terms() assumes its taxonomies argument is a numeric array

Fixing ticket title to clearer terminology. Probably worth updating the patch too.

#3 follow-up: @nbachiyski
11 years ago

I still think 0-based is the better term.

The array, which is a result of $a = array( 1, 2, 3); unset( $a[0] ); is still numeric, but its zeroth element is missing.

#4 in reply to: ↑ 3 @Viper007Bond
11 years ago

  • Summary changed from get_terms() assumes its taxonomies argument is a numeric array to get_terms() assumes its taxonomies argument is a numeric array with a 0 key

Replying to nbachiyski:

I still think 0-based is the better term.

The array, which is a result of $a = array( 1, 2, 3); unset( $a[0] ); is still numeric, but its zeroth element is missing.

True!

#5 @markjaquith
11 years ago

  • Milestone changed from Awaiting Review to Future Release

Seems prudent. Alternatively (or even additionally), we could start using using the proper array function to grab the first item, instead of [0] (i.e. current(), or even list()).

@webord
11 years ago

Using reset to get the first Item of the array

#6 @webord
11 years ago

  • Cc bordoni.dev@… added
  • Keywords dev-feedback added

#7 @wonderboymusic
11 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Future Release to 3.7

+ whitespace in the refresh

#8 @wonderboymusic
11 years ago

  • Keywords commit added

this passes all unit tests

#9 @nacin
11 years ago

Since we're adjusting whitespace, $hierarchy[$child_of] should be $hierarchy[ $child_of ] — for some time we have put spaces here when the key is a variable.

#10 @wonderboymusic
11 years ago

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

In 25108:

Use reset() to grab the first taxonomy in an array, not by numerical index. Cleans up whitespace. Props webord, nbachiyski. Fixes #23506.

#11 @boonebgorges
7 years ago

In 40924:

When querying for terms, do not assume that $taxonomies is a 0-indexed array.

In [25108], the logic of term queries was modified to avoid assuming
that the taxonomies array was numerically indexed. See #23506. This
fix was inadvertantly reverted during the refactor in [25162].

Props david.binda.
Fixes #41113.

Note: See TracTickets for help on using tickets.