Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#26339 closed defect (bug) (fixed)

Pass by reference in wp_get_object_terms()

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


Hi, I've noticed a bug in the current beta version: 3.8-beta-1-26375. I've tracked it down to this patch: (#17646).

Each term in the foreach of the $terms array is passed by reference when sanitising the term. However, this is not unset after the foreach loop.

As such the seemly innocuous code changes the last term from an object to an integer:

add_filter( 'wp_get_object_terms', 'my_get_object_terms_callback' );
function my_get_object_terms_callback( $terms ){

	// var_dump reveals an array of objects

	$term_ids = wp_list_pluck( $terms, 'term_id' );

	// last term is now an integer

	return $terms;

( I'm aware wp_get_object_terms filter isn't necessarily filtering term objects, but this highlights the bug).

Attachments (2)

26339.diff (1.6 KB) - added by stephenharris 11 years ago.
Unset references after foreach loop
26339-2.diff (1.8 KB) - added by stephenharris 11 years ago.
Assign by key rather than by reference

Download all attachments as: .zip

Change History (7)

11 years ago

Unset references after foreach loop

#1 @SergeyBiryukov
11 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 3.8

#2 @SergeyBiryukov
11 years ago

Introduced in [26010].

11 years ago

Assign by key rather than by reference

#3 @stephenharris
11 years ago

I've added an alternative patch which assigns by key rather than reference (which in my opinion is a bit more pretty).

#4 @SergeyBiryukov
11 years ago

  • Keywords commit added

26339-2.diff looks good to me. A unit test would be nice.

#5 @wonderboymusic
11 years ago

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

In 26510:

Fix lingering reference problem in wp_get_object_terms() by not setting the foreach'd vars to a reference. Adds unit test.

Props stephenharris.
Fixes #26339.

Note: See TracTickets for help on using tickets.