Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#26339 closed defect (bug) (fixed)

Pass by reference in wp_get_object_terms()

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

Description

Hi, I've noticed a bug in the current beta version: 3.8-beta-1-26375. I've tracked it down to this patch: http://core.trac.wordpress.org/attachment/ticket/17646/17646.3.diff (#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 13 years ago.
Unset references after foreach loop
26339-2.diff (1.8 KB ) - added by stephenharris 13 years ago.
Assign by key rather than by reference

Download all attachments as: .zip

Change History (7)

@stephenharris
13 years ago

Unset references after foreach loop

#1 @SergeyBiryukov
13 years ago

  • Keywords has-patch added
  • Milestone Awaiting Review3.8

#2 @SergeyBiryukov
13 years ago

Introduced in [26010].

@stephenharris
13 years ago

Assign by key rather than by reference

#3 @stephenharris
13 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
13 years ago

  • Keywords commit added

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

#5 @wonderboymusic
13 years ago

  • Owner set to wonderboymusic
  • Resolutionfixed
  • Status newclosed

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.