Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#36992 closed defect (bug) (fixed)

In get_terms(), getting a fatal error iterating on $terms inside get_terms filter.

Reported by: justinsainton's profile JustinSainton Owned by: boonebgorges's profile boonebgorges
Milestone: 4.6 Priority: normal
Severity: normal Version: 4.6
Component: Taxonomy Keywords:
Focuses: Cc:

Description

Our plugin (WP eCommerce) has a filter on get_terms for some custom sorting.

In WP 4.5.2, this code works fine. In trunk, it is broken.

After chatting a bit with @boone, our best guess is that the move to WP_Term_Query moved the filtering of get_terms() so that counts are now filtered, and before, they likely were not.

To be clear - the fault is in our code (we should have assumed possible return types of int, WP_Error, or an array), but there is a clear change in core.

For us, the intermediate solution is to simply add an is_array() check and return early. For core, it may be worth ensuring that the values are only filtered in the same places that they were in previous versions of WordPress.

Change History (9)

#2 @boonebgorges
8 years ago

In 37622:

Remove unused variable from get_terms().

Missed in [37572]. See #35381.

Props JustinSainton.
See #36992.

#3 @boonebgorges
8 years ago

  • Milestone changed from Awaiting Review to 4.6

#4 @boonebgorges
8 years ago

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

In 37623:

Taxonomy: Don't pass results of 'count' query through 'get_terms' filter.

Use of the 'get_terms' filter was consolidated in [37572], with the
introduction of WP_Term_Query. At that time, the result of 'count=true'
queries began being filtered by 'get_terms'. This breaks existing 'get_terms'
callbacks, which often assume that the returned value will be an array or a
WP_Error object.

Props JustinSainton.
Fixes #36992.

#5 follow-up: @nathanrice
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

FWIW, this is still somewhat broken. I've not had a chance to chase down why, but if you use array type hinting in your callback on the get_terms filter, you get the following error.

Catchable fatal error: Argument 1 passed to genesis_get_terms_filter() must be of the type array, string given

Obviously, this is in the Genesis theme.

Anybody know what's going on here? Why is a argument not of type array ever being passed to the get_terms filter?

#6 in reply to: ↑ 5 @boonebgorges
8 years ago

  • Keywords reporter-feedback added

Replying to nathanrice:

FWIW, this is still somewhat broken. I've not had a chance to chase down why, but if you use array type hinting in your callback on the get_terms filter, you get the following error.

Catchable fatal error: Argument 1 passed to genesis_get_terms_filter() must be of the type array, string given

Obviously, this is in the Genesis theme.

Anybody know what's going on here? Why is a argument not of type array ever being passed to the get_terms filter?

I just looked through again. At a glance, I don't see anywhere where something other than an array might be passed through the filter. It would be helpful if you could generate a full stack trace, including (a) the args being passed to get_terms() and WP_Term_Query, and (b) the non-array value that's being erroneously passed to the filter (is it a WP_Error? a boolean? or something else?)

#7 @nathanrice
8 years ago

This was sent by one of our testers. I can also send a copy of Genesis to you if you want to see any of this for yourself.

[03-Jun-2016 12:05:56 UTC] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to
genesis_get_terms_filter() must be of the type array, string given, called in 
/Users/nickcernis/www/trunk/wp-includes/plugin.php on line 235 and defined in 
/Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/term-meta.php:268
Stack trace:
#0 /Users/nickcernis/www/trunk/wp-includes/plugin.php(235): genesis_get_terms_filter('2', Array)
#1 /Users/nickcernis/www/trunk/wp-includes/taxonomy.php(1227): apply_filters('get_terms', '2', Array, Array, Object(WP_Term_Query))
#2 /Users/nickcernis/www/trunk/wp-includes/taxonomy.php(1694): get_terms(Array, Array)
#3 /Users/nickcernis/www/trunk/wp-admin/includes/class-wp-terms-list-table.php(127): wp_count_terms('category', Array)
#4 /Users/nickcernis/www/trunk/wp-admin/edit-tags.php(212): WP_Terms_List_Table->prepare_items()
#5 {main}
  thrown in /Users/nickcernis/www/trunk/wp-content/themes/genesis/lib/admin/term-meta.php on line 268

#8 @boonebgorges
8 years ago

  • Keywords reporter-feedback removed

Thanks, @nathanrice. I see now that there was an error in [37623], which I'll fix now.

#9 @boonebgorges
8 years ago

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

In 37634:

Taxonomy: No, really, don't pass results of 'count' query through 'get_terms' filter.

[37623] used the wrong parameter name (count=true instead of fields=count).

For greater flexibility and forward compatibility with other potential changes
to the return value of get_terms(), we now do a looser check: any non-array
value is excluded from the filter.

Fixes #36992.

Note: See TracTickets for help on using tickets.